package com.hdkj.lt.si.service.svg.impl;

import cn.hutool.core.lang.generator.SnowflakeGenerator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hdkj.lt.base.constants.DictConstants;
import com.hdkj.lt.core.bizms.modle.po.fault.FhzgDistLineSwitchSection;
import com.hdkj.lt.core.bizms.mp.ArgTableNameHandler;
import com.hdkj.lt.core.sys.dao.FhzgOrganSuffixMapper;
import com.hdkj.lt.modle.dto.svg.AppendDeviceDTO;
import com.hdkj.lt.modle.vo.svg.FeederSwitchVO;
import com.hdkj.lt.si.dao.DistLineSwitchSectionMapper;
import com.hdkj.lt.si.service.svg.IDistLineSwitchSectionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author lihao
 * @Date 2025/7/01 10:05
 * @Version 1.0
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class DistLineSwitchSectionServiceImpl implements IDistLineSwitchSectionService {

    private final DistLineSwitchSectionMapper distLineSwitchSectionMapper;

    private final FhzgOrganSuffixMapper fhzgOrganSuffixMapper;

    @Override
    public void saveLineSwitchSection(Long id, String feederId, List<FeederSwitchVO> kgList) {
        if (ObjectUtils.isNotEmpty(kgList) && kgList.size() > 0) {
            String objId = Optional.ofNullable(id).map(String::valueOf).orElse(feederId);
            LambdaQueryWrapper<FhzgDistLineSwitchSection> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(FhzgDistLineSwitchSection::getObjId, objId);
            distLineSwitchSectionMapper.delete(wrapper);
            List<String> psrIds = kgList.stream().map(FeederSwitchVO::getPsrId).collect(Collectors.toList());
            //动态切换表名
            //*ztj ArgTableNameHandler.setArgument(fhzgOrganSuffixMapper.getOrganSuffixByPsrIdOrFeeder(null, feederId));
            // 查询所有的拓扑段
            Map<String, List<Map<String, Object>>> switchSections = distLineSwitchSectionMapper.getSwitchSection(feederId, psrIds, Arrays.asList(DictConstants.KG_PSR_TYPE.split(",")))
                    .stream().collect(Collectors.groupingBy(e-> String.valueOf(e.get("start_id"))));
            switchSections.forEach((key, values) -> {
                String endIds = values.stream().map(map-> String.valueOf(map.get("end_id")))
                        .collect(Collectors.joining(","));
                FhzgDistLineSwitchSection item = new FhzgDistLineSwitchSection();
                //动态切换表名
                //*ztj ArgTableNameHandler.setArgument(fhzgOrganSuffixMapper.getOrganSuffixByPsrIdOrFeeder(null, feederId));
                // 查询所有的开关、公变、配变数量
                Long switchCount = distLineSwitchSectionMapper.getSwitchCount(feederId, String.valueOf(key),
                        Arrays.asList(endIds.split(",")), Arrays.asList(DictConstants.KG_PSR_TYPE.split(",")));
                Long gbCount = 0L;
                Long pbCount = 0L;
                //动态切换表名
                //*ztj ArgTableNameHandler.setArgument(fhzgOrganSuffixMapper.getOrganSuffixByPsrIdOrFeeder(null, feederId));
                Map<String, Object> transformerCount = distLineSwitchSectionMapper.getTransformerCount(
                        feederId, String.valueOf(key), Arrays.asList(endIds.split(",")),
                        Arrays.asList((DictConstants.PB_PSR_TYPE.concat(",").concat(DictConstants.KG_PSR_TYPE)).split(",")),
                        Arrays.asList(DictConstants.PB_PSR_TYPE.split(",")));
                if (ObjectUtils.isNotEmpty(transformerCount)) {
                    gbCount = Long.valueOf(String.valueOf(transformerCount.get("gb_num")));
                    pbCount = Long.valueOf(String.valueOf(transformerCount.get("pb_num")));
                }
                item.setObjId(objId).setStartSwitchId(String.valueOf(key))
                        .setEndSwitchId(endIds)
                        .setSwitchCount(switchCount).setGbCount(gbCount)
                        .setPbCount(pbCount)
                        .setCreateTime(LocalDateTime.now());
                distLineSwitchSectionMapper.insert(item);
            });
            // 处理渲染展示开关信息
            this.dealDistLineSwitchSectionShow(objId, feederId);
        }
    }

    @Override
    public List<AppendDeviceDTO> getLineSwitchSectionDetails(Long id, String feederId, List<String> kgPsrTypes) {
        String objId = Optional.ofNullable(id).map(String::valueOf).orElse(feederId);
        // 动态切换表名
        //*ztj ArgTableNameHandler.setArgument(fhzgOrganSuffixMapper.getOrganSuffixByPsrIdOrFeeder(null, feederId));
        // 获取负荷点隐藏的开关详情信息
        return distLineSwitchSectionMapper.getLineSwitchSectionDetails(objId, kgPsrTypes);
    }

    /**
     * 渲染展示开关信息
     */
    private void dealDistLineSwitchSectionShow(String objId, String feederId){
        // 先删除线路的显示开关信息
        distLineSwitchSectionMapper.deleteDistLineSwitchSectionShowByLineId(objId);
        // 开关和配变类型集合
        List<String> psrType = Arrays.asList((DictConstants.PB_PSR_TYPE.concat(",").concat(DictConstants.KG_PSR_TYPE)).split(","));
        List<String> kgType = Arrays.asList(DictConstants.KG_PSR_TYPE.split(","));
        List<String> pbType = Arrays.asList(DictConstants.PB_PSR_TYPE.split(","));
        // 动态切换表名
        //*ztj ArgTableNameHandler.setArgument(fhzgOrganSuffixMapper.getOrganSuffixByPsrIdOrFeeder(null, feederId));
        // 保存显示开关信息
        distLineSwitchSectionMapper.saveDistLineSwitchSectionShow(objId, feederId, kgType, pbType, psrType);
    }
}
