package com.hdkj.lt.bf.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdkj.lt.bf.entity.dto.CustomerAccountDTO;
import com.hdkj.lt.bf.entity.vo.*;
import com.hdkj.lt.bf.service.FhzgGenerateGraphService;
import com.hdkj.lt.core.bizms.modle.dto.SchemeQueryDTO;
import com.hdkj.lt.core.bizms.modle.dto.UserListQueryDTO;
import com.hdkj.lt.core.bizms.modle.vo.JoinecUserVO;
import com.hdkj.lt.core.bizms.modle.vo.SwitchListVO;
import com.hdkj.lt.core.bizms.modle.vo.UserListVO;
import com.hdkj.lt.core.mvc.BaseController;
import com.hdkj.hussar.ApiResponse;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 成图服务
 *
 * @author zhuzhe
 * @since 2025-05-22
 */
@Slf4j
@RestController
@RequestMapping("/fhzgGenerateGraph")
@RequiredArgsConstructor
public class FhzgGenerateGraphController extends BaseController {

    private final FhzgGenerateGraphService fhzgGenerateGraph;

    /**
     * 通过停电范围查询开关
     *
     * @param dto
     * @return
     */
    @PostMapping("/getSwitchListByOutArea")
    @ApiOperation(value = "通过停电范围查询开关")
    public ApiResponse<List<BreakerAreaVO>> getSwitchListByOutArea(@RequestBody SchemeQueryDTO dto) {
        if (dto == null) {
            return fail("参数不能为空");
        }
        if (dto.getPlanId() == null) {
            return fail("计划ID不能为空");
        }
        List<BreakerAreaVO> breakerList = fhzgGenerateGraph.getSwitchListByOutArea(dto);

        return ApiResponse.success(breakerList);
    }

    /**
     * 查询转供范围开关段
     *
     * @param dto
     * @return
     */
    @PostMapping("/getSwitchListByTransferSection")
    @ApiOperation(value = "查询转供范围开关段")
    public ApiResponse<List<BreakerAreaVO>> getSwitchListByTransferSection(@RequestBody SchemeQueryDTO dto) {
        if (dto == null) {
            return fail("参数不能为空");
        }
        if (dto.getPlanId() == null) {
            return fail("计划ID不能为空");
        }
        List<BreakerAreaVO> breakerList = fhzgGenerateGraph.getSwitchListByTransferSection(dto);

        return ApiResponse.success(breakerList);
    }

    /**
     * 根据用户查询开关
     *
     * @param dto
     * @return
     */
    @PostMapping("/getSwitchByCustomer")
    @ApiOperation(value = "根据用户查询开关")
    public ApiResponse<SwitchByUserVO> getSwitchByCustomer(@RequestBody CustomerAccountDTO dto) {
        if (dto == null) {
            return fail("参数不能为空");
        }
        if (dto.getCustomerId() == null) {
            return fail("用户ID不能为空");
        }
        SwitchByUserVO switchVO = fhzgGenerateGraph.getSwitchByCustomer(dto);

        return ApiResponse.success(switchVO);
    }


    /**
     * 通过配变查上一级开关
     *
     * @param dto
     * @return
     */
    @PostMapping("/getSwitchByDevice")
    @ApiOperation(value = "根据配变查询开关")
    public ApiResponse<SwitchByUserVO> getSwitchByDevice(@RequestBody CustomerAccountDTO dto) {
        if (dto == null) {
            return fail("参数不能为空");
        }
        if (dto.getTransformerNo() == null) {
            return fail("配变不能为空");
        }
        SwitchByUserVO switchVO = fhzgGenerateGraph.getSwitchByDevice(dto);

        return ApiResponse.success(switchVO);
    }

    /**
     * 转供路径概述，查询开关段
     *
     * @param dto
     * @return
     */
    @PostMapping("/getPathOverviewSwitch")
    @ApiOperation(value = "转供路径概述查询开关段")
    public ApiResponse<List<BreakerAreaVO>> getPathOverviewSwitch(@RequestBody SchemeQueryDTO dto) {

        if (dto == null) {
            return fail("参数不能为空");
        }
        if (dto.getPlanId() == null) {
            return fail("计划ID不能为空");
        }
        if (dto.getSchemeId() == null) {
            return fail("方案ID不能为空");
        }
        List<BreakerAreaVO> breakerAreaList = fhzgGenerateGraph.getPathOverviewSwitch(dto);

        return ApiResponse.success(breakerAreaList);
    }

    /**
     * 待转供范围分析-对侧链路承载力分析 对侧链路联络开关--默认一条
     *
     * @param dto
     * @return
     */
    @PostMapping("/getOppesiteConcatSwitch")
    @ApiOperation(value = "对侧链路联络开关")
    public ApiResponse<OppesiteConcatSwitchVO> getOppesiteConcatSwitch(@RequestBody SchemeQueryDTO dto) {

        if (dto == null) {
            return fail("参数不能为空");
        }
        if (dto.getPlanId() == null) {
            return fail("计划ID不能为空");
        }
        if (dto.getCapacityId() == null) {
            return fail("对侧线路ID不能为空");
        }
        OppesiteConcatSwitchVO oppesiteConcatSwitchVO = fhzgGenerateGraph.getOppesiteConcatSwitch(dto);

        return ApiResponse.success(oppesiteConcatSwitchVO);
    }

    /**
     * 根据开关段查询开关
     *
     * @param dto
     * @return
     */
    @PostMapping("/getSwitchListBySwitchSection")
    @ApiOperation(value = "根据开关段查询开关")
    public ApiResponse<BreakerAreaVO> getSwitchListBySwitchSection(@RequestBody SchemeQueryDTO dto) {
        if (dto == null) {
            return fail("参数不能为空");
        }
        if (dto.getSectionId() == null) {
            return fail("开关段ID不能为空");
        }
        BreakerAreaVO breakerArea = fhzgGenerateGraph.getSwitchListBySwitchSection(dto);

        return ApiResponse.success(breakerArea);
    }

    @PostMapping("/getPlanConfirmState")
    @ApiOperation(value = "查询计划确认状态")
    public ApiResponse<PlanConfirmStateVO> getPlanConfirmState(@RequestBody SchemeQueryDTO dto) {
        if (dto == null) {
            return fail("参数不能为空");
        }
        if (dto.getPlanId() == null) {
            return fail("计划ID不能为空");
        }
        PlanConfirmStateVO breakerArea = fhzgGenerateGraph.getPlanConfirmState(dto);

        return ApiResponse.success(breakerArea);
    }


    @PostMapping("/getLoadUserNums")
    @ApiOperation(value = "查询负荷段开关、分支开关、用户数量")
    public ApiResponse<List<LoadSwitchUserNumsVO>> getLoadUserNums(@RequestBody SchemeQueryDTO dto) {
        if (dto == null) {
            return fail("参数不能为空");
        }
        if (dto.getPlanId() == null) {
            return fail("计划ID不能为空");
        }
        List<LoadSwitchUserNumsVO> numsList = fhzgGenerateGraph.getLoadUserNums(dto);

        return ApiResponse.success(numsList);
    }


    @PostMapping("/getUserNumsByLoad")
    @ApiOperation(value = "通过负荷段开关查询用户数量")
    public ApiResponse<List<LoadSwitchUserNumsVO>> getUserNumsByLoad(@RequestBody SchemeQueryDTO dto) {
        if (dto == null) {
            return fail("参数不能为空");
        }
        if (dto.getPlanId() == null) {
            return fail("计划ID不能为空");
        }
        if (dto.getLoadSwitchId() == null) {
            return fail("负荷段开关ID不能为空");
        }

        List<LoadSwitchUserNumsVO> numsList = fhzgGenerateGraph.getUserNumsByLoad(dto);

        return ApiResponse.success(numsList);
    }

    @PostMapping("getUserNumsByLine")
    @ApiOperation(value = "查询线路所有分支用户数量")
    public ApiResponse<List<LoadSwitchUserNumsVO>> getUserNumsByLine(@RequestBody SchemeQueryDTO dto) {
        if (dto == null) {
            return fail("参数不能为空");
        }
        if (dto.getFeederId() == null) {
            return fail("线路Id不能为空");
        }
        List<LoadSwitchUserNumsVO> numsList = fhzgGenerateGraph.getUserNumsByLine(dto.getFeederId());

        return ApiResponse.success(numsList);
    }
    @PostMapping("getAllUserNumsByLine")
    @ApiOperation(value = "查询线路(含对侧)所有分支用户数量")
    public ApiResponse<List<LoadSwitchUserNumsVO>> getAllUserNumsByLine(@RequestBody SchemeQueryDTO dto) {
        if (dto == null) {
            return fail("参数不能为空");
        }
        if (dto.getFeederId() == null) {
            return fail("线路Id不能为空");
        }
        List<LoadSwitchUserNumsVO> numsList = fhzgGenerateGraph.getAllUserNumsByLine(dto.getFeederId());

        return ApiResponse.success(numsList);
    }

    /**
     * 根据起始开关和结束开关，查询负荷开关段下的用户详细信息
     * @author luorui
     * @date 2025/6/11 16:28
     *
     **/
    @PostMapping("/getUserListByLoadSwitchSegmentPage")
    @ApiOperation(value = "根据起始开关和结束开关，查询负荷开关段下的用户详细信息")
    public ApiResponse<Page<UserListVO>> getUserListByLoadSwitchSegmentPage(@RequestBody UserListQueryDTO dto) {
        if (dto == null) {
            return fail("参数不能为空");
        }
        if (dto.getPageSize() == null) {
            return fail("页长不能为空");
        }
        if (dto.getPageNum() == null) {
            return fail("页码不能为空");
        }
//        if (StrUtil.isBlank(dto.getQueryType())) {
//            return fail("查询类型不能为空");
//        }
        if (StrUtil.isBlank(dto.getLoadSwitchId())) {
            return fail("负荷开关id不能为空");
        }
        if (StrUtil.isBlank(dto.getObjId())) {
            return fail("场景id不能为空");
        }
//        if (StrUtil.equals(dto.getQueryType(),"1") && StrUtil.isBlank(dto.getLineId())) {
//            return fail("线路id不能为空");
//        }
//        Page<UserListVO> result = fhzgGenerateGraph.getUserListByLoadSwitchSegmentPage(dto);
        Page<UserListVO> result = fhzgGenerateGraph.getUserListByLoadSwitchSegmentPageNew(dto);
        return ApiResponse.success(result);
    }

    /**
     * 根据中压接入点查询专表列表
     * @author luorui
     * @date 2025/6/11 16:28
     *
     **/
    @PostMapping("/getJoinecUserListByPsrId")
    @ApiOperation(value = "根据中压接入点查询专表列表")
    public ApiResponse< List<JoinecUserVO>> getJoinecUserListByPsrId(@RequestBody UserListQueryDTO dto) {
        if (dto == null) {
            return fail("参数不能为空");
        }
        if (StrUtil.isBlank(dto.getJoinEc()) ) {
            return fail("中压接入点psrId不能为空");
        }
        List<JoinecUserVO>  result = fhzgGenerateGraph.getJoinecUserListByPsrId(dto.getJoinEc());
        return ApiResponse.success(result);
    }

    /**
     * 查询负荷开关段下的开关信息
     * @author lihao
     * @date 2025/7/10 16:28
     *
     **/
    @PostMapping("/getSwitchInfoByLoadSwitchSegmentPage")
    @ApiOperation(value = "查询负荷开关段下的开关信息")
    public ApiResponse<Page<SwitchListVO>> getSwitchInfoByLoadSwitchSegmentPage(@RequestBody UserListQueryDTO dto) {
        if (dto == null) {
            return fail("参数不能为空");
        }
        if (dto.getPageSize() == null) {
            return fail("页长不能为空");
        }
        if (dto.getPageNum() == null) {
            return fail("页码不能为空");
        }
        if (StrUtil.isBlank(dto.getLoadSwitchId())) {
            return fail("负荷开关id不能为空");
        }
        if (StrUtil.isBlank(dto.getObjId())) {
            return fail("场景id不能为空");
        }
        Page<SwitchListVO> result = fhzgGenerateGraph.getSwitchInfoByLoadSwitchSegmentPage(dto);
        return ApiResponse.success(result);
    }

}
