PartnerExtractHistoryController.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package com.ydtech.modules.partner.controller;
  2. import java.math.BigDecimal;
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  7. import com.ydtech.core.page.HttpResult;
  8. import com.ydtech.modules.admin.dao.SysPartnerAccountMapper;
  9. import com.ydtech.modules.admin.model.SysPartnerAccount;
  10. import com.ydtech.modules.base.controller.BaseController;
  11. import com.ydtech.modules.base.model.dto.PageDto;
  12. import com.ydtech.modules.partner.dao.PartnerPartnerAccountMapper;
  13. import com.ydtech.modules.partner.model.PartnerExtractHistory;
  14. import com.ydtech.modules.partner.model.PartnerPartnerAccount;
  15. import com.ydtech.modules.partner.model.param.PartnerRuleEntity;
  16. import com.ydtech.modules.partner.service.PartnerExtractHistoryService;
  17. import com.ydtech.modules.partner.service.PartnerLiftingRecordService;
  18. import com.ydtech.modules.partner.service.PartnerRuleService;
  19. import io.swagger.annotations.Api;
  20. import lombok.extern.slf4j.Slf4j;
  21. import org.apache.commons.lang3.StringUtils;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.transaction.annotation.Transactional;
  24. import org.springframework.web.bind.annotation.*;
  25. @Api(tags = "提现记录")
  26. @Slf4j
  27. @RestController
  28. @RequestMapping("/partnerExtractHistory")
  29. public class PartnerExtractHistoryController extends BaseController {
  30. @Autowired
  31. private PartnerExtractHistoryService partnerExtractHistoryService;
  32. @Autowired
  33. private SysPartnerAccountMapper sysPartnerAccountMapper;
  34. @Autowired
  35. private PartnerPartnerAccountMapper partnerPartnerAccountMapper;
  36. @Autowired
  37. private PartnerRuleService ruleService;
  38. @Autowired
  39. private PartnerLiftingRecordService liftingRecordService;
  40. @PostMapping("/query")
  41. public HttpResult query(@RequestBody PartnerExtractHistory extractHistory){
  42. // if (StringUtils.isEmpty(extractHistory.getGrade())){
  43. // return HttpResult.error("用户等级不能为空");
  44. // }
  45. if (extractHistory.getPageNum() == null || extractHistory.getPageSize() == null){
  46. return HttpResult.error("分页参数不能为空");
  47. }
  48. PageDto pageDto = new PageDto();
  49. pageDto.setPageNum(extractHistory.getPageNum());
  50. pageDto.setPageSize(extractHistory.getPageSize());
  51. extractHistory.setPage(buildPageRequest(pageDto));
  52. extractHistory.setUserId(getUserId());
  53. return HttpResult.ok(partnerExtractHistoryService.queryPage(extractHistory));
  54. }
  55. /**
  56. * 提现
  57. * @param extractHistory
  58. * @return
  59. */
  60. @PostMapping("/extractRecords")
  61. @Transactional(rollbackFor = Exception.class)
  62. public HttpResult extractRecords(@RequestBody PartnerExtractHistory extractHistory){
  63. if (StringUtils.isEmpty(extractHistory.getGrade())){
  64. return HttpResult.error("用户等级不能为空");
  65. }
  66. if (null != extractHistory.getAmount() &&extractHistory.getAmount().compareTo(new BigDecimal("0")) <= 0){
  67. return HttpResult.error("提现金额不能小于0");
  68. }
  69. String userId = getUserId();
  70. PartnerPartnerAccount partnerAccount = partnerPartnerAccountMapper.selectOne(new LambdaQueryWrapper<PartnerPartnerAccount>()
  71. .eq(PartnerPartnerAccount::getUserId, userId)
  72. .eq(PartnerPartnerAccount::getGrade, extractHistory.getGrade()));
  73. if (null == partnerAccount){
  74. return HttpResult.error("账号余额不足");
  75. }
  76. if (partnerAccount.getCashFee().compareTo(extractHistory.getAmount()) == -1){
  77. return HttpResult.error("账号余额不足");
  78. }
  79. extractHistory.setUserId(userId);
  80. boolean save = partnerExtractHistoryService.save(extractHistory);
  81. if (save){
  82. SysPartnerAccount account = sysPartnerAccountMapper.selectOne(new LambdaQueryWrapper<SysPartnerAccount>()
  83. .eq(SysPartnerAccount::getUserId,userId));
  84. if (account != null){
  85. account.setExtractFee(account.getExtractFee().add(extractHistory.getAmount()));
  86. account.setCashFee(account.getCashFee().add(extractHistory.getAmount()));
  87. sysPartnerAccountMapper.updateById(account);
  88. }else {
  89. account = new SysPartnerAccount();
  90. account.setUserId(userId);
  91. account.setExtractFee(extractHistory.getAmount());
  92. account.setHistoryFee(new BigDecimal("0"));
  93. account.setCashFee(extractHistory.getAmount());
  94. account.setCreateTime(new Date());
  95. sysPartnerAccountMapper.insert( account);
  96. }
  97. partnerAccount.setHistoryFee(partnerAccount.getHistoryFee().add(extractHistory.getAmount()));
  98. partnerAccount.setCashFee(partnerAccount.getCashFee().subtract(extractHistory.getAmount()));
  99. partnerPartnerAccountMapper.updateById(partnerAccount);
  100. }
  101. return HttpResult.ok(save ? "提取成功" : "提取失败");
  102. }
  103. /**
  104. * 未转入的金额
  105. * @return
  106. */
  107. @GetMapping("/queryExtractRecords")
  108. public HttpResult queryExtractRecords(){
  109. List<PartnerPartnerAccount> partnerPartnerAccounts = partnerPartnerAccountMapper.selectList(new LambdaQueryWrapper<PartnerPartnerAccount>()
  110. .eq(PartnerPartnerAccount::getUserId,getUserId()));
  111. partnerPartnerAccounts.stream().forEach(partnerPartnerAccount -> {
  112. partnerPartnerAccount.setCashFee(partnerPartnerAccount.getCashFee().subtract(partnerPartnerAccount.getExpiredAmount()));
  113. });
  114. List<Map.Entry<Integer, BigDecimal>> collect = partnerPartnerAccounts.stream()
  115. .collect(Collectors.groupingBy(
  116. PartnerPartnerAccount::getGrade,
  117. Collectors.reducing(
  118. BigDecimal.ZERO,
  119. PartnerPartnerAccount::getCashFee,
  120. BigDecimal::add
  121. )
  122. ))
  123. .entrySet().stream()
  124. .sorted(Map.Entry.comparingByKey())
  125. .collect(Collectors.toList());
  126. List<JSONObject> list = new ArrayList<>();
  127. BigDecimal total = BigDecimal.ZERO;
  128. for (Map.Entry<Integer, BigDecimal> entry : collect) {
  129. BigDecimal lockCommission = liftingRecordService.queryLockCommission(getUserId(),entry.getKey().toString());
  130. JSONObject jsonObject = new JSONObject();
  131. jsonObject.put("grade", entry.getKey().toString());
  132. if (null != lockCommission){
  133. BigDecimal subtract = entry.getValue().subtract(lockCommission);
  134. total = total.add(subtract);
  135. jsonObject.put("cashFee", subtract.toString());
  136. }else {
  137. total = total.add(entry.getValue());
  138. jsonObject.put("cashFee", entry.getValue().toString());
  139. }
  140. PartnerRuleEntity ruleEntity = ruleService.selectByGrade(entry.getKey().toString());
  141. jsonObject.put("gradeName", ruleEntity.getGradeName());
  142. list.add(jsonObject);
  143. }
  144. HashMap<String, Object> map = new HashMap<>();
  145. map.put("list", list);
  146. map.put("total", total);
  147. return HttpResult.ok(map);
  148. }
  149. }