package com.ruoyi.cai.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.cai.config.CaiProperties;
import com.ruoyi.cai.domain.UserInvite;
import com.ruoyi.cai.dto.admin.vo.UserInviteAdminVo;
import com.ruoyi.cai.dto.app.dto.InviteCountDTO;
import com.ruoyi.cai.dto.app.vo.invite.InviteHomeVo;
import com.ruoyi.cai.dto.app.vo.user.UserListVo;
import com.ruoyi.cai.executor.ExecutorConstant;
import com.ruoyi.cai.mapper.UserInviteMapper;
import com.ruoyi.cai.service.UserInviteService;
import com.ruoyi.common.core.domain.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户邀请Service业务层处理
 *
 * @author 77
 * @date 2023-12-28
 */
@Service
public class UserInviteServiceImpl extends ServiceImpl<UserInviteMapper, UserInvite> implements UserInviteService {

    @Override
    public UserInvite getByUserId(Long id) {
        return this.getOne(Wrappers.lambdaQuery(UserInvite.class).eq(UserInvite::getUserId,id).last("limit 1"));
    }
    @Autowired
    private CaiProperties caiProperties;

    @Override
    public InviteHomeVo inviteHome(Long userId) {
        InviteHomeVo vo = new InviteHomeVo();
        List<String> reward = new ArrayList<>();
        reward.add("1、邀请主播认证通过奖励#300#"+caiProperties.getCoinName());
        reward.add("2、邀请用户首次充值奖励#1200#"+caiProperties.getCoinName());
        reward.add("3、主播视频、礼物、会员分成#50%#");
        reward.add("4、邀请男用户充值提成#30%#");
        reward.add("5、邀请主播收益#7%#");
        reward.add("最终解释权归平台所有");
        vo.setReward(reward);
        InviteCountDTO inviteCount = baseMapper.countInviteAndReward(userId);
        vo.setTotalInvite(inviteCount.getTotalInvite());
        vo.setTotalReward(inviteCount.getTotalReward());
        return vo;
    }

    /**
     * 获取我邀请的人
     */
    @Override
    public Page<UserListVo> inviteUserPage(PageQuery pageQuery, Long inviteId,String usercode) {
        return baseMapper.inviteUserPage(pageQuery.build(),inviteId,usercode);
    }

    @Override
    public Page<UserInviteAdminVo> pageAdmin(PageQuery pageQuery, UserInviteAdminVo bo) {
        return baseMapper.pageAdmin(pageQuery.build(), bo);
    }

    /**
     *
     * @param geUserId 获取收益的人
     * @param seUserId 创建收益的人
     */
    @Override
    public void incsCoinAsync(Long geUserId, Long seUserId, Long amount) {
        ExecutorConstant.COMMON_EXECUTOR.execute(() -> {
            if(geUserId == null || seUserId == null || amount == null || amount <= 0){
                log.error("执行收益的递增失败【参数异常】");
                return;
            }
            UserInvite one = this.getOne(Wrappers.lambdaQuery(UserInvite.class)
                    .eq(UserInvite::getInviteId, geUserId)
                    .eq(UserInvite::getUserId, seUserId));
            if(one == null){
                log.error("执行收益的递增失败");
                return;
            }
            baseMapper.incsCoinTotal(one.getId(),amount);
        });
    }

}
