package com.sonic.frog.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sonic.frog.dao.GiftRewardRecordDao;
import com.sonic.frog.domain.entity.GiftDict;
import com.sonic.frog.domain.entity.GiftRewardRecord;
import com.sonic.frog.enums.Constants;
import com.sonic.frog.service.GiftRewardRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 礼物打赏记录服务实现类
 */
@Service
@Slf4j
public class GiftRewardRecordServiceImpl extends ServiceImpl<GiftRewardRecordDao, GiftRewardRecord> implements GiftRewardRecordService {
    @Override
    public void addGiftRewardRecord(GiftDict giftDict, String orderNo, Long fromUid, Long toUid, Integer num) {
        Long total = giftDict.getPrice() * num;
        // 用户实际收入
        long incomeTotal = new BigDecimal(total).multiply(Constants.USER_RATE).longValue();
        //抽成
        Long platformFee = total - incomeTotal;
        GiftRewardRecord giftRewardRecord = GiftRewardRecord.builder()
                .fromUid(fromUid)
                .toUid(toUid)
                .orderNo(orderNo)
                .giftId(giftDict.getId())
                .giftName(giftDict.getName())
                .price(giftDict.getPrice())
                .num(num)
                .total(giftDict.getPrice() * num)
                .incomeTotal(incomeTotal)
                .platformFee(platformFee)
                .build();
        save(giftRewardRecord);
    }
}