24282 před 1 rokem
rodič
revize
9382d1ffaa

+ 9 - 0
sd-business/src/main/java/com/sd/business/controller/statement/StatementOfAccountController.java

@@ -5,6 +5,7 @@ import com.ruoyi.common.core.domain.BaseSelectDto;
 import com.sd.business.entity.statement.dto.FileUploadDto;
 import com.sd.business.entity.statement.dto.StatementOfAccountDto;
 import com.sd.business.entity.statement.dto.StatementOfAccountSelectDto;
+import com.sd.business.entity.statement.vo.DocumentByBomVo;
 import com.sd.business.entity.statement.vo.DocumentBySkuVo;
 import com.sd.business.entity.statement.vo.ReconciliationDetailVo;
 import com.sd.business.entity.statement.vo.StatementOfAccountVo;
@@ -98,4 +99,12 @@ public class StatementOfAccountController {
         return statementOfAccountService.getDocumentBySku(dto.getId());
     }
 
+    /**
+     * bom对账
+     */
+    @PostMapping("/getDocumentByBom")
+    public List<DocumentByBomVo> getDocumentByBom(@RequestBody BaseSelectDto dto) {
+        return statementOfAccountService.getDocumentByBom(dto.getId());
+    }
+
 }

+ 5 - 0
sd-business/src/main/java/com/sd/business/entity/order/po/OrderInfo.java

@@ -225,6 +225,11 @@ public class OrderInfo extends BasePo {
     private String tag;
 
     /**
+     * 对账单id
+     */
+    private Long statementOfAccountId;
+
+    /**
      * 备注
      */
     private String remark;

+ 5 - 0
sd-business/src/main/java/com/sd/business/entity/order/po/OrderSku.java

@@ -103,6 +103,11 @@ public class OrderSku extends BasePo {
     private BigDecimal packagingMaterialCost;
 
     /**
+     * 定制加工类型
+     */
+    private String customProcessingType;
+
+    /**
      * 打印 1单面 2 双面
      */
     @NotNull(message = "打印类型不能为空")

+ 5 - 0
sd-business/src/main/java/com/sd/business/entity/order/vo/SkuSpecPriceVo.java

@@ -15,6 +15,11 @@ public class SkuSpecPriceVo {
     private BigDecimal customProcessingFee;
 
     /**
+     * 定制加工类型
+     */
+    private String customProcessingType;
+
+    /**
      * 代发费
      */
     private BigDecimal lssueFee;

+ 0 - 12
sd-business/src/main/java/com/sd/business/entity/statement/po/StatementOfAccount.java

@@ -6,7 +6,6 @@ import lombok.Getter;
 import lombok.Setter;
 
 import javax.validation.constraints.NotNull;
-import java.math.BigDecimal;
 import java.util.Date;
 
 /**
@@ -34,12 +33,6 @@ public class StatementOfAccount extends BasePo {
     private Long departmentId;
 
     /**
-     * 对账金额
-     */
-    @NotNull(message = "对账金额不能为空")
-    private BigDecimal amount;
-
-    /**
      * 对账时间段开始时间
      */
     private Date timePeriodBegin;
@@ -50,11 +43,6 @@ public class StatementOfAccount extends BasePo {
     private Date timePeriodEnd;
 
     /**
-     * 订单id关联
-     */
-    private String orderIdJoin;
-
-    /**
      * 类型 1用户自建 2系统生成
      */
     private Integer type;

+ 74 - 0
sd-business/src/main/java/com/sd/business/entity/statement/vo/DocumentByBomVo.java

@@ -0,0 +1,74 @@
+package com.sd.business.entity.statement.vo;
+
+import lombok.Builder;
+import lombok.Getter;
+import lombok.Setter;
+
+import java.math.BigDecimal;
+
+@Getter
+@Setter
+@Builder
+public class DocumentByBomVo {
+
+    /**
+     * bom规格id
+     */
+    private Long bomSpecId;
+
+    /**
+     * bom品号
+     */
+    private String bomSpecCode;
+
+    /**
+     * bom品名
+     */
+    private String bomSpecName;
+
+    /**
+     * 数量
+     */
+    private BigDecimal quantity;
+
+    /**
+     * bom单价
+     */
+    private BigDecimal unitPrice;
+
+    /**
+     * 激光logo汇总
+     */
+    private BigDecimal laserLogoSummary;
+
+    /**
+     * 激光线粒体汇总
+     */
+    private BigDecimal laserMitochondrialSummary;
+
+    /**
+     * 代发费汇总
+     */
+    private BigDecimal lssueFeeSummary;
+
+    /**
+     * 快递包材费汇总
+     */
+    private BigDecimal deliveryMaterialsFeeSummary;
+
+    /**
+     * 包装人工费汇总
+     */
+    private BigDecimal packingLaborSummary;
+
+    /**
+     * 小计
+     */
+    private BigDecimal subtotal;
+
+    /**
+     * 合计
+     */
+    private BigDecimal total;
+
+}

+ 3 - 2
sd-business/src/main/java/com/sd/business/entity/statement/vo/StatementOfAccountVo.java

@@ -5,6 +5,7 @@ import com.sd.business.entity.statement.po.StatementOfAccount;
 import lombok.Getter;
 import lombok.Setter;
 
+import java.math.BigDecimal;
 import java.util.List;
 
 /**
@@ -23,9 +24,9 @@ public class StatementOfAccountVo extends StatementOfAccount {
     private String departmentName;
 
     /**
-     * 订单编号关联
+     * 对账金额
      */
-    private String orderCodeJoin;
+    private BigDecimal amount;
 
     /**
      * 订单数量

+ 1 - 0
sd-business/src/main/java/com/sd/business/service/order/impl/OrderServiceImpl.java

@@ -255,6 +255,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderInfo> implem
                             && dto.getQuantity().compareTo(priceBillingStandardDetail.getSectionMin()) >= 0
                             && dto.getQuantity().compareTo(priceBillingStandardDetail.getSectionMax()) < 0) {
                         skuSpecPriceVo.setCustomProcessingFee(chargePrice);
+                        skuSpecPriceVo.setCustomProcessingType(chargeItem);
                     }
             }
         }

+ 7 - 1
sd-business/src/main/java/com/sd/business/service/statement/StatementOfAccountService.java

@@ -6,6 +6,7 @@ import com.sd.business.entity.statement.dto.FileUploadDto;
 import com.sd.business.entity.statement.dto.StatementOfAccountDto;
 import com.sd.business.entity.statement.dto.StatementOfAccountSelectDto;
 import com.sd.business.entity.statement.po.StatementOfAccount;
+import com.sd.business.entity.statement.vo.DocumentByBomVo;
 import com.sd.business.entity.statement.vo.DocumentBySkuVo;
 import com.sd.business.entity.statement.vo.ReconciliationDetailVo;
 import com.sd.business.entity.statement.vo.StatementOfAccountVo;
@@ -56,6 +57,11 @@ public interface StatementOfAccountService extends BaseService<StatementOfAccoun
     /**
      * sku对账
      */
-    List<DocumentBySkuVo> getDocumentBySku(Long id);
+    List<DocumentBySkuVo> getDocumentBySku(Long statementOfAccountId);
+
+    /**
+     * bom对账
+     */
+    List<DocumentByBomVo> getDocumentByBom(Long statementOfAccountId);
 
 }

+ 148 - 78
sd-business/src/main/java/com/sd/business/service/statement/impl/StatementOfAccountServiceImpl.java

@@ -2,8 +2,8 @@ package com.sd.business.service.statement.impl;
 
 import cn.hutool.core.convert.Convert;
 import cn.hutool.core.util.ObjectUtil;
-import cn.hutool.core.util.StrUtil;
 import com.baomidou.dynamic.datasource.annotation.DSTransactional;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.fjhx.file.entity.FileInfoVo;
@@ -16,10 +16,12 @@ import com.sd.business.entity.statement.dto.FileUploadDto;
 import com.sd.business.entity.statement.dto.StatementOfAccountDto;
 import com.sd.business.entity.statement.dto.StatementOfAccountSelectDto;
 import com.sd.business.entity.statement.po.StatementOfAccount;
+import com.sd.business.entity.statement.vo.DocumentByBomVo;
 import com.sd.business.entity.statement.vo.DocumentBySkuVo;
 import com.sd.business.entity.statement.vo.ReconciliationDetailVo;
 import com.sd.business.entity.statement.vo.StatementOfAccountVo;
 import com.sd.business.mapper.statement.StatementOfAccountMapper;
+import com.sd.business.service.bom.BomSpecService;
 import com.sd.business.service.department.DepartmentService;
 import com.sd.business.service.order.OrderService;
 import com.sd.business.service.order.OrderSkuService;
@@ -29,7 +31,9 @@ import com.sd.business.util.CodeEnum;
 import com.sd.framework.util.Assert;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
+import java.math.BigDecimal;
 import java.util.*;
 import java.util.function.Function;
 import java.util.stream.Collectors;
@@ -58,6 +62,9 @@ public class StatementOfAccountServiceImpl extends ServiceImpl<StatementOfAccoun
     @Autowired
     private SkuSpecService skuSpecService;
 
+    @Autowired
+    private BomSpecService bomSpecService;
+
     @Override
     public Page<StatementOfAccountVo> getPage(StatementOfAccountSelectDto dto) {
 
@@ -79,41 +86,32 @@ public class StatementOfAccountServiceImpl extends ServiceImpl<StatementOfAccoun
         Map<Long, List<FileInfoVo>> receiptFileMap = ObsFileUtil.getFileMap(idList, 1);
         Map<Long, List<FileInfoVo>> proofFileMap = ObsFileUtil.getFileMap(idList, 2);
 
-        List<String> orderIdList = records.stream()
-                .map(StatementOfAccount::getOrderIdJoin)
-                .filter(StrUtil::isNotBlank)
-                .flatMap(item -> Arrays.stream(item.split(",")))
-                .distinct()
-                .collect(Collectors.toList());
-
-        Map<Long, String> orderIdCodeMap;
-        if (orderIdList.size() == 0) {
-            orderIdCodeMap = orderService.mapKV(BaseIdPo::getId, OrderInfo::getCode,
-                    q -> q.select(BaseIdPo::getId, OrderInfo::getCode).in(BaseIdPo::getId, orderIdList));
-        } else {
-            orderIdCodeMap = new HashMap<>();
-        }
+        Map<Long, Map<String, Object>> map = orderService.listMaps(Wrappers.<OrderInfo>query()
+                        .select(
+                                "statement_of_account_id as statementOfAccountId",
+                                "sum(total_amount) as amount",
+                                "count(0) as orderNum"
+                        )
+                        .in("statement_of_account_id", idList)
+                        .groupBy("statement_of_account_id")
+                )
+                .stream()
+                .collect(Collectors.toMap(
+                        item -> Convert.toLong(item.get("statementOfAccountId")),
+                        Function.identity()
+                ));
 
         for (StatementOfAccountVo record : records) {
             record.setReceiptFileList(receiptFileMap.getOrDefault(record.getId(), Collections.emptyList()));
             record.setProofFileList(proofFileMap.getOrDefault(record.getId(), Collections.emptyList()));
-
-            String orderIdJoin = record.getOrderIdJoin();
-            StringJoiner codeJoiner = new StringJoiner(",");
-            int orderNum = 0;
-            for (String orderId : orderIdJoin.split(",")) {
-                if (StrUtil.isBlank(orderId)) {
-                    continue;
-                }
-                String orderCode = orderIdCodeMap.get(Convert.toLong(orderId));
-                if (StrUtil.isBlank(orderCode)) {
-                    continue;
-                }
-                codeJoiner.add(orderCode);
-                orderNum++;
+            Map<String, Object> tempMap = map.get(record.getId());
+            if (tempMap == null) {
+                record.setOrderNum(0);
+                record.setAmount(BigDecimal.ZERO);
+            } else {
+                record.setOrderNum(Convert.toInt(tempMap.get("orderNum"), 0));
+                record.setAmount(Convert.toBigDecimal(tempMap.get("amount"), BigDecimal.ZERO));
             }
-            record.setOrderCodeJoin(codeJoiner.toString());
-            record.setOrderNum(orderNum);
         }
 
         return page;
@@ -121,29 +119,22 @@ public class StatementOfAccountServiceImpl extends ServiceImpl<StatementOfAccoun
 
     @Override
     public List<ReconciliationDetailVo> detail(Long id) {
-        StatementOfAccount StatementOfAccount = this.getById(id);
-        String orderIdJoin = StatementOfAccount.getOrderIdJoin();
-
-        List<ReconciliationDetailVo> result = Arrays.stream(orderIdJoin.split(","))
-                .map(Convert::toLong)
-                .filter(ObjectUtil::isNotNull)
-                .distinct()
-                .map(item -> ReconciliationDetailVo.builder().orderId(item).build())
-                .collect(Collectors.toList());
-
-        orderService.attributeAssign(result, ReconciliationDetailVo::getOrderId, (item, order) -> {
-            item.setDepartmentId(order.getDepartmentId());
-            item.setOrderCode(order.getCode());
-            item.setTotalAmount(order.getTotalAmount());
-            item.setProductTotalAmount(order.getProductTotalAmount());
-            item.setCustomProcessingFee(order.getCustomProcessingFee());
-            item.setLssueFee(order.getLssueFee());
-            item.setDeliveryMaterialsFee(order.getDeliveryMaterialsFee());
-            item.setPackingLabor(order.getPackingLabor());
-            item.setPackagingMaterialCost(order.getPackagingMaterialCost());
-        });
 
-        result = result.stream().filter(item -> ObjectUtil.isNotEmpty(item.getOrderCode())).collect(Collectors.toList());
+        List<OrderInfo> orderInfoList = orderService.list(q -> q.eq(OrderInfo::getStatementOfAccountId, id));
+
+        List<ReconciliationDetailVo> result = orderInfoList.stream().map(item -> ReconciliationDetailVo.builder()
+                .orderId(item.getId())
+                .departmentId(item.getDepartmentId())
+                .orderCode(item.getCode())
+                .totalAmount(item.getTotalAmount())
+                .productTotalAmount(item.getProductTotalAmount())
+                .customProcessingFee(item.getCustomProcessingFee())
+                .lssueFee(item.getLssueFee())
+                .deliveryMaterialsFee(item.getDeliveryMaterialsFee())
+                .packingLabor(item.getPackingLabor())
+                .packagingMaterialCost(item.getPackagingMaterialCost())
+                .build()
+        ).collect(Collectors.toList());
 
         departmentService.attributeAssign(result, ReconciliationDetailVo::getDepartmentId, (item, department) -> {
             item.setDepartmentName(department.getName());
@@ -152,36 +143,57 @@ public class StatementOfAccountServiceImpl extends ServiceImpl<StatementOfAccoun
         return result;
     }
 
+    @Transactional(rollbackFor = Exception.class)
     @Override
     public void add(StatementOfAccountDto statementOfAccountDto) {
 
-        String orderIdJoin = statementOfAccountDto.getOrderIdList()
-                .stream()
-                .map(Convert::toStr)
-                .collect(Collectors.joining(","));
+        List<Long> orderIdList = statementOfAccountDto.getOrderIdList();
 
         statementOfAccountDto.setCode(CodeEnum.STATEMENT_OF_ACCOUNT_CODE.getCode());
         statementOfAccountDto.setType(1);
-        statementOfAccountDto.setOrderIdJoin(orderIdJoin);
         this.save(statementOfAccountDto);
+
+        if (ObjectUtil.isNotEmpty(orderIdList)) {
+            orderService.update(q -> q
+                    .in(BaseIdPo::getId, orderIdList)
+                    .set(OrderInfo::getStatementOfAccountId, statementOfAccountDto.getId())
+            );
+        }
+
     }
 
+    @Transactional(rollbackFor = Exception.class)
     @Override
     public void edit(StatementOfAccountDto statementOfAccountDto) {
+        Long statementOfAccountId = statementOfAccountDto.getId();
+        List<Long> orderIdList = statementOfAccountDto.getOrderIdList();
 
-        String orderIdJoin = statementOfAccountDto.getOrderIdList()
-                .stream()
-                .map(Convert::toStr)
-                .collect(Collectors.joining(","));
+        Assert.notNull(statementOfAccountId, "对账单id不能为空");
+
+        if (ObjectUtil.isNotEmpty(orderIdList)) {
+            orderService.update(q -> q
+                    .in(BaseIdPo::getId, orderIdList)
+                    .set(OrderInfo::getStatementOfAccountId, statementOfAccountId)
+            );
+        }
 
-        statementOfAccountDto.setOrderIdJoin(orderIdJoin);
+        orderService.update(q -> q
+                .eq(OrderInfo::getStatementOfAccountId, statementOfAccountId)
+                .notIn(ObjectUtil.isNotEmpty(orderIdList), BaseIdPo::getId, orderIdList)
+                .set(OrderInfo::getStatementOfAccountId, null)
+        );
 
-        this.updateById(statementOfAccountDto);
     }
 
     @DSTransactional
     @Override
     public void delete(Long id) {
+
+        orderService.update(q -> q
+                .eq(OrderInfo::getStatementOfAccountId, id)
+                .set(OrderInfo::getStatementOfAccountId, null)
+        );
+
         this.removeById(id);
         ObsFileUtil.removeFile(id);
     }
@@ -192,51 +204,49 @@ public class StatementOfAccountServiceImpl extends ServiceImpl<StatementOfAccoun
     }
 
     @Override
-    public List<DocumentBySkuVo> getDocumentBySku(Long id) {
-
-        Assert.notNull(id, "对账单id不能为空");
-        StatementOfAccount statementOfAccount = getById(id);
-        Assert.notNull(statementOfAccount, "没有找到对账单");
+    public List<DocumentBySkuVo> getDocumentBySku(Long statementOfAccountId) {
 
-        List<String> ordedrIdList = Arrays.stream(statementOfAccount.getOrderIdJoin().split(","))
-                .distinct().collect(Collectors.toList());
+        // 获取订单id列表
+        List<Long> ordedrIdList = getOrderIdList(statementOfAccountId);
 
         if (ordedrIdList.size() == 0) {
             return Collections.emptyList();
         }
 
+        // 获取订单sku
         List<OrderSku> orderSkuList = orderSkuService.list(q -> q.in(OrderSku::getOrderId, ordedrIdList));
 
         // 生成结果集
-        List<DocumentBySkuVo> result = orderSkuList.stream()
+        List<DocumentBySkuVo> documentBySkuVoList = orderSkuList.stream()
                 .map(item -> DocumentBySkuVo.builder()
                         .skuSpecId(item.getSkuId())
                         .quantity(item.getQuantity())
-                        .unitPrice(item.getUnitPrice())
-                        .subtotal(item.getUnitPrice()
+                        .unitPrice(item.getUnitPrice()
                                 .add(item.getCustomProcessingFee())
                                 .add(item.getLssueFee())
                                 .add(item.getDeliveryMaterialsFee())
                                 .add(item.getPackingLabor())
                                 .add(item.getPackagingMaterialCost()))
                         .build())
-                .peek(item -> item.setTotal(item.getQuantity().multiply(item.getSubtotal())))
+                .peek(item -> item.setSubtotal(item.getQuantity().multiply(item.getUnitPrice())))
+                .peek(item -> item.setTotal(item.getSubtotal()))
                 .collect(Collectors.toList());
 
         // 赋值sku规格品名和品号
-        skuSpecService.attributeAssign(result, DocumentBySkuVo::getSkuSpecId, (item, skuSpec) -> {
+        skuSpecService.attributeAssign(documentBySkuVoList, DocumentBySkuVo::getSkuSpecId, (item, skuSpec) -> {
             item.setSkuSpecCode(skuSpec.getCode());
             item.setSkuSpecName(skuSpec.getName());
         });
 
         // 合并单价、小计、sku规格id系统的对账数据
-        Collection<DocumentBySkuVo> documentBySkuVoCollection = result.stream()
+        Collection<DocumentBySkuVo> documentBySkuVoCollection = documentBySkuVoList.stream()
                 .sorted(Comparator.comparing(DocumentBySkuVo::getSkuSpecCode))
                 .collect(Collectors.toMap(
-                        item -> item.getUnitPrice() + ":" + item.getSubtotal() + ":" + item.getSkuSpecId(),
+                        item -> item.getUnitPrice() + ":" + item.getSkuSpecId(),
                         Function.identity(),
                         (v1, v2) -> {
                             v1.setQuantity(v2.getQuantity());
+                            v1.setSubtotal(v2.getSubtotal());
                             v1.setTotal(v2.getTotal());
                             return v1;
                         }
@@ -245,5 +255,65 @@ public class StatementOfAccountServiceImpl extends ServiceImpl<StatementOfAccoun
         return new ArrayList<>(documentBySkuVoCollection);
     }
 
+    @Override
+    public List<DocumentByBomVo> getDocumentByBom(Long statementOfAccountId) {
+
+        // 获取订单id列表
+        List<Long> ordedrIdList = getOrderIdList(statementOfAccountId);
+
+        if (ordedrIdList.size() == 0) {
+            return Collections.emptyList();
+        }
+
+        // 获取订单sku
+        List<OrderSku> orderSkuList = orderSkuService.list(q -> q.in(OrderSku::getOrderId, ordedrIdList));
+
+        List<DocumentByBomVo> result = orderSkuList.stream()
+                .map(item -> DocumentByBomVo.builder()
+                        .bomSpecId(item.getBomSpecId())
+                        .quantity(item.getQuantity())
+                        .unitPrice(item.getUnitPrice())
+                        .laserLogoSummary((Objects.equals(item.getCustomProcessingType(), "20")
+                                ? item.getCustomProcessingFee() : BigDecimal.ZERO).multiply(item.getQuantity()))
+                        .laserMitochondrialSummary((Objects.equals(item.getCustomProcessingType(), "10")
+                                ? item.getCustomProcessingFee() : BigDecimal.ZERO).multiply(item.getQuantity()))
+                        .lssueFeeSummary(item.getLssueFee().multiply(item.getQuantity()))
+                        .deliveryMaterialsFeeSummary(item.getDeliveryMaterialsFee().multiply(item.getQuantity()))
+                        .packingLaborSummary(item.getPackingLabor().multiply(item.getQuantity()))
+                        .build())
+                .collect(Collectors.toList());
+
+        bomSpecService.attributeAssign(result, DocumentByBomVo::getBomSpecId, (item, bomSpec) -> {
+            item.setBomSpecCode(bomSpec.getCode());
+            item.setBomSpecName(bomSpec.getName());
+        });
+
+        Collection<DocumentByBomVo> documentByBomVoCollection = result.stream()
+                .sorted(Comparator.comparing(DocumentByBomVo::getBomSpecCode))
+                .collect(Collectors.toMap(
+                        item -> item.getUnitPrice() + ":" + item.getBomSpecId(),
+                        Function.identity(),
+                        (v1, v2) -> {
+
+                            return v1;
+                        }
+                )).values();
+
+
+        result = new ArrayList<>(documentByBomVoCollection);
+
+
+        return result;
+    }
+
+
+    /**
+     * 根据对账单id获取对账订单id列表
+     */
+    private List<Long> getOrderIdList(Long statementOfAccountId) {
+        Assert.notNull(statementOfAccountId, "对账单id不能为空");
+        List<OrderInfo> orderList = orderService.list(q -> q.eq(OrderInfo::getStatementOfAccountId, statementOfAccountId));
+        return orderList.stream().map(BaseIdPo::getId).collect(Collectors.toList());
+    }
 
 }

+ 1 - 1
sd-starter/src/main/java/com/sd/SdApplication.java

@@ -18,7 +18,7 @@ public class SdApplication {
 
         log.info("\r\n==================================================================\r\n"
                 + ">>>>> " + SdApplication.class.getSimpleName() + " is success!\r\n"
-                + "==================================================================");
+                + "==================================================================\r\n");
 
     }
 

+ 1 - 0
sd-wln/src/main/java/com/sd/wln/service/impl/WlnOrderServiceImpl.java

@@ -580,6 +580,7 @@ public class WlnOrderServiceImpl implements WlnOrderService {
                             && Objects.equals(chargeItem, skuSpec.getMachinedPanel())
                             && orderSku.getQuantity().compareTo(priceBillingStandardDetail.getSectionMin()) >= 0
                             && orderSku.getQuantity().compareTo(priceBillingStandardDetail.getSectionMax()) < 0) {
+                        orderSku.setCustomProcessingType(chargeItem);
                         orderSku.setCustomProcessingFee(chargePrice);
                     }
             }

+ 33 - 44
sd-wln/src/main/java/com/sd/wln/service/impl/WlnStatementOfAccountImpl.java

@@ -1,8 +1,9 @@
 package com.sd.wln.service.impl;
 
 import cn.hutool.core.convert.Convert;
+import cn.hutool.core.date.DateTime;
 import cn.hutool.core.date.DateUtil;
-import com.ruoyi.common.core.domain.BaseIdPo;
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 import com.ruoyi.common.core.domain.BasePo;
 import com.sd.business.entity.order.enums.OrderStatusEnum;
 import com.sd.business.entity.order.po.OrderInfo;
@@ -17,9 +18,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
-import java.math.BigDecimal;
 import java.util.*;
-import java.util.function.Function;
 import java.util.stream.Collectors;
 
 @Service
@@ -34,16 +33,19 @@ public class WlnStatementOfAccountImpl implements WlnStatementOfAccount {
     @Autowired
     private OrderService orderService;
 
-    @Transactional
+    @Transactional(rollbackFor = Exception.class)
     @Override
     public void createStatementOfAccount() {
         Date date = new Date();
+        DateTime beginDay = DateUtil.beginOfDay(date);
+        DateTime endDay = DateUtil.endOfDay(date);
 
-        List<OrderInfo> editOrderList = new ArrayList<>();
+        List<StatementOfAccount> saveStatementOfAccountList = new ArrayList<>();
+        List<OrderInfo> editOrderInfoList = new ArrayList<>();
 
         // 获取当天出库单
-        List<OutboundOrder> list = outboundOrderService.list(q -> q
-                .between(BasePo::getCreateTime, DateUtil.beginOfDay(date), DateUtil.endOfDay(date)));
+        List<OutboundOrder> list = outboundOrderService.list(q -> q.between(BasePo::getCreateTime, beginDay, endDay));
+
         if (list.size() == 0) {
             return;
         }
@@ -57,32 +59,22 @@ public class WlnStatementOfAccountImpl implements WlnStatementOfAccount {
                 .in(OrderInfo::getWlnCode, map.keySet())
                 .ge(OrderInfo::getStatus, OrderStatusEnum.IN_PRODUCTION.getKey())
                 .notIn(OrderInfo::getStatus, OrderStatusEnum.DELETE.getKey(), OrderStatusEnum.EXCEPTION.getKey())
+                .isNull(OrderInfo::getStatementOfAccountId)
         );
 
-        // 循环订单,把生产中的订单改为生产完成
-        for (OrderInfo orderInfo : orderList) {
-            Integer status = orderInfo.getStatus();
-            if (status.equals(OrderStatusEnum.IN_PRODUCTION.getKey())) {
-                OrderInfo tempOrderInfo = new OrderInfo();
-                tempOrderInfo.setStatus(OrderStatusEnum.COMPLETION_PRODUCTION.getKey());
-                tempOrderInfo.setShippingTime(map.get(orderInfo.getWlnCode()));
-                editOrderList.add(tempOrderInfo);
-            }
+        if (orderList.size() == 0) {
+            return;
         }
 
         // 生成对账单
-        List<StatementOfAccount> statementOfAccountList = statementOfAccountService.list(q -> q
-                .eq(StatementOfAccount::getType, 2)
-                .between(BasePo::getCreateTime, DateUtil.beginOfDay(date), DateUtil.endOfDay(date))
-        );
-        Map<Long, StatementOfAccount> statementOfAccountMap = statementOfAccountList.stream()
-                .collect(Collectors.toMap(StatementOfAccount::getDepartmentId, Function.identity()));
+        Map<Long, StatementOfAccount> statementOfAccountMap = statementOfAccountService.mapKEntity(
+                StatementOfAccount::getDepartmentId,
+                q -> q.eq(StatementOfAccount::getType, 2).between(BasePo::getCreateTime, beginDay, endDay));
 
         Map<String, String> codeMap = new HashMap<>(1);
-
         Map<Long, List<OrderInfo>> orderByDepartmentIdMap = orderList.stream().collect(Collectors.groupingBy(OrderInfo::getDepartmentId));
+        orderByDepartmentIdMap.forEach((departmentId, tempOrderList) -> {
 
-        orderByDepartmentIdMap.forEach((departmentId, order) -> {
             StatementOfAccount statementOfAccount = statementOfAccountMap.computeIfAbsent(departmentId, item -> {
                 String code = codeMap.get("code");
                 if (code == null) {
@@ -91,37 +83,34 @@ public class WlnStatementOfAccountImpl implements WlnStatementOfAccount {
                     int number = Convert.toInt(code.substring(code.length() - 6));
                     codeMap.put("code", code.substring(0, code.length() - 6) + String.format("%06d", number + 1));
                 }
-
                 StatementOfAccount tempStatementOfAccount = new StatementOfAccount();
+                tempStatementOfAccount.setId(IdWorker.getId());
                 tempStatementOfAccount.setCode(codeMap.get("code"));
                 tempStatementOfAccount.setDepartmentId(departmentId);
-                tempStatementOfAccount.setTimePeriodBegin(DateUtil.beginOfDay(date));
-                tempStatementOfAccount.setTimePeriodEnd(DateUtil.endOfDay(date));
+                tempStatementOfAccount.setTimePeriodBegin(beginDay);
+                tempStatementOfAccount.setTimePeriodEnd(endDay);
                 tempStatementOfAccount.setType(2);
+                saveStatementOfAccountList.add(tempStatementOfAccount);
                 return tempStatementOfAccount;
             });
 
-            String orderIdJoin = order.stream()
-                    .map(BaseIdPo::getId)
-                    .distinct()
-                    .map(Convert::toStr)
-                    .collect(Collectors.joining(","));
-            statementOfAccount.setOrderIdJoin(orderIdJoin);
-
-            BigDecimal amount = order.stream()
-                    .map(OrderInfo::getTotalAmount)
-                    .reduce(BigDecimal.ZERO, BigDecimal::add);
-            statementOfAccount.setAmount(amount);
+            for (OrderInfo order : tempOrderList) {
+                OrderInfo editOrderInfo = new OrderInfo();
+                editOrderInfo.setId(order.getId());
+                editOrderInfo.setStatementOfAccountId(statementOfAccount.getId());
+                if (order.getStatus().equals(OrderStatusEnum.IN_PRODUCTION.getKey())) {
+                    editOrderInfo.setStatus(OrderStatusEnum.COMPLETION_PRODUCTION.getKey());
+                    editOrderInfo.setShippingTime(map.get(order.getWlnCode()));
+                }
+                editOrderInfoList.add(editOrderInfo);
+            }
 
         });
 
-        if (editOrderList.size() > 0) {
-            orderService.updateBatchById(editOrderList);
-        }
+        orderService.updateBatchById(editOrderInfoList);
 
-        List<StatementOfAccount> editStatementOfAccountList = new ArrayList<>(statementOfAccountMap.values());
-        if (editStatementOfAccountList.size() > 0) {
-            statementOfAccountService.saveOrUpdateBatch(editStatementOfAccountList);
+        if (saveStatementOfAccountList.size() > 0) {
+            statementOfAccountService.saveBatch(saveStatementOfAccountList);
         }
 
     }