package com.njcn.gather.result.controller;

import cn.hutool.core.util.ObjectUtil;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.LogUtil;
import com.njcn.gather.detection.service.IAdPariService;
import com.njcn.gather.detection.util.socket.CnSocketUtil;
import com.njcn.gather.result.pojo.param.MonitorQueryParam;
import com.njcn.gather.result.pojo.param.ResultParam;
import com.njcn.gather.result.pojo.vo.*;
import com.njcn.gather.result.service.IResultService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.HttpResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author caozehui
 * @data 2024-12-30
 */
@Slf4j
@Api(tags = "检测结果")
@RestController
@RequestMapping("/result")
@RequiredArgsConstructor
public class ResultController extends BaseController {
    private final IResultService resultService;
    private final IAdPariService adPariService;

    @OperateInfo
    @PostMapping("/formContent")
    @ApiOperation("查询检测结果-表单内容")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public HttpResult<FormContentVO> formContent(@RequestBody @Validated ResultParam.QueryParam queryParam) {
        String methodDescribe = getMethodDescribe("formContent");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, queryParam);
        FormContentVO result = resultService.getFormContent(queryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo
    @PostMapping("/treeData")
    @ApiOperation("查询检测结果-树形结构的具体检测项")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public HttpResult<List<TreeDataVO>> treeData(@RequestBody @Validated ResultParam queryParam) {
        String methodDescribe = getMethodDescribe("treeData");
        List<TreeDataVO> infoVOS = resultService.treeData(queryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, infoVOS, methodDescribe);
    }


    @OperateInfo
    @PostMapping("/resultData")
    @ApiOperation("查询检测结果-表格具体检测项")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public HttpResult<ResultVO> resultData(@RequestBody @Validated ResultParam queryParam) {
        String methodDescribe = getMethodDescribe("resultData");
        ResultVO resultVO = resultService.resultData(queryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, resultVO, methodDescribe);
    }

    /**
     * 导出灿能二楼设备
     *
     * @param queryParam
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DOWNLOAD)
    @PostMapping("/exportRawData")
    @ApiOperation("导出原始数据")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public void exportCNDev(@RequestBody @Validated ResultParam queryParam) {
        String methodDescribe = getMethodDescribe("exportRawData");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, queryParam);

        resultService.exportRawData(queryParam);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/reCalculate")
    @ApiOperation("重新计算检测结果")
    @ApiImplicitParam(name = "param", value = "重新计算参数", required = true)
    public HttpResult<Object> reCalculate(@RequestBody @Validated ResultParam.ChangeErrorSystemParam param) {
        String methodDescribe = getMethodDescribe("reCalculate");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, param);

        resultService.reCalculate(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/changeErrorSystem")
    @ApiOperation("更换误差体系")
    @ApiImplicitParam(name = "param", value = "修改参数", required = true)
    public HttpResult<Object> changeErrorSystem(@RequestBody @Validated ResultParam.ChangeErrorSystemParam param) {
        String methodDescribe = getMethodDescribe("changeErrorSystem");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, param);

        resultService.changeErrorSystem(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DELETE)
    @GetMapping("/deleteTempTable")
    @ApiOperation("删除临时表")
    @ApiImplicitParam(name = "code", value = "计划对应的表后缀code", required = true)
    public HttpResult<Object> deleteTempTable(@RequestParam("code") String code) {
        String methodDescribe = getMethodDescribe("deleteTempTable");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, code);

        resultService.deleteTempTable(code);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getContrastFormContent")
    @ApiOperation("获取比对式检测结果-表单内容")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public HttpResult<FormContentVO> getContrastFormContent(@RequestBody @Validated ResultParam.QueryParam queryParam) {
        String methodDescribe = getMethodDescribe("getContrastFormContent");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, queryParam);

        FormContentVO result = resultService.getContrastFormContent(queryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getContrastResult")
    @ApiOperation("获取比对式检测结果")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public HttpResult<ContrastResultVO> getContrastResult(@RequestBody @Validated ResultParam.QueryParam queryParam) {
        String methodDescribe = getMethodDescribe("getContrastResult");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, queryParam);

        if (ObjectUtil.isEmpty(queryParam.getNum())) {
            queryParam.setNum(adPariService.getMaxNum(queryParam.getDeviceId() + CnSocketUtil.SPLIT_TAG + queryParam.getNum()));
        }
        ContrastResultVO result = resultService.getContrastResult(queryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getMonitorResult")
    @ApiOperation("获取监测点的检测结果")
    @ApiImplicitParam(name = "devId", value = "设备id", required = true)
    public HttpResult<List<MonitorResultVO>> getMonitorResult(@RequestParam("devId") String devId) {
        String methodDescribe = getMethodDescribe("getMonitorResult");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, devId);

        List<MonitorResultVO> result = resultService.getMonitorResult(devId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/createChecksquareTask")
    @ApiOperation("调用第三方数模数据检测接口")
    @ApiImplicitParam(name = "devId", value = "设备id", required = true)
    public HttpResult<DataCheckResultVO> createChecksquareTask(@RequestParam("devId") String devId) {
        String methodDescribe = getMethodDescribe("createChecksquareTask");
        LogUtil.njcnDebug(log, "{}，调用第三方数模数据检测接口，设备id为：{}", methodDescribe, devId);

        DataCheckResultVO result = resultService.createChecksquareTaskByDevId(devId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getMonitorDataSourceResult")
    @ApiOperation("获取监测点的检测结果下拉框")
    @ApiImplicitParam(name = "monitorId", value = "监测点id", required = true)
    public HttpResult<Map<Integer, List<DataSourceResultVO>>> getMonitorDataSourceResult(@RequestParam("monitorId") String monitorId) {
        String methodDescribe = getMethodDescribe("getMonitorDataSourceResult");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, monitorId);

        Map<Integer, List<DataSourceResultVO>> result = resultService.getMonitorDataSourceResult(monitorId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/updateMonitorResult")
    @ApiOperation("更新监测点的检测结果")
    @ApiImplicitParam(name = "resultParams", value = "更新内容", required = true)
    public HttpResult<Boolean> updateMonitorResult(@RequestBody @Validated MonitorResultVO resultParams) {
        String methodDescribe = getMethodDescribe("updateMonitorResult");
        LogUtil.njcnDebug(log, "{}，更新数据为：{}", methodDescribe, resultParams);

        boolean result = resultService.updateMonitorResult(resultParams);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getCheckItem")
    @ApiOperation("获取监测点的检测项")
    @ApiImplicitParam(name = "monitorQueryParam", value = "监测点查询参数", required = true)
    public HttpResult<List<ContrastTestItemVO>> getCheckItem(@RequestBody MonitorQueryParam monitorQueryParam) {
        String methodDescribe = getMethodDescribe("getCheckItem");

        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, monitorQueryParam);
        List<ContrastTestItemVO> result = resultService.getCheckItem(monitorQueryParam.getPlanId(), monitorQueryParam.getDevId(), monitorQueryParam.getChnNum(), monitorQueryParam.getNum(), monitorQueryParam.getCode());
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }
}
