UserBenefitsServiceImpl.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package com.cyksj.service.user.impl;
  2. import com.cyksj.common.EnvCommonService;
  3. import com.cyksj.common.constant.Constant;
  4. import com.cyksj.mapper.DistributeOrdersDeductPointsRecordMapper;
  5. import com.cyksj.mapper.UserBalanceSourceRecordMapper;
  6. import com.cyksj.mapper.UserMapper;
  7. import com.cyksj.mapper.shop.YhShopDistributeOrdersDeductPointsRecordMapper;
  8. import com.cyksj.model.entity.DistributeOrdersDeductPointsRecord;
  9. import com.cyksj.model.entity.User;
  10. import com.cyksj.model.entity.UserBalanceSourceRecord;
  11. import com.cyksj.model.entity.YhShopDistributeOrdersDeductPointsRecord;
  12. import com.cyksj.service.sms.SmsService;
  13. import com.cyksj.service.user.UserBenefitsService;
  14. import lombok.RequiredArgsConstructor;
  15. import org.springframework.dao.DuplicateKeyException;
  16. import org.springframework.stereotype.Service;
  17. import java.math.BigDecimal;
  18. /*
  19. *项目名: netflix
  20. *文件名: UserBenefitsServiceImpl
  21. *创建者: JavaZou
  22. *创建时间:2023/5/18 16:06
  23. */
  24. @Service
  25. @RequiredArgsConstructor
  26. public class UserBenefitsServiceImpl implements UserBenefitsService {
  27. private final UserMapper userMapper;
  28. private final DistributeOrdersDeductPointsRecordMapper distributeOrdersDeductPointsRecordMapper;
  29. private final UserBalanceSourceRecordMapper userBalanceSourceRecordMapper;
  30. private final EnvCommonService envCommonService;
  31. private final SmsService smsService;
  32. private final YhShopDistributeOrdersDeductPointsRecordMapper yhShopDistributeOrdersDeductPointsRecordMapper;
  33. @Override
  34. public void deductDistributePoints(Long orderId, Long userId, BigDecimal deductPoints) {
  35. if (deductPoints.compareTo(BigDecimal.ZERO) <= 0) {
  36. return;
  37. }
  38. while (true) {
  39. User user = userMapper.selectById(userId);
  40. if (user == null) break;
  41. BigDecimal points = user.getPoints();
  42. Integer success = userMapper.subPointsNegative(userId, points, deductPoints);
  43. if (success == 1){
  44. //记录积分已发送后,退款后扣除积分记录
  45. deductPointsRecord(orderId, userId, deductPoints);
  46. break;
  47. }
  48. }
  49. }
  50. @Override
  51. public void addDistributePoints(Long userId, BigDecimal addPoints) {
  52. if (addPoints.compareTo(BigDecimal.ZERO) <= 0) {
  53. return;
  54. }
  55. while (true) {
  56. User user = userMapper.selectById(userId);
  57. if (user == null) break;
  58. BigDecimal points = user.getPoints();
  59. Integer success = userMapper.updatePoints(user.getId(), points, addPoints);
  60. if (success == 1) break;
  61. }
  62. }
  63. @Override
  64. public void addUserBalance(Long userId, BigDecimal addBalance, UserBalanceSourceRecord.Source source, Long yhsId, Long orderId, String detail, Boolean isRecord) {
  65. if (addBalance.compareTo(BigDecimal.ZERO) <= 0) {
  66. return;
  67. }
  68. while (true) {
  69. User user = userMapper.selectById(userId);
  70. if (user == null) break;
  71. BigDecimal balance = user.getBalance();
  72. Integer success = userMapper.addUserBalance(user.getId(), balance, addBalance);
  73. if (success == 1) {
  74. if (isRecord) {
  75. recordBalanceBySource(userId, addBalance, source, orderId, detail);
  76. }
  77. if (envCommonService.isPrdEnv() && isRecord && yhsId == 0) {
  78. try {
  79. //短信到账提醒
  80. String msg = String.format(Constant.BALANCE_NOTIFY_MSG, addBalance.divide(BigDecimal.valueOf(100)));
  81. smsService.sendSmsToRelationUser(userId, msg);
  82. } catch (Exception e) {
  83. }
  84. }
  85. break;
  86. }
  87. }
  88. }
  89. @Override
  90. public void deductUserBalance(Long userId, BigDecimal subBalance) {
  91. if (subBalance.compareTo(BigDecimal.ZERO) <= 0) {
  92. return;
  93. }
  94. while (true) {
  95. User user = userMapper.selectById(userId);
  96. if (user == null) break;
  97. BigDecimal balance = user.getBalance();
  98. Integer success = userMapper.deductUserBalance(user.getId(), balance, subBalance);
  99. if (success == 1) break;
  100. }
  101. }
  102. public void deductPointsRecord(Long orderId, Long userId, BigDecimal deductPoints) {
  103. try {
  104. DistributeOrdersDeductPointsRecord record = new DistributeOrdersDeductPointsRecord();
  105. record.setOrderId(orderId);
  106. record.setUserId(userId);
  107. record.setDeductPoints(deductPoints);
  108. distributeOrdersDeductPointsRecordMapper.insert(record);
  109. } catch (DuplicateKeyException e) {
  110. }
  111. }
  112. @Override
  113. public void recordBalanceBySource(Long userId, BigDecimal balance, UserBalanceSourceRecord.Source source, Long orderId, String detail) {
  114. UserBalanceSourceRecord userBalanceSourceRecord = new UserBalanceSourceRecord();
  115. userBalanceSourceRecord.setUserId(userId);
  116. userBalanceSourceRecord.setBalance(balance);
  117. userBalanceSourceRecord.setSource(source);
  118. userBalanceSourceRecord.setOrderId(orderId);
  119. userBalanceSourceRecord.setDetail(detail);
  120. userBalanceSourceRecordMapper.insert(userBalanceSourceRecord);
  121. }
  122. @Override
  123. public void addYhsMoney(Long userId, BigDecimal addYhsMoney) {
  124. if (addYhsMoney.compareTo(BigDecimal.ZERO) <= 0) {
  125. return;
  126. }
  127. while (true) {
  128. User user = userMapper.selectById(userId);
  129. if (user == null) break;
  130. BigDecimal yhsMoney = user.getYhsMoney();
  131. Integer success = userMapper.addYhsMoney(user.getId(), yhsMoney, addYhsMoney);
  132. if (success == 1) {
  133. break;
  134. }
  135. }
  136. }
  137. @Override
  138. public void deductYhShopTakeMoney(Long orderId, Long shopUserId, BigDecimal deductMoney) {
  139. if (deductMoney.compareTo(BigDecimal.ZERO) <= 0) {
  140. return;
  141. }
  142. while (true) {
  143. User user = userMapper.selectById(shopUserId);
  144. if (user == null) break;
  145. BigDecimal yhsMoney = user.getYhsMoney();
  146. Integer success = userMapper.subYhShopUserMoney(shopUserId, yhsMoney, deductMoney);
  147. if (success == 1){
  148. //记录提成已发送后,退款后扣除用户店铺提成 记录
  149. deductYhsMoneyRecord(orderId, shopUserId, deductMoney);
  150. break;
  151. }
  152. }
  153. }
  154. @Override
  155. public void addDistributeSubMoney(Long userId, BigDecimal addApplyMoney) {
  156. if (addApplyMoney.compareTo(BigDecimal.ZERO) <= 0) {
  157. return;
  158. }
  159. while (true) {
  160. User user = userMapper.selectById(userId);
  161. if (user == null) break;
  162. BigDecimal subApplyMoney = user.getSubApplyMoney();
  163. Integer success = userMapper.addDistributeSubMoney(user.getId(), subApplyMoney, addApplyMoney);
  164. if (success == 1) {
  165. break;
  166. }
  167. }
  168. }
  169. private void deductYhsMoneyRecord(Long orderId, Long shopUserId, BigDecimal deductMoney) {
  170. try {
  171. YhShopDistributeOrdersDeductPointsRecord record = new YhShopDistributeOrdersDeductPointsRecord();
  172. record.setOrderId(orderId);
  173. record.setShopUserId(shopUserId);
  174. record.setDeductMoney(deductMoney);
  175. yhShopDistributeOrdersDeductPointsRecordMapper.insert(record);
  176. } catch (DuplicateKeyException e) {
  177. }
  178. }
  179. }