package com.hdkj.lt.bf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdkj.lt.base.constants.DictConstants;
import com.hdkj.lt.bf.entity.convert.EntityToVOConvertor;
import com.hdkj.lt.bf.entity.dto.LineSwitchSectionRequest;
import com.hdkj.lt.bf.entity.vo.FhzgDistLineSwitchSectionVO;
import com.hdkj.lt.bf.mapper.FhzgDistLineSwitchSectionMapper;
import com.hdkj.lt.bf.mapper.FhzgGenerateGraphMapper;
import com.hdkj.lt.bf.service.FhzgDistLineSwitchSectionService;
import com.hdkj.lt.core.bizms.modle.po.fault.FhzgDistLineSwitchSection;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Sunlit
 * @description 针对表【fhzg_dist_line_switch_section(线路开关开关段信息表)】的数据库操作Service实现
 * @createDate 2025-07-02 09:09:34
 */
@Service
public class FhzgDistLineSwitchSectionServiceImpl extends ServiceImpl<FhzgDistLineSwitchSectionMapper, FhzgDistLineSwitchSection>
        implements FhzgDistLineSwitchSectionService {

    @Autowired
    FhzgGenerateGraphMapper fhzgGenerateGraphMapper;

    @Override
    public List<FhzgDistLineSwitchSectionVO> getLineSwitchSection(LineSwitchSectionRequest lineRateCapacityDTO) {
        List<FhzgDistLineSwitchSection> list = this.list(new LambdaQueryWrapper<FhzgDistLineSwitchSection>().eq(FhzgDistLineSwitchSection::getObjId, lineRateCapacityDTO.getObjId()));
        List<FhzgDistLineSwitchSectionVO> vos = EntityToVOConvertor.INSTANCE.toDistLineSwitchSectionVOList(list);
        Map<String, List<String>> showSwitchMap = fhzgGenerateGraphMapper.getOtherSwitchByObjId(lineRateCapacityDTO.getObjId(), Arrays.asList(DictConstants.KG_PSR_TYPE.split(",")))
                .stream().collect(Collectors.groupingBy(e -> e.get("show_switch_id"), Collectors.mapping(en -> en.get("psr_id"), Collectors.toList())));
        vos.forEach(vo ->
        {
            List<String> otherList;
            otherList = showSwitchMap.get(vo.getStartSwitchId());
            // 获取中间的母线信息
            if (otherList != null && !otherList.isEmpty()) {
                List<String> psrIdList = fhzgGenerateGraphMapper.getBusbarBySwitchIds(ListUtils.union(otherList, Arrays.asList(vo.getStartSwitchId())));
                otherList.addAll(psrIdList);
            }
            vo.setOtherkglist(otherList);
        });
        return vos;
    }
}




