package com.ruoyi.system.user.service.impl;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.bst.areaJoin.domain.AreaJoinQuery;
import com.ruoyi.bst.areaJoin.domain.enums.AreaJoinType;
import com.ruoyi.bst.areaJoin.service.AreaJoinDashboard;
import com.ruoyi.common.core.domain.vo.UserVO;
import com.ruoyi.common.utils.MathUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.system.user.domain.UserQuery;
import com.ruoyi.system.user.mapper.UserMapper;
import com.ruoyi.system.user.service.UserDashboard;
import com.ruoyi.system.user.service.UserValidator;

/**
 * @author wjh
 * 2024/11/22
 */
@Service
public class UserValidatorImpl implements UserValidator {

    @Autowired
    private UserDashboard userDashboard;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AreaJoinDashboard areaJoinDashboard;

    @Override
    public void validate(UserVO vo) {
        ServiceUtil.assertion(vo == null, "用户不存在");

        ServiceUtil.assertion(!this.checkUserNameUnique(vo), "用户账号%s已存在", vo.getUserName());

        // 不允许分成大于代理商
        if (vo.getAgentId() != null) {
            ServiceUtil.assertion(MathUtils.biggerThan(vo.getPoint(), vo.getAgentPoint()),
            "用户【%s】分成比例（%s%%)不能超过代理商分成比例（%s%%）", vo.getNickName(), vo.getPoint(), vo.getAgentPoint());
        }

        // 不允许分成小于底下的运营商
        // 查询最大分成比例
        UserQuery query = new UserQuery();
        query.setAgentId(vo.getUserId());
        BigDecimal maxOfPoint = userDashboard.selectMaxOfPoint(query);
        ServiceUtil.assertion(MathUtils.smallerThan(vo.getPoint(), maxOfPoint),
        "用户【%s】分成比例（%s%%)不能小于底下的运营商分成比例（%s%%）", vo.getNickName(), vo.getPoint(), maxOfPoint);

        // 不允许分成小于底下的加盟商
        AreaJoinQuery joinQuery = new AreaJoinQuery();
        joinQuery.setMchId(vo.getUserId());
        BigDecimal maxOfJoinPoint = areaJoinDashboard.selectMaxOfPoint(joinQuery);
        ServiceUtil.assertion(MathUtils.smallerThan(vo.getPoint(), maxOfJoinPoint),
        "用户【%s】分成比例（%s%%)不能小于底下的加盟商分成比例（%s%%）", vo.getNickName(), vo.getPoint(), maxOfJoinPoint);

        // 不允许分成小于底下的合伙人总和
        AreaJoinQuery partnerQuery = new AreaJoinQuery();
        partnerQuery.setMchId(vo.getUserId());
        partnerQuery.setType(AreaJoinType.PARTNER.getCode());
        BigDecimal sumOfPartnerPoint = areaJoinDashboard.selectSumOfPoint(partnerQuery);
        ServiceUtil.assertion(MathUtils.smallerThan(vo.getPoint(), sumOfPartnerPoint),
        "用户【%s】分成比例（%s%%)不能小于底下的合伙人总和（%s%%）", vo.getNickName(), vo.getPoint(), sumOfPartnerPoint);
    }

    @Override
    public boolean canView(List<Long> userIds) {
        if (CollectionUtils.isEmptyElement(userIds)) {
            return true;
        }
        UserQuery query = new UserQuery();
        query.setUserIds(userIds);
        query.setScope(true);
        List<Long> ids = userMapper.selectIdList(query);
        return new HashSet<>(ids).containsAll(userIds);
    }

    @Override
    public boolean canEdit(Long userId) {
        return canView(userId);
    }

    @Override
    public boolean checkUserNameUnique(UserVO user) {
        if (user == null) {
            return true;
        }
        return checkUserNameUnique(user.getUserName(), user.getUserId());
    }

    private boolean checkUserNameUnique(String userName, Long userId) {
        if (StringUtils.isBlank(userName)) {
            return true;
        }
        UserQuery query = new UserQuery();
        query.setEqUserName(userName);
        query.setExcludeUserId(userId);
        return userMapper.selectCount(query) == 0;
    }
}

