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.domain.UserInfo;
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.mapper.UserInviteMapper;
import com.ruoyi.cai.service.UserInfoService;
import com.ruoyi.cai.service.UserInviteService;
import com.ruoyi.cai.service.UserService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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 {

    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoService userInfoService;

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

    @Override
    public InviteHomeVo inviteHome(Long userId) {
        InviteHomeVo vo = new InviteHomeVo();
        List<String> reward = new ArrayList<>();
        reward.add("1、邀请主播认证通过奖励#300#紫贝");
        reward.add("2、邀请用户首次充值奖励#1200#紫贝");
        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 null;
    }

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

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateUserInviteRate(UserInvite bo) {
        UserInvite userInvite = this.getById(bo.getId());
        if(userInvite == null){
            throw new ServiceException("数据不存在");
        }
        UserInvite update = new UserInvite();
        update.setId(bo.getId());
        update.setGiftIncomeRate(bo.getGiftIncomeRate());
        update.setVideoIncomeRate(bo.getVideoIncomeRate());
        update.setGuardIncomeRate(bo.getGuardIncomeRate());
        update.setPayIncomeRate(bo.getPayIncomeRate());
        boolean b = this.updateById(update);
        if(b){
            UserInfo userInfo = new UserInfo();
            userInfo.setGiftIncomeRate(bo.getGiftIncomeRate());
            userInfo.setVideoIncomeRate(bo.getVideoIncomeRate());
            userInfo.setGuardIncomeRate(bo.getGuardIncomeRate());
            userInfo.setPayIncomeRate(bo.getPayIncomeRate());
            userInfoService.update(Wrappers.lambdaUpdate(UserInfo.class).eq(UserInfo::getUserId,userInvite.getUserId())
                    .set(UserInfo::getGiftIncomeRate,bo.getGiftIncomeRate())
                    .set(UserInfo::getVideoIncomeRate,bo.getVideoIncomeRate())
                    .set(UserInfo::getGuardIncomeRate,bo.getGuardIncomeRate())
                    .set(UserInfo::getPayIncomeRate,bo.getPayIncomeRate()));
        }
        return b;
    }
}
