|
|
@@ -0,0 +1,368 @@
|
|
|
+package org.dromara.bill.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
+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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.dromara.bill.domain.StatementDetail;
|
|
|
+import org.dromara.bill.domain.StatementOrder;
|
|
|
+import org.dromara.bill.domain.StatementProduct;
|
|
|
+import org.dromara.bill.domain.bo.StatementDetailBo;
|
|
|
+import org.dromara.bill.domain.bo.StatementOrderBo;
|
|
|
+import org.dromara.bill.domain.bo.StatementProductBo;
|
|
|
+import org.dromara.bill.domain.dto.StatementOrderItem;
|
|
|
+import org.dromara.bill.domain.vo.StatementDetailVo;
|
|
|
+import org.dromara.bill.domain.vo.StatementOrderVo;
|
|
|
+import org.dromara.bill.domain.vo.StatementProductVo;
|
|
|
+import org.dromara.bill.mapper.StatementDetailMapper;
|
|
|
+import org.dromara.bill.mapper.StatementOrderMapper;
|
|
|
+import org.dromara.bill.mapper.StatementProductMapper;
|
|
|
+import org.dromara.bill.service.IStatementOrderService;
|
|
|
+import org.dromara.common.core.utils.MapstructUtils;
|
|
|
+import org.dromara.common.core.utils.StringUtils;
|
|
|
+import org.dromara.common.mybatis.core.page.PageQuery;
|
|
|
+import org.dromara.common.mybatis.core.page.TableDataInfo;
|
|
|
+import org.dromara.common.redis.utils.SequenceUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 对账单主Service业务层处理
|
|
|
+ *
|
|
|
+ * @author LionLi
|
|
|
+ * @date 2026-01-19
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@RequiredArgsConstructor
|
|
|
+@Service
|
|
|
+public class StatementOrderServiceImpl extends ServiceImpl<StatementOrderMapper, StatementOrder> implements IStatementOrderService {
|
|
|
+
|
|
|
+ private final StatementOrderMapper baseMapper;
|
|
|
+
|
|
|
+ private final StatementOrderMapper statementOrderMapper;
|
|
|
+
|
|
|
+ private final StatementDetailMapper statementDetailMapper;
|
|
|
+
|
|
|
+ private final StatementProductMapper statementProductMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询对账单主
|
|
|
+ *
|
|
|
+ * @param id 主键
|
|
|
+ * @return 对账单主
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public StatementOrderVo queryById(Long id) {
|
|
|
+ // 1. 查询主表
|
|
|
+ StatementOrderVo orderVo = baseMapper.selectVoById(id);
|
|
|
+ if (orderVo == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 查询所有明细(按 statement_order_id)
|
|
|
+ LambdaQueryWrapper<StatementDetail> detailWrapper = new LambdaQueryWrapper<>();
|
|
|
+ detailWrapper.eq(StatementDetail::getStatementOrderId, id);
|
|
|
+ List<StatementDetailVo> detailVos = statementDetailMapper.selectList(detailWrapper).stream()
|
|
|
+ .map(d -> MapstructUtils.convert(d, StatementDetailVo.class))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 3. 查询所有商品(按 statement_order_id)
|
|
|
+ LambdaQueryWrapper<StatementProduct> productWrapper = new LambdaQueryWrapper<>();
|
|
|
+ productWrapper.eq(StatementProduct::getStatementOrderId, id);
|
|
|
+ List<StatementProductVo> productVos = statementProductMapper.selectList(productWrapper).stream()
|
|
|
+ .map(p -> MapstructUtils.convert(p, StatementProductVo.class))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 4. 装填到 VO
|
|
|
+ orderVo.setDetailList(detailVos);
|
|
|
+ orderVo.setProductList(productVos);
|
|
|
+
|
|
|
+ return orderVo;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<StatementDetailVo> listDetailsByCustomerId(Long customerId) {
|
|
|
+ if (customerId == null) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 第一步:先查出该客户的所有对账单 ID
|
|
|
+ LambdaQueryWrapper<StatementOrder> orderWrapper = new LambdaQueryWrapper<>();
|
|
|
+ orderWrapper.eq(StatementOrder::getCustomerId, customerId)
|
|
|
+ .eq(StatementOrder::getDelFlag, "0"); // 未删除的对账单
|
|
|
+
|
|
|
+ List<Long> statementOrderIds = baseMapper.selectList(orderWrapper).stream()
|
|
|
+ .map(StatementOrder::getId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ if (statementOrderIds.isEmpty()) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 第二步:根据这些对账单ID,查询所有明细
|
|
|
+ LambdaQueryWrapper<StatementDetail> detailWrapper = new LambdaQueryWrapper<>();
|
|
|
+ detailWrapper.in(StatementDetail::getStatementOrderId, statementOrderIds)
|
|
|
+ .eq(StatementDetail::getDelFlag, "0"); // 未删除的明细
|
|
|
+
|
|
|
+ List<StatementDetail> details = statementDetailMapper.selectList(detailWrapper);
|
|
|
+
|
|
|
+ return details.stream()
|
|
|
+ .map(d -> MapstructUtils.convert(d, StatementDetailVo.class))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public TableDataInfo<StatementDetailVo> listDetailsByCustomerIdPage(Long customerId, PageQuery pageQuery) {
|
|
|
+ if (customerId == null) {
|
|
|
+ return TableDataInfo.build(new Page<>(0, 0));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询客户的所有有效对账单
|
|
|
+ LambdaQueryWrapper<StatementOrder> orderWrapper = new LambdaQueryWrapper<>();
|
|
|
+ orderWrapper.eq(StatementOrder::getCustomerId, customerId)
|
|
|
+ .eq(StatementOrder::getDelFlag, "0");
|
|
|
+
|
|
|
+ List<StatementOrder> statementOrders = baseMapper.selectList(orderWrapper);
|
|
|
+ if (statementOrders.isEmpty()) {
|
|
|
+ return TableDataInfo.build(new Page<>(0, 0));
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Long> statementOrderIds = statementOrders.stream()
|
|
|
+ .map(StatementOrder::getId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 构建 orderId -> amount 映射
|
|
|
+ Map<Long, BigDecimal> orderIdToAmountMap = statementOrders.stream()
|
|
|
+ .collect(Collectors.toMap(
|
|
|
+ StatementOrder::getId,
|
|
|
+ StatementOrder::getAmount,
|
|
|
+ (v1, v2) -> v1
|
|
|
+ ));
|
|
|
+
|
|
|
+ // 查询明细(分页)
|
|
|
+ LambdaQueryWrapper<StatementDetail> detailWrapper = new LambdaQueryWrapper<>();
|
|
|
+ detailWrapper.in(StatementDetail::getStatementOrderId, statementOrderIds)
|
|
|
+ .eq(StatementDetail::getDelFlag, "0");
|
|
|
+
|
|
|
+ Page<StatementDetailVo> pageResult = statementDetailMapper.selectVoPage(pageQuery.build(), detailWrapper);
|
|
|
+
|
|
|
+ // 填充 statementAmount
|
|
|
+ pageResult.getRecords().forEach(vo -> {
|
|
|
+ Long orderId = vo.getStatementOrderId();
|
|
|
+ if (orderId != null) {
|
|
|
+ vo.setStatementAmount(orderIdToAmountMap.get(orderId));
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return TableDataInfo.build(pageResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public TableDataInfo<StatementProductVo> getStatementProductList(List<StatementOrderItem> items) {
|
|
|
+ if (CollectionUtils.isEmpty(items)) {
|
|
|
+ return TableDataInfo.build(Collections.emptyList());
|
|
|
+ }
|
|
|
+ //:使用自定义 SQL 实现 WHERE (a,b) IN ((1,100),(2,200))
|
|
|
+ List<StatementProductVo> productVos = statementProductMapper.selectByStatementAndOrderIds(items);
|
|
|
+
|
|
|
+ return TableDataInfo.build(productVos);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页查询对账单主列表
|
|
|
+ *
|
|
|
+ * @param bo 查询条件
|
|
|
+ * @param pageQuery 分页参数
|
|
|
+ * @return 对账单主分页列表
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public TableDataInfo<StatementOrderVo> queryPageList(StatementOrderBo bo, PageQuery pageQuery) {
|
|
|
+ LambdaQueryWrapper<StatementOrder> lqw = buildQueryWrapper(bo);
|
|
|
+ Page<StatementOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
|
|
|
+ return TableDataInfo.build(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询符合条件的对账单主列表
|
|
|
+ *
|
|
|
+ * @param bo 查询条件
|
|
|
+ * @return 对账单主列表
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<StatementOrderVo> queryList(StatementOrderBo bo) {
|
|
|
+ LambdaQueryWrapper<StatementOrder> lqw = buildQueryWrapper(bo);
|
|
|
+ return baseMapper.selectVoList(lqw);
|
|
|
+ }
|
|
|
+
|
|
|
+ private LambdaQueryWrapper<StatementOrder> buildQueryWrapper(StatementOrderBo bo) {
|
|
|
+ Map<String, Object> params = bo.getParams();
|
|
|
+ LambdaQueryWrapper<StatementOrder> lqw = Wrappers.lambdaQuery();
|
|
|
+ lqw.orderByAsc(StatementOrder::getId);
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getStatementOrderNo()), StatementOrder::getStatementOrderNo, bo.getStatementOrderNo());
|
|
|
+ lqw.eq(bo.getCustomerId() != null, StatementOrder::getCustomerId, bo.getCustomerId());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getCustomerNo()), StatementOrder::getCustomerNo, bo.getCustomerNo());
|
|
|
+ lqw.like(StringUtils.isNotBlank(bo.getCustomerName()), StatementOrder::getCustomerName, bo.getCustomerName());
|
|
|
+ lqw.eq(bo.getAmount() != null, StatementOrder::getAmount, bo.getAmount());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getStatementSelf()), StatementOrder::getStatementSelf, bo.getStatementSelf());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getStatementSelfPhone()), StatementOrder::getStatementSelfPhone, bo.getStatementSelfPhone());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getStatementStatus()), StatementOrder::getStatementStatus, bo.getStatementStatus());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getIsPaymentStatus()), StatementOrder::getIsPaymentStatus, bo.getIsPaymentStatus());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getIsInvoiceStatus()), StatementOrder::getIsInvoiceStatus, bo.getIsInvoiceStatus());
|
|
|
+ lqw.eq(bo.getStatementDate() != null, StatementOrder::getStatementDate, bo.getStatementDate());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getAnnexAddress()), StatementOrder::getAnnexAddress, bo.getAnnexAddress());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getRejectRemark()), StatementOrder::getRejectRemark, bo.getRejectRemark());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getPlatformCode()), StatementOrder::getPlatformCode, bo.getPlatformCode());
|
|
|
+ if (params != null) {
|
|
|
+ lqw.between(params.get("beginTime") != null && params.get("endTime") != null,
|
|
|
+ StatementOrder::getStatementDate, params.get("beginTime"), params.get("endTime"));
|
|
|
+ }
|
|
|
+ return lqw;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增对账单主
|
|
|
+ *
|
|
|
+ * @param bo 对账单主
|
|
|
+ * @return 是否新增成功
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Boolean insertByBo(StatementOrderBo bo) {
|
|
|
+ String statementOrderNo = SequenceUtils.generateOrderCode("RC");
|
|
|
+ bo.setStatementOrderNo(statementOrderNo);
|
|
|
+ StatementOrder entity = MapstructUtils.convert(bo, StatementOrder.class);
|
|
|
+ validEntityBeforeSave(entity);
|
|
|
+ boolean success = this.save(entity);
|
|
|
+ if (success) {
|
|
|
+ bo.setId(entity.getId());
|
|
|
+ saveDetailsAndProducts(bo);
|
|
|
+ }
|
|
|
+ return success;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改对账单主
|
|
|
+ *
|
|
|
+ * @param bo 对账单主
|
|
|
+ * @return 是否修改成功
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Boolean updateByBo(StatementOrderBo bo) {
|
|
|
+ StatementOrder entity = MapstructUtils.convert(bo, StatementOrder.class);
|
|
|
+ validEntityBeforeSave(entity);
|
|
|
+
|
|
|
+ // 先删除旧数据
|
|
|
+ deleteDetailsAndProducts(entity.getId());
|
|
|
+
|
|
|
+ // 再保存新数据
|
|
|
+ boolean success = this.updateById(entity);
|
|
|
+ if (success && CollectionUtil.isNotEmpty(bo.getDetailList())) {
|
|
|
+ saveDetailsAndProducts(bo);
|
|
|
+ }
|
|
|
+ return success;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改状态
|
|
|
+ *
|
|
|
+ * @param bo 信息
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public int updateStatus(StatementOrderBo bo) {
|
|
|
+ StatementOrder order = new StatementOrder();
|
|
|
+ order.setId(bo.getId());
|
|
|
+ order.setStatementStatus(bo.getStatementStatus());
|
|
|
+ return baseMapper.updateById(order);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 公共方法:保存明细和商品
|
|
|
+ */
|
|
|
+ private void saveDetailsAndProducts(StatementOrderBo bo) {
|
|
|
+ List<StatementDetail> details = new ArrayList<>();
|
|
|
+ List<StatementProduct> products = new ArrayList<>();
|
|
|
+
|
|
|
+ // 1. 先保存所有明细
|
|
|
+ if (CollectionUtil.isNotEmpty(bo.getDetailList())) {
|
|
|
+ for (StatementDetailBo detailBo : bo.getDetailList()) {
|
|
|
+ StatementDetail detail = MapstructUtils.convert(detailBo, StatementDetail.class);
|
|
|
+ detail.setStatementOrderId(bo.getId());
|
|
|
+ detail.setStatementOrderNo(bo.getStatementOrderNo());
|
|
|
+ details.add(detail);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 按 orderId 分组商品
|
|
|
+ Map<Long, List<StatementProductBo>> productGroupByOrderId =
|
|
|
+ CollectionUtil.isNotEmpty(bo.getProductList())
|
|
|
+ ? bo.getProductList().stream()
|
|
|
+ .collect(Collectors.groupingBy(StatementProductBo::getOrderId))
|
|
|
+ : Collections.emptyMap();
|
|
|
+
|
|
|
+ // 3. 为每个明细,找到对应的商品
|
|
|
+ for (StatementDetailBo detailBo : bo.getDetailList()) {
|
|
|
+ Long originalOrderId = detailBo.getOrderId();
|
|
|
+ List<StatementProductBo> productListForThisOrder = productGroupByOrderId.get(originalOrderId);
|
|
|
+
|
|
|
+ if (CollectionUtil.isNotEmpty(productListForThisOrder)) {
|
|
|
+ for (StatementProductBo productBo : productListForThisOrder) {
|
|
|
+ StatementProduct product = MapstructUtils.convert(productBo, StatementProduct.class);
|
|
|
+ product.setStatementOrderId(bo.getId());
|
|
|
+ product.setStatementOrderNo(bo.getStatementOrderNo());
|
|
|
+ product.setOrderId(originalOrderId);
|
|
|
+ product.setOrderNo(detailBo.getOrderNo());
|
|
|
+ products.add(product);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!details.isEmpty()) {
|
|
|
+ statementDetailMapper.insertBatch(details);
|
|
|
+ }
|
|
|
+ if (!products.isEmpty()) {
|
|
|
+ statementProductMapper.insertBatch(products);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 公共方法:删除明细和商品
|
|
|
+ */
|
|
|
+ private void deleteDetailsAndProducts(Long statementOrderId) {
|
|
|
+ statementDetailMapper.deleteByStatementOrderId(statementOrderId);
|
|
|
+ statementProductMapper.deleteByStatementOrderId(statementOrderId);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存前的数据校验
|
|
|
+ */
|
|
|
+ private void validEntityBeforeSave(StatementOrder entity) {
|
|
|
+ //TODO 做一些数据校验,如唯一约束
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验并批量删除对账单主信息
|
|
|
+ *
|
|
|
+ * @param ids 待删除的主键集合
|
|
|
+ * @param isValid 是否进行有效性校验
|
|
|
+ * @return 是否删除成功
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
|
|
|
+ if (isValid) {
|
|
|
+ //TODO 做一些业务上的校验,判断是否需要校验
|
|
|
+ }
|
|
|
+ return baseMapper.deleteByIds(ids) > 0;
|
|
|
+ }
|
|
|
+}
|