package com.mvc.controller.taizhang;

import com.mvc.model.TaskBatchRemoteModel;
import com.mvc.model.CorrectMap;
import com.mvc.model.ErrorMap;
import com.mvc.service.taizhang.TaskBatchRemoteService;
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.Map;

@Api(tags = " 批量远程任务控制类")
@RequestMapping("/TaskBatchRemoteAction")
@Controller
public class TaskBatchRemoteAction {

    @Resource
    private TaskBatchRemoteService taskBatchRemoteService;

    @ApiOperation("分页查询批量远程任务")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findAllTaskBatchRemote(@RequestBody TaskBatchRemoteModel model) {
        try {
            Map<String, Object> res = taskBatchRemoteService.findAllTaskBatchRemote(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 = "/saveTaskBatchRemote", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveTaskBatchRemote(@RequestBody TaskBatchRemoteModel model) {
        try {
            synchronized (this.getClass()) {//防止APP连续提交
                String msg = taskBatchRemoteService.checkValid(model,true);
                if (StringUtils.isNotEmpty(msg)) {
                    Map<String, Object> resMap = new ErrorMap();
                    resMap.put("error", msg);
                    return resMap;
                }

                if (taskBatchRemoteService.getUnfinishedTaskBatchRemoteBySourceTask(model.getSourceTaskId()) != null) {
                    Map<String, Object> resMap = new ErrorMap();
                    resMap.put("error", "存在未执行的同批次任务");
                    return resMap;
                }
                TaskBatchRemoteModel resModel = taskBatchRemoteService.saveTaskBatchRemote(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 = "/updateTaskBatchRemote", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateDeviceAcc(@RequestBody TaskBatchRemoteModel model) {
        try {
            String msg = taskBatchRemoteService.checkValid(model,false);
            if (StringUtils.isNotEmpty(msg)) {
                Map<String, Object> resMap = new ErrorMap();
                resMap.put("error", msg);
                return resMap;
            }

            TaskBatchRemoteModel resModel = taskBatchRemoteService.updateTaskBatchRemote(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 = "/deleteTaskBatchRemote/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> deleteTaskBatchRemote(@PathVariable("id") int id) {
        try {
            int res = taskBatchRemoteService.deleteTaskBatchRemote(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;
        }
    }

    @ApiOperation("根据taskBatchId获取对象")
    @RequestMapping(value = "/findTaskBatchRemoteByTaskBatchId/{taskBatchId}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findTaskBatchRemoteByTaskBatchId(@PathVariable("taskBatchId") String taskBatchId) {
        try {
            var res = taskBatchRemoteService.findTaskBatchRemoteByTaskBatchId(taskBatchId);
            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;
        }
    }
}
