package com.hzs.member.structure.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.constant.CountryConstants;
import com.hzs.common.core.constant.MemberConstants;
import com.hzs.common.core.constant.MemberFrameworkConstants;
import com.hzs.common.core.constant.TableNameConstants;
import com.hzs.common.core.constant.msg.MemberMsgConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EPlaceDept;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.sign.Base64Util;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.member.achieve.CuMemberSettlePeriod;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.member.ext.CuMemberSettleExt;
import com.hzs.common.domain.system.base.BdCountry;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.member.achieve.service.ICuMemberSettlePeriodService;
import com.hzs.member.achieve.service.ICuMemberTreeService;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.member.structure.controller.vo.CuMemberSettlePeriodVo;
import com.hzs.system.base.ICountryServiceApi;
import com.hzs.system.config.IGradeServiceApi;
import com.hzs.system.config.dto.GradeDTO;
import com.hzs.system.sys.IUserServiceApi;
import com.hzs.system.sys.dto.UserAuthorityDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Description: 架构管理
 */
@RestController
@RequestMapping("/manage/member-structure")
public class MemberStructureController extends BaseController {

    @Autowired
    private ICuMemberSettlePeriodService iCuMemberSettlePeriodService;
    @Autowired
    private ICuMemberTreeService iCuMemberTreeService;
    @Autowired
    private ICuMemberService iCuMemberService;

    @DubboReference
    ICountryServiceApi iCountryServiceApi;
    @DubboReference
    IUserServiceApi iUserServiceApi;
    @DubboReference
    IGradeServiceApi iGradeServiceApi;

    /**
     * 默认层数
     */
    Integer defaultLevel = 5;

    /**
     * 安置架构网体
     */
    @AccessPermissions("Arcgurecture")
    @GetMapping("/az-framework")
    public AjaxResult azFramework(Long memberSettlePeriodId, String memberCode, Integer level) {
        if (level == null) {
            level = 7;
        }
        // 体系
        UserAuthorityDTO userAuthorityDTO = iUserServiceApi.getUserAuthority(SecurityUtils.getUserId()).getData();
        List<Integer> vertexIdList = userAuthorityDTO.getVertexIdList();
        Date date = new Date();
        String time = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, date);

        Long pkMember;
        if (StringUtils.isNotBlank(memberCode)) {
            CuMember member = iCuMemberService.getMemberByCode(memberCode);
            if (null == member) {
                return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.MEMBER_NOT_EXITS));
            }
            pkMember = member.getPkId();
        } else {
            return AjaxResult.error("请输入会员编号");
        }

//        if (vertexIdList != null && StringUtils.isNotBlank(memberCode)) {
//            // 体系不为空 并且会员编号不为空 只能查询自己伞下以及自己的
//            List<CuMemberExt> memberExtList = iCuMemberService.checkMemberVertex(vertexIdList, memberCode);
//            if (memberExtList.size() == 0) {
//                return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.QUERY_VERTEX_CODE));
//            }
//        }

        Integer period = null;
        Date settleDate = null;
        if (memberSettlePeriodId != null) {
            CuMemberSettlePeriod csPeriod = iCuMemberSettlePeriodService.getById(memberSettlePeriodId);
            period = csPeriod.getPkId();
            settleDate = csPeriod.getSettleDate();
        } else {
            CuMemberSettlePeriod csPeriod = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(time);
            if (csPeriod != null) {
                period = csPeriod.getPkId();
                settleDate = csPeriod.getSettleDate();
            }
        }
        // 查询当天 (秒结)
        if (settleDate != null
                && DateUtils.parseDateToFormat(DateUtils.YYYY_MM_DD, settleDate).equals(DateUtils.parseDateToFormat(DateUtils.YYYY_MM_DD, new Date()))) {
            // 会员结算秒结表
            String settleTableName = TableNameConstants.CU_MEMBER_SECOND + period;
            // 查询所有用户表数据 id
            // 查询秒结 表 如果 秒结表 没有则去 历史表
            Integer tableNumber = iCuMemberTreeService.queryExistOracleTable(settleTableName);
            if (tableNumber != null && tableNumber == 1) {
                List<CuMemberSettleExt> cuMemberSettleExts;
                // 查询今日会员表是否有值
                Long treeMemberCount = iCuMemberTreeService.selectByselectByTreeMemberCount(settleTableName, pkMember);
                if (treeMemberCount > 0) {
                    //表格存在
                    cuMemberSettleExts = iCuMemberTreeService.selectByTreeMemberList(pkMember, settleTableName, level, vertexIdList);
                } else {
                    DateTime dateTime = DateUtil.offsetDay(DateUtils.currentDate(), -1);
                    time = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, dateTime);
                    CuMemberSettlePeriod csPeriod = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(time);
                    if (csPeriod != null) {
                        period = csPeriod.getPkId();
                    }
                    settleTableName = TableNameConstants.CU_MEMBER_SETTLE + period;
                    cuMemberSettleExts = iCuMemberTreeService.selectByTreeMemberList(pkMember, settleTableName, level, vertexIdList);
                }
                // 查询顶点
                CuMemberSettleExt memberSettleExt = iCuMemberTreeService.queryCuMemberTreeByPkMemberTable(settleTableName, pkMember);
                if (MemberConstants.GJ30720200CODE.equals(memberSettleExt.getParentMemberCode())) {
                    memberSettleExt.setParentMemberCode(memberSettleExt.getMemberCode());
                }
                memberSettleExt.setLevel(0);
                // 添加自身
                cuMemberSettleExts.add(memberSettleExt);
                List<Tree<String>> build = iCuMemberService.systemAzSetListTree(cuMemberSettleExts, memberSettleExt);
                // 安置架构 无点位 追加空点位
                R<Map<Integer, BdCountry>> allCountryMap = iCountryServiceApi.getAllCountryMap();
                Map<Integer, BdCountry> allCountryData = allCountryMap.getData();
                List<Tree<String>> trees = setTreeList(build, level, allCountryData);
                return AjaxResult.success(trees);
            } else {
                // 查询表格是否存在
                tableNumber = iCuMemberTreeService.queryExistOracleTable(settleTableName);
                if (tableNumber == null) {
                    settleTableName = null;
                }
                // 查询顶点
                CuMemberSettleExt memberSettleExt = iCuMemberTreeService.queryCuMemberTreeByPkMemberTable(settleTableName, pkMember);
                if (MemberConstants.GJ30720200CODE.equals(memberSettleExt.getParentMemberCode())) {
                    memberSettleExt.setParentMemberCode(memberSettleExt.getMemberCode());
                }
                memberSettleExt.setLevel(0);
                //查询下级
                List<CuMemberSettleExt> list = iCuMemberTreeService.queryCuMemberTreePlaceParentHistoryTable(settleTableName, pkMember, new ArrayList<>(), level);
                // 无法查询到自身 所有单独查询自身并且添加
                list.add(memberSettleExt);
                List<Tree<String>> build = iCuMemberService.systemAzSetListTree(list, memberSettleExt);
                // 安置架构 无点位 追加空点位
                R<Map<Integer, BdCountry>> allCountryMap = iCountryServiceApi.getAllCountryMap();
                Map<Integer, BdCountry> allCountryData = allCountryMap.getData();
                return AjaxResult.success(setTreeList(build, level, allCountryData));
            }
        } else {
            // 查询 历史
            String settleTableName = TableNameConstants.CU_MEMBER_SETTLE + period;
            Integer tableNumber = iCuMemberTreeService.queryExistOracleTable(settleTableName);
            if (tableNumber != null && tableNumber == 1) {
                // 表示表格存在
                // 查询顶点
                CuMemberSettleExt memberSettleExt = iCuMemberTreeService.queryCuMemberTreeByPkMemberTable(settleTableName, pkMember);
                if (MemberConstants.GJ30720200CODE.equals(memberSettleExt.getParentMemberCode())) {
                    memberSettleExt.setParentMemberCode(memberSettleExt.getMemberCode());
                }
                memberSettleExt.setLevel(0);
                //查询下级
                List<CuMemberSettleExt> list = iCuMemberTreeService.queryCuMemberTreePlaceParentHistoryFramework(settleTableName, pkMember, new ArrayList<>(), level);
                // 无法查询到自身 所有单独查询自身并且添加
                list.add(memberSettleExt);
                List<Tree<String>> build = iCuMemberService.systemAzSetListTree(list, memberSettleExt);
                // 安置架构 无点位 追加空点位
                R<Map<Integer, BdCountry>> allCountryMap = iCountryServiceApi.getAllCountryMap();
                Map<Integer, BdCountry> allCountryData = allCountryMap.getData();
                return AjaxResult.success(setTreeList(build, level, allCountryData));
            }
        }
        return AjaxResult.success();
    }

    public List<Tree<String>> setTreeList(List<Tree<String>> build, Integer numberOfPlies, Map<Integer, BdCountry> allCountryData) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        String nationalFlag1 = allCountryData.get(pkCountry).getNationalFlag1();
        String nationalFlag2 = allCountryData.get(pkCountry).getNationalFlag2();
        for (Tree<String> stringTree : build) {
            Integer level = (Integer) stringTree.get("level");
            if (level < (numberOfPlies - 1)) {
                if (stringTree.getChildren() != null) {
                    if (stringTree.getChildren().size() == 0) {
                        Tree<String> emptyNode1 = TreeUtil.createEmptyNode(RandomUtil.randomNumbers(5));
                        emptyNode1.putExtra("placeDept", EPlaceDept.LEFT_DEPT.getValue());
                        emptyNode1.putExtra("level", level + 1);
                        emptyNode1.putExtra("memberCode", "");
                        emptyNode1.setName("-");
                        emptyNode1.setParentId(stringTree.getId());
                        emptyNode1.putExtra("avatarUrl", MemberFrameworkConstants.xuni);
                        emptyNode1.setId(RandomUtil.randomNumbers(3));
                        emptyNode1.putExtra("settleCountryUrl", nationalFlag1);
                        emptyNode1.putExtra("settleCountryUrl2", nationalFlag2);
                        emptyNode1.putExtra("countryUrl", nationalFlag1);
                        emptyNode1.putExtra("countryUrl2", nationalFlag2);
                        stringTree.getChildren().add(emptyNode1);
                        setTreeList(stringTree.getChildren(), numberOfPlies, allCountryData);

                        Tree<String> emptyNode2 = TreeUtil.createEmptyNode(RandomUtil.randomNumbers(5));
                        emptyNode2.putExtra("placeDept", EPlaceDept.RIGHT_DEPT.getValue());
                        emptyNode2.putExtra("level", level + 1);
                        emptyNode2.putExtra("memberCode", "");
                        emptyNode2.setName("-");
                        emptyNode2.setParentId(stringTree.getId());
                        emptyNode2.putExtra("settleCountryUrl", nationalFlag1);
                        emptyNode2.putExtra("settleCountryUrl2", nationalFlag2);
                        emptyNode2.putExtra("countryUrl", nationalFlag1);
                        emptyNode2.putExtra("countryUrl2", nationalFlag2);
                        emptyNode2.setId(RandomUtil.randomNumbers(3));
                        emptyNode2.putExtra("avatarUrl", MemberFrameworkConstants.xuni);
                        stringTree.getChildren().add(emptyNode2);
                        setTreeList(stringTree.getChildren(), numberOfPlies, allCountryData);
                    } else if (stringTree.getChildren().size() == 1) {
                        List<Tree<String>> cli = stringTree.getChildren();
                        Integer pd = (Integer) cli.get(0).get("placeDept");
                        Tree<String> emptyNode = TreeUtil.createEmptyNode(RandomUtil.randomNumbers(5));
                        if (pd.equals(EPlaceDept.LEFT_DEPT.getValue())) {
                            emptyNode.putExtra("placeDept", EPlaceDept.RIGHT_DEPT.getValue());
                            emptyNode.putExtra("level", level + 1);
                            emptyNode.putExtra("memberCode", "");
                            emptyNode.setName("-");
                            emptyNode.setParentId(stringTree.getId());
                            emptyNode.setId(RandomUtil.randomNumbers(3));
                            emptyNode.putExtra("avatarUrl", MemberFrameworkConstants.xuni);
                            emptyNode.putExtra("settleCountryUrl", nationalFlag1);
                            emptyNode.putExtra("settleCountryUrl2", nationalFlag2);
                            emptyNode.putExtra("countryUrl", nationalFlag1);
                            emptyNode.putExtra("countryUrl2", nationalFlag2);
                            stringTree.getChildren().add(emptyNode);
                            setTreeList(stringTree.getChildren(), numberOfPlies, allCountryData);

                        } else {
                            emptyNode.putExtra("placeDept", EPlaceDept.LEFT_DEPT.getValue());
                            emptyNode.putExtra("level", level + 1);
                            emptyNode.putExtra("memberCode", "");
                            emptyNode.setName("-");
                            emptyNode.setParentId(stringTree.getId());
                            emptyNode.setId(RandomUtil.randomNumbers(3));
                            emptyNode.putExtra("avatarUrl", MemberFrameworkConstants.xuni);
                            emptyNode.putExtra("settleCountryUrl", nationalFlag1);
                            emptyNode.putExtra("settleCountryUrl2", nationalFlag2);
                            emptyNode.putExtra("countryUrl", nationalFlag1);
                            emptyNode.putExtra("countryUrl2", nationalFlag2);
                            stringTree.getChildren().add(emptyNode);
                            List<Tree<String>> strTree = stringTree.getChildren();
                            List<Tree<String>> sortChildren = new ArrayList<>();
                            sortChildren.add(strTree.get(1));
                            sortChildren.add(strTree.get(0));
                            stringTree.setChildren(sortChildren);
                            setTreeList(sortChildren, numberOfPlies, allCountryData);
                        }
                    } else {
                        List<Tree<String>> children = stringTree.getChildren();
                        // 按照安置部门进行排序
                        children.sort(Comparator.comparing(o -> o.get("placeDept").toString()));
//                        List<Tree<String>> sortChildren = new ArrayList<>();
//                        int pd = Integer.parseInt(children.get(0).get("placeDept").toString());
//                        if (pd == 1) {
//                            sortChildren.add(children.get(0));
//                            sortChildren.add(children.get(1));
//                        } else {
//                            sortChildren.add(children.get(1));
//                            sortChildren.add(children.get(0));
//                        }
                        stringTree.setChildren(children);
                        setTreeList(stringTree.getChildren(), numberOfPlies, allCountryData);
                    }
                } else {
                    // 等于空
                    List<Tree<String>> children = new ArrayList<>();
                    Tree<String> emptyNode1 = TreeUtil.createEmptyNode(RandomUtil.randomNumbers(5));
                    emptyNode1.putExtra("placeDept", EPlaceDept.LEFT_DEPT.getValue());
                    emptyNode1.putExtra("level", level + 1);
                    emptyNode1.putExtra("memberCode", "");
                    emptyNode1.setName("-");
                    emptyNode1.putExtra("avatarUrl", MemberFrameworkConstants.xuni);
                    emptyNode1.putExtra("settleCountryUrl", nationalFlag1);
                    emptyNode1.putExtra("settleCountryUrl2", nationalFlag2);
                    emptyNode1.putExtra("countryUrl", nationalFlag1);
                    emptyNode1.putExtra("countryUrl2", nationalFlag2);
                    emptyNode1.setParentId(stringTree.getId());
                    emptyNode1.setId(RandomUtil.randomNumbers(3));
                    children.add(emptyNode1);

                    Tree<String> emptyNode2 = TreeUtil.createEmptyNode(RandomUtil.randomNumbers(5));
                    emptyNode2.putExtra("placeDept", EPlaceDept.RIGHT_DEPT.getValue());
                    emptyNode2.putExtra("level", level + 1);
                    emptyNode2.putExtra("memberCode", "");
                    emptyNode2.setName("-");
                    emptyNode2.putExtra("avatarUrl", MemberFrameworkConstants.xuni);
                    emptyNode2.setParentId(stringTree.getId());
                    emptyNode2.putExtra("settleCountryUrl", nationalFlag1);
                    emptyNode2.putExtra("settleCountryUrl2", nationalFlag2);
                    emptyNode2.putExtra("countryUrl", nationalFlag1);
                    emptyNode2.putExtra("countryUrl2", nationalFlag2);
                    emptyNode2.setId(RandomUtil.randomNumbers(3));
                    children.add(emptyNode2);

                    stringTree.setChildren(children);
                    setTreeList(children, numberOfPlies, allCountryData);
                }
            }
        }
        return build;
    }

    /**
     * 推荐架构网体
     */
    @AccessPermissions("Recommend")
    @GetMapping("/tj-framework")
    public AjaxResult tjFramework(Long memberSettlePeriodId, String memberCode, Integer level) {
        Date date = new Date();
        String time = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, date);
        // 体系
        UserAuthorityDTO userAuthorityDTO = iUserServiceApi.getUserAuthority(SecurityUtils.getUserId()).getData();
        List<Integer> vertexIdList = userAuthorityDTO.getVertexIdList();
        Long pkMember;
        if (StringUtils.isNotBlank(memberCode)) {
            CuMember member = iCuMemberService.getMember(memberCode);
            if (member == null) {
                return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.MEMBER_NOT_EXITS));
            }
            pkMember = member.getPkId();
        } else {
            return AjaxResult.error("请输入会员编号");
        }

        Integer period = null;
        Date settleDate = null;
        if (memberSettlePeriodId != null) {
            CuMemberSettlePeriod csPeriod = iCuMemberSettlePeriodService.getById(memberSettlePeriodId);
            period = csPeriod.getPkId();
            settleDate = csPeriod.getSettleDate();
        } else {
            CuMemberSettlePeriod csPeriod = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(time);
            if (csPeriod != null) {
                period = csPeriod.getPkId();
                settleDate = csPeriod.getSettleDate();
            }
        }
        // 查询当天 (秒结)
        if (settleDate != null
                && DateUtils.parseDateToFormat(DateUtils.YYYY_MM_DD, settleDate).equals(DateUtils.parseDateToFormat(DateUtils.YYYY_MM_DD, new Date()))) {
            // 查询所有用户表数据 id

            // 会员结算秒结表
            String settleTableName = TableNameConstants.CU_MEMBER_SECOND + period;
            Integer tableNumber = iCuMemberTreeService.queryExistOracleTable(settleTableName);
            if (tableNumber != null && tableNumber == 1) {
                //表格存在
                if (level == null) {
                    level = defaultLevel;
                }
                List<CuMemberSettleExt> cuMemberSettleExts;
                // 查询今日会员表是否有值
                Long treeMemberCount = iCuMemberTreeService.selectByselectByTreeMemberCount(settleTableName, pkMember);
                if (treeMemberCount > 0) {
                    //表格存在
                    cuMemberSettleExts = iCuMemberTreeService.queryCuMemberTreeParentTable(settleTableName, pkMember, level, vertexIdList);
                } else {
                    DateTime dateTime = DateUtil.offsetDay(DateUtils.currentDate(), -1);
                    time = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, dateTime);
                    CuMemberSettlePeriod csPeriod = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(time);
                    if (csPeriod != null) {
                        period = csPeriod.getPkId();
                    }
                    settleTableName = TableNameConstants.CU_MEMBER_SETTLE + period;
                    cuMemberSettleExts = iCuMemberTreeService.queryCuMemberTreeParentTable(settleTableName, pkMember, level, vertexIdList);
                }
                // 查询顶点
                CuMemberSettleExt memberSettleExt = iCuMemberTreeService.queryCuMemberTreeByPlacePkMemberTable(settleTableName, pkMember);
                if (MemberConstants.GJ30720200CODE.equals(memberSettleExt.getParentMemberCode())) {
                    memberSettleExt.setParentMemberCode(memberSettleExt.getMemberCode());
                }
                memberSettleExt.setLevel(0);
                // 添加自身
                cuMemberSettleExts.add(memberSettleExt);

                List<Tree<String>> build = iCuMemberService.systemTjSetListTree(cuMemberSettleExts, memberSettleExt, settleTableName, true);
                // 安置架构 无点位 追加空点位
                return AjaxResult.success(build);
            } else {
                // 查询 历史
                // 查询表格是否存在
                if (level == null) {
                    level = defaultLevel;
                }
                if (tableNumber == null) {
                    settleTableName = null;
                }
                // 查询顶点
                CuMemberSettleExt memberSettleExt = iCuMemberTreeService.queryCuMemberTreeByPlacePkMemberTable(settleTableName, pkMember);
                if (MemberConstants.GJ30720200CODE.equals(memberSettleExt.getParentMemberCode())) {
                    memberSettleExt.setParentMemberCode(memberSettleExt.getMemberCode());
                }
                memberSettleExt.setLevel(0);
                //查询下级
                List<CuMemberSettleExt> list = iCuMemberTreeService.queryCuMemberTreeParentTable(settleTableName, pkMember, level, vertexIdList);
                // 无法查询到自身 所有单独查询自身并且添加
                list.add(memberSettleExt);
                List<Tree<String>> build = iCuMemberService.systemTjSetListTree(list, memberSettleExt, settleTableName, true);
                // 安置架构 无点位 追加空点位
                return AjaxResult.success(build);
            }
        } else {
            if (level == null) {
                level = defaultLevel;
            }
            DateTime dateTime = DateUtil.offsetDay(DateUtils.currentDate(), -1);
            time = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, dateTime);
            CuMemberSettlePeriod csPeriod = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(time);
            if (csPeriod != null) {
                period = csPeriod.getPkId();
            }
            // 查询 历史
            String settleTableName = TableNameConstants.CU_MEMBER_SETTLE + period;
            Integer tableNumber = iCuMemberTreeService.queryExistOracleTable(settleTableName);

            if (tableNumber == null) {
                // 表示表格存在
                settleTableName = null;
            }
            // 查询顶点
            CuMemberSettleExt memberSettleExt = iCuMemberTreeService.queryCuMemberTreeByPlacePkMemberTable(settleTableName, pkMember);

            if (StringUtils.isNotBlank(memberSettleExt.getParentMemberCode()) && MemberConstants.GJ30720200CODE.equals(memberSettleExt.getParentMemberCode())) {
                memberSettleExt.setParentMemberCode(memberSettleExt.getMemberCode());
            }
            memberSettleExt.setLevel(0);
            //查询下级
            List<CuMemberSettleExt> list = iCuMemberTreeService.queryCuMemberTreeParentTable(settleTableName, pkMember, level, vertexIdList);
            // 无法查询到自身 所有单独查询自身并且添加
            list.add(memberSettleExt);
            List<Tree<String>> build = iCuMemberService.systemTjSetListTree(list, memberSettleExt, settleTableName, false);
            // 安置架构 无点位 追加空点位
            return AjaxResult.success(build);
        }
    }

    /**
     * 安置架构五
     *
     * @return
     */
    @GetMapping("/az-framework-five")
    public AjaxResult azFrameWorkFive(String memberCode, Integer level) {
        if (level == null) {
            level = 10;
        }
        // 体系
        UserAuthorityDTO userAuthorityDTO = iUserServiceApi.getUserAuthority(SecurityUtils.getUserId()).getData();
        List<Integer> vertexIdList = userAuthorityDTO.getVertexIdList();
        List<Long> userVertexList = userAuthorityDTO.getUserVertexList();
        Long pkMember;
        if (StringUtils.isNotBlank(memberCode)) {
            CuMember member = iCuMemberService.getMemberByCode(memberCode);
            if (member == null) {
                return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.MEMBER_NOT_EXITS));
            }
            pkMember = member.getPkId();
        } else {
            if (userVertexList != null && userVertexList.size() > 0) {
                CuMember member = iCuMemberService.getMemberById(userVertexList.get(0));
                pkMember = member.getPkId();
//                memberCode = member.getMemberCode();
            } else {
                return AjaxResult.error("请输入会员编号");
            }
        }

//        if (vertexIdList != null && StringUtils.isNotBlank(memberCode)) {
//            // 体系不为空 并且会员编号不为空 只能查询自己伞下以及自己的
//            List<CuMemberExt> memberExtList = iCuMemberService.checkMemberVertex(vertexIdList, memberCode);
//            if (memberExtList.size() == 0) {
//                return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.QUERY_VERTEX_CODE));
//            }
//        }

        // 查询当天 (秒结)
        // 会员结算秒结表
        List<CuMemberSettleExt> cuMemberSettleExts = iCuMemberTreeService.selectByTreeMemberListFive(pkMember, level, vertexIdList);
        // 查询顶点
        CuMemberSettleExt memberSettleExt = iCuMemberTreeService.queryCuMemberTreeByPkMemberTable(null, pkMember);
        if (MemberConstants.GJ30720200CODE.equals(memberSettleExt.getParentMemberCode())) {
            memberSettleExt.setParentMemberCode(memberSettleExt.getMemberCode());
        }
        memberSettleExt.setLevel(0);
        // 添加自身
        cuMemberSettleExts.add(memberSettleExt);
        List<Tree<String>> build = iCuMemberService.systemAzSetListTreeFive(cuMemberSettleExts, memberSettleExt);
        // 安置架构 无点位 追加空点位
        R<Map<Integer, BdCountry>> allCountryMap = iCountryServiceApi.getAllCountryMap();
        Map<Integer, BdCountry> allCountryData = allCountryMap.getData();
        List<Tree<String>> trees = setTreeList(build, level, allCountryData);
        return AjaxResult.success(trees);
    }

    /**
     * 查询最近一月所有期数
     *
     * @return
     */
    @GetMapping("/get-member-settle-period")
    public AjaxResult getMemberSettlePeriod() {
        // 查询最近一月的数据
        DateTime dateTime = DateUtil.lastMonth();
        LambdaQueryWrapper<CuMemberSettlePeriod> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(CuMemberSettlePeriod::getSettleDate);
        queryWrapper.between(CuMemberSettlePeriod::getSettleDate, dateTime, new Date());
        List<CuMemberSettlePeriod> list = iCuMemberSettlePeriodService.list(queryWrapper);
        List<CuMemberSettlePeriodVo> resultList = new ArrayList<>();
        for (CuMemberSettlePeriod cuMemberSettlePeriod : list) {
            CuMemberSettlePeriodVo cuMemberSettlePeriodVo = BeanUtil.copyProperties(cuMemberSettlePeriod, CuMemberSettlePeriodVo.class);
            if (DateUtils.parseDateToFormat(DateUtils.YYYY_MM_DD, cuMemberSettlePeriod.getSettleDate()).equals(DateUtils.parseDateToFormat(DateUtils.YYYY_MM_DD, DateUtils.currentDate()))) {
                cuMemberSettlePeriodVo.setIsThisDay(EYesNo.YES.getIntValue());
            } else {
                cuMemberSettlePeriodVo.setIsThisDay(EYesNo.NO.getIntValue());
            }
            resultList.add(cuMemberSettlePeriodVo);
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 获取头像信息
     *
     * @return
     */
    @GetMapping("/get-avatar-info")
    public AjaxResult getAvatarUrl() {
        JSONArray array = new JSONArray();
        // 虚拟
        JSONObject other = new JSONObject();
        other.put("name", "虚拟");
        other.put("value", MemberFrameworkConstants.xuni);
        array.add(other);
        // 已注销
        JSONObject zxOther = new JSONObject();
        zxOther.put("name", "已注销");
        zxOther.put("value", MemberFrameworkConstants.LOG_OFF);
        array.add(zxOther);
        // 待激活  非中国
        if (!SecurityUtils.getPkCountry().equals(CountryConstants.CHINA_COUNTRY)) {
            JSONObject djhOther = new JSONObject();
            djhOther.put("name", "待激活");
            djhOther.put("value", MemberFrameworkConstants.toBeActivated);
            array.add(djhOther);
        }
        R<List<GradeDTO>> list = iGradeServiceApi.selectGradeAll(SecurityUtils.getPkCountry());

        for (GradeDTO date : list.getData()) {
            JSONObject js = new JSONObject();
            js.put("name", date.getGradeName());
            js.put("value", date.getIcon());
            array.add(js);
        }
        return AjaxResult.success(array);
    }

    /**
     * 图片转base64
     *
     * @return
     */
    @PostMapping("/get-url-base64")
    public Map<String, String> getUrlBase64(@RequestBody Map<String, String> urlMap) {
        Map<String, String> resultMap = new HashMap<>();
        for (Map.Entry<String, String> ss : urlMap.entrySet()) {
            String base64Url = Base64Util.toUrlBase64(ss.getValue());
            resultMap.put(ss.getKey() + "Base64", base64Url);
        }
        return resultMap;
    }

}
