package com.njcn.csdevice.controller.line;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.annotation.OperateInfo;
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.HttpResultUtil;
import com.njcn.csdevice.pojo.dto.CsLineDTO;
import com.njcn.csdevice.pojo.param.CsLineParam;
import com.njcn.csdevice.pojo.po.CsEquipmentDeliveryPO;
import com.njcn.csdevice.pojo.po.CsLinePO;
import com.njcn.csdevice.pojo.vo.LineDetailDataVO;
import com.njcn.csdevice.pojo.vo.PqSensitiveUserLineVO;
import com.njcn.csdevice.service.CsLinePOService;
import com.njcn.device.biz.mapper.OverLimitWlMapper;
import com.njcn.device.biz.pojo.po.Overlimit;
import com.njcn.device.biz.utils.COverlimitUtil;
import com.njcn.web.controller.BaseController;
import com.njcn.web.pojo.param.BaseParam;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

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

/**
 * Description:
 * Date: 2023/5/24  19:34【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/csline")
@Api(tags = "监测点")
@AllArgsConstructor
public class CslineController extends BaseController {

    private final CsLinePOService csLinePOService;
    private final OverLimitWlMapper overLimitWlMapper;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryLineById")
    @ApiOperation("监测点查询通过id获取")
    @ApiImplicitParam(name = "ids", value = "设备id获集合", required = true)
    public HttpResult<List<CsLinePO>> queryLineById(@RequestParam List<String> ids){
        String methodDescribe = getMethodDescribe("queryLineById");
        List<CsLinePO> csLinePOS = csLinePOService.listByIds(ids);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, csLinePOS, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getPositionById")
    @ApiOperation("通过id查询监测点位置")
    @ApiImplicitParam(name = "id", value = "设备id", required = true)
    public HttpResult<String> getPositionById(@RequestParam String id){
        String methodDescribe = getMethodDescribe("getPositionById");
        String position = csLinePOService.lambdaQuery().eq(CsLinePO::getLineId,id).eq(CsLinePO::getStatus,1 ).one().getPosition();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, position, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryLineByDevId")
    @ApiOperation("监测点查询通过设备id获取")
    @ApiImplicitParam(name = "id", value = "设备id集合", required = true)
    public HttpResult<List<CsLinePO>> queryLineByDevId(@RequestParam String id){
        String methodDescribe = getMethodDescribe("queryLineByDevId");
        List<CsLinePO> csLinePOS = csLinePOService.queryByDevId(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, csLinePOS, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/addList")
    @ApiOperation("批量新增监测点")
    @ApiImplicitParam(name = "list", value = "监测点数据集", required = true)
    @Transactional(rollbackFor = Exception.class)
    public HttpResult<String> addLineList(@RequestBody List<CsLinePO> list){
        String methodDescribe = getMethodDescribe("addLineList");
        csLinePOService.saveBatch(list);
        for(CsLinePO csLinePO: list){
           Overlimit overlimit = COverlimitUtil.globalAssemble(csLinePO.getVolGrade().floatValue(),10f,10f,10f,0,1);
           overlimit.setId(csLinePO.getLineId());
            overLimitWlMapper.deleteById(csLinePO.getLineId());
            overLimitWlMapper.insert(overlimit);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/findByNdid")
    @ApiOperation("根据ndid查询监测点")
    @ApiImplicitParam(name = "id", value = "ndid", required = true)
    @ApiIgnore
    public HttpResult<List<CsLinePO>> findByNdid(@RequestParam String id){
        String methodDescribe = getMethodDescribe("findByNdid");
        List<CsLinePO> lineList= csLinePOService.findByNdid(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, lineList, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getCustomDetailByLineId")
    @ApiOperation("根据id查询监测点")
    public HttpResult<Map<String,String>> getCustomDetailByLineId(@RequestParam String id){
        String methodDescribe = getMethodDescribe("getCustomDetailByLineId");
        Map<String,String> map = csLinePOService.getCustomDetailByLineId(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, map, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/updateLine")
    @ApiOperation("修改监测点信息")
    @ApiImplicitParam(name = "csLineParam", value = "监测点信息", required = true)
    @ApiIgnore
    public HttpResult<Boolean> updateLine(@RequestBody @Validated CsLineParam csLineParam) {
        String methodDescribe = getMethodDescribe("updateLine");
        csLinePOService.updateLine(csLineParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/updateIds")
    @ApiOperation("修改监测点模板id和数据集id")
    @ApiImplicitParam(name = "csLineParam", value = "监测点信息", required = true)
    @ApiIgnore
    public HttpResult<Boolean> updateIds(@RequestBody @Validated CsLineParam csLineParam) {
        String methodDescribe = getMethodDescribe("updateIds");
        csLinePOService.updateIds(csLineParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getById")
    @ApiOperation("根据监测点id获取监测点详情")
    @ApiImplicitParam(name = "lineId", value = "监测点id", required = true)
    public HttpResult<CsLinePO> getById(@RequestParam String lineId) {
        String methodDescribe = getMethodDescribe("getById");
        LambdaQueryWrapper<CsLinePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CsLinePO::getLineId,lineId).eq(CsLinePO::getStatus,1);
        CsLinePO po = csLinePOService.getOne(queryWrapper);
        if (Objects.isNull(po.getLineNo())) {
            po.setLineNo(po.getClDid());
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, po, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getOverLimitDataByIds")
    @ApiOperation("根据监测点id集合获取国标限值")
    @ApiImplicitParam(name = "ids", value = "监测点id集合", required = true)
    public HttpResult<List<Overlimit>> getOverLimitData(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("getOverLimitDataByIds");
        List<Overlimit> result = overLimitWlMapper.selectBatchIds(ids);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getOverLimitData")
    @ApiOperation("根据监测点id获取国标限值")
    @ApiImplicitParam(name = "id", value = "监测点id", required = true)
    public HttpResult<Overlimit> getOverLimitData(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getOverLimitData");
        Overlimit result = overLimitWlMapper.selectById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

//    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
//    @PostMapping("/getLinesByDevList")
//    @ApiOperation("根据装置id集合获取监测点id集合")
//    @ApiImplicitParam(name = "ids", value = "装置id集合", required = true)
//    public HttpResult<List<CsLinePO>> getLinesByDevList(@RequestParam("ids") List<String> ids) {
//        String methodDescribe = getMethodDescribe("getLinesByDevList");
//        List<CsLinePO> list = csLinePOService.getLinesByDevList(ids);
//        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
//    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getLinesByDevList")
    @ApiOperation("根据装置id集合获取监测点id集合")
    @ApiImplicitParam(name = "ids", value = "装置id集合", required = true)
    public HttpResult<List<CsLinePO>> getLinesByDevList(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("getLinesByDevList");
        List<CsLinePO> list = csLinePOService.getLinesByDevList(ids);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/addCldLine")
    @ApiOperation("新增云前置监测点")
    @ApiImplicitParam(name = "param", value = "param", required = true)
    public HttpResult<List<CsLinePO>> addCldLine(@RequestBody @Validated CsLineParam param) {
        String methodDescribe = getMethodDescribe("addCldLine");
        csLinePOService.addCldLine(param);
        //新增监测点限值
        Overlimit overlimit = COverlimitUtil.globalAssemble(param.getVolGrade().floatValue(),param.getProtocolCapacity().floatValue(),param.getDevCapacity().floatValue(),param.getShortCircuitCapacity().floatValue(),0,0);
        overlimit.setId(param.getLineId());
        overLimitWlMapper.deleteById(param.getLineId());
        overLimitWlMapper.insert(overlimit);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/updateCldLine")
    @ApiOperation("修改云前置监测点")
    @ApiImplicitParam(name = "param", value = "param", required = true)
    public HttpResult<List<CsLinePO>> updateCldLine(@RequestBody CsLineParam param) {
        String methodDescribe = getMethodDescribe("updateCldLine");
        csLinePOService.updateCldLine(param);
        //修改监测点限值
        Overlimit overlimit = COverlimitUtil.globalAssemble(param.getVolGrade().floatValue(),param.getProtocolCapacity().floatValue(),param.getDevCapacity().floatValue(),param.getShortCircuitCapacity().floatValue(),0,0);
        overlimit.setId(param.getLineId());
        overLimitWlMapper.deleteById(param.getLineId());
        overLimitWlMapper.insert(overlimit);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/delCldLine")
    @ApiOperation("删除云前置监测点")
    @ApiImplicitParam(name = "id", value = "监测点id", required = true)
    public HttpResult<List<CsLinePO>> delCldLine(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("delCldLine");
        csLinePOService.deleteCldLine(id);
        overLimitWlMapper.deleteById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/updateLineDataByList")
    @ApiOperation("根据装置集合修改监测点信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "list", value = "装置id集合", required = true),
            @ApiImplicitParam(name = "id", value = "模板id", required = true),
            @ApiImplicitParam(name = "setId", value = "数据集id", required = true),
    })
    @ApiIgnore
    public HttpResult<String> updateDataByList(@RequestParam("list") List<String> list, @RequestParam("id") String id, @RequestParam("setId") String setId){
        String methodDescribe = getMethodDescribe("updateLineDataByList");
        csLinePOService.updateLineDataByList(list,id,setId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getAllLine")
    @ApiOperation("获取系统中所有监测点id")
    public HttpResult<List<String>> getAllLine(){
        String methodDescribe = getMethodDescribe("getAllLine");
        List<String> list = csLinePOService.getAllLine();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getAllLineDetail")
    @ApiOperation("获取所有监测点id详情")
    public HttpResult<List<CsLineDTO>> getAllLineDetail(){
        String methodDescribe = getMethodDescribe("getAllLineDetail");
        List<CsLineDTO> list = csLinePOService.getAllLineDetail();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getSimpleLine")
    @ApiOperation("查询登录用户拥有的监测点台账")
    public HttpResult<List<CsLinePO>> getSimpleLine(){
        String methodDescribe = getMethodDescribe("getSimpleLine");
        List<CsLinePO> list = csLinePOService.getSimpleLine();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getLineByName")
    @ApiOperation("根据名称获取监测点")
    @ApiImplicitParam(name = "lineName", value = "监测点名称", required = true)
    public HttpResult<List<CsLinePO>> getLineByName(@RequestParam("lineName") String lineName) {
        String methodDescribe = getMethodDescribe("getLineByName");
        List<CsLinePO> list = csLinePOService.getLineByName(lineName);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/list")
    @ApiOperation("查询监测点列表")
    @ApiImplicitParam(name = "param", value = "param", required = true)
    public HttpResult<List<CsLinePO>> list(@RequestBody CsLinePO param) {
        String methodDescribe = getMethodDescribe("list");
        List<CsLinePO> list = csLinePOService.getLineList(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getDevBySensitiveUser")
    @ApiOperation("根据敏感用户查询装置")
    public HttpResult<List<CsEquipmentDeliveryPO>> getDevBySensitiveUser(@RequestBody List<String> list) {
        String methodDescribe = getMethodDescribe("getDevBySensitiveUser");
        List<CsEquipmentDeliveryPO> result = csLinePOService.getDevBySensitiveUser(list);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getSensitiveUserLineList")
    @ApiOperation("获取敏感负荷用户监测点列表")
    @ApiImplicitParam(name = "param", value = "查询参数", required = true)
    public HttpResult<Page<PqSensitiveUserLineVO>> getSensitiveUserLineList(@RequestBody BaseParam param) {
        String methodDescribe = getMethodDescribe("getSensitiveUserLineList");
        Page<PqSensitiveUserLineVO> page = csLinePOService.getSensitiveUserLineList(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/uploadReport")
    @ApiOperation("上传治理报告")
    public HttpResult<Boolean> uploadReport(@ApiParam(value = "文件",name="报告文件", required = true) @RequestPart("file") MultipartFile file,@ApiParam(value = "lineId", name="监测点ID", required = true) @RequestParam String lineId) {
        String methodDescribe = getMethodDescribe("uploadReport");
        boolean success = csLinePOService.uploadReport(file, lineId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, success, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getReportUrl")
    @ApiOperation("获取治理报告链接")
    public HttpResult<String> getReportUrl(@ApiParam(value = "lineId", name="监测点ID", required = true) @RequestParam String lineId) {
        String methodDescribe = getMethodDescribe("getReportUrl");
        String url = csLinePOService.getReportUrl(lineId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, url, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getLineDetailData")
    @ApiOperation("根据监测点id获取监测点详情")
    @ApiImplicitParam(name = "id", value = "监测点id", required = true)
    public HttpResult<LineDetailDataVO> getLineDetailData(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getLineDetailData");
        LineDetailDataVO result = csLinePOService.getLineDetailData(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

}
