package com.njcn.device.pq.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.device.line.mapper.LineMapper;
import com.njcn.device.pq.enums.LineBaseEnum;
import com.njcn.device.pq.mapper.PqsDeviceUnitMapper;
import com.njcn.device.pq.pojo.po.Line;
import com.njcn.device.biz.pojo.po.PqsDeviceUnit;
import com.njcn.device.pq.pojo.vo.PqsDeviceUnitVo;
import com.njcn.device.pq.pojo.vo.TerminalTree;
import com.njcn.device.pq.service.IPqsDeviceUnitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
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 LineMapper lineMapper;

    @Override
    public List<PqsDeviceUnitVo> nodeList(String devFlag) {
        List<Line> lines = lineMapper.selectList(new LambdaQueryWrapper<Line>()
                .in(Line::getLevel, Arrays.asList(0, 1, 2, 3))
                .eq(Line::getState, 1)

        );
        List<PqsDeviceUnitVo.DeviceUnit> deviceUnits = this.baseMapper.deviceUnitList(devFlag);
        List<TerminalTree> provinceList = lineMapper.getProvinceList(null, 0);
        Map<String, String> areaMap = provinceList.stream().collect(Collectors.toMap(TerminalTree::getId, TerminalTree::getName));
        lines.stream().filter(x -> x.getLevel().equals(LineBaseEnum.PROVINCE_LEVEL.getCode())).forEach(x -> {
                    if (areaMap.containsKey(x.getId())) {
                        x.setName(areaMap.get(x.getId()));
                    }
                }
        );
        List<PqsDeviceUnitVo> pqsDeviceUnitVos = recursionSelectList("0", lines, deviceUnits);
        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) {
        PqsDeviceUnit pqsDeviceUnit = this.baseMapper.deviceUnitByID(lineID);
        if(ObjectUtil.isNotNull(pqsDeviceUnit)){
            return pqsDeviceUnit;
        }
        return new PqsDeviceUnit();
    }

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

    /***
     * 递归生成树
     * @param id 父节点id
     * @param deptInfos
     * @param deviceUnits
     * @return
     */
    private static List<PqsDeviceUnitVo> recursionSelectList(String id, List<Line> deptInfos, List<PqsDeviceUnitVo.DeviceUnit> deviceUnits) {
        List<PqsDeviceUnitVo> menuSelectList = new ArrayList<>();
        Optional.ofNullable(deptInfos).ifPresent(customers -> customers.stream()
                .filter(x -> x.getPid().equals(id))
                .forEach(dto -> {
                    PqsDeviceUnitVo tree = new PqsDeviceUnitVo();
                    tree.setId(dto.getId());
                    tree.setName(dto.getName());
                    if (dto.getLevel().equals(LineBaseEnum.SUB_LEVEL.getCode())) {
                        List<PqsDeviceUnitVo.DeviceUnit> unit = getUnit(dto.getId(), deviceUnits);
                        tree.setChildren(unit);
                    } else {
                        tree.setChildren(recursionSelectList(dto.getId(), deptInfos, deviceUnits));
                    }
                    menuSelectList.add(tree);
                }));
        return menuSelectList;
    }

    /**
     * 查询数据单位终端信息集合
     *
     * @param id
     * @param deviceUnits
     * @return
     */
    private static List<PqsDeviceUnitVo.DeviceUnit> getUnit(String id, List<PqsDeviceUnitVo.DeviceUnit> deviceUnits) {
        return deviceUnits.stream().filter(x -> x.getPid().equals(id)).collect(Collectors.toList());
    }
}
