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

import cn.hutool.core.collection.CollectionUtil;
import com.njcn.common.pojo.enums.common.ServerEnum;
import com.njcn.common.utils.EnumUtils;

import com.njcn.product.cnuser.user.pojo.dto.DeptDTO;
import com.njcn.product.cnuser.user.mapper.DeptMapper;
import com.njcn.product.terminal.mysqlTerminal.pojo.dto.DeptGetBase;
import com.njcn.product.terminal.mysqlTerminal.pojo.dto.DeptGetChildrenMoreDTO;
import com.njcn.product.terminal.mysqlTerminal.pojo.dto.LineDevGetDTO;
import com.njcn.product.terminal.mysqlTerminal.pojo.param.DeptGetLineParam;
import com.njcn.product.terminal.mysqlTerminal.service.CommTerminalService;
import com.njcn.product.terminal.mysqlTerminal.service.DeptLineService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * pqs
 *
 * @author cdf
 * @date 2023/5/10
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class CommTerminalServiceImpl implements CommTerminalService {

    //redis前缀
    private final String commTerminal = "commTerminal#";

    private final DeptLineService deptLineService;
    private final DeptMapper deptMapper;


    @Override
    public List<DeptGetChildrenMoreDTO> deptGetLine(DeptGetLineParam deptGetLineParam) {
        List<DeptGetChildrenMoreDTO> result = new ArrayList<>();
        List<DeptGetBase> temDept = getDeptChildrenByParent(deptGetLineParam);
        Map<String, String> deptMap = temDept.stream().collect(Collectors.toMap(DeptGetBase::getUnitId, DeptGetBase::getUnitName));
         Map<String, List<LineDevGetDTO>> map = deptLineService.lineDevGet(filterDataTypeNew(deptGetLineParam.getServerName()),
                 1,deptGetLineParam.getLineRunFlag());
        temDept.forEach(item -> {
            DeptGetChildrenMoreDTO deptGetChildrenMoreDTO = new DeptGetChildrenMoreDTO();
            deptGetChildrenMoreDTO.setUnitId(item.getUnitId());
            deptGetChildrenMoreDTO.setUnitName(item.getUnitName());
            deptGetChildrenMoreDTO.setUnitChildrenList(item.getUnitChildrenList());
            deptGetChildrenMoreDTO.setDeptLevel(item.getDeptLevel());
            List<String> deptIds = item.getUnitChildrenList();
            if (CollectionUtil.isNotEmpty(deptIds)) {
                List<LineDevGetDTO> lineList = new ArrayList<>();
                deptIds.forEach(i -> {
                    if (map.containsKey(i)) {
                        map.get(i).forEach(x->{
                            if(deptMap.containsKey(x.getUnitId())){
                                x.setUnitName(deptMap.get(x.getUnitId()));
                            }
                        });
                        lineList.addAll(map.get(i));
                    }
                });

                //去重
                ArrayList<LineDevGetDTO> collect = lineList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(
                        Comparator.comparing(LineDevGetDTO::getPointId)
                )), ArrayList::new));

                deptGetChildrenMoreDTO.setLineBaseList(collect);
            }
            result.add(deptGetChildrenMoreDTO);
        });
        return result;
    }

    /**
     * 基础获取单位信息
     * @author cdf
     * @date 2023/5/10
     */

    public List<DeptGetBase> getDeptChildrenByParent(DeptGetLineParam deptGetLineParam) {
        /*List<DeptGetBase> redisResult = (List<DeptGetBase>) redisUtil.getObjectByKey(commTerminal + deptGetLineParam.getDeptId());
        if (CollectionUtil.isNotEmpty(redisResult)) {
            return redisResult;
        }*/
        List<DeptGetBase> result = new ArrayList<>();
        List<DeptDTO> deptDTOList = deptMapper.getDeptDescendantIndexes(deptGetLineParam.getDeptId(), Stream.of(0, 1).collect(Collectors.toList()));
        deptDTOList.forEach(it -> {
            DeptGetBase deptGetBase = new DeptGetBase();
            deptGetBase.setUnitId(it.getId());
            deptGetBase.setUnitName(it.getName());
            deptGetBase.setDeptLevel(getDeptLevel(it.getPids()));
            List<String> deptChildren = deptDTOList.stream().filter(deptDTO -> deptDTO.getPids().contains(it.getId())).map(DeptDTO::getId).collect(Collectors.toList());
            deptChildren.add(it.getId());
            deptGetBase.setUnitChildrenList(deptChildren);
            result.add(deptGetBase);
        });
        return result;
    }


    private Integer getDeptLevel(String pids) {
        List<String> list = Arrays.stream(pids.split(",")).map(String::trim).collect(Collectors.toList());
        return list.size();
    }

    private List<Integer> filterDataTypeNew(String serverName) {
        List<Integer> devType = new ArrayList<>();
        devType.add(2);
        ServerEnum serverEnum = EnumUtils.getServerEnumByName(serverName);
        switch (serverEnum) {
            case EVENT:
                devType.add(0);
                break;
            case HARMONIC:
                devType.add(1);
                break;
            default:
                devType.add(0);
                devType.add(1);
                break;
        }
        return devType;
    }
}
