package com.webchat.service;

import com.webchat.common.enums.WalletTransEventEnum;
import com.webchat.domain.vo.dto.OpenRedPacketMessageDTO;
import com.webchat.repository.dao.IRedPacketRecordDAO;
import com.webchat.repository.entity.RedPacketRecordEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 程序员七七， https://www.coderutil.com网站作者
 * @date 2024/11/9 13:51
 */
@Service
public class RedPacketRecordService {

    @Autowired
    private UserWalletService userWalletService;

    @Autowired
    private IRedPacketRecordDAO redPacketRecordDAO;

    /**
     * 红包拆分消息消费
     *
     * @param openRedPacketMessageDTO
     */
    @Transactional
    public void consumption(OpenRedPacketMessageDTO openRedPacketMessageDTO) {

        // 1. 拆红包记录
        RedPacketRecordEntity redPacketRecordEntity = new RedPacketRecordEntity();
        redPacketRecordEntity.setRedPacketId(openRedPacketMessageDTO.getRedPacketId());
        redPacketRecordEntity.setUserId(openRedPacketMessageDTO.getUserId());
        redPacketRecordEntity.setMoney(openRedPacketMessageDTO.getMoney());
        redPacketRecordEntity.setCreateDate(new Date(openRedPacketMessageDTO.getTime()));
        redPacketRecordDAO.save(redPacketRecordEntity);

        // 2. 钱包收入
        userWalletService.doTrans(WalletTransEventEnum.RECEIVE_PACKET,
                                    openRedPacketMessageDTO.getUserId(),
                                    openRedPacketMessageDTO.getTargetUserId(),
                                    openRedPacketMessageDTO.getMoney());



    }
}
