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.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; @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() .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() .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 partnerPartnerAccounts = partnerPartnerAccountMapper.selectList(new LambdaQueryWrapper() .eq(PartnerPartnerAccount::getUserId,getUserId())); List> 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 list = new ArrayList<>(); for (Map.Entry entry : collect) { JSONObject jsonObject = new JSONObject(); jsonObject.put("grade", entry.getKey().toString()); jsonObject.put("cashFee", entry.getValue().toString()); PartnerRuleEntity ruleEntity = ruleService.selectByGrade(entry.getKey().toString()); jsonObject.put("gradeName", ruleEntity.getGradeName()); list.add(jsonObject); } HashMap map = new HashMap<>(); map.put("list", list); map.put("total", partnerPartnerAccounts.stream() .map(PartnerPartnerAccount::getCashFee) .filter(Objects::nonNull) .reduce(BigDecimal.ZERO, BigDecimal::add)); return HttpResult.ok(map); } }