package com.mvc.controller.taizhang;

import com.mvc.model.ResPageModel;
import com.mvc.model.ResultGeekerModel;
import com.mvc.model.TaskSpecialCfgModel;
import com.mvc.service.taizhang.TaskSpecialCfgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Api(tags = "专项配置接口")
@RequestMapping("/TaskSpecialCfgAction")
@Controller
public class TaskSpecialCfgAction {

    @Resource
    private TaskSpecialCfgService taskSpecialCfgService;

    @ApiOperation("查询所有专项配置")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel findAllTaskSpecialCfg(@RequestBody TaskSpecialCfgModel model) {
        ResultGeekerModel resultModel;
        try {
            ResPageModel res = taskSpecialCfgService.findAllTaskSpecialCfg(model);
            resultModel = new ResultGeekerModel(res);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("新增专项配置")
    @RequestMapping(value = "/saveTaskSpecialCfg", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel saveTaskSpecialCfg(@RequestBody TaskSpecialCfgModel model) {
        ResultGeekerModel resultModel;
        try
        {
            String res = taskSpecialCfgService.saveTaskSpecialCfg(model);
            resultModel = new ResultGeekerModel(res,true);
            /*TaskSpecialCfgModel temp = taskSpecialCfgService.findTaskSpecialCfgsByImei(model.getImei());
            if (temp != null) {
                resultModel = new ResultGeekerModel("当前IMEI存在未执行的任务!");
            } else {
                String msg = taskSpecialCfgService.checkTaskSpecialCfg(model);
                if (msg != null) {
                    resultModel = new ResultGeekerModel(msg);
                } else {
                    String res = taskSpecialCfgService.saveTaskSpecialCfg(model);
                    resultModel = new ResultGeekerModel(res,true);
                }
            }*/
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("更新专项配置")
    @RequestMapping(value = "/updateTaskSpecialCfg", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel updateTaskSpecialCfg(@RequestBody TaskSpecialCfgModel model) {
        ResultGeekerModel resultModel;
        try {
            TaskSpecialCfgModel temp = taskSpecialCfgService.findTaskSpecialCfgsByImei(model.getImei());
            if (temp != null && temp.getId() != model.getId()) {
                resultModel = new ResultGeekerModel("当前IMEI存在未执行的任务!");
            } else {
                String msg = taskSpecialCfgService.checkTaskSpecialCfg(model);
                if (msg != null) {
                    resultModel = new ResultGeekerModel(msg);
                } else {
                    TaskSpecialCfgModel res = taskSpecialCfgService.updateTaskSpecialCfg(model);
                    resultModel = new ResultGeekerModel(res);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("删除专项配置")
    @RequestMapping(value = "/deleteTaskSpecialCfg", method = RequestMethod.GET)
    @ResponseBody
    public ResultGeekerModel deleteTaskSpecialCfg(int id) {
        ResultGeekerModel resultModel;
        try {
            int res = taskSpecialCfgService.deleteTaskSpecialCfg(id);
            resultModel = new ResultGeekerModel(res);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }
}
