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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdkj.lt.base.exception.BusinessException;
import com.hdkj.lt.bf.entity.dto.XlRelDTO;
import com.hdkj.lt.bf.entity.vo.XlRelVO;
import com.hdkj.lt.bf.mapper.FhzgGridOperationAbnormalityMapper;
import com.hdkj.lt.bf.mapper.XlRelMapper;
import com.hdkj.lt.bf.service.IXlRelService;
import com.hdkj.lt.core.bizms.modle.po.XlRel;
import com.hdkj.lt.core.bizms.mp.ArgTableNameHandler;
import com.hdkj.lt.core.sys.dao.FhzgOrganSuffixMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 线路与线路关系多对多 服务实现类
 * </p>
 *
 * @author luorui
 * @since 2025-07-25
 */
@Service
@RequiredArgsConstructor
public class XlRelServiceImpl extends ServiceImpl<XlRelMapper, XlRel> implements IXlRelService {

    private final FhzgOrganSuffixMapper fhzgOrganSuffixMapper;

    private final FhzgGridOperationAbnormalityMapper fhzgGridOperationAbnormalityMapper;
    
    private final static String QUERY_TYPE_OPERATION = "3";

    private final static String QUERY_TYPE_WHOLE = "4";//根据站房查询所有联络

    @Override
    public List<XlRelVO> getContactSwitchListByFeeder(XlRelDTO xlRelDTO) {
        Optional.of(xlRelDTO).orElseThrow(() -> new BusinessException("请求参数为空"));
        List<XlRelVO> relVOList;
        if (StrUtil.equals(QUERY_TYPE_OPERATION, xlRelDTO.getQueryType())) {
            //如果是运行调优 则需要关联运行调优事件查询,   或者方案
            Optional.ofNullable(xlRelDTO.getOaId()).orElseThrow(() -> new BusinessException("运行调优事件编码或方案编码为空"));
            // 动态表名切换
            ArgTableNameHandler.setArgument(fhzgOrganSuffixMapper.getOrganSuffixByPsrIdOrFeeder(null, xlRelDTO.getLineId()));
            relVOList = baseMapper.getContactSwitchListByFeederOperation(xlRelDTO);
        } else  if (StrUtil.equals(QUERY_TYPE_WHOLE, xlRelDTO.getQueryType())) {
            //如果是运行调优 则需要关联运行调优事件查询,   或者方案
            Optional.ofNullable(xlRelDTO.getStationId()).orElseThrow(() -> new BusinessException("站房编码为空"));
            //  动态表名切换
//            ArgTableNameHandler.setArgument(fhzgOrganSuffixMapper.getOrganSuffixByStationId(xlRelDTO.getStationId()));
            relVOList = baseMapper.getContactSwitchListByStationId(xlRelDTO);
        } else {
            Optional.ofNullable(xlRelDTO.getLineId()).orElseThrow(() -> new BusinessException("线路编码为空"));
            // 动态表名切换
            ArgTableNameHandler.setArgument(fhzgOrganSuffixMapper.getOrganSuffixByPsrIdOrFeeder(null, xlRelDTO.getLineId()));
            relVOList = baseMapper.getContactSwitchListByFeeder(xlRelDTO);
        }
        return relVOList;
    }


    private List<XlRelVO> getXlRelVOByKgId( List<String> psrIds){
        List<XlRelVO> relVOList = new ArrayList<>();
        if (CollUtil.isNotEmpty(psrIds)) {
            //从联络开关表中筛选出数据
            LambdaQueryWrapper<XlRel> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(XlRel::getLlsbId, psrIds);
            // 动态表名切换
            ArgTableNameHandler.setArgument(fhzgOrganSuffixMapper.getOrganSuffixByPsrIdOrFeeder(psrIds.get(0), null));
            List<XlRel> xlRelList = baseMapper.selectList(queryWrapper);
            if (CollUtil.isNotEmpty(xlRelList)) {
                for (XlRel xlRel : xlRelList) {
                    XlRelVO xlRelVO = new XlRelVO();
                    BeanUtil.copyProperties(xlRel, xlRelVO);
                    relVOList.add(xlRelVO);
                }
            }
        }
        return relVOList;
    }
}
