package com.mvc.controller.aiiden;

import com.mvc.model.CorrectMap;
import com.mvc.model.ErrorMap;
import com.mvc.model.TaskReviewModel;
import com.mvc.othermodel.TaskReviewTJModel;
import com.mvc.service.taizhang.DeviceAlarmService;
import com.mvc.service.test.TaiZhangTest;
import com.mvc.service.aiiden.TaskReviewService;
import com.mvc.utils.NotifyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Api(tags = "数据审核接口")
@RequestMapping("/TaskReviewAction")
@Controller
public class TaskReviewAction {
    @Resource
    private TaskReviewService taskReviewService;

    private static Logger log = LoggerFactory.getLogger(TaskReviewAction.class);

    @ApiOperation("查询指定日期的审核列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findAllTaskReview(@RequestBody TaskReviewModel model) {
        try {
            Map<String, Object> res = taskReviewService.findAllTaskReview(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 = "/saveTaskReview", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveAppAuth(@RequestBody TaskReviewModel model) {
        try {
            TaskReviewModel resModel = taskReviewService.saveTaskReview(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 = "/updateTaskReview", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateDeviceAcc(@RequestBody TaskReviewModel model) {
        try {
            TaskReviewModel resModel = taskReviewService.updateTaskReview(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 = "/sendReviewToSls", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> sendReviewToSls(@RequestBody TaskReviewModel model) {
        try {
            TaskReviewModel resModel = taskReviewService.sendReviewToSls(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 = "/revokeReview/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> revokeReview(@PathVariable("id") Integer id) {
        try {
            TaskReviewModel resModel = taskReviewService.revokeReview(id);
            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 = "/getTaskReviewCount/{taskDate}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getTaskReviewCount(@PathVariable("taskDate") String taskDate) {
        try {
            TaskReviewTJModel res = taskReviewService.getTaskReviewCount(taskDate);
            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 = "/generateTaskReviewByStationAcc/{taskDate}/{deviceType}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> generateTaskReviewByStationAcc(@PathVariable("taskDate") String taskDate, @PathVariable("deviceType") String deviceType) {
        try {
            taskReviewService.generateTaskReviewByStationAcc(taskDate, deviceType);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", "1");
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("内部使用-根据任务日期生成审核数据")
    @RequestMapping(value = "/generateReviewDataByTaskDate/{taskDate}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> generateReviewDataByTaskDate(@PathVariable("taskDate") String taskDate) {
        try {
            log.warn("手动生成审核数据开始......");
            taskReviewService.generateReviewDataByTaskDate(taskDate);
            log.warn("手动生成审核数据结束......");
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", "1");
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据日期和单位代码获取审核统计数据")
    @RequestMapping(value = "/getTaskReviewCountByUnitCode/{taskDate}/{unitCode}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getTaskReviewCountByUnitCode(@PathVariable("taskDate") String taskDate, @PathVariable("unitCode") String unitCode) {
        try {
            TaskReviewTJModel res = taskReviewService.getTaskReviewCountByUnitCode(taskDate, unitCode);
            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 = "/generateServerData/{sampleTime}/{deviceType}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> generateServerData(@PathVariable("sampleTime") String sampleTime, @PathVariable("deviceType") String deviceType) {
        try {
            taskReviewService.generateServerData(sampleTime, deviceType);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", "1");
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @Resource
    private TaiZhangTest taiZhangTest;

    @Resource
    private DeviceAlarmService deviceAlarmService;

    @Resource
    private NotifyUtil notifyUtil;

    @ApiOperation("获取大模型识别结果")
    @RequestMapping(value = "/setNotifyEnable/{enable}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> setNotifyEnable(@PathVariable("enable") String enable) {
        try {
            /** 数据转发 开始 */
            /*TAireadModel aireadModel =new TAireadModel();
            aireadModel.setStationCode("5827009901");
            if(StringUtils.isNotEmpty(pumpWellCode)&&pumpWellCode.length()==10){
                aireadModel.setStationCode(pumpWellCode);
            }
            aireadModel.setFunCode("32");
            aireadModel.setResultValue(BigDecimal.valueOf(125.32));
            //aireadModel.setResultValue(new BigDecimal(967.1));
            aireadModel.setSampleTime(new Date());
            aireadModel.setRptTime(new Date());
            aireadModel.setVoltage(new BigDecimal(3.55));
            aireadModel.setRsrp(60);

            hdForwarder.generateMessage(aireadModel);*/
            /** 数据转发 结束 */

            /** 台账更新部分 开始 */
            //taiZhangTest.test();//第一步
            //taiZhangTest.sjhzdeb();//第二步
            //taiZhangTest.updateStationAcc();//第三步
            //taiZhangTest.updateCustomizeGroup();//重新重点关注新站码
            /** 台账更新部分 结束 */

            /** 邮件推送开关 开始 */
            NotifyUtil.mIsSend = enable.equals("1");
            //if (StringUtils.isNotEmpty(deviceSns) && !deviceSns.equals("0")) {//不为空,不为0时,替换新的SN串
                //NotifyUtil.mDeviceSns = deviceSns;
            //}
            /** 邮件推送开关 结束 */

            StringBuilder sb = new StringBuilder();
            sb.append("邮件推送:");
            sb.append(NotifyUtil.mIsSend ? "开启" : "关闭");
            //sb.append("；");
            //sb.append("指定分组:");
            //sb.append(NotifyUtil.mDeviceSns);
            String res = sb.toString();

            /** 发送邮件 开始 */
            /*TAireadModel aireadModel =new TAireadModel();
            aireadModel.setDeviceSn("00000003241");
            aireadModel.setIdentValue(new BigDecimal(999));
            aireadModel.setSzsbz("0,0,0,0,0,0,0,0,-,-,113,91");
            aireadModel.setSzsbgl("0,0,0,0,0,0,0,0,-,-,113,91");
            aireadModel.setFunCode("36");
            //aireadModel.setSmallJpgUrl("E:\\shangchuan\\11.png");
            aireadModel.setBigJpgUrl("http://127.0.0.1:6090/image/rawdata/2023-11/0000003237/D0000003237_20231126104605_B.jpg");
            notifyUtil.sendEMail(aireadModel);*/
            /** 发送邮件 结束 */

            /** 生成预警数据测试 开始 */
            //deviceAlarmService.generateDeviceAlarmByTime();
            /** 生成预警数据测试 开始 */

            //byte[] buffer = FileUtil.readBytesFile("E:/shangchuan/14.png");
            //String image = Base64.getEncoder().encodeToString(buffer);
            ////var model = onnxIdenService.postInParam(pumpWellCode,image);
            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 = "/downloadExcel", method = RequestMethod.GET)
    @ResponseBody
    public void getTongji_excel(HttpServletResponse response) {
        taskReviewService.downloadExcel(response);
    }

    @ApiOperation("根据单位编码等获取审核记录列表")
    @RequestMapping(value = "/findTaskReviewList/{unitCode}/{taskDate}/{reviewFlag}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findTaskReviewList(@PathVariable("unitCode") String unitCode,@PathVariable("taskDate")  String taskDate,@PathVariable("reviewFlag") String reviewFlag) {
        try {
            List<TaskReviewModel> resList = taskReviewService.findTaskReviewList(unitCode, taskDate, reviewFlag);
            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;
        }
    }

    @ApiOperation("下载数据至Excel(根据单位代码等)")
    @RequestMapping(value = "/downloadTaskReviewList/{unitCode}/{taskDate}/{reviewFlag}", method = RequestMethod.GET)
    @ResponseBody
    public void getTongji_excel(HttpServletResponse response,@PathVariable("unitCode") String unitCode,@PathVariable("taskDate")  String taskDate,@PathVariable("reviewFlag") String reviewFlag) {
        taskReviewService.downloadTaskReviewList(response,unitCode, taskDate, reviewFlag);
    }
}
