package com.hzs.member.base.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.EMemberChildNodeType;
import com.hzs.common.domain.member.achieve.CuMemberRetailDetail;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.member.base.mapper.CuMemberRetailDetailMapper;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.member.base.service.ICumemberRetailDetail;
import com.hzs.member.structure.controller.vo.ChildCodeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class ICumemberRetailDetailImpl extends ServiceImpl<CuMemberRetailDetailMapper, CuMemberRetailDetail> implements ICumemberRetailDetail {
    @Autowired
    private ICuMemberService iCuMemberService;
    @Override
    public List<ChildCodeVO> getChildList(String memberCode, Integer stage, Integer stageStatus, String settleTableName) {
        return baseMapper.getChildList(memberCode,stage,stageStatus,settleTableName);
    }

    @Override
    public CuMemberRetailDetail getCuMemberRetailDetail(String memberCode, Integer stage, Integer stageStatus,String childNode,String settleTableName) {
        return baseMapper.getCuMemberRetailDetailExt(memberCode,stage,stageStatus,childNode,settleTableName);
    }

    @Override
    public Integer getFindCumemberRetailDetail(String settleTableName) {
        return baseMapper.getFindCumemberRetailDetail(settleTableName);
    }

    @Override
    public List<Tree<String>> convertToNodeList(CuMemberRetailDetail cuMemberRetailDetail) {
        // 所有会员id
        Set<Long> ids = new LinkedHashSet<>();
        ids.add(cuMemberRetailDetail.getPkMember());
        // 过滤空的会员id
        Stream.of(
                cuMemberRetailDetail.getPointMember11(), cuMemberRetailDetail.getPointMember12(),
                cuMemberRetailDetail.getPointMember21(), cuMemberRetailDetail.getPointMember22(), cuMemberRetailDetail.getPointMember23(), cuMemberRetailDetail.getPointMember24(),
                cuMemberRetailDetail.getPointMember31(), cuMemberRetailDetail.getPointMember32(), cuMemberRetailDetail.getPointMember33(), cuMemberRetailDetail.getPointMember34(),
                cuMemberRetailDetail.getPointMember35(), cuMemberRetailDetail.getPointMember36(), cuMemberRetailDetail.getPointMember37(), cuMemberRetailDetail.getPointMember38()
        ).filter(Objects::nonNull).forEach(ids::add);

        // 查找对应会员信息
        List<CuMember> members;
        if (ids.isEmpty()) {
            members = Collections.emptyList();
        } else {
            members = iCuMemberService.listByIds(new ArrayList<>(ids));
        }

        Map<Long, CuMember> memberMap = members.stream()
                .collect(Collectors.toMap(CuMember::getPkId, Function.identity(), (a, b) -> a));

        // 构建node
        List<Map<String, Object>> nodeList = new ArrayList<>();
        nodeList.add(makeNode(EMemberChildNodeType.ROOT.getId(), EMemberChildNodeType.ROOT.getParentId(), EMemberChildNodeType.ROOT.getLabel(), cuMemberRetailDetail.getPkMember(), memberMap));

        nodeList.add(makeNode(EMemberChildNodeType.NODE1.getId(), EMemberChildNodeType.NODE1.getParentId(), EMemberChildNodeType.NODE1.getLabel(), cuMemberRetailDetail.getPointMember11(), memberMap));
        nodeList.add(makeNode(EMemberChildNodeType.NODE2.getId(), EMemberChildNodeType.NODE2.getParentId(), EMemberChildNodeType.NODE2.getLabel(), cuMemberRetailDetail.getPointMember12(), memberMap));

        nodeList.add(makeNode(EMemberChildNodeType.NODE3.getId(), EMemberChildNodeType.NODE3.getParentId(), EMemberChildNodeType.NODE3.getLabel(), cuMemberRetailDetail.getPointMember21(), memberMap));
        nodeList.add(makeNode(EMemberChildNodeType.NODE4.getId(), EMemberChildNodeType.NODE4.getParentId(), EMemberChildNodeType.NODE4.getLabel(), cuMemberRetailDetail.getPointMember22(), memberMap));
        nodeList.add(makeNode(EMemberChildNodeType.NODE5.getId(), EMemberChildNodeType.NODE5.getParentId(), EMemberChildNodeType.NODE5.getLabel(), cuMemberRetailDetail.getPointMember23(), memberMap));
        nodeList.add(makeNode(EMemberChildNodeType.NODE6.getId(), EMemberChildNodeType.NODE6.getParentId(), EMemberChildNodeType.NODE6.getLabel(), cuMemberRetailDetail.getPointMember24(), memberMap));

        nodeList.add(makeNode(EMemberChildNodeType.NODE7.getId(), EMemberChildNodeType.NODE7.getParentId(), EMemberChildNodeType.NODE7.getLabel(), cuMemberRetailDetail.getPointMember31(), memberMap));
        nodeList.add(makeNode(EMemberChildNodeType.NODE8.getId(), EMemberChildNodeType.NODE8.getParentId(), EMemberChildNodeType.NODE8.getLabel(), cuMemberRetailDetail.getPointMember32(), memberMap));
        nodeList.add(makeNode(EMemberChildNodeType.NODE9.getId(), EMemberChildNodeType.NODE9.getParentId(), EMemberChildNodeType.NODE9.getLabel(), cuMemberRetailDetail.getPointMember33(), memberMap));
        nodeList.add(makeNode(EMemberChildNodeType.NODE10.getId(), EMemberChildNodeType.NODE10.getParentId(), EMemberChildNodeType.NODE10.getLabel(), cuMemberRetailDetail.getPointMember34(), memberMap));
        nodeList.add(makeNode(EMemberChildNodeType.NODE11.getId(), EMemberChildNodeType.NODE11.getParentId(), EMemberChildNodeType.NODE11.getLabel(), cuMemberRetailDetail.getPointMember35(), memberMap));
        nodeList.add(makeNode(EMemberChildNodeType.NODE12.getId(), EMemberChildNodeType.NODE12.getParentId(), EMemberChildNodeType.NODE12.getLabel(), cuMemberRetailDetail.getPointMember36(), memberMap));
        nodeList.add(makeNode(EMemberChildNodeType.NODE13.getId(), EMemberChildNodeType.NODE13.getParentId(), EMemberChildNodeType.NODE13.getLabel(), cuMemberRetailDetail.getPointMember37(), memberMap));
        nodeList.add(makeNode(EMemberChildNodeType.NODE14.getId(), EMemberChildNodeType.NODE14.getParentId(), EMemberChildNodeType.NODE14.getLabel(), cuMemberRetailDetail.getPointMember38(), memberMap));

        // 构造树结构
        return TreeUtil.build(nodeList, EMemberChildNodeType.ROOT.getParentId(), (node, tree) -> {
            tree.setId((String) node.get("id"));
            tree.setParentId((String) node.get("parentId"));
            tree.setName((String) node.get("label"));
            tree.putExtra("memberCode", node.get("memberCode"));
            tree.putExtra("memberName", node.get("memberName"));
            tree.putExtra("nodeCode", node.get("nodeCode"));
        });
    }
    private Map<String, Object> makeNode(String id, String parentId, String label,
                                         Long memberId, Map<Long, CuMember> memberMap) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("parentId", parentId);
        map.put("label", label);
        map.put("nodeCode", label);

        if (memberId != null && memberMap.containsKey(memberId)) {
            CuMember member = memberMap.get(memberId);
            map.put("memberCode", member.getMemberCode());
            map.put("memberName", member.getMemberName());
        } else {
            // 没有信息置空
            map.put("memberCode", "");
            map.put("memberName", "");
        }
        return map;
    }

}
