金币系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
6.8 KiB

4 months ago
  1. package com.example.demo.serviceImpl;
  2. import com.example.demo.Util.GoldTistV2;
  3. import com.example.demo.domain.entity.*;
  4. import com.example.demo.domain.vo.ConsumeDetail;
  5. import com.example.demo.domain.vo.SumConsume;
  6. import com.example.demo.mapper.ConsumeMapper;
  7. import com.example.demo.mapper.UserMapper;
  8. import com.example.demo.sevice.ConsumeService;
  9. import com.github.pagehelper.PageHelper;
  10. import com.github.pagehelper.PageInfo;
  11. import lombok.RequiredArgsConstructor;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.cache.annotation.CacheConfig;
  14. import org.springframework.cache.annotation.CacheEvict;
  15. import org.springframework.cache.annotation.Cacheable;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.transaction.annotation.Transactional;
  18. import org.springframework.util.ObjectUtils;
  19. import java.math.BigDecimal;
  20. import java.time.LocalDate;
  21. import java.time.Month;
  22. import java.util.List;
  23. import java.util.UUID;
  24. @Service
  25. @Transactional
  26. @RequiredArgsConstructor
  27. @CacheConfig(cacheNames = "consume")
  28. public class ConsumeServiceImpl implements ConsumeService {
  29. @Autowired
  30. ConsumeMapper consumeMapper;
  31. private final UserMapper userMapper;
  32. private final UserServiceImpl userService;
  33. @CacheEvict(value = {"consume", "detailY"}, allEntries = true)
  34. @Override
  35. //新增消费记录
  36. public int insert(DetailY detailY) throws Exception {
  37. User sUser = userMapper.select(detailY.getJwcode());
  38. if(ObjectUtils.isEmpty(sUser)){
  39. throw new Exception("无此精网号");
  40. } else if (detailY.getJwcode()==null||detailY.getJwcode().equals("")) {
  41. throw new Exception("精网号不能为空");
  42. } else if (detailY.getAllGold()==null||detailY.getAllGold().compareTo(BigDecimal.ZERO) < 0) {
  43. throw new Exception("消费金币总数不能为空或小于等于0");
  44. }
  45. // 生成UUID作为订单编号
  46. String uuid = UUID.randomUUID().toString().replace("-", ""); // 去掉UUID中的'-'
  47. detailY.setOrderCode(uuid);
  48. int result =consumeMapper.insert(detailY);
  49. System.out.println(detailY+"----------------------------------------");
  50. if (result != 1) {
  51. throw new Exception("Failed to insert another entity");
  52. }
  53. BigDecimal paidGold1 =detailY.getRechargeCoin();
  54. BigDecimal freeGold1 =detailY.getFreeCoin();
  55. BigDecimal taskGold1 =detailY.getTaskCoin();
  56. String name = detailY.getName();
  57. String username =detailY.getUsername();
  58. String area = detailY.getArea();
  59. String jwcode = detailY.getJwcode();
  60. String product = detailY.getProductName();
  61. UserGold userGold = userMapper.selectGold(jwcode);
  62. BigDecimal buyJb =userGold.getBuyJb();
  63. BigDecimal coreJb=userGold.getCoreJb();
  64. buyJb = buyJb.add(paidGold1);
  65. coreJb = coreJb.add(taskGold1);
  66. // 设置更新后的Sumgold回到user对象
  67. userGold.setBuyJb(buyJb);
  68. userGold.setCoreJb(coreJb);
  69. LocalDate now = LocalDate.now();
  70. // 判断当前日期是在六月之前还是之后
  71. Month currentMonth = now.getMonth();
  72. boolean isBeforeJune = currentMonth.getValue() < Month.JUNE.getValue();
  73. boolean isJune = currentMonth.getValue() == Month.JUNE.getValue();
  74. boolean isAfterJune = currentMonth.getValue() > Month.JUNE.getValue();
  75. // 根据月份更新 free6 或 free12
  76. if (isBeforeJune || isJune) {
  77. // 如果是六月前,优先扣减 free6
  78. BigDecimal free6 = userGold.getFree6();
  79. if (free6.add(freeGold1).compareTo(BigDecimal.ZERO) >= 0) {
  80. // 如果 free6 足够扣减,直接扣减
  81. userGold.setFree6(free6.add(freeGold1));
  82. } else {
  83. // 如果 free6 不足,扣减 free6 到 0,剩余部分从 free12 扣减
  84. BigDecimal remaining = free6.add(freeGold1); // 剩余需要扣减的金币
  85. userGold.setFree6(BigDecimal.ZERO); // 将 free6 归零
  86. userGold.setFree12(userGold.getFree12().add(remaining)); // 从 free12 扣减剩余部分
  87. }
  88. } else if (isAfterJune) {
  89. // 如果是六月后,优先扣减 free12
  90. BigDecimal free12 = userGold.getFree12();
  91. if (free12.add(freeGold1).compareTo(BigDecimal.ZERO) >= 0) {
  92. // 如果 free12 足够扣减,直接扣减
  93. userGold.setFree12(free12.add(freeGold1));
  94. } else {
  95. // 如果 free12 不足,扣减 free12 到 0,剩余部分从 free6 扣减
  96. BigDecimal remaining = free12.add(freeGold1); // 剩余需要扣减的金币
  97. userGold.setFree12(BigDecimal.ZERO); // 将 free12 归零
  98. userGold.setFree6(userGold.getFree6().add(remaining)); // 从 free6 扣减剩余部分
  99. }
  100. }
  101. // 设置更新后的Sumgold回到user对象
  102. System.out.println(userGold+"----------------------------------------------------------");
  103. result = userMapper.updateGold(userGold);
  104. if (result != 1) {
  105. throw new Exception("Failed to insert another entity");
  106. }
  107. //添加表单数据
  108. // 更新用户对象以反映新的余额
  109. result = userMapper.updateGold(userGold);
  110. GoldTistV2.addCoinNew(jwcode, 65, (paidGold1.add(freeGold1).add(taskGold1).divide(new BigDecimal("100"))).doubleValue(), "购买商品", paidGold1.doubleValue(),name,product);
  111. return result;
  112. }
  113. @Cacheable(key="#root.method.name")
  114. @Override
  115. public User getByUserId(Integer userId) {
  116. return consumeMapper.getByUserId(userId);
  117. }
  118. @Cacheable(key="#root.method.name")
  119. @Override
  120. public Admin getByadminId(Integer adminId) {
  121. return consumeMapper.getByadminId(adminId);
  122. }
  123. @Cacheable(key="#root.method.name")
  124. @Override
  125. public List<ConsumeDetail> search(ConsumeDetail consumeDetail) {
  126. return consumeMapper.select(consumeDetail);
  127. }
  128. @Cacheable(key = "#root.method.name + ':' + #pageNum + '-' + #pageSize + '-' + T(java.util.Objects).hashCode(#consumeDetail)")
  129. @Override
  130. public PageInfo<ConsumeDetail> searchForPage(Integer pageNum, Integer pageSize, ConsumeDetail consumeDetail) {
  131. PageHelper.startPage(pageNum, pageSize);
  132. List<ConsumeDetail> list = consumeMapper.select(consumeDetail);
  133. return new PageInfo<>(list);
  134. }
  135. @Override
  136. public SumConsume getSumConsume(SumConsume sumConsume) {
  137. return consumeMapper.getSumConsume(sumConsume);
  138. }
  139. @Override
  140. public List<String> getConsume() {
  141. return consumeMapper.getConsume();
  142. }
  143. public List<Detail> getDeatil(String jwcode){
  144. return consumeMapper.getDeatil(jwcode);
  145. }
  146. public List<Product> getProduct(String name){
  147. return consumeMapper.getProduct(name);
  148. }
  149. }