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

import cn.hutool.core.collection.CollectionUtil;
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.GeneratrixAndPowerStationDTO;
import com.njcn.device.pms.pojo.dto.GeneratrixAndPowerStationSonDTO;
import com.njcn.device.pms.pojo.dto.PmsGeneratrixDTO;
import com.njcn.device.pms.pojo.param.ConditionParam;
import com.njcn.device.pms.pojo.param.GeneratrixWireParam;
import com.njcn.device.pms.pojo.param.PmsBaseParam;
import com.njcn.device.pms.pojo.param.PmsGeneratrixParam;
import com.njcn.device.pms.pojo.po.GeneratrixWire;
import com.njcn.device.pms.pojo.po.PmsMidLedger;
import com.njcn.device.pms.service.majornetwork.IGeneratrixWireService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.pojo.param.BaseParam;
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 javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Objects;

/**
 * pms-device
 *
 * @author cdf
 * @date 2022/10/26
 */
@RestController
@RequestMapping("/pms/generatrixWire")
@Slf4j
@Api(tags = "台账-线路")
@RequiredArgsConstructor
public class PmsGeneratrixWireController extends BaseController {

    private final IGeneratrixWireService iGeneratrixWireService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.ADD)
    @PostMapping("/addGeneratrixWire")
    @ApiOperation("新增线路")
    @ApiImplicitParam(name = "generatrixWireParam", value = "线路实体", required = true)
    @TerminalOperationLogDesc(value = "新增线路",module = 2)
    public HttpResult<Boolean> addGeneratrixWire(@RequestBody @Validated GeneratrixWireParam generatrixWireParam) {
        String methodDescribe = getMethodDescribe("addGeneratrixWire");
        boolean result = iGeneratrixWireService.addGeneratrixWire(generatrixWireParam);
        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("/updateGeneratrixWire")
    @ApiOperation("修改线路")
    @ApiImplicitParam(name = "generatrixWireParam", value = "线路实体", required = true)
    @TerminalOperationLogDesc(value = "修改线路",module = 2)
    public HttpResult<Boolean> updateGeneratrixWire(@RequestBody @Validated GeneratrixWireParam generatrixWireParam) {
        String methodDescribe = getMethodDescribe("updateGeneratrixWire");
        boolean result = iGeneratrixWireService.updateGeneratrixWire(generatrixWireParam);
        if(result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);

    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.DELETE)
    @DeleteMapping("/delGeneratrixWire")
    @ApiOperation("删除线路")
    @ApiImplicitParam(name = "generatrixWireIds", value = "线路ids", required = true)
    @TerminalOperationLogDesc(value = "删除线路",module = 2)
    public HttpResult<Boolean> delGeneratrixWire(@RequestBody @NotEmpty(message = "线路id不可为空") List<String> generatrixWireIds) {
        String methodDescribe = getMethodDescribe("delGeneratrixWire");
        boolean result = iGeneratrixWireService.delGeneratrixWire(generatrixWireIds);
        if(result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);

    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getGeneratrixWireById")
    @ApiOperation("根据线路id获取线路")
    @ApiImplicitParam(name = "generatrixWireId", value = "线路id", required = true)
    public HttpResult<GeneratrixWire> getGeneratrixWireById(@RequestParam("generatrixWireId") @NotBlank(message = "线路id为空") String generatrixWireId) {
        String methodDescribe = getMethodDescribe("getGeneratrixWireById");
        GeneratrixWire result = iGeneratrixWireService.getGeneratrixWireById(generatrixWireId);
        if(Objects.nonNull(result)){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);

    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getGeneratrixWireList")
    @ApiOperation("获取所有线路下拉框使用")
    public HttpResult<List<GeneratrixWire>> getGeneratrixWireList(@RequestBody PmsBaseParam pmsBaseParam) {
        String methodDescribe = getMethodDescribe("getGeneratrixWireList");
        List<GeneratrixWire> result = iGeneratrixWireService.getGeneratrixWireList(pmsBaseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getGeneratrixWirePageList")
    @ApiOperation("分页获取线路列表")
    @ApiImplicitParam(name = "baseParam", value = "基本查询体", required = true)
    public HttpResult<Page<GeneratrixWire>> getGeneratrixWirePageList(@RequestBody BaseParam baseParam) {
        String methodDescribe = getMethodDescribe("getGeneratrixWirePageList");
        Page<GeneratrixWire> result = iGeneratrixWireService.getGeneratrixWirePageList(baseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


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

    /**
     * 获取母线与电站关联信息
     *
     * @param param 条件参数
     * @return com.njcn.common.pojo.response.HttpResult<java.util.List < com.njcn.device.pms.pojo.dto.PmsGeneratrixDTO>>
     * @author yzh
     * @date 2022/11/3
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getGeneratrixAndPowerStationInfo")
    @ApiOperation("获取母线与电站关联信息")
    @ApiImplicitParam(name = "param", value = "条件参数", required = true)
    public HttpResult<List<GeneratrixAndPowerStationDTO>> getGeneratrixAndPowerStationInfo(@RequestBody @Validated PmsGeneratrixParam param) {
        String methodDescribe = getMethodDescribe("getGeneratrixAndPowerStationInfo");
        List<GeneratrixAndPowerStationDTO> monitorInfo = iGeneratrixWireService.getGeneratrixAndPowerStationInfo(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, monitorInfo, methodDescribe);
    }

    /**
     * @Description: 普测计划页面调用根据条件查询母线信息
     * @Param: [param]
     * @return: com.njcn.common.pojo.response.HttpResult<java.util.List<com.njcn.device.pms.pojo.dto.GeneratrixAndPowerStationDTO>>
     * @Author: clam
     * @Date: 2022/12/5
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getGeneratrixByCondition")
    @ApiOperation("普测计划页面调用根据条件查询母线信息")
    @ApiImplicitParam(name = "param", value = "条件参数", required = true)
    public HttpResult<List<GeneratrixAndPowerStationSonDTO>> getGeneratrixByCondition(@RequestBody @Validated ConditionParam param) {
        String methodDescribe = getMethodDescribe("getGeneratrixByCondition");
        List<GeneratrixAndPowerStationSonDTO> monitorInfo = iGeneratrixWireService.getGeneratrixByCondition(param);
        if (CollectionUtil.isEmpty(monitorInfo)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.NO_DATA, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, monitorInfo, methodDescribe);
        }
    }


    /**
     * 根据电站id匹配中台母线信息
     * @param stationId
     * @return
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getMidBusBar")
    @ApiOperation("根据电站id匹配中台母线信息")
    @ApiImplicitParam(name = "station", value = "电站id", required = true)
    public HttpResult<List<PmsMidLedger>> getBusBar(@RequestParam("station") String stationId) {
        String methodDescribe = getMethodDescribe("getBusBar");
        List<PmsMidLedger> pmsMidLedgerList = iGeneratrixWireService.getBusBar(stationId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, pmsMidLedgerList, methodDescribe);
    }


    /**
     * 同步中台母线id
     * @author cdf
     * @date 2023/12/19
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/batchSync")
    @ApiOperation("同步中台母线id")
    public HttpResult<Boolean> batchSync() {
        String methodDescribe = getMethodDescribe("batchSync");
        iGeneratrixWireService.batchSync();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
    }

}
