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.CostStandardDetail;
import com.zzsmart.qomo.kn.cost.manage.mapper.CostStandardDetailMapper;
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 com.zzsmart.qomo.kn.cost.manage.vo.CostStandardDetailVO;
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.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

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

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