package com.mvc.controller.taizhang;

import com.mvc.model.CorrectMap;
import com.mvc.model.ErrorMap;
import com.mvc.model.TTaskDownModel;
import com.mvc.service.taizhang.TTaskDownService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "下行任务表接口")
@RequestMapping("/TTaskDownAction")
@Controller
public class TTaskDownAction {

    @Autowired
    private TTaskDownService tTaskDownService;

    @ApiOperation("下行任务分页查询")
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> findAllTTaskDown(@RequestBody TTaskDownModel model){
        try {
            Map<String,Object> res= tTaskDownService.findAllTTaskDown(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 = "/saveTTaskDown",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> saveTTaskDown(@RequestBody TTaskDownModel model){
        try {
            if(tTaskDownService.existTTaskDownSameLevel(model)){
                Map<String,Object> resMap=new ErrorMap();
                resMap.put("error","当前设备存在同等级同类型且未执行的任务");
                return resMap;
            }
            TTaskDownModel resModel= tTaskDownService.saveTTaskDown(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 = "/updateTTaskDown",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateTTaskDown(@RequestBody TTaskDownModel model){
        try {
            TTaskDownModel resModel= tTaskDownService.updateTTaskDown(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 = "/deleteTTaskDown/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> deleteTTaskDown(@PathVariable("id") int id){
        try {
            int  res= tTaskDownService.deleteTTaskDown(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("查询下行任务")
    @RequestMapping(value = "/findTTaskDown/{deviceSn}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> findTTaskDown(@PathVariable("deviceSn") String deviceSn){
        try {
            List<TTaskDownModel> resList= tTaskDownService.findTTaskDown(deviceSn,"","1");
            Map<String,Object> resMap=new CorrectMap();
            resMap.put("res",resList);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String,Object> resMap=new ErrorMap();
            resMap.put("error",e.getMessage());
            return resMap;
        }
    }
}
