package cn.newfeifan.mall.module.distri.service.sharepath;

import cn.newfeifan.mall.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.newfeifan.mall.module.distri.service.duser.DuserService;
import cn.newfeifan.mall.module.member.dal.dataobject.user.MemberUserDO;
import cn.newfeifan.mall.module.member.service.user.MemberUserService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;


import cn.newfeifan.mall.module.distri.controller.admin.sharepath.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.sharepath.SharePathDO;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;

import cn.newfeifan.mall.module.distri.dal.mysql.sharepath.SharePathMapper;

import java.util.List;
import java.util.stream.Collectors;

import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.newfeifan.mall.module.distri.enums.ErrorCodeConstants.*;

/**
 * 分销用户关系 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class SharePathServiceImpl implements SharePathService {

    @Resource
    private SharePathMapper sharePathMapper;

    @Resource
    @Lazy
    private DuserService duserService;

    @Resource
    private MemberUserService memberUserService;


    @Override
    public Long createSharePath(SharePathSaveReqVO createReqVO) {
        // 插入
        SharePathDO sharePath = BeanUtils.toBean(createReqVO, SharePathDO.class);
        sharePathMapper.insert(sharePath);
        // 返回
        return sharePath.getId();
    }


    @Override
    public void createSharePathByUserId(SharePathSaveReqVO createReqVO) {
        // 如果直推人为空, 则直接结束
        if (createReqVO.getDescendant() == null) {
            return;
        }

        // 判断当前直推人是否有父亲
        Boolean has = hasParent(createReqVO.getDescendant());
        if (has) {
            throw exception(HAS_PARENT);
        }
        MemberUserDO ancestor = memberUserService.getUser(createReqVO.getAncestor());
        MemberUserDO descendant = memberUserService.getUser(createReqVO.getDescendant());

        // 创建直推人以及推荐人的关系
        // 需要判断当前推荐人跟直推人是否已经存在于用户表(duser)了
        duserService.hasUserAndCreat(createReqVO);

        // 存储他们之间的关系
        sharePathMapper.addNode(
                createReqVO.getAncestor(),
                ancestor.getName(),
                ancestor.getNickname(),
                ancestor.getMobile(),

                createReqVO.getDescendant(),
                descendant.getName(),
                descendant.getNickname(),
                descendant.getMobile());

    }

    private Boolean hasParent(Long descendant) {
        SharePathDO sharePathDO = sharePathMapper.selectOne(new LambdaQueryWrapperX<SharePathDO>()
                .eqIfPresent(SharePathDO::getDescendant, descendant)
                .eqIfPresent(SharePathDO::getDepth, 1));
        return sharePathDO != null;
    }

    @Override
    public void updateSharePath(SharePathSaveReqVO updateReqVO) {
        // 校验存在
        validateSharePathExists(updateReqVO.getId());
        // 更新
        SharePathDO updateObj = BeanUtils.toBean(updateReqVO, SharePathDO.class);
        sharePathMapper.updateById(updateObj);
    }

    @Override
    public void deleteSharePath(Long id) {
        // 校验存在
        validateSharePathExists(id);
        // 删除
        sharePathMapper.deleteById(id);
    }

    private void validateSharePathExists(Long id) {
        if (sharePathMapper.selectById(id) == null) {
            throw exception(SHARE_PATH_NOT_EXISTS);
        }
    }

    @Override
    public SharePathDO getSharePath(Long id) {
        return sharePathMapper.selectById(id);
    }

    @Override
    public PageResult<SharePathDO> getSharePathPage(SharePathPageReqVO pageReqVO) {
        return sharePathMapper.selectPage(pageReqVO);
    }

    @Override
    public TreeNode getTree() {


        return null;
    }

    @Override
    public void transferenceRela(SharePathtransFerenceRelaReqVO sharePathtransFerenceRelaReqVO) {
        // todo 转让用户关系
    }

    @Override
    public List<Long> sonsId(Long userId) {
        List<SharePathDO> sharePathDOS = sharePathMapper.selectList(new LambdaQueryWrapperX<SharePathDO>().eq(SharePathDO::getAncestor, userId));
        return sharePathDOS.stream().map(SharePathDO::getDescendant).collect(Collectors.toList());

    }



}