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

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hdkj.lt.core.bizms.modle.po.FeederTopo;
import com.hdkj.lt.core.bizms.modle.po.GridPdOutageEventBase;
import com.hdkj.lt.core.bizms.modle.po.fault.FaultPowerCutPvUsers;
import com.hdkj.lt.core.bizms.modle.po.fault.FaultPowerCutSwitch;
import com.hdkj.lt.mq.entity.dto.BrakerTransformerRelationDTO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 查询故障信息 Mapper接口
 * @author zhuzhe
 * @since 2025-05-30
 */
@Mapper
public interface FaultFhzgReasoningPlatformMapper extends BaseMapper<FeederTopo> {

    // 查询故障信息
    @Select("select * from grid_pd_outage_event_base where id = #{faultId} ")
    GridPdOutageEventBase getFaultInfoById(@Param("faultId") String faultId);

    @Insert("insert into fhzg_fault_power_cut_switch(id,fault_id,start_switch,end_switch,remark,status,is_full_line,analysis_json,create_time) " +
            " values(#{id},#{faultId},#{startSwitch},#{endSwitch},#{remark},#{status},#{isFullLine},#{analysisJson},NOW())")
    void saveFaultPowerCutSwitch(FaultPowerCutSwitch powerCutSwitch);

    @Select(" <script>" +
            "  WITH RECURSIVE hierarchy AS (\n" +
            "    SELECT \n" +
            "        psr_id,\n" +
            "        name,\n" +
            "        start_kg_id,\n" +
            "        psr_type," +
            "        is_start_kg, \n" +
            "        1 AS level\n" +
            "    FROM  t_feeder_topo\n" +
            "    WHERE feeder = #{feeder} \n" +
            "        AND psr_id = #{psrId} \n" +
            "    UNION ALL\n" +
            "    SELECT \n" +
            "        parent.psr_id,\n" +
            "        parent.name,\n" +
            "        parent.start_kg_id,\n" +
            "        parent.psr_type," +
            "        parent.is_start_kg, " +
            "        child.level + 1 AS level\n" +
            "    FROM  t_feeder_topo parent\n" +
            "    JOIN  hierarchy child ON child.start_kg_id = parent.psr_id\n" +
            "    WHERE  parent.feeder = #{feeder}\n" +
            ")\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" +
            "    child.psr_type AS child_psr_type, " +
            "    m.dwd_psr_type, " +
            "    child.is_start_kg  \n" +
            "FROM \n" +
            "    hierarchy child\n" +
            "LEFT JOIN \n" +
            "    hierarchy parent ON child.start_kg_id = parent.psr_id " +
            "LEFT JOIN fhzg_device_type_mapping m on child.psr_type = m.topo_psr_type " +
            "  order by child.level" +
            "</script>")
    List<BrakerTransformerRelationDTO> getFaultTree(@Param("feeder")String feeder, @Param("psrId")String psrId, @Param("kgPsrType")List<String> kgPsrType);


    @Delete("delete from fhzg_fault_power_cut_switch where fault_id = #{faultId}")
    void deleteFaultPowerCutSwitch(@Param("faultId") Long faultId);

    @Delete("delete from fhzg_fault_power_cut_pv_users where fault_id = #{faultId}")
    void deleteFaultPowerCutUsers(@Param("faultId") Long faultId);


    @Insert("<script> " +
            " insert into fhzg_fault_power_cut_pv_users(cust_id, fault_id ,user_type,create_time)\n" +
            " select DISTINCT a.cust_id as cust_id, #{powerCutPvUsers.faultId} as fault_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") FaultPowerCutPvUsers powerCutPvUsers,
                           @Param("pbList") List<String> pbList);

}
