package cn.newfeifan.mall.module.distri.dal.mysql.sharepath;

import java.util.*;

import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.newfeifan.mall.framework.mybatis.core.mapper.BaseMapperX;
import cn.newfeifan.mall.module.distri.dal.dataobject.sharepath.SharePathDO;
import org.apache.ibatis.annotations.Mapper;
import cn.newfeifan.mall.module.distri.controller.admin.sharepath.vo.*;
import org.apache.ibatis.annotations.Param;

/**
 * 分销用户关系 Mapper
 *
 * @author 非繁人
 */
@Mapper
public interface SharePathMapper extends BaseMapperX<SharePathDO> {

    default PageResult<SharePathDO> selectPage(SharePathPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<SharePathDO>()
                .eqIfPresent(SharePathDO::getDepth, reqVO.getDepth())
                .betweenIfPresent(SharePathDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(SharePathDO::getAncestor, reqVO.getAncestor())
                .eqIfPresent(SharePathDO::getDescendant, reqVO.getDescendant())
                .eqIfPresent(SharePathDO::getSort, reqVO.getSort())
                .likeIfPresent(SharePathDO::getAncName, reqVO.getAncName())
                .eqIfPresent(SharePathDO::getAncPhone, reqVO.getAncPhone())
                .likeIfPresent(SharePathDO::getDescName, reqVO.getDescName())
                .eqIfPresent(SharePathDO::getDescPhone, reqVO.getDescPhone())
                .likeIfPresent(SharePathDO::getAncNickName, reqVO.getAncNickName())
                .likeIfPresent(SharePathDO::getDescNickName, reqVO.getDescNickName())
                .orderByDesc(SharePathDO::getId));
    }
    default PageResult<SharePathDO> selectDescendantsPage(SharePathPageReqVO reqVO) {
        LambdaQueryWrapperX<SharePathDO> queryWrapper = new LambdaQueryWrapperX<SharePathDO>()
                .eqIfPresent(SharePathDO::getAncestor, reqVO.getAncestor());
        if(reqVO.getDepth() != null){
            queryWrapper.eq(SharePathDO::getDepth, 1);
            queryWrapper.orderByAsc(SharePathDO::getSort);
        }
        return selectPage(reqVO, queryWrapper);
    }

    void addNode(@Param("parentId") Long parentId,
                 @Param("parentName") String parentName,
                 @Param("parentNickName") String parentNickName,
                 @Param("parentMobile") String parentMobile,

                 @Param("newDescendantId") Long newDescendantId,
                 @Param("newDescendantName") String newDescendantName,
                 @Param("newDescendantNickName") String newDescendantNickName,
                 @Param("newDescendantMobile") String newDescendantMobile);


    int getMaxSortByParentId(@Param("parentId") Long parentId);

    // 删除节点及其子节点的旧祖先关系
    void deleteOldAncestors(@Param("nodeId") Long nodeId);

    // 添加节点及其子节点的新祖先关系
    void addNewAncestors(@Param("nodeId") Long nodeId, @Param("newParentId") Long newParentId);


    // 查询该节点的所有子节点信息
    List<Long> findAllDescendants(@Param("ancestorId") Long ancestorId);

    // 查询该节点的顺序号小于该节点兄弟节点
    List<Long> findLessSortedSiblings(@Param("descendantId") Long descendantId);


    List<TreeNode> selectDescendants(@Param("ancestor") Long userId,  @Param("maxDepth") Integer maxDepth);

    default Long selectAncestorByDescendant(Long userId){
        return selectOne(new LambdaQueryWrapperX<SharePathDO>()
                .eq(SharePathDO::getDescendant, userId)
                .eq(SharePathDO::getDepth, 1)
        ).getAncestor();
    }

    /**
     * 根据是否为游客查询分享关系
     * @param ancestor 上级id
     * @param visitor 是否游客
     * @return 分销关系
     */
    List<SharePathDO> selectListByVisitor(@Param("ancestor") Long ancestor, @Param("visitor") Boolean visitor);
}




