package com.ruoyi.ss.withdraw.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.ServiceConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.ss.channelWithdraw.domain.ChannelWithdrawVO;
import com.ruoyi.ss.channelWithdraw.service.ChannelWithdrawService;
import com.ruoyi.ss.user.domain.UserVO;
import com.ruoyi.ss.user.service.IUserService;
import com.ruoyi.ss.userWithdraw.domain.UserWithdraw;
import com.ruoyi.ss.userWithdraw.service.IUserWithdrawService;
import com.ruoyi.ss.withdraw.domain.Withdraw;
import com.ruoyi.ss.withdraw.domain.WithdrawVO;
import com.ruoyi.ss.withdraw.domain.bo.WithdrawBO;
import com.ruoyi.ss.withdraw.domain.dto.MchWithdrawDTO;
import com.ruoyi.ss.withdraw.domain.dto.WithdrawApprovalDTO;
import com.ruoyi.ss.withdraw.mapper.WithdrawMapper;
import com.ruoyi.ss.withdraw.service.IWithdrawService;
import com.ruoyi.ss.withdraw.service.IWithdrawValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

import static com.ruoyi.common.constant.ServiceConstants.USER_TYPE_MERCHANT;
import static com.ruoyi.common.constant.ServiceConstants.USER_TYPE_PARTNER;
import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 提现记录Service业务层处理
 *
 * @author qzz
 * @date 2024-09-28
 */
@Service
public class WithdrawValidatorImpl implements IWithdrawValidator
{
    @Autowired
    private IWithdrawService withdrawService;

    @Autowired
    private ChannelWithdrawService channelWithdrawService;

    @Autowired
    private IUserWithdrawService userWithdrawService;

    @Autowired
    private IUserService userService;

    /**
     * 提现前校验
     *
     * // todo 提现前校验  1. 校验提现渠道是否可用  2. 用户余额是否充足（包含提现中的金额）  3.非商户不能提现  4. 系统提现渠道是否存在  5.用户提现渠道是否存在
     */
    @Override
    public WithdrawBO preWithdraw(MchWithdrawDTO dto) {

        UserWithdraw userWithdraw = userWithdrawService.selectRlUserWithdrawByUserChannelId(dto.getUserWithdrawChannelId());
        ServiceUtil.assertion(userWithdraw == null, "用户提现渠道不存在");
        ServiceUtil.assertion(!userWithdraw.getIsOpen(), "用户提现渠道不可用");

        ChannelWithdrawVO channelWithdrawVO = channelWithdrawService.selectChannelWithdrawByChannelId(userWithdraw.getChannelId());
        ServiceUtil.assertion(channelWithdrawVO == null, "系统提现渠道不存在");
        ServiceUtil.assertion(!channelWithdrawVO.getEnabled(), "系统提现渠道不可用");

        UserVO userVO = userService.selectUserById(getUserId());
        ServiceUtil.assertion(!Objects.equals(userVO.getUserId(), userWithdraw.getUserId()), "非本人用户提现渠道，不可用");
        ServiceUtil.assertion(!(userVO.getUserType().equals(USER_TYPE_MERCHANT) || userVO.getUserType().equals(USER_TYPE_PARTNER)), "非商户/合伙人不能提现");
        //可提现金额 = 余额 - 提现中金额 - 今日订单收入
        BigDecimal withdrawableAmount = userService.getWithdrawAbleAmount(userVO);
        ServiceUtil.assertion(withdrawableAmount.compareTo(dto.getAmount()) < 0, "可提现金额不足");

        ServiceUtil.assertion(userWithdraw.getMaxAmount().compareTo(dto.getAmount()) < 0, "提现金额超出最大限额");
        ServiceUtil.assertion(userWithdraw.getMinAmount().compareTo(dto.getAmount()) > 0, "提现金额低于最小限额");

        WithdrawBO bo = new WithdrawBO();
        bo.setUser(userVO);
        bo.setUserWithdraw(userWithdraw);
        bo.setDto(dto);
        bo.setChannelWithdraw(channelWithdrawVO);
        return bo;
    }
}
