admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.yeshi.fanli.service.impl.money;
 
import com.aliyun.openservices.ons.api.Message;
import com.yeshi.fanli.dao.mybatis.UserInfoMapper;
import com.yeshi.fanli.dao.mybatis.money.UserMoneyDetailMapper;
import com.yeshi.fanli.dao.mybatis.share.ShareMapper;
import com.yeshi.fanli.dto.money.UserMoneyChangeDTO;
import com.yeshi.fanli.dto.mq.user.UserTopicTagEnum;
import com.yeshi.fanli.dto.mq.user.body.UserMoneyChangeMQMsg;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.money.UserMoneyDetail;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.money.UserMoneyService;
import com.yeshi.fanli.service.manger.msg.RocketMQManager;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.mq.cmq.UserMoneyChangeCMQManager;
import com.yeshi.fanli.util.rocketmq.MQMsgBodyFactory;
import com.yeshi.fanli.util.rocketmq.MQTopicName;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
 
@Service
public class UserMoneyServiceImpl implements UserMoneyService {
 
    @Resource
    private ShareMapper shareMapper;
 
    @Resource
    private UserInfoMapper userInfoMapper;
 
    @Resource
    private UserMoneyDetailMapper userMoneyDetailMapper;
 
    @Resource
    private RocketMQManager rocketMQManager;
 
    @Resource
    private UserMoneyChangeCMQManager userMoneyChangeCMQManager;
 
    @Override
    public BigDecimal getMoneyToday(Long uid) {
        BigDecimal money = shareMapper.getMoneyToday(uid + "");
        return money == null ? new BigDecimal(0) : money;
    }
 
    @Override
    public BigDecimal getMoneyMonth(Long uid) {
        BigDecimal money = shareMapper.getMoneyMonth(uid + "");
        return money == null ? new BigDecimal(0) : money;
    }
 
    @Override
    public BigDecimal getMoneyLastMonth(Long uid) {
        BigDecimal money = shareMapper.getMoneyLastMonth(uid + "");
 
        return money == null ? new BigDecimal(0) : money;
    }
 
    @Transactional
    @Override
    public void subUserMoney(Long uid, BigDecimal money, UserMoneyDetail detail) {
        userMoneyDetailMapper.insertSelective(detail);
        userInfoMapper.subHongBaoByUid(uid, money);
        try {
            if (!Constant.IS_TEST)
                userMoneyChangeCMQManager
                        .addUserMoneyChangeMsg(new UserMoneyChangeDTO(uid, new BigDecimal(0).subtract(money)));
        } catch (Exception e) {
            try {
                LogHelper.errorDetailInfo(e);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }
 
    @Transactional
    @Override
    public void addUserMoney(Long uid, BigDecimal money, UserMoneyDetail detail) {
        userMoneyDetailMapper.insertSelective(detail);
        userInfoMapper.addHongBaoByUid(uid, money);
        try {
            if (!Constant.IS_TEST)
                userMoneyChangeCMQManager.addUserMoneyChangeMsg(new UserMoneyChangeDTO(uid, money));
        } catch (Exception e) {
            try {
                LogHelper.errorDetailInfo(e);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        // 发送资金到账消息
 
        if (!Constant.IS_TEST) {
            UserMoneyChangeMQMsg msg = new UserMoneyChangeMQMsg(uid, money, new Date());
            Message message = MQMsgBodyFactory.create(MQTopicName.TOPIC_USER, UserTopicTagEnum.userMoneyAdd, msg);
            rocketMQManager.sendNormalMsg(message, null);
        }
    }
 
    @Override
    public BigDecimal getBalance(Long uid) {
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(uid);
        if (userInfo == null)
            return null;
        return userInfo.getMyHongBao();
    }
 
}