diff --git a/src/main/java/com/example/demo/serviceImpl/cash/CashRefundServiceImpl.java b/src/main/java/com/example/demo/serviceImpl/cash/CashRefundServiceImpl.java index 0cbf58e..09f16b2 100644 --- a/src/main/java/com/example/demo/serviceImpl/cash/CashRefundServiceImpl.java +++ b/src/main/java/com/example/demo/serviceImpl/cash/CashRefundServiceImpl.java @@ -1,5 +1,6 @@ package com.example.demo.serviceImpl.cash; - +import com.example.demo.domain.entity.Admin; +import com.example.demo.Util.BusinessException; import com.example.demo.Util.GoldTistV2; import com.example.demo.config.RabbitMQConfig; import com.example.demo.domain.entity.User; @@ -10,39 +11,37 @@ import com.example.demo.domain.vo.cash.CashRecordRefund; import com.example.demo.domain.vo.cash.CashRefundMessage; import com.example.demo.domain.vo.coin.Messages; import com.example.demo.domain.vo.coin.Result; +import com.example.demo.exception.SystemException; +import com.example.demo.mapper.cash.CashCollectionMapper; import com.example.demo.mapper.cash.CashRefundMapper; import com.example.demo.mapper.coin.AuditMapper; import com.example.demo.mapper.coin.MarketMapper; -import com.example.demo.mapper.coin.OperationLogMapper; import com.example.demo.mapper.coin.RefundMapper; import com.example.demo.service.cash.RefundService; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; -import jakarta.servlet.http.HttpServletRequest; -import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; +import java.math.RoundingMode; import java.time.LocalDate; -import java.time.LocalDateTime; -import java.util.Date; -import java.util.List; -import java.util.UUID; +import java.util.*; +import java.util.function.Function; +import java.util.stream.Collectors; import static org.apache.commons.lang3.StringUtils.substring; /** * @program: GOLD * @ClassName CashRefundServiceImpl - * @description: 处理退款相关业务逻辑 + * @description: * @author: huangqizhen * @create: 2025−09-28 15:02 * @Version 1.0 **/ @Service -@Transactional + public class CashRefundServiceImpl implements RefundService { @Autowired @@ -61,29 +60,114 @@ public class CashRefundServiceImpl implements RefundService { @Override public PageInfo select(Integer pageNum, Integer pageSize, CashRecordDTO cashRecordDTO) { PageHelper.startPage(pageNum, pageSize); -// System.out.println(goldDetail.getMarkets()); List list = cashRefundMapper.select(cashRecordDTO); + + if (list.isEmpty()) { + return new PageInfo<>(list); + } + System.out.println("135"); + // 批量收集ID + Set relatedIds = new HashSet<>(); + Set marketIds = new HashSet<>(); + Set submitterIds = new HashSet<>(); + Set auditIds = new HashSet<>(); + + list.forEach(item -> { + if (item.getRelatedId() != null) relatedIds.add(item.getRelatedId()); + if (item.getMarket() != null) marketIds.add(item.getMarket()); + if (item.getSubmitterId() != null) submitterIds.add(item.getSubmitterId()); + if (item.getAuditId() != null) auditIds.add(item.getAuditId()); + }); + + // 批量查询 + Map cashCollectionMap = cashCollectionMapper.selectBatchIds(relatedIds) + .stream().collect(Collectors.toMap(CashCollection::getId, Function.identity())); + + Map marketNameMap = marketMapper.getMarketByIds(marketIds) + .stream().collect(Collectors.toMap(Market::getId, Market::getName)); + + Map submitterNameMap = auditMapper.getNamesByIds(submitterIds) + .stream().collect(Collectors.toMap(Admin::getId, Admin::getAdminName)); + + Map auditMap = cashRefundMapper.getAuditBatch(auditIds) + .stream().collect(Collectors.toMap(LhlAudit::getId, Function.identity())); + + // 处理数据 + list.forEach(item -> { + CashCollection cashCollection = cashCollectionMap.get(item.getRelatedId()); + if (cashCollection != null) { + processCashCollection(item, cashCollection); + } + + String marketName = marketNameMap.get(item.getMarket()); + String submitter = submitterNameMap.get(item.getSubmitterId()); + LhlAudit lhlAudit = auditMap.get(item.getAuditId()); + + item.setMarketName(marketName != null ? marketName : ""); + item.setSubmitter(submitter != null ? submitter : ""); + + if (lhlAudit != null) { + item.setAreaServise(lhlAudit.getAreaServise()); + item.setAreaFinance(lhlAudit.getAreaFinance()); + item.setAreaCharge(lhlAudit.getAreaCharge()); + item.setHeadFinance(lhlAudit.getHeadFinace()); + } + }); + return new PageInfo<>(list); } - // 添加退款订单 + private void processCashCollection(CashRecordDTO item, CashCollection cashCollection) { + // 设置默认值 + Integer freeGold = cashCollection.getFreeGold() != null ? cashCollection.getFreeGold() : 0; + Integer permanentGold = cashCollection.getPermanentGold() != null ? cashCollection.getPermanentGold() : 0; + + BigDecimal free = new BigDecimal(freeGold).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP); + BigDecimal permanent = new BigDecimal(permanentGold).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP); + + item.setGold(permanent); + item.setFree(free); + item.setActivity(cashCollection.getActivity()); + item.setPaymentCurrency(cashCollection.getPaymentCurrency()); + item.setPaymentAmount(cashCollection.getPaymentAmount()); + item.setReceivedCurrency(cashCollection.getReceivedCurrency()); + item.setReceivedAmount(cashCollection.getReceivedAmount()); + item.setPayType(cashCollection.getPayType()); + item.setPayTime(cashCollection.getPayTime()); + item.setPayBankCode(cashCollection.getBankCode()); + item.setPaySubmitter(cashCollection.getSubmitterName()); + item.setAudit(cashCollection.getAuditName()); + item.setReceivedTime(cashCollection.getReceivedTime()); + item.setPayVoucher(cashCollection.getVoucher()); + item.setPayRemark(cashCollection.getRemark()); + item.setHandlingCharge(cashCollection.getHandlingCharge()); + + // 处理金币金额 + if (item.getPermanentGold() != null) { + item.setPermanentGold(item.getPermanentGold().divide(new BigDecimal(100), 2, RoundingMode.HALF_UP)); + } + if (item.getFreeGold() != null) { + item.setFreeGold(item.getFreeGold().divide(new BigDecimal(100), 2, RoundingMode.HALF_UP)); + } + } + @Override - public int add(CashRecordRefund cashRecordRefund) throws Exception { - if (cashRecordRefund.getJwcode() == null) { - throw new Exception("未输入精网号"); + public int add(CashRecordRefund cashRecordRefund) throws Exception { + if(cashRecordRefund.getJwcode()==null){ + throw new Exception("未输入精网号") ; } - if (cashRecordRefund.getRefundModel() == null) { - throw new Exception("请填充退款类型"); + if(cashRecordRefund.getRefundModel()== null){ + throw new Exception("请填充退款类型") ; } - if (cashRecordRefund.getRefundReason() == null) { - throw new Exception("请填写退款理由"); + if(cashRecordRefund.getRefundReason()== null){ + throw new Exception("请填写退款理由") ; } CashRecordDone cashRecordDonetwo = new CashRecordDone(); cashRecordDonetwo.setAreaServise(cashRecordRefund.getAreaServise()); cashRefundMapper.addAudit(cashRecordDonetwo); cashRecordRefund.setAuditId(cashRecordDonetwo.getId()); -// cashRecordRefund.setStatus(10); + cashRecordRefund.setStatus(10); //生成订单号后半部分 String orderNumber = cashRecordRefund.getOrderCode(); //构建订单信息 @@ -93,8 +177,8 @@ public class CashRefundServiceImpl implements RefundService { CashRecordDone cashRecordDone1 = new CashRecordDone(); cashRecordDone1.setId(cashRecordRefund.getId()); cashRecordDone1.setStatus(6); - if (cashRecordDone1.getId() != null || cashRecordDone1.getOrderCode() != null) - cashRefundMapper.updateStatus(cashRecordDone1); + if (cashRecordDone1.getId()!=null||cashRecordDone1.getOrderCode()!= null) + return cashRefundMapper.updateStatus(cashRecordDone1); else return Result.error("提交失败").getCode(); // 发送退款创建消息 @@ -112,48 +196,44 @@ public class CashRefundServiceImpl implements RefundService { return Result.success("提交成功").getCode(); } - // 更新退款订单信息 @Override public int update(CashRecordDone cashRecordDone) throws Exception { - if (cashRecordDone.getJwcode() == null) { + if (cashRecordDone.getJwcode()== null) { throw new RuntimeException("未输入精网号"); } - if (cashRecordDone.getPaymentAmount() == null) { + if (cashRecordDone.getPaymentAmount()== null) { throw new RuntimeException("未输入付款金额"); } - if (cashRecordDone.getPaymentCurrency() == null) { - throw new RuntimeException("未输入付款币种"); + if (cashRecordDone.getPaymentCurrency()== null){ + throw new RuntimeException("未输入付款币种"); } - if (cashRecordDone.getRefundModel() == null) { + if (cashRecordDone.getRefundModel()== null) { throw new RuntimeException("请填写退款类型"); } - if (cashRecordDone.getRefundReason() == null) { + if (cashRecordDone.getRefundReason()== null) { throw new RuntimeException("请填写退款理由"); } int result = cashRefundMapper.update(cashRecordDone); return (result > 0 ? Result.success("提交成功") : Result.error("提交失败")).getCode(); } - // 撤回退款订单 @Override public int withdraw(CashRecordDone cashRecordDone) { return cashRefundMapper.withdraw(cashRecordDone.getId()); } - // 审核退款订单 @Override - public int review(CashRecordDone cashRecordDone) throws Exception { - if (cashRecordDone.getStatus() == 12 || cashRecordDone.getStatus() == 22) { - if (cashRecordDone.getOrderCode() == null) { + public int review(CashRecordDone cashRecordDone) throws Exception { + if(cashRecordDone.getStatus()== 12|| cashRecordDone.getStatus()== 22){ + if(cashRecordDone.getOrderCode()== null){ throw new RuntimeException("未输入订单号"); } - CashRecordDone cashRecordDone1 = new CashRecordDone(); +CashRecordDone cashRecordDone1 = new CashRecordDone(); cashRecordDone1.setOrderCode(cashRecordDone.getOrderCode().substring(2)); cashRecordDone1.setStatus(4); - if (cashRecordDone1.getId() != null || cashRecordDone1.getOrderCode() != null) { - cashRefundMapper.updateStatus(cashRecordDone1); - } - } + if (cashRecordDone1.getId()!=null||cashRecordDone1.getOrderCode()!= null){ + cashRefundMapper.updateStatus(cashRecordDone1); + }} cashRefundMapper.updateAudit(cashRecordDone); int result = cashRefundMapper.review(cashRecordDone); CashRecordDTO cashRecordDTO = cashRefundMapper.selectById(cashRecordDone.getId()); @@ -177,56 +257,51 @@ public class CashRefundServiceImpl implements RefundService { return (result > 0 ? Result.success("提交成功") : Result.error("提交失败")).getCode(); } - // 执行退款操作 @Override public int executor(CashRecordDone cashRecordDone) throws Exception { - if (cashRecordDone.getRefundVoucher() == null) { + if(cashRecordDone.getRefundVoucher()== null){ throw new RuntimeException("未输入退款凭证"); } - if (cashRecordDone.getRefundTime() == null) { + if(cashRecordDone.getRefundTime()== null){ throw new RuntimeException("未输入退款时间"); } - if (cashRecordDone.getRefundRemark() == null) { + if(cashRecordDone.getRefundRemark()== null){ throw new RuntimeException("未输入退款备注"); } - if (cashRecordDone.getRefundChannels() == null) { + if(cashRecordDone.getRefundChannels()== null){ throw new RuntimeException("未输入退款途径"); } - if (cashRecordDone.getRefundCurrency() == null) { + if(cashRecordDone.getRefundCurrency()== null){ throw new RuntimeException("未输入退款币种"); } - if (cashRecordDone.getRefundAmount() == null) { + if(cashRecordDone.getRefundAmount()== null){ throw new RuntimeException("未输入退款金额"); } int result = cashRefundMapper.executor(cashRecordDone); - return (result > 0 ? Result.success("提交成功") : Result.error("提交失败")).getCode(); } - // 更新退款订单状态 @Override public int updateStatus(CashRecordDone cashRecordDone) { return cashRefundMapper.updateStatus(cashRecordDone); } - // 最终审核退款订单 @Override public int finalreview(CashRecordDone cashRecordDone) { - if (cashRecordDone.getPermanentGold() == null) { + if(cashRecordDone.getPermanentGold()== null){ cashRecordDone.setPermanentGold(0); } - if (cashRecordDone.getFreeGold() == null) { + if(cashRecordDone.getFreeGold()== null){ cashRecordDone.setFreeGold(0); } - if (cashRecordDone.getStatus() == 32) { + if(cashRecordDone.getStatus()== 32){ CashRecordDone cashRecordDone1 = new CashRecordDone(); cashRecordDone1.setOrderCode(cashRecordDone.getOrderCode().substring(2)); cashRecordDone1.setStatus(4); - if (cashRecordDone1.getId() != null || cashRecordDone1.getOrderCode() != null) { - cashRefundMapper.updateStatus(cashRecordDone1); - } - } - if (cashRecordDone.getGoodsName() != null && cashRecordDone.getStatus() == 40 && + if (cashRecordDone1.getId()!=null||cashRecordDone1.getOrderCode()!= null){ + cashRefundMapper.updateStatus(cashRecordDone1); + }} + if (cashRecordDone.getGoodsName() != null &&cashRecordDone.getStatus() ==40 && (cashRecordDone.getGoodsName().equals("金币充值") || cashRecordDone.getGoodsName().contains("金币充值"))) { UserGoldRecord userGoldRecord = new UserGoldRecord(); @@ -236,7 +311,7 @@ public class CashRefundServiceImpl implements RefundService { userGoldRecord.setRefundType("金币退款"); userGoldRecord.setCrefundModel(cashRecordDone.getRefundModel()); userGoldRecord.setJwcode(cashRecordDone.getJwcode()); - userGoldRecord.setSumGold(cashRecordDone.getPermanentGold() + cashRecordDone.getFreeGold()); + userGoldRecord.setSumGold(cashRecordDone.getPermanentGold()+cashRecordDone.getFreeGold()); userGoldRecord.setPermanentGold(cashRecordDone.getPermanentGold()); int currentMonth = LocalDate.now().getMonthValue(); if (currentMonth >= 1 && currentMonth <= 6) { @@ -257,17 +332,16 @@ public class CashRefundServiceImpl implements RefundService { userGoldRecord.setCreateTime(new Date()); userGoldRecord.setUpdateTime(new Date()); String auditName = auditMapper.getName(cashRecordDone.getAuditId()); - refundMapper.add(userGoldRecord); - User user = new User(); - user.setJwcode(userGoldRecord.getJwcode()); + refundMapper.add(userGoldRecord); + User user = new User(); + user.setJwcode(userGoldRecord.getJwcode()); user.setCurrentPermanentGold(BigDecimal.valueOf(-userGoldRecord.getPermanentGold())); //当前永久金币 user.setCurrentFreeJune(BigDecimal.valueOf(-userGoldRecord.getFreeJune())); //当前六月免费金币 user.setCurrentFreeDecember(BigDecimal.valueOf(-userGoldRecord.getFreeDecember())); //当前十二月免费金币 auditMapper.updateUserGold(user); - GoldTistV2.addCoinNew(userGoldRecord.getJwcode().toString(), 58, //退款免费+永久金币-充值 - (double) (userGoldRecord.getFreeDecember() + userGoldRecord.getFreeJune() + userGoldRecord.getPermanentGold()) / 100, - userGoldRecord.getRemark(), (double) userGoldRecord.getPermanentGold() / 100, auditName, "退款金币充值"); - } + GoldTistV2.addCoinNew(userGoldRecord.getJwcode().toString(), 58, //退款免费+永久金币-充值 + (double) (userGoldRecord.getFreeDecember()+userGoldRecord.getFreeJune()+userGoldRecord.getPermanentGold() ) /100, + userGoldRecord.getRemark(),(double) userGoldRecord.getPermanentGold() / 100, auditName, "退款金币充值");} cashRefundMapper.updateAudit(cashRecordDone); int result = cashRefundMapper.review(cashRecordDone); @@ -291,51 +365,154 @@ public class CashRefundServiceImpl implements RefundService { return (result > 0 ? Result.success("提交成功") : Result.error("提交失败")).getCode(); } - // 辅助方法:根据状态码获取状态描述 - private String getStatusDescription(Integer status) { - switch (status) { - case 10: - return "地区财务待审核"; - case 11: - return "地区财务手动撤回待编辑提交"; - case 12: - return "地区财务驳回"; - case 20: - return "地区负责人待审核"; - case 22: - return "地区负责人驳回"; - case 30: - return "总部财务待审核"; - case 32: - return "总部财务驳回"; - case 40: - return "执行人待处理"; - case 41: - return "执行人已处理,退款结束"; - default: - return "未知状态"; - } - } - - // 财务查询退款订单列表 @Override public PageInfo financeSelect(Integer pageNum, Integer pageSize, CashRecordDTO cashRecordDTO) { PageHelper.startPage(pageNum, pageSize); // System.out.println(goldDetail.getMarkets()); List list = cashRefundMapper.financeSelect(cashRecordDTO); + if (list.isEmpty()) { + return new PageInfo<>(list); + } + + // 批量收集ID + Set relatedIds = new HashSet<>(); + Set marketIds = new HashSet<>(); + Set submitterIds = new HashSet<>(); + Set auditIds = new HashSet<>(); + + list.forEach(item -> { + if (item.getRelatedId() != null) relatedIds.add(item.getRelatedId()); + if (item.getMarket() != null) marketIds.add(item.getMarket()); + if (item.getSubmitterId() != null) submitterIds.add(item.getSubmitterId()); + if (item.getAuditId() != null) auditIds.add(item.getAuditId()); + }); + + // 批量查询 + Map cashCollectionMap = cashCollectionMapper.selectBatchIds(relatedIds) + .stream().collect(Collectors.toMap(CashCollection::getId, Function.identity())); + + Map marketNameMap = marketMapper.getMarketByIds(marketIds) + .stream().collect(Collectors.toMap(Market::getId, Market::getName)); + + Map submitterNameMap = auditMapper.getNamesByIds(submitterIds) + .stream().collect(Collectors.toMap(Admin::getId, Admin::getAdminName)); + + Map auditMap = cashRefundMapper.getAuditBatch(auditIds) + .stream().collect(Collectors.toMap(LhlAudit::getId, Function.identity())); + + // 处理数据 + list.forEach(item -> { + CashCollection cashCollection = cashCollectionMap.get(item.getRelatedId()); + if (cashCollection != null) { + processCashCollection(item, cashCollection); + } + + String marketName = marketNameMap.get(item.getMarket()); + String submitter = submitterNameMap.get(item.getSubmitterId()); + LhlAudit lhlAudit = auditMap.get(item.getAuditId()); + + item.setMarketName(marketName != null ? marketName : ""); + item.setSubmitter(submitter != null ? submitter : ""); + + if (lhlAudit != null) { + item.setAreaServise(lhlAudit.getAreaServise()); + item.setAreaFinance(lhlAudit.getAreaFinance()); + item.setAreaCharge(lhlAudit.getAreaCharge()); + item.setHeadFinance(lhlAudit.getHeadFinace()); + } + }); + return new PageInfo<>(list); } @Override public PageInfo exSelect(Integer pageNum, Integer pageSize, CashRecordDTO cashRecordDTO) { PageHelper.startPage(pageNum, pageSize); +// System.out.println(goldDetail.getMarkets()); List list = cashRefundMapper.exSelect(cashRecordDTO); + if (list.isEmpty()) { + return new PageInfo<>(list); + } + + // 批量收集ID + Set relatedIds = new HashSet<>(); + Set marketIds = new HashSet<>(); + Set submitterIds = new HashSet<>(); + Set auditIds = new HashSet<>(); + + list.forEach(item -> { + if (item.getRelatedId() != null) relatedIds.add(item.getRelatedId()); + if (item.getMarket() != null) marketIds.add(item.getMarket()); + if (item.getSubmitterId() != null) submitterIds.add(item.getSubmitterId()); + if (item.getAuditId() != null) auditIds.add(item.getAuditId()); + }); + + // 批量查询 + Map cashCollectionMap = cashCollectionMapper.selectBatchIds(relatedIds) + .stream().collect(Collectors.toMap(CashCollection::getId, Function.identity())); + + Map marketNameMap = marketMapper.getMarketByIds(marketIds) + .stream().collect(Collectors.toMap(Market::getId, Market::getName)); + + Map submitterNameMap = auditMapper.getNamesByIds(submitterIds) + .stream().collect(Collectors.toMap(Admin::getId, Admin::getAdminName)); + + Map auditMap = cashRefundMapper.getAuditBatch(auditIds) + .stream().collect(Collectors.toMap(LhlAudit::getId, Function.identity())); + + // 处理数据 + list.forEach(item -> { + CashCollection cashCollection = cashCollectionMap.get(item.getRelatedId()); + if (cashCollection != null) { + processCashCollection(item, cashCollection); + } + + String marketName = marketNameMap.get(item.getMarket()); + String submitter = submitterNameMap.get(item.getSubmitterId()); + LhlAudit lhlAudit = auditMap.get(item.getAuditId()); + + item.setMarketName(marketName != null ? marketName : ""); + item.setSubmitter(submitter != null ? submitter : ""); + + if (lhlAudit != null) { + item.setAreaServise(lhlAudit.getAreaServise()); + item.setAreaFinance(lhlAudit.getAreaFinance()); + item.setAreaCharge(lhlAudit.getAreaCharge()); + item.setHeadFinance(lhlAudit.getHeadFinace()); + } + }); + return new PageInfo<>(list); } - @Override public void addOnline(CashRecordRefund cashRecordRefund) { + if(cashRecordRefund.getJwcode()==null){ + throw new BusinessException("未输入精网号") ; + } + if(cashRecordRefund.getRefundModel()== null){ + throw new BusinessException("请填充退款类型") ; + } + if(cashRecordRefund.getRefundReason()== null){ + throw new BusinessException("请填写退款理由") ; + } + CashRecordDone cashRecordDonetwo = new CashRecordDone(); + cashRecordDonetwo.setAreaServise(cashRecordRefund.getAreaServise()); + cashRefundMapper.addAudit(cashRecordDonetwo); + cashRecordRefund.setAuditId(cashRecordDonetwo.getId()); + cashRecordRefund.setStatus(20); + //生成订单号后半部分 + String orderNumber = cashRecordRefund.getOrderCode(); + //构建订单信息 + cashRecordRefund.setOrderCode("TK" + orderNumber); //订单号 + cashRecordRefund.setMarket(String.valueOf(Integer.valueOf(marketMapper.getMarketId(cashRecordRefund.getMarket())))); + cashRefundMapper.insert(cashRecordRefund); + CashRecordDone cashRecordDone1 = new CashRecordDone(); + cashRecordDone1.setId(cashRecordRefund.getId()); + cashRecordDone1.setStatus(6); + if (cashRecordDone1.getId()!=null||cashRecordDone1.getOrderCode()!= null) + cashRefundMapper.updateStatus(cashRecordDone1); + else throw new SystemException("提交失败") ; } }