Helius
2021-11-26 4017fe347792c7e28695c455a40874f0c647cc9b
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
package com.xcong.excoin.rabbit.consumer;
 
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.configurations.RabbitMqConfig;
import com.xcong.excoin.modules.fish.dao.MemberAccountGoldDao;
import com.xcong.excoin.modules.fish.entity.MemberAccountGold;
import com.xcong.excoin.websocket.fish.model.MsgModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
 
import java.math.BigDecimal;
 
/**
 * @author wzy
 * @date 2021-11-26
 **/
@Slf4j
@Component
@ConditionalOnProperty(prefix = "app", name = "fish-hit", havingValue = "true")
public class FishHitConsumer {
 
    @Autowired
    private MemberAccountGoldDao memberAccountGoldDao;
 
    @RabbitListener(queues = RabbitMqConfig.QUEUE_FISH_HIT)
    public void fishHit(String content) {
        log.info("收到打渔消息:{}", content);
        MsgModel msg = JSONObject.parseObject(content, MsgModel.class);
 
        synchronized (this) {
            MemberAccountGold accountGold = memberAccountGoldDao.selectByMemberIdForLock(msg.getMemberId());
            BigDecimal available = accountGold.getAvailableBalance();
 
            BigDecimal gold = BigDecimal.valueOf(msg.getObtain()).subtract(msg.getConsume());
 
            if (gold.compareTo(BigDecimal.ZERO) < 0 && gold.abs().compareTo(available) > 0) {
                log.info("余额不足");
                return;
            }
            memberAccountGoldDao.updateTotalBalanceAndAvailableBalance(accountGold.getId(), gold, gold, BigDecimal.ZERO);
        }
    }
}