package com.hdkj.lt.bf.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdkj.lt.bf.entity.FhzgDistCustomer;
import com.hdkj.lt.bf.entity.FhzgPlanPowerCut;
import com.hdkj.lt.bf.entity.dto.BrakerTransformerRelationDTO;
import com.hdkj.lt.bf.entity.vo.LoadSwitchUserNumsVO;
import com.hdkj.lt.bf.entity.vo.SwitchByUserVO;
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.po.FeederTopo;
import com.hdkj.lt.core.bizms.modle.po.plan.FhzgPlanAwaitTransferSwitchSection;
import com.hdkj.lt.core.bizms.modle.po.plan.FhzgPlanOppositeLineCarryCapacity;
import com.hdkj.lt.core.bizms.modle.po.plan.FhzgPlanOppositeLineConcatSwitch;
import com.hdkj.lt.core.bizms.modle.po.plan.FhzgPlanPowerCutSwitch;
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 org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

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

/**
 * 停电计划转供方案过程态详情分页查询
 *
 * @author zhuzhe
 * @since 2025-05-16
 */
@Mapper
public interface FhzgGenerateGraphMapper {

    /**
     * 根据计划id查询停电范围
     *
     * @param dto
     * @return
     */
    List<FhzgPlanPowerCutSwitch> selectPowerCutSwitchList(SchemeQueryDTO dto);

    FhzgPlanPowerCut selectFhzgPlanById(Long planId);

    /**
     * 查询转供范围
     *
     * @param dto
     * @return
     */
    List<FhzgPlanAwaitTransferSwitchSection> selectSwitchSectionList(SchemeQueryDTO dto);

    /**
     * 查询用户信息
     *
     * @param customerId
     * @return
     */
    FhzgDistCustomer selectCustomerById(Long customerId);

    /**
     * 根据低压用户查询所属开关
     *
     * @param psrId,kgPsrType
     * @return
     */
    List<SwitchByUserVO> selectSwitchByCustomer(@Param("psrId") String psrId, @Param("kgPsrType") List<String> kgPsrType);

    /**
     * 查询对侧线路
     *
     * @param capacityId
     * @return
     */
    FhzgPlanOppositeLineCarryCapacity getOppositeLineCarryCapacity(@Param("capacityId") Long capacityId);


    /**
     * 查询对侧线路联络开关
     *
     * @param capacityId
     * @return
     */
    List<FhzgPlanOppositeLineConcatSwitch> getOppositeLineConcatSwitch(Long capacityId);

    @Select("select * from t_feeder_topo ${ew.customSqlSegment} ")
    List<FeederTopo> getFeederTopo(@Param(Constants.WRAPPER) LambdaQueryWrapper<FeederTopo> wrapper);

    // 根据线路查找开关之间的树形结构，不包括配变
    @Select(" <script>" +
            "WITH RECURSIVE hierarchy AS (\n" +
            "    SELECT psr_id, name, start_kg_id, 1 AS level  \n" +
            "    FROM t_feeder_topo \n" +
            "    WHERE feeder = #{feeder} \n" +
            "        AND psr_type IN " +
            "             <foreach collection = 'kgPsrType' item = 'item' open = '(' separator = ',' close = ')'>" +
            "               #{item}  " +
            "             </foreach>" +
            "     <if test= 'isStartKg != null' >  " +
            "        AND is_start_kg = #{isStartKg} " +
            "     </if>  \n" +
            "     <if test= 'parentId != null' >  " +
            "        AND psr_id = #{parentId} " +
            "     </if>  \n" +
            "    UNION ALL\n" +
            "    SELECT \n" +
            "        child.psr_id,\n" +
            "        child.name,\n" +
            "        child.start_kg_id,\n" +
            "        parent.level + 1 AS level\n" +
            "    FROM t_feeder_topo child\n" +
            "    JOIN hierarchy parent ON child.start_kg_id = parent.psr_id\n" +
            "    WHERE child.feeder = #{feeder}\n" +
            "        AND child.psr_type IN " +
            "             <foreach collection = 'kgPsrType' item = 'item' open = '(' separator = ',' close = ')'>" +
            "               #{item}  " +
            "             </foreach>" +
            ")\n" +
            "SELECT \n" +
            "    child.level,\n" +
            "    parent.psr_id AS parent_psr_id,\n" +
            "    parent.name AS parent_name,\n" +
            "    child.psr_id AS child_psr_id,\n" +
            "    child.name AS child_name \n" +
            "FROM  hierarchy child  \n" +
            "RIGHT JOIN hierarchy parent ON child.start_kg_id = parent.psr_id \n" +
            "GROUP BY parent.psr_id, child.psr_id, parent.name, child.name, child.level \n" +
            "order by child.level " +
            "</script>")
    List<BrakerTransformerRelationDTO> getBreakerTree(@Param("isStartKg") String isStartKg, @Param("parentId") String parentId, @Param("feeder") String feeder, @Param("kgPsrType") List<String> kgPsrType);


    /**
     * 查询开关段
     *
     * @param sectionId
     * @return
     */
    @Select("select * from fhzg_plan_await_transfer_switch_section where id = #{sectionId} ")
    FhzgPlanAwaitTransferSwitchSection getSwitchSectionById(@Param("sectionId") Long sectionId);

    @Select("select a.load_switch_id,count(distinct a.start_switch_id) as switchNum ,count(1) userNum \n" +
            "from fhzg_plan_await_transfer_switch_section a,\n" +
            "(SELECT DISTINCT if(b.psr_id is null, a.transformer_no, b.join_ec) as psr_id, a.id\n" +
            "   from fhzg_dist_transformer_customer a \n" +
            "   left join dwd_shb_ds_joinec_transformer_base b on a.transformer_no = b.psr_id) b\n" +
            "where a.start_switch_id = b.psr_id \n" +
            "  and a.plan_id = #{planId} \n" +
            "  group by a.load_switch_id   \n")
    List<LoadSwitchUserNumsVO> getLoadUserNums(@Param("planId") Long planId);

    @Select("select a.start_switch_id as psr_id, count(distinct b.id) as userNum \n" +
            "from fhzg_plan_await_transfer_switch_section a, fhzg_plan_await_transfer_users b  \n" +
            "where a.plan_id = b.plan_id  \n" +
            "  and a.id = b.switch_section_id  \n" +
            "  and a.plan_id = #{planId}  \n" +
            "  and a.load_switch_id = #{loadSwitchId}  \n" +
            "group by a.start_switch_id")
    List<LoadSwitchUserNumsVO> getUserNumsByLoad(SchemeQueryDTO dto);

    List<LoadSwitchUserNumsVO> getUserNumsByLine(@Param("feeder") String feederId, @Param("kgPsrTypes") List<String> kgPsrTypes);

    String selectSwitchByCustomerId(@Param("customerId") String customerId);

    List<LoadSwitchUserNumsVO> getAllUserNumsByLine(@Param("feeder") String feederId, @Param("pbPsrTypes") List<String> pbPsrTypes);

    Page<UserListVO> getUserListByLoadSwitchSegmentPage(Page page, @Param("params") UserListQueryDTO dto, List<String> pbPsrType);

    List<UserListVO> getUserListByLoadSwitchSegmentPageNew(@Param("params") UserListQueryDTO dto, List<String> pbPsrType);

    @Select("select psr_id psrId,cons_no consNo,name consName  from dwd_shb_ds_joinec_transformer_base  where join_ec=#{joinEc}")
    List<JoinecUserVO> getJoinecUserListByPsrId(@Param("joinEc") String joinEc);

    Integer isMainFeeder(@Param("feeder") String feederId);

    List<String> getBreakerPsrId(@Param("startKg") String startKg, @Param("feeder") String feeder, @Param("endKg") String endKg, @Param("kgPsrTypes") List<String> kgPsrTypes);

    List<Map<String, String>> getBreakerPsrIdByPlanId(@Param("planId") Long planId);

    List<Map<String, String>> getBreakerPsrIdByFaultId(@Param("faultId") Long faultId);

    Page<SwitchListVO> getSwitchInfoByLoadSwitchId(Page page, @Param("params") UserListQueryDTO dto, List<String> kgType);

    List<Map<String, String>> getOtherSwitchByObjId(@Param("objId") String objId, @Param("kgType") List<String> kgType);

    List<String> getBusbarBySwitchIds(@Param("switchIds")List<String> switchIds);

    String getStationName(@Param("psrId") String psrId, @Param("lineId") String lineId);

    String getSwitchListByOutArea(@Param("id") Long planId);

    void saveSwitchListByOutArea(@Param("id") Long planId, @Param("outArea") String jsonString);

    String getPathOverviewSwitch(@Param("id") Long planId, @Param("schemeId") Long schemeId);

    void savePathOverviewSwitch(@Param("id") Long planId, @Param("schemeId") Long schemeId, @Param("PathOverviewSwitch") String PathOverviewSwitch);

    String getSwitchListBySwitchSection(@Param("id") Long planId);

    void saveSwitchListBySwitchSection(@Param("id") Long planId, @Param("SwitchListBySwitchSection") String SwitchListBySwitchSection);

    String getZbPsrId(@Param("psrId") String psrId);
}
