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

140 lines
5.0 KiB

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