|
|
@@ -3,24 +3,38 @@ package com.td.boss.game.comcnttosnb.service;
|
|
|
import cn.hutool.core.convert.Convert;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.util.NumberUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import com.td.boss.common.pojo.Result;
|
|
|
import com.td.boss.common.service.CommonServiceImpl;
|
|
|
+import com.td.boss.config.enums.ResultEnum;
|
|
|
import com.td.boss.game.comcnttosnb.pojo.ComCntToSnb;
|
|
|
import com.td.boss.game.comcnttosnb.repository.ComCntToSnbRespository;
|
|
|
import com.td.boss.game.comcnttosnb.vo.ComCntToSnbVo;
|
|
|
+import com.td.boss.game.comsnbtran.service.ComSnbTranService;
|
|
|
+import com.td.boss.game.comsnbtran.vo.ComSnbTranVo;
|
|
|
import com.td.boss.game.comusers.service.ComUsersService;
|
|
|
+import com.td.boss.game.comusers.vo.ComUsersVo;
|
|
|
import com.td.boss.util.CopyUtil;
|
|
|
+import com.td.boss.util.RedisData;
|
|
|
+import com.td.boss.util.RedisLock;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
import java.util.Date;
|
|
|
|
|
|
@Service
|
|
|
public class ComCntToSnbServiceImpl extends CommonServiceImpl<ComCntToSnbVo, ComCntToSnb, Integer> implements ComCntToSnbService {
|
|
|
+
|
|
|
@Autowired
|
|
|
private ComCntToSnbRespository comCntToSnbRespository;
|
|
|
@Autowired
|
|
|
private ComUsersService comUsersService;
|
|
|
+ @Autowired
|
|
|
+ private ComSnbTranService comSnbTranService;
|
|
|
+ @Autowired
|
|
|
+ private RedisLock redisLock;
|
|
|
|
|
|
/**
|
|
|
* 通过userid查看snb领取情况
|
|
|
@@ -38,9 +52,12 @@ public class ComCntToSnbServiceImpl extends CommonServiceImpl<ComCntToSnbVo, Com
|
|
|
* 通过userid领取snb
|
|
|
*
|
|
|
* @param userId 用户id
|
|
|
+ * @return {@link Result}<{@link String}>
|
|
|
*/
|
|
|
@Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
public Result<String> receiveByUserId(String userId) {
|
|
|
+
|
|
|
ComCntToSnb comCntToSnb = comCntToSnbRespository.findByUserId(userId).orElse(null);
|
|
|
if (comCntToSnb == null || comCntToSnb.getStatus().equals(0)) {
|
|
|
return Result.of(null, false, "非法领取");
|
|
|
@@ -53,18 +70,60 @@ public class ComCntToSnbServiceImpl extends CommonServiceImpl<ComCntToSnbVo, Com
|
|
|
if (day - comCntToSnb.getReceivedDay() <= 0) {
|
|
|
return Result.of(null, false, "领取失败,今日已领取");
|
|
|
}
|
|
|
- DateUtil.offsetDay(comCntToSnb.getBeginTime(), comCntToSnb.getReceivedDay());
|
|
|
- // 本次领取量
|
|
|
- comCntToSnb.setReceivedQuantity(
|
|
|
- NumberUtil.add(
|
|
|
- comCntToSnb.getReceivedQuantity(),
|
|
|
- NumberUtil.mul(comCntToSnb.getUnitQuantity(), day)
|
|
|
- ));
|
|
|
- // 本次领取天数
|
|
|
- comCntToSnb.setReceivedDay(comCntToSnb.getReceivedDay() + day);
|
|
|
- comCntToSnbRespository.save(comCntToSnb);
|
|
|
- //TODO 更新comUsers
|
|
|
- //TODO 插入comSnbTran
|
|
|
- return Result.of(null, false, "领取成功");
|
|
|
+
|
|
|
+ long time = System.currentTimeMillis() + RedisData.getSnbTimeout();
|
|
|
+ String redisKey = StrUtil.format("SNB_SAVE_{}", userId);
|
|
|
+
|
|
|
+ try {
|
|
|
+ // redis锁
|
|
|
+ if (!redisLock.lock(redisKey, String.valueOf(time))) {
|
|
|
+ return Result.of(null, false, ResultEnum.SEED_SALE_SAVE_LOCK.getMessage(), ResultEnum.SEED_SALE_SAVE_LOCK.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 本次领取量
|
|
|
+ BigDecimal thisReceiveQuantity = NumberUtil.mul(comCntToSnb.getUnitQuantity(), day);
|
|
|
+ comCntToSnb.setReceivedQuantity(comCntToSnb.getReceivedQuantity().add(thisReceiveQuantity));
|
|
|
+ // 本次领取天数
|
|
|
+ comCntToSnb.setReceivedDay(comCntToSnb.getReceivedDay() + day);
|
|
|
+ // 更新领取信息
|
|
|
+ comCntToSnbRespository.save(comCntToSnb);
|
|
|
+
|
|
|
+ // 更新comUsers snb+
|
|
|
+ ComUsersVo comUsersVo = comUsersService.findByUserId(userId);
|
|
|
+ comUsersVo.setSnbPart(comUsersVo.getSnbPart() + Convert.toDouble(thisReceiveQuantity));
|
|
|
+ comUsersService.save(comUsersVo);
|
|
|
+
|
|
|
+ // 插入comSnbTran
|
|
|
+ ComSnbTranVo snbTran = new ComSnbTranVo();
|
|
|
+ //记录兑换id
|
|
|
+ snbTran.setTranId(Convert.toStr(comCntToSnb.getId()));
|
|
|
+ snbTran.setUserId(userId);
|
|
|
+ snbTran.setTranName("领取SNB");
|
|
|
+// snbTran.setTranType();
|
|
|
+ snbTran.setTranAmount(Convert.toInt(thisReceiveQuantity));
|
|
|
+// snbTran.setTranPrice();
|
|
|
+ snbTran.setTranDescribe(String.format("user领取SNB、本次领取:{},剩余:", thisReceiveQuantity, comCntToSnb.getTotalQuantity().multiply(comCntToSnb.getReceivedQuantity())));
|
|
|
+ snbTran.setIsAdd(1);
|
|
|
+ snbTran.setBeforeSnb(0);
|
|
|
+ snbTran.setTranSnb(0);
|
|
|
+ snbTran.setAfterSnb(0);
|
|
|
+ // 交易的部分数据
|
|
|
+// snbTran.setTranAmountPart(0d);
|
|
|
+ // 交易的snb
|
|
|
+ snbTran.setTranSnbPart(Convert.toDouble(thisReceiveQuantity));
|
|
|
+ // 交易之前的snb
|
|
|
+ snbTran.setBeforeSnbPart(Convert.toDouble(comCntToSnb.getReceivedQuantity().subtract(thisReceiveQuantity)));
|
|
|
+ // 交易完成后的snb
|
|
|
+ snbTran.setAfterSnbPart(Convert.toDouble(comCntToSnb.getReceivedQuantity()));
|
|
|
+ comSnbTranService.save(snbTran);
|
|
|
+
|
|
|
+ // redis解锁
|
|
|
+ redisLock.unlock(redisKey, String.valueOf(time));
|
|
|
+ return Result.of(null, false, "领取成功");
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ redisLock.unlock(redisKey, String.valueOf(time));
|
|
|
+ throw new RuntimeException(e.getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
}
|