package com.mvc.controller.copy;

import com.mvc.model.TCheckModel;
import com.mvc.model.CorrectMap;
import com.mvc.model.ErrorMap;
import com.mvc.service.copy.TCheckService;
import com.mvc.service.taizhang.TAicbqCfgService;
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("/TCheckAction")
@Controller
public class TCheckAction {

    @Resource
    private TCheckService tCheckService;

    @Resource
    private TAicbqCfgService tAicbqCfgService;

    @ApiOperation("拷机业务主表分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findAllTCheck(@RequestBody TCheckModel model) {
        try {
            Map<String, Object> res = tCheckService.findAllTCheck(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("根据checkid查找拷机主表")
    @RequestMapping(value = "/findBycheckId/{checkId}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findBycheckById(@PathVariable("checkId") String checkId) {
        try {
            TCheckModel resModel = tCheckService.findBycheckById(checkId);
            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 = "/saveTCheck", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveTCheck(@RequestBody TCheckModel model) {
        try {
            TCheckModel resModel = tCheckService.saveTCheck(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 = "/updateTCheck", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateDeviceAcc(@RequestBody TCheckModel model) {
        try {
            TCheckModel resModel = tCheckService.updateTCheck(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 = "/deleteTCheck/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> deleteTCheck(@PathVariable("id") int id) {
        try {
            int res = tCheckService.deleteTCheck(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 = "/startCopy/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> startCopy(@PathVariable("id") int id) {
        try {
            TCheckModel res = tCheckService.startStressTesting(id, true);
            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("开始拷机调用方法(APP端使用)")
    @RequestMapping(value = "/appStartStressTesting/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> appStartStressTesting(@PathVariable("id") int id) {
        try {
            TCheckModel res = tCheckService.startStressTesting(id, true);
            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 = "/endCopy", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> endCopy(@RequestBody TCheckModel model) {
        try {
            TCheckModel res = tCheckService.endStressTesting(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 = "/addDetail/{id}/{platId}/{deviceSns}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> addDetail(@PathVariable("id") int id, @PathVariable("platId") String platId, @PathVariable("deviceSns") String deviceSns) {
        try {
            String msg = tCheckService.checkDataLegitimacy(deviceSns);
            if (StringUtils.isNotEmpty(msg)) {
                Map<String, Object> resMap = new ErrorMap();
                resMap.put("error", msg);
                return resMap;
            }

            TCheckModel res = tCheckService.addDetail(id, platId, deviceSns);
            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("添加明细拷机调用方法-加入电池绑定(POST)")
    @RequestMapping(value = "/addDetail", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addDetail(@RequestBody TCheckModel model) {
        try {
            String msg = tCheckService.checkDataLegitimacy(model);
            if (StringUtils.isNotEmpty(msg)) {
                Map<String, Object> resMap = new ErrorMap();
                resMap.put("error", msg);
                return resMap;
            }

            TCheckModel res = tCheckService.addDetail(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 = "/deleteDetail/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> deleteDetail(@PathVariable("id") Integer id) {
        try {
            int res = tCheckService.deleteDetail(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("根据checkid查找拷机主表不封装明细")
    @RequestMapping(value = "/findBycheckByIdWithMx/{checkId}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findBycheckByIdWithMx(@PathVariable("checkId") String checkId) {
        try {
            TCheckModel res = tCheckService.findBycheckByIdWithOutMx(checkId);
            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 = "/findCheckDetailLatestAiRead", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findCheckDetailLatestAiRead(@RequestBody TCheckModel model) {
        try {
            Map<String, Object> resModel = tCheckService.findCheckDetailLatestAiRead(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 = "/checkSampleIntervalByCheckDetails", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> checkSampleIntervalByCheckDetails(@RequestBody TCheckModel model) {
        try {
            String res = tAicbqCfgService.checkSampleIntervalByCheckDetails(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;
        }
    }
}
