| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- package com.ydtech.modules.partner.controller;
- import java.math.BigDecimal;
- import java.util.*;
- import java.util.stream.Collectors;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.ydtech.core.page.HttpResult;
- import com.ydtech.modules.admin.dao.SysPartnerAccountMapper;
- import com.ydtech.modules.admin.model.SysPartnerAccount;
- import com.ydtech.modules.base.controller.BaseController;
- import com.ydtech.modules.base.model.dto.PageDto;
- import com.ydtech.modules.partner.dao.PartnerPartnerAccountMapper;
- import com.ydtech.modules.partner.model.PartnerExtractHistory;
- import com.ydtech.modules.partner.model.PartnerPartnerAccount;
- import com.ydtech.modules.partner.model.param.PartnerRuleEntity;
- import com.ydtech.modules.partner.service.PartnerExtractHistoryService;
- import com.ydtech.modules.partner.service.PartnerLiftingRecordService;
- import com.ydtech.modules.partner.service.PartnerRuleService;
- import io.swagger.annotations.Api;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.web.bind.annotation.*;
- @Api(tags = "提现记录")
- @Slf4j
- @RestController
- @RequestMapping("/partnerExtractHistory")
- public class PartnerExtractHistoryController extends BaseController {
- @Autowired
- private PartnerExtractHistoryService partnerExtractHistoryService;
- @Autowired
- private SysPartnerAccountMapper sysPartnerAccountMapper;
- @Autowired
- private PartnerPartnerAccountMapper partnerPartnerAccountMapper;
- @Autowired
- private PartnerRuleService ruleService;
- @Autowired
- private PartnerLiftingRecordService liftingRecordService;
- @PostMapping("/query")
- public HttpResult query(@RequestBody PartnerExtractHistory extractHistory){
- // if (StringUtils.isEmpty(extractHistory.getGrade())){
- // return HttpResult.error("用户等级不能为空");
- // }
- if (extractHistory.getPageNum() == null || extractHistory.getPageSize() == null){
- return HttpResult.error("分页参数不能为空");
- }
- PageDto pageDto = new PageDto();
- pageDto.setPageNum(extractHistory.getPageNum());
- pageDto.setPageSize(extractHistory.getPageSize());
- extractHistory.setPage(buildPageRequest(pageDto));
- extractHistory.setUserId(getUserId());
- return HttpResult.ok(partnerExtractHistoryService.queryPage(extractHistory));
- }
- /**
- * 提现
- * @param extractHistory
- * @return
- */
- @PostMapping("/extractRecords")
- @Transactional(rollbackFor = Exception.class)
- public HttpResult extractRecords(@RequestBody PartnerExtractHistory extractHistory){
- if (StringUtils.isEmpty(extractHistory.getGrade())){
- return HttpResult.error("用户等级不能为空");
- }
- if (null != extractHistory.getAmount() &&extractHistory.getAmount().compareTo(new BigDecimal("0")) <= 0){
- return HttpResult.error("提现金额不能小于0");
- }
- String userId = getUserId();
- PartnerPartnerAccount partnerAccount = partnerPartnerAccountMapper.selectOne(new LambdaQueryWrapper<PartnerPartnerAccount>()
- .eq(PartnerPartnerAccount::getUserId, userId)
- .eq(PartnerPartnerAccount::getGrade, extractHistory.getGrade()));
- if (null == partnerAccount){
- return HttpResult.error("账号余额不足");
- }
- if (partnerAccount.getCashFee().compareTo(extractHistory.getAmount()) == -1){
- return HttpResult.error("账号余额不足");
- }
- extractHistory.setUserId(userId);
- boolean save = partnerExtractHistoryService.save(extractHistory);
- if (save){
- SysPartnerAccount account = sysPartnerAccountMapper.selectOne(new LambdaQueryWrapper<SysPartnerAccount>()
- .eq(SysPartnerAccount::getUserId,userId));
- if (account != null){
- account.setExtractFee(account.getExtractFee().add(extractHistory.getAmount()));
- account.setCashFee(account.getCashFee().add(extractHistory.getAmount()));
- sysPartnerAccountMapper.updateById(account);
- }else {
- account = new SysPartnerAccount();
- account.setUserId(userId);
- account.setExtractFee(extractHistory.getAmount());
- account.setHistoryFee(new BigDecimal("0"));
- account.setCashFee(extractHistory.getAmount());
- account.setCreateTime(new Date());
- sysPartnerAccountMapper.insert( account);
- }
- partnerAccount.setHistoryFee(partnerAccount.getHistoryFee().add(extractHistory.getAmount()));
- partnerAccount.setCashFee(partnerAccount.getCashFee().subtract(extractHistory.getAmount()));
- partnerPartnerAccountMapper.updateById(partnerAccount);
- }
- return HttpResult.ok(save ? "提取成功" : "提取失败");
- }
- /**
- * 未转入的金额
- * @return
- */
- @GetMapping("/queryExtractRecords")
- public HttpResult queryExtractRecords(){
- List<PartnerPartnerAccount> partnerPartnerAccounts = partnerPartnerAccountMapper.selectList(new LambdaQueryWrapper<PartnerPartnerAccount>()
- .eq(PartnerPartnerAccount::getUserId,getUserId()));
- partnerPartnerAccounts.stream().forEach(partnerPartnerAccount -> {
- partnerPartnerAccount.setCashFee(partnerPartnerAccount.getCashFee().subtract(partnerPartnerAccount.getExpiredAmount()));
- });
- List<Map.Entry<Integer, BigDecimal>> collect = partnerPartnerAccounts.stream()
- .collect(Collectors.groupingBy(
- PartnerPartnerAccount::getGrade,
- Collectors.reducing(
- BigDecimal.ZERO,
- PartnerPartnerAccount::getCashFee,
- BigDecimal::add
- )
- ))
- .entrySet().stream()
- .sorted(Map.Entry.comparingByKey())
- .collect(Collectors.toList());
- List<JSONObject> list = new ArrayList<>();
- BigDecimal total = BigDecimal.ZERO;
- for (Map.Entry<Integer, BigDecimal> entry : collect) {
- BigDecimal lockCommission = liftingRecordService.queryLockCommission(getUserId(),entry.getKey().toString());
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("grade", entry.getKey().toString());
- if (null != lockCommission){
- BigDecimal subtract = entry.getValue().subtract(lockCommission);
- total = total.add(subtract);
- jsonObject.put("cashFee", subtract.toString());
- }else {
- total = total.add(entry.getValue());
- jsonObject.put("cashFee", entry.getValue().toString());
- }
- PartnerRuleEntity ruleEntity = ruleService.selectByGrade(entry.getKey().toString());
- jsonObject.put("gradeName", ruleEntity.getGradeName());
- list.add(jsonObject);
- }
- HashMap<String, Object> map = new HashMap<>();
- map.put("list", list);
- map.put("total", total);
- return HttpResult.ok(map);
- }
- }
|