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

import com.neusoft.smart.pos.dto.ota.request.UpgradeStrategyGroupRequest;
import com.neusoft.smart.pos.dto.ota.request.UpgradeStrategyQueryRequest;
import com.neusoft.smart.pos.dto.ota.response.UpgradeStrategyGroupResponse;
import com.neusoft.smart.pos.dto.ota.response.UpgradeStrategyResponse;
import com.neusoft.smart.pos.framework.bean.PaginationExtendBean;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.ota.constants.OTAErrorCode;
import com.neusoft.smart.pos.ota.service.UpgradeStrategyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.util.Pair;
import org.springframework.validation.BindingResult;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(value = "设备升级策略管理restful接口", description = "设备升级策略管理restful接口")
public class UpgradeStrategyController {
	
	@Autowired
	private UpgradeStrategyService upgradeStrategyService;
	
	@PostMapping("/upgradeStrategy")
	@ApiOperation(value = "升级策略添加接口", notes = "升级策略添加接口")
	public ResponseData<?> create(@Valid @RequestBody UpgradeStrategyGroupRequest upgradeStrategyGroupRequest, BindingResult bindingResult) {
		upgradeStrategyService.create(upgradeStrategyGroupRequest);
		return new ResponseData<>();
	}
	
	@PutMapping("/upgradeStrategy/cancel/{groupId}")
	@ApiOperation(value = "升级策略取消接口", notes = "升级策略取消接口")
	public ResponseData<?> cancel(@PathVariable Integer groupId) {
		upgradeStrategyService.cancel(groupId);
		return new ResponseData<>();
	}
	
	
	@ApiOperation(value = "分页查询升级策略接口", notes = "分页查询升级策略接口")
	@PostMapping("/upgradeStrategies")
	public ResponseData<PaginationExtendBean<UpgradeStrategyGroupResponse>> groupList(@RequestBody UpgradeStrategyQueryRequest upgradeStrategyQueryRequest) {
		PaginationExtendBean<UpgradeStrategyGroupResponse> paginationExtendBean = new PaginationExtendBean<>();
		paginationExtendBean.setRows(upgradeStrategyService.getGroupList(upgradeStrategyQueryRequest));
		paginationExtendBean.setTotal(upgradeStrategyService.queryGroupCount(upgradeStrategyQueryRequest));
		return new ResponseData<>(paginationExtendBean);
	}
	
	@GetMapping("/upgradeStrategy/relation/{groupId}")
	@ApiOperation(value = "通过策略groupId查询机具和升级本版的关系接口", notes = "通过策略groupId查询机具和升级本版的关系接口")
	public ResponseData<List<UpgradeStrategyResponse>> list(@PathVariable Integer groupId) {
		ResponseData<List<UpgradeStrategyResponse>> responseData = new ResponseData<>();
		responseData.setResult(upgradeStrategyService.queryList(groupId));
		return responseData;
	}
	
	@PutMapping("/upgradeStrategy/updateStatus")
	@ApiOperation(value = "升级策略更新状态接口", notes = "升级策略更新状态接口")
	public ResponseData<?> updateStatus(@RequestBody List<Pair<Integer, Integer>> ids) {
		upgradeStrategyService.setUpgradeStatus(ids);
		return new ResponseData<>();
	}
	
	/**
	 * 数据库到redis
	 *
	 * @return
	 */
	@GetMapping("/strategy/load2Redis")
	@ApiOperation(value = "重新加载设备更新数据到redis接口", notes = "重新加载设备更新数据到redis接口")
	public String load2Redis() {
		try {
			upgradeStrategyService.loadData2Redis();
			return "ok";
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessCommonException(OTAErrorCode.LOAD_REDIS_ERROR);
		}
	}
}
