package com.bonus.material.home.service.impl;

import com.bonus.common.biz.domain.TreeBuild;
import com.bonus.common.biz.domain.TreeNode;
import com.bonus.common.biz.domain.TypeInfo;
import com.bonus.common.biz.domain.vo.AreaVo;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.material.book.domain.CompanyPersonPhoneKey;
import com.bonus.material.device.domain.vo.DevInfoVo;
import com.bonus.material.device.mapper.DevInfoMapper;
import com.bonus.material.home.mapper.MaTypeInfoMapper;
import com.bonus.material.home.service.MaTypeInfoSevice;
import com.bonus.material.ma.mapper.MaTypeMapper;
import com.bonus.common.biz.domain.MaTypeProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MaTypeInfoServiceImpl implements MaTypeInfoSevice {

    @Resource
    private MaTypeInfoMapper maTypeInfoMapper;

    @Resource
    private MaTypeMapper maTypeMapper;

    @Resource
    private DevInfoMapper devInfoMapper;

    /**
     * 首页查询分类树
     * @return
     */
    @Override
    public AjaxResult getMaTypeInfoList(TypeInfo typeInfo) {
        List<TreeNode> groupList = new ArrayList<>();
        List<TreeNode> list = new ArrayList<>();
        try {
            list = maTypeInfoMapper.getMaTypeInfoList(typeInfo);
            List<TypeInfo> typeCountList = maTypeInfoMapper.getMaCountByTypeId(typeInfo);
            // 计算4级菜单的上架数
            for (TreeNode treeNode : list) {
                for (TypeInfo typeCount : typeCountList) {
                    if ("4".equals(treeNode.getLevel()) && treeNode.getId() == typeCount.getTypeId().longValue()) {
                        treeNode.setMaCount(typeCount.getMaCount());
                    }
                }
            }
            // 计算3级菜单的上架数
            Map<Long, List<TreeNode>> maTypeCountMap = list.stream().filter(o -> "4".equals(o.getLevel())).collect(Collectors.groupingBy(TreeNode::getParentId));
            for (TreeNode treeNode : list) {
                List<TreeNode> mapValues = maTypeCountMap.get(treeNode.getId());
                if (CollectionUtils.isNotEmpty(mapValues)) {
                    int sum = 0;
                    for (TreeNode node : mapValues) {
                        sum += node.getMaCount() == null ? 0 : node.getMaCount();
                    }
                    treeNode.setMaCount(sum);
                }
            }
            //填充自定义属性
            fillProperties(list);
            if (CollectionUtils.isNotEmpty(list)) {
                // 创建树形结构（数据集合作为参数）
                TreeBuild treeBuild = new TreeBuild(list);
                // 原查询结果转换树形结构
                groupList = treeBuild.buildTree();
            }
        } catch (Exception e) {
            log.error("设备类型树-查询失败", e);
        }
        return AjaxResult.success(groupList);
    }

    private void fillProperties(List<TreeNode> treeNodes) {
        List<MaTypeProperty> maTypeProperties = maTypeMapper.selectMaTypePropertyNames();
        Map<Long, List<MaTypeProperty>> maTypePropertiesMap = new HashMap<>();
        if (!org.springframework.util.CollectionUtils.isEmpty(maTypeProperties)) {
            maTypePropertiesMap = maTypeProperties.stream().collect(Collectors.groupingBy(MaTypeProperty::getTypeId));
        }
        for (TreeNode treeNode : treeNodes) {
            treeNode.setMaTypeProperties(maTypePropertiesMap.get(treeNode.getId()));
        }
    }

    /**
     * 首页搜索热搜装备
     * @param devInfoVo
     * @return
     */
    @Override
    public List<DevInfoVo> getHotSearch(DevInfoVo devInfoVo) {
        //暂取前五热度点击设备
        return maTypeInfoMapper.getHotSearch(devInfoVo);
    }

    /**
     * 装备共享页面之一级设备类型
     * @return
     */
    @Override
    public List<TypeInfo> getTypeList(TypeInfo typeInfo) {
        ArrayList<TypeInfo> infoArrayList = new ArrayList<>();
        if (typeInfo.getTypeId() == null) {
            return maTypeInfoMapper.getParentIdList(typeInfo);
        }
        List<TypeInfo> typeList = maTypeInfoMapper.getParentIdList(typeInfo);
        TypeInfo info = maTypeInfoMapper.getTypeId(typeInfo);
        if (info != null && info.getParentId() != null) {
            Long parentId = info.getParentId();
            TypeInfo info1 = new TypeInfo();
            info1.setTypeId(parentId);
            List<TypeInfo> list = maTypeInfoMapper.getParentIdList(info1);
            if (CollectionUtils.isNotEmpty(list)) {
                infoArrayList.addAll(list);
            }
        }
        infoArrayList.addAll(typeList);
        return infoArrayList;
    }

    /**
     * 租用需求大厅区域
     * @param areaVo
     * @return
     */
    @Override
    public List<AreaVo> getArea(AreaVo areaVo) {
        return maTypeInfoMapper.getArea(areaVo);
    }

    /**
     * @param typeInfo
     * @return
     */
    @Override
    public List<TypeInfo> getToolTypeList(TypeInfo typeInfo) {
        return maTypeInfoMapper.getToolTypeList(typeInfo);
    }

}
