package com.njcn.product.terminal.oracleDevice.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.product.terminal.oracleDevice.mapper.PqDeviceMapper;
import com.njcn.product.terminal.oracleDevice.mapper.PqGdCompanyMapper;
import com.njcn.product.terminal.oracleDevice.mapper.PqLineMapper;
import com.njcn.product.terminal.oracleDevice.mapper.PqSubstationMapper;
import com.njcn.product.terminal.oracleDevice.pojo.dto.OracleLedgerTreeDTO;
import com.njcn.product.terminal.oracleDevice.pojo.po.PqDevice;
import com.njcn.product.terminal.oracleDevice.pojo.po.PqGdCompany;
import com.njcn.product.terminal.oracleDevice.pojo.po.PqLine;
import com.njcn.product.terminal.oracleDevice.pojo.po.PqSubstation;
import com.njcn.product.terminal.oracleDevice.service.LedgerTreeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author: cdf
 * @CreateTime: 2025-08-28
 * @Description:
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class LedgerTreeServiceImpl implements LedgerTreeService {

    private final PqLineMapper pqLineMapper;

    private final PqGdCompanyMapper pqGdCompanyMapper;

    private final PqDeviceMapper pqDeviceMapper;

    private final PqSubstationMapper substationMapper;


    @Override
    public List<OracleLedgerTreeDTO> getTree() {
        List<PqLine> pqLineList = pqLineMapper.selectList(null);
        List<OracleLedgerTreeDTO> lineDtoList = pqLineList.stream().map(it->{
            OracleLedgerTreeDTO oracleLedgerTreeDTO = new OracleLedgerTreeDTO();
            oracleLedgerTreeDTO.setId(it.getLineIndex());
            oracleLedgerTreeDTO.setName(it.getSubvName()+"_"+it.getName());
            oracleLedgerTreeDTO.setPid(it.getDevIndex());
            oracleLedgerTreeDTO.setLevel(4);
            return oracleLedgerTreeDTO;
        }).collect(Collectors.toList());
        List<PqDevice> deviceList = pqDeviceMapper.selectList(new LambdaQueryWrapper<PqDevice>().eq(PqDevice::getDevflag,0).eq(PqDevice::getDevmodel,1));
        List<OracleLedgerTreeDTO> devDtoList = deviceList.stream().map(it->{
            OracleLedgerTreeDTO oracleLedgerTreeDTO = new OracleLedgerTreeDTO();
            oracleLedgerTreeDTO.setId(it.getDevIndex());
            oracleLedgerTreeDTO.setName(it.getName());
            oracleLedgerTreeDTO.setPid(it.getSubIndex());
            oracleLedgerTreeDTO.setLevel(3);
            oracleLedgerTreeDTO.setChildren(lineDtoList.stream().filter(line->Objects.equals(it.getDevIndex(),line.getPid())).collect(Collectors.toList()));
            return oracleLedgerTreeDTO;
        }).collect(Collectors.toList());
        List<PqSubstation> substationList = substationMapper.selectList(null);
        List<OracleLedgerTreeDTO> stationDtoList = substationList.stream().map(it->{
            OracleLedgerTreeDTO oracleLedgerTreeDTO = new OracleLedgerTreeDTO();
            oracleLedgerTreeDTO.setId(it.getSubIndex());
            oracleLedgerTreeDTO.setName(it.getName());
            oracleLedgerTreeDTO.setPid(it.getGdIndex());
            oracleLedgerTreeDTO.setLevel(2);
            oracleLedgerTreeDTO.setChildren(devDtoList.stream().filter(dev->Objects.equals(it.getSubIndex(),dev.getPid())).collect(Collectors.toList()));
            return oracleLedgerTreeDTO;
        }).collect(Collectors.toList());
        List<PqGdCompany> pqGdCompanyList = pqGdCompanyMapper.selectList(null);
        List<OracleLedgerTreeDTO> gdDtoList = pqGdCompanyList.stream().map(it->{
            OracleLedgerTreeDTO oracleLedgerTreeDTO = new OracleLedgerTreeDTO();
            oracleLedgerTreeDTO.setId(it.getGdIndex().intValue());
            oracleLedgerTreeDTO.setName(it.getName());
            oracleLedgerTreeDTO.setPid(0);
            oracleLedgerTreeDTO.setLevel(1);
            oracleLedgerTreeDTO.setChildren(stationDtoList.stream().filter(sub->Objects.equals(it.getGdIndex().intValue(),sub.getPid())).collect(Collectors.toList()));
            return oracleLedgerTreeDTO;
        }).collect(Collectors.toList());
        return gdDtoList;
    }
}
