admin
2022-05-19 a44f2c3b5db92069ea2813ecf8cb12a6ab3b2203
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.yeshi.makemoney.app.service.impl.goldcorn;
 
import com.ks.lib.common.exception.ParamsException;
import com.ks.push.exception.BPushTaskException;
import com.yeshi.makemoney.app.dto.mq.GoldCornSettleMQMsg;
import com.yeshi.makemoney.app.entity.SystemEnum;
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornConsumeRecord;
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornConsumeType;
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornSettleRecord;
import com.yeshi.makemoney.app.entity.money.UserMoneyRecord;
import com.yeshi.makemoney.app.entity.user.UserInfo;
import com.yeshi.makemoney.app.exception.goldcorn.GoldCornConsumeRecordException;
import com.yeshi.makemoney.app.exception.goldcorn.GoldCornMoneyExchangeRateRecordException;
import com.yeshi.makemoney.app.exception.goldcorn.GoldCornSettleRecordException;
import com.yeshi.makemoney.app.exception.money.UserMoneyRecordException;
import com.yeshi.makemoney.app.exception.user.UserInfoException;
import com.yeshi.makemoney.app.service.inter.goldcorn.*;
import com.yeshi.makemoney.app.service.inter.money.UserMoneyService;
import com.yeshi.makemoney.app.service.inter.msg.UserMsgNotifyService;
import com.yeshi.makemoney.app.service.inter.user.UserInfoService;
import com.yeshi.makemoney.app.service.manager.PushManager;
import com.yeshi.makemoney.app.service.query.goldcorn.GoldCornGetRecordQuery;
import com.yeshi.makemoney.app.utils.factory.UserMoneyRecordFactory;
import com.yeshi.makemoney.app.utils.factory.goldcorn.GoldCornConsumeRecordFactory;
import com.yeshi.makemoney.app.utils.factory.msg.UserMsgFactory;
import com.yeshi.makemoney.app.utils.goldcorn.GoldCornUtil;
import com.yeshi.makemoney.app.utils.mq.CMQManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.ThreadUtil;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
/**
 * @author hxh
 * @title: GoldCornSettleServiceImpl
 * @description: TODO
 * @date 2022/4/7 11:47
 */
@Service
public class GoldCornSettleServiceImpl implements GoldCornSettleService {
 
    @Resource
    private GoldCornGetRecordService goldCornGetRecordService;
 
    @Resource
    private GoldCornConsumeRecordService goldCornConsumeRecordService;
 
    @Resource
    private GoldCornMoneyExchangeRateRecordService goldCornMoneyExchangeRateRecordService;
 
    @Resource
    private UserInfoService userInfoService;
 
    @Resource
    private UserMoneyService userMoneyService;
 
    @Resource
    private UserMsgNotifyService userMsgNotifyService;
 
    @Resource
    private PushManager pushManager;
 
    @Resource
    private GoldCornSettleRecordService goldCornSettleRecordService;
 
    @Override
    public void startSettle(Date date, String remarks, SystemEnum system) throws GoldCornSettleRecordException, ParamsException {
        String day = GoldCornUtil.getFormatDay(date);
 
        if (GoldCornUtil.convertFormatDay(day).getTime() >= GoldCornUtil.convertFormatDay(GoldCornUtil.getFormatDay(new Date())).getTime()) {
            throw new ParamsException(ParamsException.CODE_PARAMS_NOT_ENOUGH, "只能结算今天以前的日期");
        }
 
        BigDecimal rate = goldCornMoneyExchangeRateRecordService.getRate(day, system, new Date());
        if (rate == null) {
            throw new ParamsException(ParamsException.CODE_PARAMS_NOT_ENOUGH, "汇率为空");
        }
 
        //统计金币数量
        GoldCornGetRecordQuery query = new GoldCornGetRecordQuery();
        query.setDay(day);
        Map<String, Long> numMap = goldCornGetRecordService.sumGoldCornNum(query);
        if (numMap.get(day) == null) {
            throw new ParamsException(ParamsException.CODE_PARAMS_NOT_ENOUGH, "金币数量统计出错");
        }
 
 
        GoldCornSettleRecord record = new GoldCornSettleRecord();
        record.setSystem(system);
        record.setDay(day);
        record.setRate(rate);
        record.setCornNum(numMap.get(day));
        record.setTotalMoney(new BigDecimal(0));
 
 
        long count = goldCornGetRecordService.countUidsByDay(day);
        record.setUserCount(count);
        record.setSettledUserCount(0L);
        goldCornSettleRecordService.add(record);
 
        int pageSize = 1000;
        int page = (int) (count % pageSize == 0 ? count / pageSize : count / pageSize + 1);
        for (int i = 0; i < page; i++) {
            List<Long> list = goldCornGetRecordService.getUidsByDay(day, i + 1, pageSize);
            for (Long uid : list) {
                //加入到CMQ
                CMQManager.getInstance().addGoldCornSettleMsg(new GoldCornSettleMQMsg(record.getId(), uid, day, System.currentTimeMillis()));
            }
        }
 
 
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void settle(Long uid, String day, BigDecimal rate, String settleId) throws GoldCornConsumeRecordException, UserMoneyRecordException {
        long cornNum = goldCornGetRecordService.getGoldCornByDay(uid, GoldCornUtil.convertFormatDay(day));
        //金币数量
        GoldCornConsumeRecord record = GoldCornConsumeRecordFactory.createExchange(uid, day, (int) cornNum, rate, settleId);
        try {
            UserMoneyRecord moneyRecord = UserMoneyRecordFactory.createGoldCornExchange(record);
            userMoneyService.addUserMoney(moneyRecord);
        } catch (ParamsException e) {
            throw new UserMoneyRecordException(e.getCode(), e.getMsg());
        }
        goldCornConsumeRecordService.add(record);
        //消息通知不需要在主线程
        ThreadUtil.run(new Runnable() {
            @Override
            public void run() {
                try {
                    userMsgNotifyService.notify(UserMsgFactory.createGoldCornExchange(record, userMoneyService.getBalance(uid)));
                    //添加站外推送
                    UserInfo user = userInfoService.get(uid);
                    if (user == null) {
                        return;
                    }
                    pushManager.pushGoldCornSettle(user.getSystem(), record.getUid(), Math.abs(record.getCornNum()), new BigDecimal(record.getMoney()).divide(new BigDecimal(100), 2, RoundingMode.FLOOR));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
 
    @Override
    public void settle(Long uid, String day, String settleId) throws UserInfoException, GoldCornMoneyExchangeRateRecordException, UserMoneyRecordException, GoldCornConsumeRecordException {
        UserInfo user = userInfoService.get(uid);
        if (user == null) {
            throw new UserInfoException(UserInfoException.CODE_NOT_EXIST, "用户不存在");
        }
 
        BigDecimal rate = null;
        try {
            rate = goldCornMoneyExchangeRateRecordService.getRate(day, user.getSystem(), new Date());
        } catch (ParamsException e) {
            throw new GoldCornMoneyExchangeRateRecordException(GoldCornMoneyExchangeRateRecordException.CODE_PARAMS_NOT_ENOUGH, e.getMsg());
        }
 
        if (rate == null) {
            throw new GoldCornMoneyExchangeRateRecordException(GoldCornMoneyExchangeRateRecordException.CODE_NOT_EXIST, "未获取到汇率");
        }
 
        settle(uid, day, rate, settleId);
    }
}