package com.njcn.cssystem.service.impl;

import cn.hutool.core.collection.CollUtil;
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.cssystem.mapper.EleEpdPqdMapper;
import com.njcn.cssystem.pojo.po.EleEpdPqd;
import com.njcn.cssystem.pojo.vo.EleEpdTreeVO;
import com.njcn.cssystem.service.IEleEpdPqdService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.pojo.po.DictData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xuyang
 * @since 2023-05-24
 */
@Service
@RequiredArgsConstructor
public class EleEpdPqdServiceImpl extends ServiceImpl<EleEpdPqdMapper, EleEpdPqd> implements IEleEpdPqdService {

    private final DicDataFeignClient dicDataFeignClient;

    private final String STR_ONE = "#";

    private final String STR_TWO = "$";

    @Override
    public List<EleEpdTreeVO> eleEpdChooseTree() {
        DictData dic = dicDataFeignClient.getDicDataByNameAndTypeName(DicDataTypeEnum.CS_DATA_TYPE.getName(), DicDataEnum.PQD.getName()).getData();
        LambdaQueryWrapper<EleEpdPqd> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EleEpdPqd::getDataType, dic.getId()).orderByAsc(EleEpdPqd::getSort);
        List<EleEpdPqd> list = this.getBaseMapper().selectList(lambdaQueryWrapper);
        Map<String, List<EleEpdPqd>> map = list.stream().collect(Collectors.groupingBy(EleEpdPqd::getName, LinkedHashMap::new, Collectors.toList()));

        List<EleEpdTreeVO> tree = new ArrayList<>();
        map.forEach((key, value) -> {

            EleEpdTreeVO reportTreeVO = new EleEpdTreeVO();
            if (Objects.isNull(value.get(0).getOtherName())) {
                reportTreeVO.setName(value.get(0).getName());
            } else {
                reportTreeVO.setName(value.get(0).getOtherName());
            }
            reportTreeVO.setShowName(value.get(0).getShowName());

            //存在1-50次 2-50次情况
            if (Objects.nonNull(value.get(0).getHarmStart()) && Objects.nonNull(value.get(0).getHarmEnd())) {
                List<EleEpdTreeVO> reHarm = new ArrayList<>();
                for (int i = value.get(0).getHarmStart(); i <= value.get(0).getHarmEnd(); i++) {
                    EleEpdTreeVO reportTreeCount = new EleEpdTreeVO();
                    reportTreeCount.setName(reportTreeVO.getName() + "_" + i);
                    reportTreeCount.setShowName(i + "次" + value.get(0).getShowName());
                    reportTreeVO.setFlag(1);
                    assPhase(value, reportTreeCount, reportTreeCount.getName());
                    reHarm.add(reportTreeCount);
                }
                reportTreeVO.setChildren(reHarm);
            } else {
                assPhase(value, reportTreeVO, reportTreeVO.getName());
            }
            tree.add(reportTreeVO);
        });
        return tree;
    }

    /*组装相别*/
    private void assPhase(List<EleEpdPqd> value, EleEpdTreeVO reportTreeItem, String key) {
        List<EleEpdTreeVO> phaseTree = new ArrayList<>();
        value.forEach(item -> {
            if (Objects.nonNull(item.getPhase()) && !"M".equals(item.getPhase())) {
                List<EleEpdTreeVO> statTree = new ArrayList<>();
                EleEpdTreeVO reportTreePhase = new EleEpdTreeVO();
                reportTreePhase.setName(item.getPhase());
                reportTreePhase.setShowName(item.getPhase());
//                reportTreePhase.setUnit(item.getUnit());
                assStatMethod(item, statTree, key, item.getPhase());
                reportTreePhase.setChildren(statTree);
                phaseTree.add(reportTreePhase);
                reportTreeItem.setChildren(phaseTree);

            } else {
                List<EleEpdTreeVO> statTree = new ArrayList<>();
                assStatMethod(item, statTree, key, "M");
                reportTreeItem.setChildren(statTree);
            }
        });

    }

    private void assStatMethod(EleEpdPqd item, List<EleEpdTreeVO> statTree, String oneKey, String twoKey) {
        //存在向别为M但是Stat_Method不为空
        if (StrUtil.isNotBlank(item.getStatMethod())) {
            String[] arr = item.getStatMethod().split(",");
            List<String> stat = Stream.of(arr).collect(Collectors.toList());
            //根据表id获取表名称
            String tableName = dicDataFeignClient.getDicDataById(item.getClassId().trim()).getData().getName();
            if (CollUtil.isNotEmpty(stat)) {
                stat.forEach(statItem -> {
                    EleEpdTreeVO reportTreeStat = new EleEpdTreeVO();
                    reportTreeStat.setUnit(item.getUnit());
                    String tem = "";
                    if (Objects.nonNull(item.getLimitName())) {
                        tem = STR_ONE + item.getLimitName();
                    } else {
                        tem = "#NO";
                    }
                    if (StrUtil.isNotBlank(twoKey)) {
                        reportTreeStat.setName(STR_TWO + oneKey + STR_ONE + twoKey + STR_ONE + statItem + STR_ONE + tableName + tem.trim() + STR_TWO);
                    } else {
                        reportTreeStat.setName(STR_TWO + oneKey + STR_ONE + statItem + STR_ONE + tableName + tem.trim() + STR_TWO);
                    }

                    reportTreeStat.setShowName(statItem);
                    statTree.add(reportTreeStat);
                });
            }
        }
    }

}

