金币系统后端
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.

147 lines
5.4 KiB

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