|
|
@@ -0,0 +1,406 @@
|
|
|
+package com.sckw.payment.service.wallet;
|
|
|
+
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+import com.sckw.core.common.enums.StringConstant;
|
|
|
+import com.sckw.core.model.enums.EntTypeEnum;
|
|
|
+import com.sckw.core.model.enums.SystemTypeEnum;
|
|
|
+import com.sckw.core.web.context.LoginUserHolder;
|
|
|
+import com.sckw.core.web.response.result.PageDataResult;
|
|
|
+import com.sckw.payment.enums.PrepayTypEnum;
|
|
|
+import com.sckw.payment.model.KwpBizWallet;
|
|
|
+import com.sckw.payment.model.KwpBizWalletPrepay;
|
|
|
+import com.sckw.payment.repose.KwpBizWalletPageResponse;
|
|
|
+import com.sckw.payment.repose.KwpBizWalletPrepayPageResponse;
|
|
|
+import com.sckw.payment.repose.vo.KwpBizWalletPrepayExcelVO;
|
|
|
+import com.sckw.payment.repose.vo.KwpBizWalletVO;
|
|
|
+import com.sckw.payment.repository.KwpBizWalletPrepayRepository;
|
|
|
+import com.sckw.payment.repository.KwpBizWalletRepository;
|
|
|
+import com.sckw.payment.request.KwpBizWalletPrepayQueryRequest;
|
|
|
+import com.sckw.payment.request.KwpBizWalletPrepayRequest;
|
|
|
+import com.sckw.payment.request.KwpBizWalletQueryRequest;
|
|
|
+import com.sckw.payment.request.KwpBizWalletSaveRequest;
|
|
|
+import com.sckw.system.api.RemoteSystemService;
|
|
|
+import com.sckw.system.api.model.dto.res.EntCacheResDto;
|
|
|
+import com.sckw.system.api.model.dto.res.KwsEntDeptDto;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.dubbo.config.annotation.DubboReference;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Author: donglang
|
|
|
+ * Time: 2025-11-03
|
|
|
+ * Des: 钱包管理服务
|
|
|
+ * Version: 1.0
|
|
|
+ */
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class KwpBizWalletService {
|
|
|
+
|
|
|
+ private final KwpBizWalletRepository kwpBizWalletRepository;
|
|
|
+
|
|
|
+ private final KwpBizWalletPrepayRepository kwpBizWalletPrepayRepository;
|
|
|
+
|
|
|
+ @DubboReference(version = "1.0.0", group = "design", check = false)
|
|
|
+ private RemoteSystemService remoteSystemService;
|
|
|
+
|
|
|
+
|
|
|
+ private static final String OFFLINE_WALLET = "线下钱包";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页查询钱包明细
|
|
|
+ */
|
|
|
+ public PageDataResult<KwpBizWalletPageResponse> pageBizWalletList(KwpBizWalletQueryRequest request) {
|
|
|
+ int systemType = LoginUserHolder.getSystemType();
|
|
|
+
|
|
|
+ //非管理端,返回空分页结果
|
|
|
+ if (systemType != SystemTypeEnum.COMPANY.getCode()) {
|
|
|
+ return PageDataResult.empty(request.getPage(), request.getPageSize());
|
|
|
+ }
|
|
|
+
|
|
|
+ //查询当前登录企业
|
|
|
+ EntCacheResDto entCacheResDto = remoteSystemService.queryEntCacheById(LoginUserHolder.getEntId());
|
|
|
+ if (entCacheResDto == null) {
|
|
|
+ throw new RuntimeException("entId:" + LoginUserHolder.getEntId() + ", 企业信息不存在!");
|
|
|
+ }
|
|
|
+ List<KwpBizWalletPageResponse> walletList = new ArrayList<>();
|
|
|
+ int entType = Integer.parseInt(entCacheResDto.getEntTypes());
|
|
|
+ if (entType == EntTypeEnum.SUPPLIER.getCode()) {
|
|
|
+ //供应商及子企业
|
|
|
+ walletList = querySupWallet(entCacheResDto);
|
|
|
+ } else if (entType == EntTypeEnum.PURCHASER.getCode()) {
|
|
|
+ //采购商企业
|
|
|
+ walletList = queryProWallet(entCacheResDto.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ //模糊查询过滤
|
|
|
+ List<KwpBizWalletPageResponse> walletResponses = filterByFuzzyName(walletList, request);
|
|
|
+
|
|
|
+ //排序、内存分页
|
|
|
+ walletResponses.sort(Comparator.comparing(KwpBizWalletPageResponse::getEntId));
|
|
|
+ List<List<KwpBizWalletPageResponse>> pageList = Lists.partition(walletResponses, request.getPageSize());
|
|
|
+ List<KwpBizWalletPageResponse> walletPageList = pageList.size() >= request.getPage() ?
|
|
|
+ pageList.get(request.getPage() - 1) : Collections.emptyList();
|
|
|
+
|
|
|
+ return PageDataResult.success(request.getPage(), request.getPageSize(), (long) walletPageList.size(), walletPageList);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询供应商企业钱包信息
|
|
|
+ *
|
|
|
+ * @param entCacheResDto
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<KwpBizWalletPageResponse> querySupWallet(EntCacheResDto entCacheResDto) {
|
|
|
+ List<Long> entIds = new ArrayList<>();
|
|
|
+ entIds.add(entCacheResDto.getId());
|
|
|
+ //查询供应商的子级企业
|
|
|
+ List<KwsEntDeptDto> kwsEntDeptDtoList = remoteSystemService.queryEntDeptByPid(entCacheResDto.getParentId());
|
|
|
+ List<Long> parentId = kwsEntDeptDtoList.stream().map(KwsEntDeptDto::getEntId).collect(Collectors.toList());
|
|
|
+ entIds.addAll(parentId);
|
|
|
+
|
|
|
+ //查询供应企业所关联的钱包数据
|
|
|
+ LambdaQueryWrapper<KwpBizWallet> queryWrapper = Wrappers.<KwpBizWallet>lambdaQuery()
|
|
|
+ .in(KwpBizWallet::getSupEntId, entIds);
|
|
|
+
|
|
|
+ List<KwpBizWallet> bizWalletList = kwpBizWalletRepository.list(queryWrapper);
|
|
|
+ if (bizWalletList == null || CollectionUtils.isEmpty(bizWalletList)) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ return getAndConvertWallet(entIds, bizWalletList);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询采购商企业钱包信息
|
|
|
+ *
|
|
|
+ * @param entId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<KwpBizWalletPageResponse> queryProWallet(Long entId) {
|
|
|
+ //查询采购企业所关联的钱包数据
|
|
|
+ LambdaQueryWrapper<KwpBizWallet> queryWrapper = Wrappers.<KwpBizWallet>lambdaQuery()
|
|
|
+ .in(KwpBizWallet::getProEntId, entId);
|
|
|
+ List<KwpBizWallet> bizWalletList = kwpBizWalletRepository.list(queryWrapper);
|
|
|
+ if (bizWalletList == null || bizWalletList.isEmpty()) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ //查询钱包所关联的供应商企业id
|
|
|
+ List<Long> entIds = bizWalletList.stream().map(KwpBizWallet::getSupEntId).collect(Collectors.toList());
|
|
|
+
|
|
|
+ return getAndConvertWallet(entIds, bizWalletList);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取钱包数据
|
|
|
+ *
|
|
|
+ * @param entIds
|
|
|
+ * @param bizWalletList
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<KwpBizWalletPageResponse> getAndConvertWallet(List<Long> entIds, List<KwpBizWallet> bizWalletList) {
|
|
|
+
|
|
|
+ //根据供应企业id分组
|
|
|
+ Map<Long, List<KwpBizWallet>> walletGroupBySup = bizWalletList.stream().collect(
|
|
|
+ Collectors.groupingBy(KwpBizWallet::getSupEntId));
|
|
|
+
|
|
|
+ List<KwpBizWalletPageResponse> walletList = new ArrayList<>();
|
|
|
+ for (Long id : entIds) {
|
|
|
+ List<KwpBizWallet> walletGroupList = walletGroupBySup.getOrDefault(id, new ArrayList<>());
|
|
|
+ if (walletGroupList.isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ KwpBizWalletPageResponse walletPageResp = new KwpBizWalletPageResponse();
|
|
|
+ walletPageResp.setEntId(id);
|
|
|
+ //企业名称
|
|
|
+ walletPageResp.setEntName(queryEntById(id));
|
|
|
+ //钱包总数
|
|
|
+ walletPageResp.setWalletNum(walletGroupList.size());
|
|
|
+ //总余额
|
|
|
+ BigDecimal totalAmount = calTotalAmount(walletGroupList);
|
|
|
+ walletPageResp.setTotalAmount(totalAmount);
|
|
|
+ //钱包信息
|
|
|
+ List<KwpBizWalletVO> bizWalletVOList = conversionWallet(walletGroupList);
|
|
|
+ walletPageResp.setWalletVOList(bizWalletVOList);
|
|
|
+
|
|
|
+ walletList.add(walletPageResp);
|
|
|
+ }
|
|
|
+ return walletList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算总余额
|
|
|
+ */
|
|
|
+ private BigDecimal calTotalAmount(List<KwpBizWallet> wallets) {
|
|
|
+ return wallets.stream().map(w ->
|
|
|
+ w.getAmount().add(w.getPrepayAmount())).reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 钱包信息转换
|
|
|
+ *
|
|
|
+ * @param walletGroupList
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<KwpBizWalletVO> conversionWallet(List<KwpBizWallet> walletGroupList) {
|
|
|
+ List<KwpBizWalletVO> walletVOList = walletGroupList.stream().map(wallet -> {
|
|
|
+ KwpBizWalletVO walletVO = KwpBizWalletVO.toPageResp(wallet);
|
|
|
+ walletVO.setSupEntName(queryEntById(wallet.getSupEntId()));
|
|
|
+ walletVO.setProEntName(queryEntById(wallet.getProEntId()));
|
|
|
+ return walletVO;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ walletVOList.sort(Comparator.comparing(KwpBizWalletVO::getCreateTime));
|
|
|
+ return walletVOList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 模糊查询过滤(供应商企业名称、采购商企业名称)
|
|
|
+ */
|
|
|
+ private List<KwpBizWalletPageResponse> filterByFuzzyName(List<KwpBizWalletPageResponse> walletList, KwpBizWalletQueryRequest request) {
|
|
|
+ if (CollectionUtils.isEmpty(walletList)) {
|
|
|
+ return walletList;
|
|
|
+ }
|
|
|
+ return walletList.stream()
|
|
|
+ // 过滤供应商企业名称
|
|
|
+ .filter(wallet -> {
|
|
|
+ if (org.springframework.util.StringUtils.hasText(request.getSupEntName())) {
|
|
|
+ return wallet.getEntName() != null && wallet.getEntName().toLowerCase().contains(request.getSupEntName().toLowerCase());
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ })
|
|
|
+ // 过滤采购商企业名称
|
|
|
+ .map(wallet -> {
|
|
|
+ List<KwpBizWalletVO> filteredVOs = wallet.getWalletVOList().stream()
|
|
|
+ .filter(vo -> {
|
|
|
+ if (org.springframework.util.StringUtils.hasText(request.getProEntName())) {
|
|
|
+ return vo.getProEntName() != null && vo.getProEntName().toLowerCase().contains(request.getProEntName().toLowerCase());
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ wallet.setWalletVOList(filteredVOs);
|
|
|
+ return wallet;
|
|
|
+ })
|
|
|
+ // 过滤二级列表为空的一级供应商
|
|
|
+ .filter(wallet -> !wallet.getWalletVOList().isEmpty())
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询企业名称
|
|
|
+ */
|
|
|
+ private String queryEntById(Long entId) {
|
|
|
+ if (entId == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ EntCacheResDto entCacheResDto = remoteSystemService.queryEntCacheById(entId);
|
|
|
+ if (entCacheResDto == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return entCacheResDto.getFirmName();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增钱包
|
|
|
+ */
|
|
|
+ public void addWallet(KwpBizWalletSaveRequest request) {
|
|
|
+ int systemType = LoginUserHolder.getSystemType();
|
|
|
+ //非管理端,不可新建钱包
|
|
|
+ if (systemType != SystemTypeEnum.COMPANY.getCode()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //查询当前登录企业
|
|
|
+ EntCacheResDto entCacheResDto = remoteSystemService.queryEntCacheById(LoginUserHolder.getEntId());
|
|
|
+ if (entCacheResDto == null) {
|
|
|
+ throw new RuntimeException("entId:" + LoginUserHolder.getEntId() + ", 企业信息不存在!");
|
|
|
+ }
|
|
|
+ //校验
|
|
|
+ int entType = Integer.parseInt(entCacheResDto.getEntTypes());
|
|
|
+ if (entType != EntTypeEnum.SUPPLIER.getCode()) {
|
|
|
+ throw new RuntimeException("entId:" + LoginUserHolder.getEntId() + ", 非供应商企业不能新建钱包!");
|
|
|
+ }
|
|
|
+
|
|
|
+ KwpBizWallet kwpBizWallet = new KwpBizWallet();
|
|
|
+ kwpBizWallet.setSupEntId(request.getSupEntId());
|
|
|
+ kwpBizWallet.setProEntId(request.getProEntId());
|
|
|
+ //采购商企业名称
|
|
|
+ String entName = queryEntById(request.getProEntId());
|
|
|
+ kwpBizWallet.setName(entName + StringConstant.HYPHEN + OFFLINE_WALLET);
|
|
|
+ kwpBizWalletRepository.save(kwpBizWallet);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 钱包状态更新
|
|
|
+ */
|
|
|
+ public void updateWallet(Long id) {
|
|
|
+ if (id == null) {
|
|
|
+ throw new RuntimeException("钱包id不能为空!");
|
|
|
+ }
|
|
|
+ KwpBizWallet wallet = kwpBizWalletRepository.getById(id);
|
|
|
+ if (wallet == null) {
|
|
|
+ throw new RuntimeException("钱包id:" + id + ", 钱包信息不存在!");
|
|
|
+ }
|
|
|
+ if (wallet.getState() == 0) {
|
|
|
+ wallet.setState(1);
|
|
|
+ } else {
|
|
|
+ wallet.setState(0);
|
|
|
+ }
|
|
|
+ kwpBizWalletRepository.updateById(wallet);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 钱包金额更新
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void updateWalletAmount(KwpBizWalletPrepayRequest request) {
|
|
|
+ KwpBizWallet wallet = kwpBizWalletRepository.getById(request.getWalletId());
|
|
|
+ if (wallet == null) {
|
|
|
+ throw new RuntimeException("钱包id:" + request.getWalletId() + ", 钱包信息不存在!");
|
|
|
+ }
|
|
|
+ if (request.getType() == 0) {
|
|
|
+ //预付
|
|
|
+ wallet.setAmount(wallet.getAmount().add(request.getAmount()));
|
|
|
+ } else {
|
|
|
+ //扣款 扣减金额不能大于可用余额
|
|
|
+ if (request.getAmount().compareTo(wallet.getAmount()) > 0) {
|
|
|
+ throw new RuntimeException("扣款金额不能大于可用金额!, 扣款金额:" + request.getAmount() + ", 可用金额:" + wallet.getAmount());
|
|
|
+ }
|
|
|
+ wallet.setAmount(wallet.getAmount().subtract(request.getAmount()));
|
|
|
+ }
|
|
|
+ //钱包金额跟新
|
|
|
+ kwpBizWalletRepository.updateById(wallet);
|
|
|
+
|
|
|
+ //创建钱包金额更新日志
|
|
|
+ KwpBizWalletPrepay walletOperation = new KwpBizWalletPrepay();
|
|
|
+ walletOperation.setWalletId(request.getWalletId());
|
|
|
+ walletOperation.setAmount(request.getAmount());
|
|
|
+ walletOperation.setType(request.getType());
|
|
|
+ walletOperation.setVoucherUrl(request.getVoucherUrl());
|
|
|
+ walletOperation.setRemark(request.getRemark());
|
|
|
+ kwpBizWalletPrepayRepository.save(walletOperation);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 钱包冻账明细
|
|
|
+ */
|
|
|
+ public PageDataResult<KwpBizWalletPrepayPageResponse> walletPrepayDetail(KwpBizWalletPrepayQueryRequest request) {
|
|
|
+ Page<KwpBizWalletPrepay> page = new Page<>(request.getPage(), request.getPageSize());
|
|
|
+ LambdaQueryWrapper<KwpBizWalletPrepay> queryWrapper = Wrappers.<KwpBizWalletPrepay>lambdaQuery()
|
|
|
+ .eq(request.getType() != null, KwpBizWalletPrepay::getType, request.getType())
|
|
|
+ .like(StringUtils.isNotBlank(request.getRemark()), KwpBizWalletPrepay::getRemark, request.getRemark())
|
|
|
+ .ge(request.getMinAmount() != null, KwpBizWalletPrepay::getAmount, request.getMinAmount())
|
|
|
+ .lt(request.getMaxAmount() != null, KwpBizWalletPrepay::getAmount, request.getMaxAmount())
|
|
|
+ .ge(request.getStartTime() != null, KwpBizWalletPrepay::getCreateTime, request.getStartTime())
|
|
|
+ .lt(request.getEndTime() != null, KwpBizWalletPrepay::getCreateTime, request.getEndTime());
|
|
|
+ Page<KwpBizWalletPrepay> walletPrepayPage = kwpBizWalletPrepayRepository.page(page, queryWrapper);
|
|
|
+
|
|
|
+ //若为null,返回空分页结果
|
|
|
+ if (walletPrepayPage == null || CollectionUtils.isEmpty(walletPrepayPage.getRecords())) {
|
|
|
+ return PageDataResult.empty(request.getPage(), request.getPageSize());
|
|
|
+ }
|
|
|
+
|
|
|
+ List<KwpBizWalletPrepayPageResponse> walletPrepayPageList = new ArrayList<>();
|
|
|
+ for (KwpBizWalletPrepay record : walletPrepayPage.getRecords()) {
|
|
|
+ KwpBizWalletPrepayPageResponse pageResp = KwpBizWalletPrepayPageResponse.toPageResp(record);
|
|
|
+ walletPrepayPageList.add(pageResp);
|
|
|
+ }
|
|
|
+
|
|
|
+ return PageDataResult.success(request.getPage(), request.getPageSize(), (long) walletPrepayPageList.size(), walletPrepayPageList);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 钱包冻账明细导出
|
|
|
+ */
|
|
|
+ public List<KwpBizWalletPrepayExcelVO> queryExportWalletPrepay(KwpBizWalletPrepayQueryRequest request) {
|
|
|
+ //查询导出数据
|
|
|
+ PageDataResult<KwpBizWalletPrepayPageResponse> walletPrepayPageResult = walletPrepayDetail(request);
|
|
|
+ return walletPrepayPageResult.getList().stream().map(vo -> {
|
|
|
+ KwpBizWalletPrepayExcelVO excelVO = KwpBizWalletPrepayExcelVO.toPageResp(vo);
|
|
|
+
|
|
|
+ //查询钱包信息
|
|
|
+ KwpBizWallet wallet = kwpBizWalletRepository.getById(vo.getId());
|
|
|
+ if (wallet == null) {
|
|
|
+ throw new RuntimeException("钱包id:" + request.getWalletId() + ", 钱包信息不存在!");
|
|
|
+ }
|
|
|
+ excelVO.setWalletName(wallet.getName());
|
|
|
+ excelVO.setAmount(wallet.getAmount());
|
|
|
+ excelVO.setPrepayAmount(wallet.getPrepayAmount());
|
|
|
+
|
|
|
+ //查询企业名称
|
|
|
+ String entName = queryEntById(wallet.getSupEntId());
|
|
|
+ excelVO.setSupEntName(entName);
|
|
|
+
|
|
|
+ //变动类型
|
|
|
+ Integer type = vo.getType();
|
|
|
+ String descByCode = PrepayTypEnum.getDescByCode(type);
|
|
|
+ excelVO.setType(descByCode);
|
|
|
+
|
|
|
+ return excelVO;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|