package com.zzsmart.qomo.kn.cost.manage.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostStandardDetail;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostStandardResult;
import com.zzsmart.qomo.kn.cost.manage.mapper.AppSceneCostStandardDetailMapper;
import com.zzsmart.qomo.kn.cost.manage.mapper.AppSceneCostStandardResultMapper;
import com.zzsmart.qomo.kn.cost.manage.service.BomTreeService;
import com.zzsmart.qomo.kn.cost.manage.util.BomTreeBuilder;
import com.zzsmart.qomo.kn.cost.manage.vo.BomTreeInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class BomTreeServiceImpl implements BomTreeService {
    @Autowired
    AppSceneCostStandardDetailMapper costStandardDetailMapper;

    @Autowired
    AppSceneCostStandardResultMapper appSceneCostStandardResultMapper;

    /**
     * 根据物料号查询物料树
     *
     * @param materialNumber
     * @return
     */
    @Override
    public List<BomTreeInfoVO> getBomTreeByMaterialNumber(String materialNumber, String version_number) {
        QueryWrapper<AppSceneCostStandardResult> wrapper = new QueryWrapper<>();
        boolean bEmpty=true;
        //设置条件
        if (StrUtil.isNotEmpty(materialNumber)) {
            // 物料号
            wrapper.like("material_number", materialNumber);
            bEmpty=false;
        }
        if (StrUtil.isNotEmpty(version_number)) {
            // 版本号
            wrapper.like("version_number", version_number);
            bEmpty=false;
        }
        if(bEmpty){
            return new ArrayList<>();
        }
        //查询所有满足条件的父节点
        List<AppSceneCostStandardResult> parentNodeList = appSceneCostStandardResultMapper.selectList(wrapper);
        //所有子类节点
        List<AppSceneCostStandardResult> childrenNodeList = new ArrayList<>();
        Map<String, AppSceneCostStandardResult> parentDataMap = new TreeMap<>();
        for(AppSceneCostStandardResult ascsr: parentNodeList){
            if(ascsr.getLayer().equals("0")){
                parentDataMap.put(ascsr.getMaterialNumber(),ascsr);
            }else{
                childrenNodeList.add(ascsr);
            }
        }
        List<BomTreeInfoVO> bomTreeInfoVOS = BomTreeBuilder.buildBomTree(childrenNodeList, parentDataMap);
        return bomTreeInfoVOS;
//        if (parentNodeList.size() > 0) {
//            //父类结果只有1个
//            if (parentNodeList.size() == 1) {
//                parentDataMap = parentNodeList.stream().collect(Collectors.toMap(AppSceneCostStandardResult::getMaterialNumber, vo -> vo));
//            } else {
//                //父类结果有多个（找出最上级节点）
//                Map<String, List<AppSceneCostStandardResult>> levelGroupMap = new HashMap<>();
//                if (parentNodeList != null && parentNodeList.size() > 0) {
//                    levelGroupMap = parentNodeList.stream().filter(s->s.getLowOrderCode()!=null).collect(Collectors.groupingBy(AppSceneCostStandardResult::getLowOrderCode));
//                    TreeMap<String, List<AppSceneCostStandardResult>> levelTreeMap = new TreeMap<>(levelGroupMap);
//                    parentDataMap = levelTreeMap.firstEntry().getValue().stream().filter(s->s.getMaterialNumber()!=null).collect(Collectors.toMap(AppSceneCostStandardResult::getMaterialNumber, vo -> vo));
//                }
//            }
//            //查询所有父节点下的子节点(即第二层叶子节点)
//            List<AppSceneCostStandardResult> costStandardDetails = new ArrayList<>();
//            if (parentDataMap.size() > 0) {
//                QueryWrapper<AppSceneCostStandardResult> wrapper1 = new QueryWrapper<>();
//                wrapper1.eq("version_number", version_number);
//                int num = 0;
//                for (String materialNumber1 : parentDataMap.keySet()) {
//                    num = num + 1;
//                    if (num > 1) {
//                        wrapper1.or().eq("parent_matnr", materialNumber1);
//                    } else {
//                        wrapper1.eq("parent_matnr", materialNumber1);
//                    }
//                }
//                costStandardDetails = appSceneCostStandardResultMapper.selectList(wrapper1);
//            }
//            //第三层叶子节点
//            List<AppSceneCostStandardResult> threeNodes = new ArrayList<>();
//            if (costStandardDetails != null && costStandardDetails.size() > 0) {
//                List<String> materialNoList = costStandardDetails.stream().map(AppSceneCostStandardResult::getMaterialNumber).collect(Collectors.toList());
//                QueryWrapper<AppSceneCostStandardResult> wrapper2 = new QueryWrapper<>();
//                wrapper2.in("parent_matnr", materialNoList);
//                wrapper2.eq("version_number", version_number);
//                wrapper2.eq("low_order_code",2);
//                threeNodes = appSceneCostStandardResultMapper.selectList(wrapper2);
//            }
//            //把第二层叶子节点转化为bomtreeinfoVO对象集合
//            for (int i = 0; i < costStandardDetails.size(); i++) {
//                AppSceneCostStandardResult costStandardDetail = costStandardDetails.get(i);
//                childrenNodeList.add(costStandardDetail);
//            }
//            //把第三层叶子节点转化为bomtreeinfoVO对象集合
//            for (int i = 0; i < threeNodes.size(); i++) {
//                AppSceneCostStandardResult costStandardDetail = threeNodes.get(i);
//                childrenNodeList.add(costStandardDetail);
//            }
//        }
//        List<BomTreeInfoVO> bomTreeInfoVOS = BomTreeBuilder.buildBomTree(childrenNodeList, parentDataMap);
//        return bomTreeInfoVOS;
    }
}
