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.

255 lines
11 KiB

5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
3 months ago
5 months ago
5 months ago
  1. package com.example.demo.serviceImpl.coin;
  2. import com.example.demo.domain.entity.UserGoldRecord;
  3. import com.example.demo.mapper.coin.*;
  4. import com.example.demo.service.coin.MarketService;
  5. import com.example.demo.service.coin.RefundService;
  6. import com.github.pagehelper.PageHelper;
  7. import com.github.pagehelper.PageInfo;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Service;
  10. import org.springframework.transaction.annotation.Transactional;
  11. import com.example.demo.domain.vo.coin.*;
  12. import java.math.BigDecimal;
  13. import java.time.LocalDate;
  14. import java.util.ArrayList;
  15. import java.util.Date;
  16. import java.util.List;
  17. import java.util.Objects;
  18. import java.util.stream.Collectors;
  19. /**
  20. * @program: gold-java
  21. * @ClassName RefundServiceImpl
  22. * @description:
  23. * @author: Double
  24. * @create: 202506-27 11:50
  25. * @Version 1.0
  26. **/
  27. @Service
  28. public class RefundServiceImpl implements RefundService {
  29. @Autowired
  30. private RefundMapper refundMapper;
  31. @Autowired
  32. private ConsumeMapper consumeMapper;
  33. @Autowired
  34. private UserMapper userMapper;
  35. @Autowired
  36. private MarketMapper marketMapper;
  37. @Autowired
  38. private RechargeMapper rechargeMapper;
  39. //查找全部退款
  40. @Override
  41. public PageInfo<RefundUser> selectAll(Integer pageNum, Integer pageSize, RefundUser refundUser) throws Exception {
  42. List<String> markets = marketMapper.getMarketIds(refundUser.getMarkets());
  43. refundUser.setMarkets(markets);
  44. PageHelper.startPage(pageNum, pageSize);
  45. List<RefundUser> refundUsers = refundMapper.selectAll(refundUser);
  46. return new PageInfo<>(refundUsers);
  47. }
  48. //统计退款金额
  49. @Override
  50. public Gold statsGold(RefundUser refundUser) {
  51. List<String> markets = marketMapper.getMarketIds(refundUser.getMarkets());
  52. if (markets.contains("9") || markets.contains("9999")) {
  53. markets = null;
  54. }
  55. refundUser.setMarkets(markets);
  56. Gold gold = refundMapper.sumGold(refundUser);
  57. return gold;
  58. }
  59. //查找筛选退款
  60. @Override
  61. public PageInfo<RefundUser> selectBy(Integer pageNum, Integer pageSize, RefundUser refundUser) throws Exception {
  62. List<String> markets = marketMapper.getMarketIds(refundUser.getMarkets());
  63. if (markets.contains("9") || markets.contains("9999")) {
  64. markets = null;
  65. }
  66. refundUser.setMarkets(markets);
  67. PageHelper.startPage(pageNum, pageSize);
  68. List<RefundUser> refundUsers = refundMapper.selectBy(refundUser);
  69. return new PageInfo<>(refundUsers);
  70. }
  71. //添加退款记录
  72. @Transactional
  73. @Override
  74. public Result add(RefundUser refundUser) {
  75. if (refundUser.getTaskGold() == null || refundUser.getFreeGold() == null || refundUser.getPermanentGold() == null) {
  76. return Result.error("金币不能为空");
  77. }
  78. if (refundUser.getTaskGold().compareTo(BigDecimal.ZERO) < 0 ||
  79. refundUser.getFreeGold().compareTo(BigDecimal.ZERO) < 0 ||
  80. refundUser.getPermanentGold().compareTo(BigDecimal.ZERO) < 0) {
  81. return Result.error("金币不能为负数");
  82. }
  83. if (refundUser.getJwcode() < 10000000 || refundUser.getJwcode() > 99999999) {
  84. return Result.error("精网号位数小于8位或大于8位");
  85. }
  86. GoldUser goldUser = userMapper.selectUser(refundUser.getJwcode().toString());
  87. if (goldUser == null) {
  88. return Result.error("用户不存在");
  89. }
  90. UserGoldRecord userGoldRecord = new UserGoldRecord();
  91. if(refundUser.getOrderCode()==null|| refundUser.getOrderCode().isEmpty()){
  92. return Result.error("订单号不能为空");
  93. }
  94. String goodsName = "";
  95. if(refundUser.getGoodsName()==null|| refundUser.getGoodsName().isEmpty()){
  96. goodsName = "空";
  97. }else {
  98. goodsName = refundUser.getGoodsName();
  99. }
  100. String orderCode = refundUser.getOrderCode();
  101. userGoldRecord.setGoodsName(goodsName); // 商品A
  102. userGoldRecord.setOrderCode("TK_" + orderCode); // TKXF202506281545524400006580
  103. userGoldRecord.setJwcode(refundUser.getJwcode());
  104. userGoldRecord.setRefundType(refundUser.getRefundType());
  105. userGoldRecord.setRefundModel(refundUser.getRefundModel());
  106. // 获取当前月份(1-12)
  107. int currentMonth = LocalDate.now().getMonthValue();
  108. // 根据当前月份设置对应字段
  109. if(Objects.equals(refundUser.getRefundType(), "商品退款")){
  110. if (currentMonth >= 1 && currentMonth <= 6) {
  111. // 1-6月:设置6月额度,12月保持默认值
  112. userGoldRecord.setFreeJune(0);
  113. userGoldRecord.setFreeDecember(refundUser.getFreeGold().intValue());
  114. } else {
  115. // 7-12月:设置12月额度,6月保持默认值
  116. userGoldRecord.setFreeJune(refundUser.getFreeGold().intValue());
  117. userGoldRecord.setFreeDecember(0);
  118. }
  119. //信息输入
  120. userGoldRecord.setPermanentGold(refundUser.getPermanentGold().intValue());
  121. userGoldRecord.setTaskGold(refundUser.getTaskGold().intValue());
  122. userGoldRecord.setSumGold(refundUser.getSumGold().intValue());
  123. }else{
  124. GoldUser gold = userMapper.selectGold(refundUser.getJwcode().toString());
  125. if (refundUser.getFreeGold().compareTo(gold.getNowFreeDecember().add(gold.getNowFreeJune())) > 0
  126. || refundUser.getPermanentGold().compareTo(gold.getNowPermanentGold()) > 0
  127. || refundUser.getTaskGold().compareTo(gold.getNowTaskGold()) > 0) {
  128. return Result.error("金币数量不足");
  129. }
  130. // 根据当前月份设置对应字段
  131. if (currentMonth >= 1 && currentMonth <= 6) {
  132. // 1-6月:设置6月额度,12月保持默认值
  133. if (refundUser.getFreeGold().compareTo(gold.getNowFreeJune()) > 0) {
  134. userGoldRecord.setFreeJune(-gold.getNowFreeJune().intValue());
  135. userGoldRecord.setFreeDecember(-(refundUser.getFreeGold().subtract(gold.getNowFreeJune())).intValue());
  136. } else {
  137. userGoldRecord.setFreeJune(-refundUser.getFreeGold().intValue());
  138. userGoldRecord.setFreeDecember(0);
  139. }
  140. } else {
  141. // 7-12月:设置12月额度,6月保持默认值
  142. if (refundUser.getFreeGold().compareTo(gold.getNowFreeDecember()) > 0) {
  143. userGoldRecord.setFreeDecember(-gold.getNowFreeDecember().intValue());
  144. userGoldRecord.setFreeJune(-(refundUser.getFreeGold().subtract(gold.getNowFreeDecember())).intValue());
  145. } else {
  146. userGoldRecord.setFreeDecember(-refundUser.getFreeGold().intValue());
  147. userGoldRecord.setFreeJune(0);
  148. }
  149. }
  150. //信息输入
  151. userGoldRecord.setPermanentGold(-refundUser.getPermanentGold().intValue());
  152. userGoldRecord.setTaskGold(-refundUser.getTaskGold().intValue());
  153. userGoldRecord.setSumGold(-refundUser.getSumGold().intValue());
  154. }
  155. userGoldRecord.setPayPlatform("金币系统");
  156. userGoldRecord.setRemark(refundUser.getRemark());
  157. userGoldRecord.setAdminId(refundUser.getAdminId());
  158. userGoldRecord.setType((byte) 2);
  159. userGoldRecord.setAuditStatus(0);
  160. userGoldRecord.setCreateTime(new Date());
  161. userGoldRecord.setUpdateTime(new Date());
  162. List<ConsumeUser> list1 = consumeMapper.selectOrderCodeByJwcode(refundUser);
  163. List<RechargeUser> list2 = rechargeMapper.selectOrderCodeByJwcode(refundUser);
  164. if(Objects.equals(refundUser.getRefundType(), "商品退款")){
  165. //消费订单号校验
  166. if (list1 == null || list1.isEmpty()) {
  167. return Result.error("该用户没有该订单号");
  168. } else if (list1.size() == 1) {
  169. ConsumeUser consumeUser = list1.getFirst();
  170. if (consumeUser.getIsRefund().equals(1)) {
  171. return Result.error("该订单已被退款或待审核");
  172. }
  173. } else {
  174. return Result.error("订单重复");
  175. }
  176. }else{
  177. //充值订单号校验
  178. if (list2 == null || list2.isEmpty()) {
  179. return Result.error("该用户没有该订单号");
  180. } else if (list2.size() == 1) {
  181. RechargeUser rechargeUser = list2.getFirst();
  182. if (rechargeUser.getIsRefund().equals(1)) {
  183. return Result.error("该订单已被退款或待审核");
  184. }
  185. } else {
  186. return Result.error("订单重复");
  187. }
  188. }
  189. refundMapper.add(userGoldRecord);
  190. consumeMapper.updateIsRefund(orderCode);
  191. return Result.success();
  192. }
  193. //获取退款类型
  194. @Override
  195. public List<String> getRefundType() {
  196. List<String> list = refundMapper.getRefundType();
  197. List<String> filteredList = list.stream()
  198. .filter(Objects::nonNull) // 过滤掉 null 值
  199. .filter(str -> !str.trim().isEmpty()) // 过滤掉空字符串和仅包含空白字符的字符串
  200. .collect(Collectors.toList());
  201. return filteredList;
  202. }
  203. //查找退款商品
  204. @Override
  205. public List<RefundUser> selectGoods(RefundUser refundUser) {
  206. List<RefundUser> refundUsers = refundMapper.selectGoods(refundUser);
  207. List<RefundUser> list = new ArrayList<>();
  208. for (RefundUser record : refundUsers) {
  209. if (record == null) {
  210. continue;
  211. }
  212. String orderCode = record.getOrderCode() != null ? record.getOrderCode() : "无订单号";
  213. String goodsName = record.getGoodsName() != null ? record.getGoodsName() : "无商品名";
  214. BigDecimal permanentGold = record.getPermanentGold() != null ? record.getPermanentGold() : BigDecimal.ZERO;
  215. BigDecimal freeGold = record.getFreeGold() != null ? record.getFreeGold() : BigDecimal.ZERO;
  216. BigDecimal taskGold = record.getTaskGold() != null ? record.getTaskGold() : BigDecimal.ZERO;
  217. // 计算总和
  218. // 拼接格式:订单号_商品名(例如:XF20250629_商品BC)
  219. String combined = orderCode + "_" + goodsName;
  220. RefundUser newRefundUser = new RefundUser();
  221. newRefundUser.setOrderCode(orderCode);
  222. newRefundUser.setGoodsName(goodsName);
  223. newRefundUser.setSumGold(permanentGold.add(freeGold).add(taskGold));
  224. newRefundUser.setPermanentGold(record.getPermanentGold());
  225. newRefundUser.setFreeGold(record.getFreeGold());
  226. newRefundUser.setTaskGold(record.getTaskGold());
  227. newRefundUser.setIsRefund(record.getIsRefund());
  228. newRefundUser.setType(record.getType());
  229. list.add(newRefundUser);
  230. }
  231. return list;
  232. }
  233. }