package com.ruoyi.cai.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.cai.domain.*;
import com.ruoyi.cai.dto.app.query.GiveGiftRes;
import com.ruoyi.cai.dto.app.vo.index.UserGiftIndexVo;
import com.ruoyi.cai.enums.AccountBusinessEnum;
import com.ruoyi.cai.enums.ConsumeLogType;
import com.ruoyi.cai.manager.IdManager;
import com.ruoyi.cai.manager.LockManager;
import com.ruoyi.cai.mapper.UserGiftMapper;
import com.ruoyi.cai.service.*;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 礼物记录Service业务层处理
 *
 * @author 77
 * @date 2023-12-23
 */
@RequiredArgsConstructor
@Service
public class UserGiftServiceImpl extends ServiceImpl<UserGiftMapper, UserGift> implements UserGiftService {

    @Autowired
    private GiftService giftService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private UserService userService;
    @Autowired
    private AnchorService anchorService;
    @Autowired
    private RedissonClient redissonClient;


    @Override
    public List<UserGiftIndexVo> selectGiftList(Long userId) {
        return baseMapper.selectGiftList(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean giveGift(GiveGiftRes query) {
        Long fromUserId = LoginHelper.getUserId();
        Gift gift = giftService.getById(query.getGiftId());
        if(gift == null){
            throw new ServiceException("礼物不存在");
        }
        String guardLock = LockManager.getSendGuardLock(fromUserId);
        RLock lock = redissonClient.getLock(guardLock);
        if(lock.isLocked()){
            throw new ServiceException("您点击太快了");
        }
        try {
            lock.lock(3, TimeUnit.SECONDS);
            User fromUser = userService.getById(fromUserId);
            User toUser = userService.getById(query.getToUserId());
            Anchor anchor = anchorService.getByUserId(query.getToUserId());
            Long price = gift.getPrice();
            Long traceId = IdManager.nextId();
            Long giftAmount = query.getGiftCount() * price;
            ConsumeLog consumeLog = new ConsumeLog();
            consumeLog.init(fromUser,toUser);
            consumeLog.setTraceId(traceId);
            consumeLog.setType(ConsumeLogType.GIFT.getCode());
            consumeLog.setAmount(giftAmount);
            consumeLog.setTargetRate(anchor.getGiftRate());
            accountService.decr(consumeLog, AccountBusinessEnum.GIFT);
            UserGift userGift = new UserGift();
            userGift.setType(query.getType());
            userGift.setFromUid(fromUserId);
            userGift.setFromMobile(fromUser.getMobile());
            userGift.setFromUsercode(fromUser.getUsercode());
            userGift.setToUid(query.getToUserId());
            userGift.setToMobile(toUser.getMobile());
            userGift.setToUsercode(toUser.getUsercode());
            userGift.setGiftId(query.getGiftId());
            userGift.setGiftName(gift.getName());
            userGift.setGiftImg(gift.getImg());
            userGift.setGiftPrice(gift.getPrice());
            userGift.setGiftCount(query.getGiftCount());
            userGift.setGiftAmount(giftAmount);
            userGift.setTraceId(traceId);
            this.save(userGift);
        }finally {
            lock.unlock();
        }
        return true;
    }
}
