package com.njcn.device.pms.service.majornetwork.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.device.biz.pojo.po.PqsDeviceUnit;
import com.njcn.device.pms.mapper.majornetwork.PqsDeviceUnitMapper;
import com.njcn.device.pms.pojo.po.Monitor;
import com.njcn.device.pms.pojo.po.PmsTerminal;
import com.njcn.device.pms.pojo.vo.DeviceUnitVo;
import com.njcn.device.pms.service.majornetwork.IMonitorService;
import com.njcn.device.pms.service.majornetwork.IPqsDeviceUnitService;
import com.njcn.device.pms.service.majornetwork.ITerminalService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wr
 * @since 2023-08-21
 */
@Service
@RequiredArgsConstructor
public class PqsDeviceUnitServiceImpl extends ServiceImpl<PqsDeviceUnitMapper, PqsDeviceUnit> implements IPqsDeviceUnitService {

    private final ITerminalService terminalService;
    private final IMonitorService monitorService;

    @Override
    public List<DeviceUnitVo> nodeList(String devFlag) {
        List<DeviceUnitVo> pqsDeviceUnitVos = new ArrayList<>();
        List<PmsTerminal> list = terminalService.list(new LambdaQueryWrapper<PmsTerminal>()
                .eq(PmsTerminal::getStatus, DataStateEnum.ENABLE.getCode())
                .eq(StrUtil.isNotBlank(devFlag), PmsTerminal::getTerminalState, devFlag)
        );
        if(CollUtil.isEmpty(list)){
            return pqsDeviceUnitVos;
        }
        List<String> terminal = list.stream().map(PmsTerminal::getId).collect(Collectors.toList());
        //获取所有终端信息
        List<PqsDeviceUnit> pqsDeviceUnits = this.listByIds(terminal);
        Map<String, PqsDeviceUnit> unitMap = pqsDeviceUnits.stream().collect(Collectors.toMap(PqsDeviceUnit::getDevIndex, Function.identity()));
        Map<String, List<PmsTerminal>> orgMap = list.stream().collect(Collectors.groupingBy(PmsTerminal::getOrgId));
        orgMap.forEach((key, value) -> {
            DeviceUnitVo unitVo = new DeviceUnitVo();
            unitVo.setId(key);
            unitVo.setName(value.get(0).getOrgName());
            Map<String, List<PmsTerminal>> subMap = value.stream().collect(Collectors.groupingBy(PmsTerminal::getPowerStationId));

            List<DeviceUnitVo> subUnitVos = new ArrayList<>();
            subMap.forEach((subKey, subValue) -> {
                DeviceUnitVo subUnitVo = new DeviceUnitVo();
                subUnitVo.setId(subKey);
                subUnitVo.setName(subValue.get(0).getPowerrName());
                Map<String, List<PmsTerminal>> terMap = subValue.stream().collect(Collectors.groupingBy(PmsTerminal::getId));
                List<DeviceUnitVo.DeviceUnit> terUnitVos = new ArrayList<>();
                terMap.forEach((terKey, terValue) -> {
                    for (PmsTerminal pmsTerminal : terValue) {
                        DeviceUnitVo.DeviceUnit terUnitVo = new DeviceUnitVo.DeviceUnit();
                        terUnitVo.setId(pmsTerminal.getId());
                        terUnitVo.setName(pmsTerminal.getName());
                        terUnitVo.setDevFlag(pmsTerminal.getTerminalState());
                        PqsDeviceUnit pqsDeviceUnit;
                        if (unitMap.containsKey(terKey)) {
                            pqsDeviceUnit = unitMap.get(terKey);
                        } else {
                            pqsDeviceUnit = new PqsDeviceUnit();
                        }
                        BeanUtil.copyProperties(pqsDeviceUnit,terUnitVo);
                        terUnitVos.add(terUnitVo);
                    }
                });
                subUnitVo.setChildren(terUnitVos);
                subUnitVos.add(subUnitVo);
            });
            unitVo.setChildren(subUnitVos);
            pqsDeviceUnitVos.add(unitVo);
        });
        return pqsDeviceUnitVos;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveDeviceUnit(PqsDeviceUnit unit) {
        PqsDeviceUnit byId = this.getById(unit.getDevIndex());
        if (ObjectUtil.isNotNull(byId)) {
            return this.updateById(unit);
        }
        return this.save(unit);
    }

    @Override
    public PqsDeviceUnit lineUnitDetail(String lineID) {
        Monitor byId = monitorService.getById(lineID);
        if (ObjectUtil.isNotNull(byId)) {
            PqsDeviceUnit unit  = this.getById(byId.getTerminalId());
            if (ObjectUtil.isNotNull(unit)) {
                return unit;
            }
            return new PqsDeviceUnit();
        }
        return new PqsDeviceUnit();
    }

    @Override
    public PqsDeviceUnit devUnitDetail(String devID) {
        PqsDeviceUnit byId = this.getById(devID);
        if (ObjectUtil.isNotNull(byId)) {
            return byId;
        }
        return new PqsDeviceUnit();
    }

}
