package com.hdkj.lt.mq.mapper.base;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.hdkj.lt.core.bizms.modle.po.FeederTopo;
import com.hdkj.lt.core.bizms.modle.po.plan.FhzgPlanPowerCutPvUsers;
import com.hdkj.lt.core.bizms.modle.po.plan.FhzgPlanPowerCutSwitch;
import com.hdkj.lt.mq.entity.dto.BrakerTransformerRelationDTO;
import com.hdkj.lt.mq.entity.dto.TopoDTO;
import com.hdkj.lt.mq.entity.po.FhzgFaultPowerCut;
import com.hdkj.lt.mq.entity.po.FhzgPlanPowerCut;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 查询topo表数据 Mapper接口
 *
 * @author zhousc
 * @since 2025-05-07
 */
@Mapper
public interface FhzgReasoningPlatformMapper extends BaseMapper<FeederTopo> {

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

    @Select("select a.* from t_feeder_topo a, t_feeder_topo b where a.psr_id = b.start_kg_id where b.psr_id = #{psrId} ")
    List<FeederTopo> getParentByPsrId(@Param("psrId") String psrId);

    @Select("select * from fhzg_plan_power_cut where id = #{planId} ")
    FhzgPlanPowerCut getFhzgPlanPowerCut(@Param("planId") Long planId);

    @Select("select * from fhzg_fault_power_cut where id = #{faultId} ")
    FhzgFaultPowerCut getFhzgFaultPowerCut(@Param("faultId") Long faultId);

    // 根据线路查找开关之间的树形结构，不包括配变
    @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" +
            "LEFT 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);


    // 根据线路查找开关之间的树形结构，包括配变，不包含层级
    @Select("<script> " +
            "  SELECT \n" +
            "    parent.psr_id parent_psr_id,\n" +
            "    parent.name parent_name,\n" +
            "    child.psr_id AS child_psr_id,\n" +
            "    child.name as child_name,\n" +
            "    GROUP_CONCAT(trans.psr_id SEPARATOR ',') device_ids,\n" +
            "    GROUP_CONCAT(trans.name SEPARATOR ',') device_names\n" +
            "FROM t_feeder_topo child\n" +
            "LEFT JOIN  t_feeder_topo parent ON child.start_kg_id = parent.psr_id " +
            "     AND parent.psr_type in  " +
            "        <foreach collection = 'kgPsrType' item = 'item' open = '(' separator = ',' close = ')'>" +
            "            #{item}  " +
            "        </foreach>" +
            "LEFT JOIN  t_feeder_topo trans ON trans.start_kg_id = parent.psr_id  " +
            "     AND trans.psr_type in  " +
            "        <foreach collection = 'pbPsrType' item = 'item' open = '(' separator = ',' close = ')'>" +
            "            #{item}  " +
            "        </foreach>" +
            "WHERE child.feeder = #{feeder} \n" +
            "    AND child.psr_type in " +
            "        <foreach collection = 'kgPsrType' item = 'item' open = '(' separator = ',' close = ')'>" +
            "            #{item}  " +
            "        </foreach>" +
            "GROUP BY parent.psr_id, child.psr_id,parent.name,child.name" +
            "</script>")
    List<BrakerTransformerRelationDTO> getBreakerTransformerTree(@Param("feeder") String feeder,
                                                                 @Param("kgPsrType")List<String> kgPsrType, @Param("pbPsrType")List<String> pbPsrType);

    @Select(" <script>" +
            "WITH RECURSIVE hierarchy AS (\n" +
            "    SELECT psr_id, name, start_kg_id  \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= '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" +
            "    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" +
            "    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" +
           "   GROUP_CONCAT(trans.psr_id SEPARATOR ',') device_ids, \n" +
           "   GROUP_CONCAT(trans.name SEPARATOR ',') device_names \n" +
            "  FROM  hierarchy child  \n" +
            "  LEFT JOIN hierarchy parent ON child.start_kg_id = parent.psr_id \n" +
            "  LEFT JOIN t_feeder_topo trans ON trans.start_kg_id = parent.psr_id \n" +
            "    AND trans.psr_type in" +
            "    <foreach collection = 'pbPsrType' item = 'item' open = '(' separator = ',' close = ')'>" +
            "            #{item}  " +
            "        </foreach>" +
            "GROUP BY parent.psr_id, child.psr_id, parent.name, child.name \n" +
            "</script>")
    List<BrakerTransformerRelationDTO> getBreakerTransformerByStartPsrId(@Param("feeder") String feeder, @Param("parentId") String parentId,
                                                                 @Param("kgPsrType")List<String> kgPsrType, @Param("pbPsrType")List<String> pbPsrType);


    @Select("<script> " +
            "  SELECT \n" +
            "    t.start_kg_id parent_psr_id,   \n" +
            "    t.start_kg_name parent_name,    \n" +
            "    GROUP_CONCAT(t.psr_id SEPARATOR ',') device_ids,  \n" +
            "    GROUP_CONCAT(t.name SEPARATOR ',') device_names   \n" +
            "  FROM t_feeder_topo t where start_kg_id = #{parentId} " +
            "   and t.psr_type in  " +
            "        <foreach collection = 'pbPsrType' item = 'item' open = '(' separator = ',' close = ')'>" +
            "            #{item}  " +
            "        </foreach>" +
            " GROUP BY t.start_kg_id, t.start_kg_name" +
            "</script>")
    BrakerTransformerRelationDTO getBreakerByswitch(@Param("feeder") String feeder, @Param("parentId") String parentId,
                                                          @Param("pbPsrType") List<String> pbPsrType);

    @Insert("insert into fhzg_plan_power_cut_switch(id,plan_id,start_switch,end_switch,remark,status,is_full_line,create_time) " +
            " values(#{id},#{planId},#{startSwitch},#{endSwitch},#{remark},#{status},#{isFullLine},NOW())")
    void savePowerCutSwitch(FhzgPlanPowerCutSwitch powerCutSwitch);

    @Insert("<script> " +
            " insert into fhzg_plan_power_cut_pv_users(cust_id, plan_id ,user_type,create_time)\n" +
            " select DISTINCT a.cust_id as cust_id, #{powerCutPvUsers.planId} as plan_id , a.user_type,now() " +
            "from fhzg_dist_pv_customer a " +
            " where a.transformer_no in " +
            "        <foreach collection = 'pbList' item = 'item' open = '(' separator = ',' close = ')'>" +
            "            #{item}  " +
            "        </foreach>" +
            "</script> ")
    void savePowerCutUsers(@Param("powerCutPvUsers") FhzgPlanPowerCutPvUsers powerCutPvUsers,
                              @Param("pbList") List<String> pbList);

    @Delete("delete from fhzg_plan_power_cut_switch where plan_id = #{planId}")
    void deletePowerCutSwitch(@Param("planId") Long planId);
    @Delete("delete from fhzg_plan_power_cut_pv_users where plan_id = #{planId}")
    void deletePowerCutUsers(@Param("planId") Long planId);

    List<TopoDTO> getBreakListByFeeder(@Param("feeder") String feeder);
}
