package com.mvc.controller.taizhang;

import com.mvc.constant.BaseConstant;
import com.mvc.constant.TaskStatus;
import com.mvc.model.CorrectMap;
import com.mvc.model.TaskRemoteCfgModel;
import com.mvc.model.ErrorMap;
import com.mvc.service.taizhang.TaskRemoteCfgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Api(tags = "远程配置参数接口")
@RequestMapping("/TaskRemoteCfgAction")
@Controller
public class TaskRemoteCfgAction {

    @Resource
    private TaskRemoteCfgService taskRemoteCfgService;


    @ApiOperation("远程配置参数分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findAllTaskRemoteCfg(@RequestBody TaskRemoteCfgModel model) {
        try {
            Map<String, Object> res = taskRemoteCfgService.findAllTaskRemoteCfg(model);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("新增远程配置参数")
    @RequestMapping(value = "/saveTaskRemoteCfg", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveTaskRemoteCfg(@RequestBody TaskRemoteCfgModel model) {
        try {
            TaskRemoteCfgModel resModel = null;
            synchronized (this.getClass()) {//防止APP连续提交
                String error = taskRemoteCfgService.checkDataLegitimacy(model, true);
                if (StringUtils.isNotEmpty(error)) {
                    Map<String, Object> resMap = new ErrorMap();
                    resMap.put("error", error);
                    return resMap;
                }
                resModel = taskRemoteCfgService.saveTaskRemoteCfg(model);
            }
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", resModel);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("更新远程配置参数")
    @RequestMapping(value = "/updateTaskRemoteCfg", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateTaskRemoteCfg(@RequestBody TaskRemoteCfgModel model) {
        try {
            String error = null;
            TaskRemoteCfgModel taskRemoteCfgModel = taskRemoteCfgService.findTaskRemoteCfgById(model.getId());
            if (taskRemoteCfgModel == null) {
                error = "当前任务已不存在,请刷新后重试";
            }

            boolean isCancel = false;
            if (taskRemoteCfgModel.getFinishedFlag().equals(TaskStatus.NOT_EXECUTED.getCode()) &&
                    model.getFinishedFlag().equals(TaskStatus.CANCELLED.getCode())) {
                isCancel = true;
            }

            //不是取消时才校验 2025-03-18
            if(!isCancel){
                error = taskRemoteCfgService.checkDataLegitimacy(model, false);

                if (StringUtils.isEmpty((error))) {
                    if (taskRemoteCfgModel.getFinishedFlag().equals(BaseConstant.VALID_FLAG)) {
                        error = "当前任务已完成,请刷新后查看";
                    }
                }
            }

            if (StringUtils.isNotEmpty(error)) {
                Map<String, Object> resMap = new ErrorMap();
                resMap.put("error", error);
                return resMap;
            }

            TaskRemoteCfgModel resModel = taskRemoteCfgService.updateTaskRemoteCfg(model);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", resModel);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("删除远程配置参数")
    @RequestMapping(value = "/deleteTaskRemoteCfg/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> deleteTaskRemoteCfg(@PathVariable("id") int id) {
        try {
            int res = taskRemoteCfgService.deleteTaskRemoteCfg(id);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }
}
