package com.xcong.excoin.rabbit.consumer; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONObject; import com.xcong.excoin.configurations.RabbitMqConfig; import com.xcong.excoin.modules.fish.dao.CannonOwnRecordDao; import com.xcong.excoin.modules.fish.dao.MemberAccountGoldDao; import com.xcong.excoin.modules.fish.entity.CannonOwnRecord; 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; import java.util.List; /** * @author wzy * @date 2021-11-26 **/ @Slf4j @Component @ConditionalOnProperty(prefix = "app", name = "fish-hit", havingValue = "true") public class FishHitConsumer { @Autowired private MemberAccountGoldDao memberAccountGoldDao; @Autowired private CannonOwnRecordDao cannonOwnRecordDao; @RabbitListener(queues = RabbitMqConfig.QUEUE_FISH_HIT) public void fishHit(String content) { MsgModel msg = JSONObject.parseObject(content, MsgModel.class); synchronized (this) { List forts = cannonOwnRecordDao.selectCannonOwnRecordsByMemberIdAndCannonCode(msg.getMemberId(), msg.getFortCode()); if (CollUtil.isEmpty(forts)) { log.info("未拥有该炮台:{}", content); return; } MemberAccountGold accountGold = memberAccountGoldDao.selectByMemberIdForLock(msg.getMemberId()); BigDecimal available = accountGold.getAvailableBalance(); BigDecimal gold; // 由于前端打渔实现逻辑为,点击炮台发射子弹时触发一次,打中鱼再触发一次,所以,扣减和增加分开算 if (msg.getObtain() == 0) { gold = msg.getConsume().negate(); } else { gold = BigDecimal.valueOf(msg.getObtain()); } if (msg.getConsume().compareTo(available) > 0) { log.info("余额不足:{}", content); return; } memberAccountGoldDao.updateTotalBalanceAndAvailableBalance(accountGold.getId(), gold, gold, BigDecimal.ZERO); } } }