package com.njcn.device.pms.controller.ledgerManger;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.HttpResultUtil;
import com.njcn.device.pms.annotation.TerminalOperationLogDesc;
import com.njcn.device.pms.pojo.dto.PmsMonitorDTO;
import com.njcn.device.pms.pojo.dto.PmsMonitorInfoDTO;
import com.njcn.device.pms.pojo.param.*;
import com.njcn.device.pms.pojo.param.gw.TypicalSourceParam;
import com.njcn.device.pms.pojo.po.Monitor;
import com.njcn.device.pms.pojo.vo.MonitorVO;
import com.njcn.device.pms.pojo.vo.PmsMonitorVO;
import com.njcn.device.pms.pojo.vo.gw.TypicalSourceOnLine;
import com.njcn.device.pms.service.majornetwork.IMonitorService;

import com.njcn.device.biz.pojo.po.Overlimit;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hongawen
 * @since 2022-10-14
 */
@RestController
@RequestMapping("/pms/monitor")
@Slf4j
@Api(tags = "台账-主网监测点信息")
@RequiredArgsConstructor
public class PmsMonitorController extends BaseController {

    private final IMonitorService monitorService;

    /**
     * 获取监测点信息
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getMonitorInfo")
    @ApiOperation("获取指定的监测点信息")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "pmsMonitorInfoParam", value = "获取指定的监测点信息条件", required = true)
//    })
    public HttpResult<List<PmsMonitorInfoDTO>> getMonitorInfo(@RequestBody @Validated PmsMonitorInfoParam pmsMonitorInfoParam) {
        String methodDescribe = getMethodDescribe("getMonitorInfo");
        List<PmsMonitorInfoDTO> monitorInfo = monitorService.getMonitorInfo(pmsMonitorInfoParam);
        if (CollectionUtil.isEmpty(monitorInfo)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.NO_DATA, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, monitorInfo, methodDescribe);
        }
    }

    /***
     * 根据条件获取监测点信息列表
     * @author jianghaifei
     * @date 2022-10-20 20:12
     * @param pmsMonitorParam
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getMonitorInfoListByCond")
    @ApiOperation("根据条件获取监测点信息列表")
    public HttpResult<List<PmsMonitorDTO>> getMonitorInfoListByCond(@RequestBody PmsMonitorParam pmsMonitorParam) {
        String methodDescribe = getMethodDescribe("getMonitorInfoListByCond");
        List<PmsMonitorDTO> list = monitorService.getMonitorInfoListByCond(pmsMonitorParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    /***
     * 获取所有监测点信息
     * @author jianghaifei
     * @date 2022-10-24 20:01
     * @param
     * @return com.njcn.common.pojo.response.HttpResult<java.util.List<com.njcn.device.pms.pojo.vo.PmsMonitorVO>>
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getAllMonitorList")
    @ApiOperation("获取所有监测点信息")
    public HttpResult<List<PmsMonitorVO>> getAllMonitorList() {
        String methodDescribe = getMethodDescribe("getAllMonitorList");
        List<PmsMonitorVO> monitorList = monitorService.getAllMonitorList();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, monitorList, methodDescribe);
    }



    /**
     * 分页获取所有主网监测点
     * @author cdf
     * @date 2022/11/25
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getAllMonitorPageList")
    @ApiOperation("分页获取所有主网监测点")
    @ApiImplicitParam(name = "baseParam",required = true)
    public HttpResult<Page<Monitor>> getAllMonitorPageList(@RequestBody TerminalQueryParam baseParam) {
        String methodDescribe = getMethodDescribe("getAllMonitorList");
        Page<Monitor> monitor= monitorService.getAllMonitorPageList(baseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, monitor, methodDescribe);
    }


    /**
     * 分页获取所有主网用户台账
     * @author cdf
     * @date 2022/11/25
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getAllMainUserPageList")
    @ApiOperation("分页获取所有主网用户台账")
    @ApiImplicitParam(name = "baseParam",required = true)
    public HttpResult<Page<Monitor>> getAllMainUserPageList(@RequestBody TerminalQueryParam baseParam) {
        String methodDescribe = getMethodDescribe("getAllMainUserPageList");
        Page<Monitor> monitor= monitorService.getAllMainUserPageList(baseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, monitor, methodDescribe);
    }

    /**
     * 获取所有主网监测点
     * @author cdf
     * @date 2022/11/29
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getMonitorList")
    @ApiOperation("获取所有主网监测点(或者指定监测点编号查询)")
    @ApiImplicitParam(name = "monitorIds",value = "主网监测点编号",required = true)
    public HttpResult<List<Monitor>> getMonitorList(@RequestBody List<String> monitorIds) {
        String methodDescribe = getMethodDescribe("getMonitorList");
        List<Monitor> monitor= monitorService.getMonitorList(monitorIds);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, monitor, methodDescribe);
    }

    /**
     * 使用同源监测点编码获取监测点信息
     * @author cdf
     * @date 2022/11/29
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getMonitorListByMid")
    @ApiOperation("使用同源监测点编码获取监测点信息")
    @ApiImplicitParam(name = "midIds",value = "主网监测点编号",required = true)
    public HttpResult<List<Monitor>> getMonitorListByMid(@RequestBody List<String> midIds) {
        String methodDescribe = getMethodDescribe("getMonitorListByMid");
        List<Monitor> monitor= monitorService.getMonitorListByMid(midIds);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, monitor, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getMonitorPage")
    @ApiOperation("获取所有主网监测点(或者指定监测点编号查询)")
    @ApiImplicitParam(name = "baseParam",value = "主网监测点编号",required = true)
    public HttpResult<Page<Monitor>> getMonitorPage(@RequestBody TerminalQueryParam baseParam) {
        String methodDescribe = getMethodDescribe("getMonitorPage");
        Page<Monitor> monitor= monitorService.getMonitorPage(baseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, monitor, methodDescribe);
    }

    /**
     * 根据条件获取所有主网监测点
     * @author cdf
     * @date 2022/11/29
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getMonitorListByParam")
    @ApiOperation("根据条件获取所有主网监测点")
    @ApiImplicitParam(name = "monitorParam",value = "查询参数体",required = true)
    public HttpResult<List<Monitor>> getMonitorListByParam(@RequestBody MonitorParam monitorParam) {
        String methodDescribe = getMethodDescribe("getMonitorListByParam");
        List<Monitor> monitor= monitorService.getMonitorListByParam(monitorParam);
        if (Objects.isNull(monitor)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.NO_DATA, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, monitor, methodDescribe);
        }
    }



    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.ADD)
    @PostMapping("/addMonitor")
    @ApiOperation("新增主网监测点")
    @ApiImplicitParam(name = "monitorParam", value = "主网监测点实体", required = true)
    @TerminalOperationLogDesc(value = "新增主网监测点",module = 6)
    public HttpResult<Boolean> addMonitor(@RequestBody @Validated MonitorParam monitorParam) {
        String methodDescribe = getMethodDescribe("addMonitor");
        boolean result = monitorService.addMonitor(monitorParam);
        if(result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);

    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.UPDATE)
    @PostMapping("/updateMonitor")
    @ApiOperation("修改主网监测点")
    @TerminalOperationLogDesc(value = "修改主网监测点",module = 6)
    @ApiImplicitParam(name = "monitorParam", value = "主网监测点实体", required = true)
    public HttpResult<Boolean> updateMonitor(@RequestBody @Validated MonitorParam monitorParam) {
        String methodDescribe = getMethodDescribe("updateMonitor");
        boolean result = monitorService.updateMonitor(monitorParam);
        if(result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);

    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.UPDATE)
    @DeleteMapping("/delMonitor")
    @ApiOperation("删除主网监测点")
    @TerminalOperationLogDesc(value = "删除主网监测点",module = 6)
    @ApiImplicitParam(name = "monitorIds", value = "主网监测点ids", required = true)
    public HttpResult<Boolean> delMonitor(@RequestBody @NotEmpty(message = "主网监测点id不可为空") List<String> monitorIds) {
        String methodDescribe = getMethodDescribe("delMonitor");
        boolean result = monitorService.delMonitor(monitorIds);
        if(result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);

    }


    /**
     * 退役监测点
     * @author cdf
     * @date 2023/2/20
     */

    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.UPDATE)
    @PostMapping("/retirementMonitor")
    @ApiOperation("退役服役监测点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "0.主网 1.配网", required = true),
            @ApiImplicitParam(name = "lineIds", value = "监测点ids", required = true)
    })
    public HttpResult<Boolean> retirementMonitor(@NotNull(message = "监测类别不可为空") @RequestParam("type") Integer type,
                                                 @NotEmpty(message = "监测点不可为空") @RequestBody List<String> lineIds) {
        String methodDescribe = getMethodDescribe("retirementMonitor");
        boolean result = monitorService.retirementMonitor(type,lineIds);
        if(result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, 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 = monitorService.getOverLimitData(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.UPDATE)
    @PostMapping("/updateStatus")
    @ApiOperation("修改主网监测点数据状态")
    @ApiImplicitParam(name = "monitorParam", value = "主网监测点实体", required = true)
    public HttpResult<Boolean> updateStatus(@RequestBody @Validated MonitorStatus monitorParam) {
        String methodDescribe = getMethodDescribe("updateStatus");
        boolean result = monitorService.updateStatus(monitorParam);
        if(result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);

    }

    @PostMapping("/getMonitorTerminal")
    @ApiOperation("获取台账和终端信息(波形专用)")
    @ApiImplicitParam(name = "param", value = "主网监测点实体", required = true)
    public HttpResult<MonitorVO> getMonitorTerminal(@RequestBody MonitorTerminalParam param) {
        String methodDescribe = getMethodDescribe("getMonitorTerminal");
        MonitorVO monitorVO = monitorService.monitorTerminal(param);
        if (ObjectUtil.isEmpty(monitorVO)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.NO_DATA, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, monitorVO, methodDescribe);
        }

    }


    /**
     * oracle系统监测点转mysql主网监测点
     * @author cdf
     * @date 2023/6/8
     */
    @PostMapping("/importOracleLedgerToMysql")
    @ApiOperation("oracle系统监测点转mysql主网监测点")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.UPLOAD)
    public HttpResult<Object> importOracleLedgerToMysql(@ApiParam(value = "文件", required = true) @RequestPart("file") MultipartFile file, HttpServletResponse response) {
        String methodDescribe = getMethodDescribe("importOracleLedgerToMysql");
        String result = monitorService.importOracleLedgerToMysql(file,response);
        if (Objects.isNull(result)) {
            return null;
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);

    }


    /**
     * 附件批量修改台账信息(地市核验过的台账信息excel通过此接口导入表中,后续3.0完善后需要删除)
     * @author cdf
     * @date 2024/2/26
     */
    @PostMapping("/importUpdateLedger")
    @ApiOperation("附件批量修改台账信息")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.UPLOAD)
    public HttpResult<Object> importUpdateLedger(@ApiParam(value = "文件", required = true) @RequestPart("file") MultipartFile file, HttpServletResponse response) {
        String methodDescribe = getMethodDescribe("importUpdateLedger");
        String result = monitorService.importUpdateLedger(file,response);

        if (Objects.isNull(result)) {
            return null;
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);

    }




    public static void main(String[] args) throws IOException {
        /*try {
            // 创建一个临时文件，前缀为"temp_"，后缀为".txt"，存储在默认的临时文件夹中
            File tempFile = File.createTempFile("temp_", ".txt");

            // 输出临时文件的路径
            System.out.println("临时文件路径： " + tempFile.getAbsolutePath());

            // 关闭临时文件，以便系统自动删除它
            tempFile.deleteOnExit();
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/monitorTypicalList")
    @ApiOperation("典型源荷下穿监测点信息接口")
    public HttpResult<Page<TypicalSourceOnLine>> monitorTypicalList(@RequestBody TypicalSourceParam param) {
        String methodDescribe = getMethodDescribe("monitorTypicalList");
        Page<TypicalSourceOnLine> monitorPage = monitorService.monitorTypicalList(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, monitorPage, methodDescribe);
    }


}

