package com.neusoft.smart.pos.ota.controller;

import com.neusoft.smart.pos.dto.ota.request.UpgradePackageQueryRequest;
import com.neusoft.smart.pos.dto.ota.request.UpgradePackageRequest;
import com.neusoft.smart.pos.dto.ota.response.UpgradePackageResponse;
import com.neusoft.smart.pos.framework.bean.PaginationExtendBean;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.framework.utils.LocalDateTimeUtils;
import com.neusoft.smart.pos.ota.service.UpgradePackageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@RestController
@Api(value = "更新包管理restful接口", description = "更新包管理restful接口")
public class UpgradePackageController {
	
	@Autowired
	private UpgradePackageService upgradePackageService;
	
	
	@PostMapping("/upgradePackage")
	@ApiOperation(value = "更新包添加接口", notes = "更新包添加接口")
	public ResponseData<UpgradePackageResponse> create(@Valid @RequestBody UpgradePackageRequest upgradePackageRequest, BindingResult bindingResult) {
		return new ResponseData<>(upgradePackageService.create(upgradePackageRequest));
	}
	
	@DeleteMapping("/upgradePackage/{packageId}")
	@ApiOperation(value = "更新包删除接口", notes = "更新包删除接口")
	public ResponseData<?> delete(@NotNull @PathVariable Integer packageId) {
		upgradePackageService.delete(packageId);
		return new ResponseData<>();
	}
	
	
	@ApiOperation(value = "分页查询更新包接口", notes = "分页查询更新包接口")
	@PostMapping("/upgradePackages")
	public ResponseData<PaginationExtendBean<UpgradePackageResponse>> list(@RequestBody UpgradePackageQueryRequest upgradePackageQueryRequest) {
		PaginationExtendBean<UpgradePackageResponse> paginationExtendBean = new PaginationExtendBean<>();
		paginationExtendBean.setRows(upgradePackageService.getList(upgradePackageQueryRequest));
		paginationExtendBean.setTotal(upgradePackageService.queryCount(upgradePackageQueryRequest));
		return new ResponseData<>(paginationExtendBean);
	}
	
	@GetMapping("/upgradePackage/{packageId}")
	@ApiOperation(value = "更新包详情接口", notes = "更新包详情接口")
	public ResponseData<UpgradePackageResponse> get(@PathVariable Integer packageId) {
		return new ResponseData<>(upgradePackageService.get(packageId));
	}
	
	@GetMapping("/upgradePackage/export")
	@ApiOperation(value = "导出版本excel接口", notes = "导出版本excel接口")
	public void export(UpgradePackageQueryRequest upgradePackageQueryRequest, HttpServletResponse response) throws Exception {
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
		response.setHeader("Content-Disposition", "attachment;filename=" + new String(("版本列表-" + LocalDateTimeUtils.getDateTime() + ".xlsx").getBytes(), "iso-8859-1"));
		upgradePackageService.export(upgradePackageQueryRequest, response.getOutputStream());
	}
}
