package com.njcn.csdevice.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.csdevice.mapper.CsEquipmentDeliveryMapper;
import com.njcn.csdevice.mapper.CsLinePOMapper;
import com.njcn.csdevice.pojo.po.CsEquipmentDeliveryPO;
import com.njcn.csdevice.pojo.po.CsLinePO;
import com.njcn.csdevice.pojo.vo.CsLedgerVO;
import com.njcn.csdevice.service.IWlLedgerService;
import com.njcn.system.api.DictTreeFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.pojo.vo.DictTreeVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 类的介绍：
 *
 * @author xuyang
 * @version 1.0.0
 * @createTime 2024/4/9 14:45
 */
@Service
@RequiredArgsConstructor
public class IWlLedgerServiceImpl implements IWlLedgerService {
    private final DictTreeFeignClient dictTreeFeignClient;
    private final CsEquipmentDeliveryMapper csEquipmentDeliveryMapper;
    private final CsLinePOMapper csLinePOMapper;

    @Override
    public List<CsLedgerVO> getWlDevice() {
        List<CsLedgerVO> result = new ArrayList<>();
        // 获取便携式设备字典
        DictTreeVO vo = Optional.ofNullable(dictTreeFeignClient.queryByCode(DicDataEnum.PORTABLE.getCode()).getData()).orElse(null);
        if (vo == null) {
            return result;
        }
        // 获取便携设备列表
        List<CsEquipmentDeliveryPO> devList = csEquipmentDeliveryMapper.selectList(
                new LambdaQueryWrapper<CsEquipmentDeliveryPO>()
                        .eq(CsEquipmentDeliveryPO::getDevType, vo.getId())
                        .ne(CsEquipmentDeliveryPO::getRunStatus, 0)
        );
        if (CollUtil.isEmpty(devList)) {
            return result;
        }
        // 组装结果返回
        result = devList.stream().map(dev -> {
            CsLedgerVO csLedgerVO = new CsLedgerVO();
            csLedgerVO.setId(dev.getId());
            csLedgerVO.setName(dev.getName());
            csLedgerVO.setLevel(0);
            csLedgerVO.setComFlag(dev.getRunStatus());
            return csLedgerVO;
        }).collect(Collectors.toList());
        return result;
    }

    @Override
    public List<CsLedgerVO> getWlDeviceTree() {
        List<CsLedgerVO> result = new ArrayList<>();
        // 获取便携式设备字典
        DictTreeVO vo = Optional.ofNullable(dictTreeFeignClient.queryByCode(DicDataEnum.PORTABLE.getCode()).getData()).orElse(null);
        if (vo == null) {
            return result;
        }
        // 获取便携设备列表
        List<CsEquipmentDeliveryPO> devList = csEquipmentDeliveryMapper.selectList(
                new LambdaQueryWrapper<CsEquipmentDeliveryPO>()
                        .eq(CsEquipmentDeliveryPO::getDevType, vo.getId())
                        .ne(CsEquipmentDeliveryPO::getRunStatus, 0)
        );
        if (CollUtil.isEmpty(devList)) {
            return result;
        }
        // 获取监测点列表
        List<String> devIds = devList.stream().map(CsEquipmentDeliveryPO::getId).collect(Collectors.toList());
        List<CsLinePO> lineList = csLinePOMapper.selectList(
                new LambdaQueryWrapper<CsLinePO>()
                        .in(CsLinePO::getDeviceId, devIds)
        );
        // 组装结果返回
        result = devList.stream().map(dev -> {
            CsLedgerVO csLedgerVO = new CsLedgerVO();
            csLedgerVO.setId(dev.getId());
            csLedgerVO.setName(dev.getName());
            csLedgerVO.setLevel(0);
            csLedgerVO.setComFlag(dev.getRunStatus());
            List<CsLedgerVO> children = lineList.stream()
                    .filter(line -> Objects.equals(dev.getId(), line.getDeviceId()))
                    .map(line -> {
                        CsLedgerVO vo2 = new CsLedgerVO();
                        vo2.setId(line.getLineId());
                        vo2.setPid(dev.getId());
                        vo2.setName(line.getName());
                        vo2.setLevel(1);
                        return vo2;
                    }).collect(Collectors.toList());
            csLedgerVO.setChildren(children);
            return csLedgerVO;
        }).collect(Collectors.toList());
        return result;
    }
}
