From 24f7f046f8854d63839ad52d0cff34ea45a0f449 Mon Sep 17 00:00:00 2001
From: gongzuming <gongzuming>
Date: 星期二, 12 十一月 2024 13:10:17 +0800
Subject: [PATCH] 优化

---
 src/main/java/com/mzl/flower/service/payment/DeliveryOrderService.java |  233 ++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 203 insertions(+), 30 deletions(-)

diff --git a/src/main/java/com/mzl/flower/service/payment/DeliveryOrderService.java b/src/main/java/com/mzl/flower/service/payment/DeliveryOrderService.java
index 74354c9..54010dd 100644
--- a/src/main/java/com/mzl/flower/service/payment/DeliveryOrderService.java
+++ b/src/main/java/com/mzl/flower/service/payment/DeliveryOrderService.java
@@ -7,9 +7,11 @@
 import com.mzl.flower.config.security.SecurityUtils;
 import com.mzl.flower.constant.Constants;
 import com.mzl.flower.dto.request.payment.*;
+import com.mzl.flower.dto.request.report.QueryAppSupplierDTO;
 import com.mzl.flower.dto.response.flower.ParamItemDTO;
 import com.mzl.flower.dto.response.flower.StationStatisticDTO;
 import com.mzl.flower.dto.response.payment.*;
+import com.mzl.flower.dto.response.report.AppSupplierStatisticsVO;
 import com.mzl.flower.dto.response.supplier.SupplierOrderDTO;
 import com.mzl.flower.entity.payment.*;
 import com.mzl.flower.entity.supplier.Station;
@@ -21,6 +23,7 @@
 import com.mzl.flower.mapper.supplier.StationMapper;
 import com.mzl.flower.service.BaseService;
 import com.mzl.flower.service.flower.FlowerParamService;
+import com.mzl.flower.service.report.OrderReportService;
 import com.mzl.flower.utils.UUIDGenerator;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
@@ -34,6 +37,7 @@
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
+import java.time.temporal.ChronoUnit;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -72,6 +76,9 @@
 
     @Autowired
     private OrderItemSettlementService orderItemSettlementService;
+
+    @Autowired
+    private OrderReportService orderReportService;
 
     public void createDeliveryOrder(Order order) {
         String orderId = order.getId();
@@ -119,7 +126,19 @@
 
     public Page<DeliveryOrderListDTO> selectDeliveryOrderList(Page page, DeliveryOrderQueryDTO dto) {
         Supplier s = getCurrentSupplier();
-        dto.setStatusList(splitParam(dto.getStatus()));
+        if(Constants.DELIVERY_ORDER_STATUS.ARRIVED.name().equals(dto.getStatus())){
+            List<String> statusLs = new ArrayList<>();
+            statusLs.add(Constants.DELIVERY_ORDER_STATUS.ARRIVED.name());
+            statusLs.add(Constants.DELIVERY_ORDER_STATUS.CHECKED.name());
+            dto.setStatusList(statusLs);
+        } else if (Constants.DELIVERY_ORDER_STATUS.PENDING.name().equals(dto.getStatus())) {
+            List<String> statusLs = new ArrayList<>();
+            statusLs.add(Constants.DELIVERY_ORDER_STATUS.NO_LOCATION.name());
+            statusLs.add(Constants.DELIVERY_ORDER_STATUS.PENDING.name());
+            dto.setStatusList(statusLs);
+        } else {
+            dto.setStatusList(splitParam(dto.getStatus()));
+        }
         List<DeliveryOrderListDTO> ls = deliveryOrderMapper.selectDoList(page, s.getId(), dto);
         if (ls != null && ls.size() > 0) {
             List<String> dIds = new ArrayList<>();
@@ -179,6 +198,8 @@
     }
 
     public Page<DeliveryOrderStatisticsDTO> selectSupplierDoStatistics(Page page){
+
+        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
         LocalDate theDay = parseLocalDate("2024-08-03");
 
         long current = page.getCurrent();
@@ -187,7 +208,7 @@
         current = current == 0 ? 1 : current;
 
         long endDays = (current - 1) * size;
-        long beginDays = endDays + size;
+        long beginDays = endDays + size-1;
         LocalDate beginDate = now.plusDays(-beginDays);
         LocalDate endDate = now.plusDays(-endDays);
 
@@ -207,6 +228,7 @@
                     , fivePm.plusDays(-1), fivePm);
 
             DeliveryOrderStatisticsDTO dto = new DeliveryOrderStatisticsDTO();
+            dto.setId(endDate.format(formatter));
             dto.setDate(endDate);
             dto.setNum(0);
             dto.setPrice(new BigDecimal(0));
@@ -219,6 +241,14 @@
         }
 
         page.setRecords(ls);
+
+
+        // 获取当前日期
+        LocalDate today = LocalDate.now();
+        // 计算当前日期与给定日期的天数差
+        long daysBetween = ChronoUnit.DAYS.between(theDay, today);
+        page.setTotal(daysBetween);
+
         return page;
     }
 
@@ -226,13 +256,45 @@
         return deliveryOrderItemMapper.selectDoItemList(id);
     }
 
+    public List<DeliveryOrderItemDTO> getSupplierDeliveryOrderItems(PostQueryDTO dto){
+        List<String> idList = dto.getIds();
+        List<DeliveryOrderItemDTO> ls = new ArrayList<>();
+        if(idList != null && idList.size() > 0){
+            for(String id : idList){
+                ls.add(getSupplierDeliveryOrderItem(id));
+            }
+        }
+
+        return ls;
+    }
+
+    public List<DeliveryOrderItemDTO> getSupplierDeliveryOrderItemsByItemId(PostQueryDTO dto){
+        List<String> idList = dto.getIds();
+        List<DeliveryOrderItemDTO> ls = new ArrayList<>();
+        if(idList != null && idList.size() > 0){
+            for(String id : idList){
+                DeliveryOrderItem item = deliveryOrderItemMapper.selectOne(new QueryWrapper<DeliveryOrderItem>()
+                        .eq("order_item_id", id));
+                ls.add(getSupplierDeliveryOrderItem(item));
+            }
+        }
+
+        return ls;
+    }
+
     public DeliveryOrderItemDTO getSupplierDeliveryOrderItem(String id) {
+        DeliveryOrderItem item = deliveryOrderItemMapper.selectById(id);
+
+        return getSupplierDeliveryOrderItem(item);
+    }
+
+    public DeliveryOrderItemDTO getSupplierDeliveryOrderItem(DeliveryOrderItem item) {
         DeliveryOrderItemDTO dto = new DeliveryOrderItemDTO();
 
         Supplier s = getCurrentSupplier();
         dto.setSupplierName(s.getName());
 
-        DeliveryOrderItem item = deliveryOrderItemMapper.selectById(id);
+
         Order d = orderMapper.selectById(item.getOrderId());
         dto.setOrderNo(d.getOrderNo());
         dto.setWarehouseName(d.getWarehouseName());
@@ -250,7 +312,21 @@
 
         dto.setParams(parseArray(oi.getFlowerParams(), ParamItemDTO.class));
 
+        dto.setNum(oi.getNum());
+
         return dto;
+    }
+
+    public List<DeliveryOrderItemDTO> getDeliveryOrderItems(PostQueryDTO dto){
+        List<String> idList = dto.getIds();
+        List<DeliveryOrderItemDTO> ls = new ArrayList<>();
+        if(idList != null && idList.size() > 0){
+            for(String id : idList){
+                ls.add(getDeliveryOrderItem(id));
+            }
+        }
+
+        return ls;
     }
 
     public DeliveryOrderItemDTO getDeliveryOrderItem(String id) {
@@ -272,6 +348,8 @@
         dto.setFlowerCategory(oi.getFlowerCategory());
 
         dto.setParams(parseArray(oi.getFlowerParams(), ParamItemDTO.class));
+
+        dto.setNum(oi.getNum());
 
         return dto;
     }
@@ -354,7 +432,7 @@
         );
         if (items != null && items.size() > 0) {
             for (OrderItem i : items) {
-                i.setStatus(Constants.CHECK_OPERATE.ok.name());
+                i.setStatus(Constants.ORDER_ITEM_STATUS.ok.name());
                 i.setCheckTime(LocalDateTime.now());
                 orderItemMapper.updateById(i);
             }
@@ -370,6 +448,10 @@
         );
         if (count == 0) {
             Order o = orderMapper.selectById(orderId);
+            if(!Constants.ORDER_STATUS_BACKEND.COLLECTION.name().equals(o.getStatusBackend())){
+                log.warn("非待集货状态,不可设置待发货");
+                return;
+            }
             o.setStatusBackend(Constants.ORDER_STATUS_BACKEND.SEND.name());
             o.update(SecurityUtils.getUserId());
             orderMapper.updateById(o);
@@ -399,12 +481,14 @@
             }
 
             Integer orderCountToday = deliveryOrderMapper.selectCount(new QueryWrapper<DeliveryOrder>()
+                    .eq("deleted", 0)
                     .eq("supplier_id", supplierId)
                     .gt("create_time", startDate)
                     .le("create_time", endDate));
             dto.setOrderCountToday(orderCountToday == null ? 0 : orderCountToday);
 
             Integer orderCountYesterday = deliveryOrderMapper.selectCount(new QueryWrapper<DeliveryOrder>()
+                    .eq("deleted", 0)
                     .eq("supplier_id", supplierId)
                     .gt("create_time", startDate.plusDays(-1))
                     .le("create_time", endDate.plusDays(-1)));
@@ -412,13 +496,16 @@
 
             LocalDateTime ffPm = LocalDate.now().withDayOfMonth(1).atTime(17, 0, 0).plusDays(-1);
             Integer orderCountMonth = deliveryOrderMapper.selectCount(new QueryWrapper<DeliveryOrder>()
+                    .eq("deleted", 0)
                     .eq("supplier_id", supplierId)
                     .gt("create_time", ffPm));
             dto.setOrderCountMonth(orderCountMonth == null ? 0 : orderCountMonth);
 
             BigDecimal salesRate = new BigDecimal(0);
             Integer orderTotal = deliveryOrderMapper.selectCount(new QueryWrapper<DeliveryOrder>()
-                    .eq("supplier_id", supplierId));
+                    .eq("deleted", 0)
+                    .eq("supplier_id", supplierId)
+            );
             Integer salesCount = orderItemSalesMapper.getSupplierSalesCount(supplierId);
             if (orderTotal != null && orderTotal > 0) {
                 BigDecimal rate = new BigDecimal(salesCount).divide(new BigDecimal(orderTotal), 2, RoundingMode.HALF_UP);
@@ -426,11 +513,17 @@
             }
             dto.setSalesRate(salesRate);
 
-            Integer deliverCount = deliveryOrderItemMapper.getDoItemCount(supplierId, Constants.DELIVERY_ORDER_STATUS.NO_LOCATION.name(), startDate, endDate);
-            dto.setDeliverCount(deliverCount == null ? 0 : deliverCount);
+            Integer deliverCount1 = deliveryOrderItemMapper.getDoItemCount(supplierId, Constants.DELIVERY_ORDER_STATUS.NO_LOCATION.name(), startDate, endDate);
+            Integer deliverCount2 = deliveryOrderItemMapper.getDoItemCount(supplierId, Constants.DELIVERY_ORDER_STATUS.PENDING.name(), startDate, endDate);
+            Integer deliverCount = getInteger(deliverCount1) + getInteger(deliverCount2);
+            dto.setDeliverCount(deliverCount);
+
+            Integer deliverCount3 = deliveryOrderItemMapper.getDoItemCount(supplierId, Constants.DELIVERY_ORDER_STATUS.ARRIVED.name(), startDate, endDate);
+            Integer deliverCount4 = deliveryOrderItemMapper.getDoItemCount(supplierId, Constants.DELIVERY_ORDER_STATUS.CHECKED.name(), startDate, endDate);
+            Integer dc = getInteger(deliverCount3) + getInteger(deliverCount4);
+            dto.setDeliverTotal(dc);
 
             Integer deliverTotal = deliveryOrderItemMapper.getDoItemCount(supplierId, null, startDate, endDate);
-            dto.setDeliverTotal(deliverTotal == null ? 0 : deliverTotal);
             dto.setDealCountToday(deliverTotal);
 
             LocalDateTime e = LocalDate.now().withDayOfMonth(1).atTime(17, 0, 0).plusDays(-1);
@@ -441,28 +534,41 @@
             LocalDateTime s1 = endDate.plusDays(-30);
             Integer dealCountDay30 = deliveryOrderItemMapper.getDoItemCount(supplierId, null, s1, endDate);
             dto.setDealCountDay30(dealCountDay30);
+
+            // 供应商 总成交、本月成交、上月成交、今日成交、缺货(本月)、上月质检缺货、本月质检降级、上月质检降级、本月质检补货、上月质检补货
+            QueryAppSupplierDTO appDto=new QueryAppSupplierDTO();
+            appDto.setSupplierId(supplierId);
+            final AppSupplierStatisticsVO appSupplierStatistics = orderReportService.getAppSupplierStatistics(appDto);
+            BeanUtils.copyProperties(appSupplierStatistics,dto);
         }
 
         return dto;
     }
 
-    public List<StationStatisticDTO> statisticStationList(String name, LocalDateTime startDate, LocalDateTime endDate) {
+    private Integer getInteger(Integer c){
+        return c == null ? 0 : c;
+    }
+
+    public List<StationStatisticDTO> statisticStationList(String name, LocalDateTime startDate
+            , LocalDateTime endDate, String status) {
         String userId = SecurityUtils.getUserId();
         LambdaQueryWrapper<Station> lambdaQueryWrapper = new LambdaQueryWrapper<>();
-        lambdaQueryWrapper.like(Station::getUserIds, userId);
+        lambdaQueryWrapper.like(Station::getUserIds, "%" + userId + "%");
         lambdaQueryWrapper.eq(Station::getType, "0");
-        boolean hasZc = stationMapper.selectCount(lambdaQueryWrapper)>0;//当前用户是否有总仓权限,总仓能查看到所有集货站的数据
+        boolean hasZc = stationMapper.selectCount(lambdaQueryWrapper) > 0;//当前用户是否有总仓权限,总仓能查看到所有集货站的数据
+        List<String> statusList = splitParam(status);
         if(hasZc){
-            List<StationStatisticDTO> stationStatisticDTOS = deliveryOrderMapper.statisticStationList(startDate, endDate,name,null);
+            List<StationStatisticDTO> stationStatisticDTOS = deliveryOrderMapper.statisticStationList(startDate, endDate,name,null, statusList);
             return stationStatisticDTOS;
         }else{
-            List<StationStatisticDTO> stationStatisticDTOS = deliveryOrderMapper.statisticStationList(startDate, endDate,name,userId);
+            List<StationStatisticDTO> stationStatisticDTOS = deliveryOrderMapper.statisticStationList(startDate, endDate, name, userId, statusList);
             return stationStatisticDTOS;
         }
     }
 
     //////////////////////////////////////////////////////////////新接口
     public Page<DeliveryOrderList4CheckDTO> selectSupplierDoList4Check(Page page, DeliveryOrderStationQueryDTO dto) {
+        dto.setStatusList(splitParam(dto.getStatus()));
         List<DeliveryOrderList4CheckDTO> ls = deliveryOrderMapper.selectSupplierDoList4Check(page, dto);
 
         page.setRecords(ls);
@@ -473,6 +579,7 @@
         if(dto.getSupplierId() == null){
             throw new ValidationException("供应商id不能为空");
         }
+        dto.setStatusList(splitParam(dto.getStatus()));
         List<DeliveryOrder4CheckDTO> ls = deliveryOrderMapper.selectSupplierDoInfo4Check(page, dto);
 
         if(ls != null && ls.size() > 0){
@@ -520,7 +627,7 @@
         orderItemSettlementService.saveItemSettlementInfo(oi, SecurityUtils.getUserId(), Constants.S_TYPE.CHECK);
     }
 
-    private BigDecimal calculateOrderItemDeduct(String id){
+    public BigDecimal calculateOrderItemDeduct(String id){
         List<OrderItemCheck> cLs = orderItemCheckMapper.selectList(new QueryWrapper<OrderItemCheck>()
                 .eq("order_item_id", id)
                 .eq("audit_status", Constants.CHECK_AUDIT_STATUS.AGREED.name())
@@ -558,7 +665,7 @@
         return dto;
     }
 
-    public OrderItemCheckNumDTO getOtherCheck(String itemId, String type){
+    public OrderItemCheckNumDTO getOtherCheck(String itemId, String type, boolean isSupplier){
         OrderItemCheck c = orderItemCheckMapper.selectOne(new QueryWrapper<OrderItemCheck>()
                 .eq("type", type)
                 .eq("order_item_id", itemId)
@@ -570,6 +677,14 @@
             BeanUtils.copyProperties(c, dto);
             User user = userMapper.selectById(c.getCreateBy());
             dto.setCreateName(user.getNickName());
+
+            dto.setPictureList(parseArray(c.getPictures(), String.class));
+
+            if(Constants.CHECK_TYPE.lack.name().equals(c.getType()) && isSupplier){
+                OrderItem oi = orderItemMapper.selectById(itemId);
+                BigDecimal lackFeeSupplier = oi.getSupplierPrice().multiply(new BigDecimal(c.getNum()));
+                dto.setDeductAmount(lackFeeSupplier);
+            }
         }
 
         return dto;
@@ -577,38 +692,94 @@
 
     public void doReduceCheck(OrderItemCheckCommitReduceDTO dto){
         dto.setType(Constants.CHECK_TYPE.reduce.name());
-        doCheck(dto, dto.getPictureList(), dto.getTargetLevel(), dto.getDeductAmount());
+        doCheck(dto, dto.getTargetLevel(), dto.getDeductAmount());
     }
 
-    public void doCheck(OrderItemCheckCommitDTO dto
-            , List<String> pictureList, String targetLevel, BigDecimal deductAmount) {
+    public void doCheck(OrderItemCheckCommitDTO dto, String targetLevel, BigDecimal deductAmount) {
         if(dto.getNum() == null || dto.getNum() == 0){
             throw new ValidationException("处理数量不能为0");
         }
-        OrderItemCheck c = orderItemCheckMapper.selectOne(new QueryWrapper<OrderItemCheck>()
-                .eq("type", dto.getType())
-                .eq("order_item_id", dto.getOrderItemId())
-        );
+
+        List<String> pictureList = dto.getPictureList();
+        /*if(pictureList == null || pictureList.size() == 0){
+            throw new ValidationException("质检图片不能为空");
+        }*/
+
         OrderItem oi = orderItemMapper.selectById(dto.getOrderItemId());
         if(dto.getNum() > oi.getNum()){
             throw new ValidationException("处理数量不能大于商品数量");
         }
-        BigDecimal deduct = deductAmount;
-        if(deduct == null){
-            deduct = new BigDecimal(0);
-            if(Constants.CHECK_TYPE.replace.name().equals(dto.getType())){
-                deduct = oi.getSupplierPrice().multiply(new BigDecimal(dto.getNum()));
-            } else if(Constants.CHECK_TYPE.lack.name().equals(dto.getType())){
-                deduct = oi.getPrice().multiply(new BigDecimal(dto.getNum()));
+
+        Order o = orderMapper.selectById(oi.getOrderId());
+        if(StringUtils.isNotEmpty(o.getTransferId())){
+            throw new ValidationException("已质检退款,不可提交质检记录");
+        }
+
+        if(!Constants.ORDER_STATUS_BACKEND.COLLECTION.name().equals(o.getStatusBackend())
+            && !Constants.ORDER_STATUS_BACKEND.SEND.name().equals(o.getStatusBackend())){
+            throw new ValidationException("订单状态异常,不可提交质检记录");
+        }
+
+        List<OrderItemCheck> cLs = orderItemCheckMapper.selectList(new QueryWrapper<OrderItemCheck>()
+                .ne("type", dto.getType())
+                .eq("order_item_id", dto.getOrderItemId())
+        );
+        int otherNum = 0;
+        if(cLs != null && cLs.size() > 0){
+            for(OrderItemCheck c : cLs){
+                if(Constants.CHECK_AUDIT_STATUS.REJECTED.name().equals(c.getAuditStatus())){
+                    continue;
+                }
+                otherNum += c.getNum();
             }
         }
+        if(otherNum + dto.getNum() > oi.getNum()){
+            throw new ValidationException("处理总数量不能大于商品数量");
+        }
+
+        BigDecimal deduct = new BigDecimal(0);
+        BigDecimal realPrice = getAmount(oi.getRealPrice());
+        if(Constants.CHECK_TYPE.replace.name().equals(dto.getType())){
+            deduct = oi.getSupplierPrice().multiply(new BigDecimal(dto.getNum()));
+        } else if(Constants.CHECK_TYPE.lack.name().equals(dto.getType())){
+            deduct = realPrice.multiply(new BigDecimal(dto.getNum()));
+            //质检退款,按照这个单价扣,如果3扎都缺货,按照总的金额退
+            //缺货数量等于商品总数时按真实总价退款
+            if(dto.getNum().intValue() == oi.getNum() && oi.getRealTotal() != null){
+                deduct = oi.getRealTotal();
+            }
+        } else if (Constants.CHECK_TYPE.reduce.name().equals(dto.getType())) {
+            if(deductAmount == null){
+                throw new ValidationException("降级金额不能为空");
+            }
+            deduct = deductAmount.multiply(new BigDecimal(dto.getNum()));
+            BigDecimal dt = realPrice.multiply(new BigDecimal(dto.getNum()));
+            if(deduct.compareTo(dt) > 0
+                    || (oi.getRealTotal() != null && deduct.compareTo(oi.getRealTotal()) > 0)){
+                throw new ValidationException("降级金额不能大于商品处理数量的支付的金额");
+            }
+            BigDecimal st = oi.getSupplierPrice().multiply(new BigDecimal(dto.getNum()));
+            if(deduct.compareTo(st) > 0){
+                throw new ValidationException("降级金额不能大于商品处理数量的供应商的金额");
+            }
+        }
+
+        OrderItemCheck c = orderItemCheckMapper.selectOne(new QueryWrapper<OrderItemCheck>()
+                .eq("type", dto.getType())
+                .eq("order_item_id", dto.getOrderItemId())
+        );
         if(c != null){
+            if(Constants.CHECK_AUDIT_STATUS.AGREED.name().equals(c.getAuditStatus())){
+                throw new ValidationException("已审核通过,不可修改");
+            }
             c.setCheckTime(LocalDateTime.now());
             c.setNum(dto.getNum());
             c.setRemarks(dto.getRemarks());
             c.setPictures(toJSONString(pictureList));
             c.setTargetLevel(targetLevel);
             c.setDeductAmount(deduct);
+            c.setAuditStatus(null);
+            c.setAuditTime(null);
             c.update(SecurityUtils.getUserId());
             orderItemCheckMapper.updateById(c);
         } else {
@@ -628,7 +799,7 @@
         }
     }
 
-    public void checkOrderItemStatus(String id){
+    public String checkOrderItemStatus(String id){
         OrderItem oi = orderItemMapper.selectById(id);
         List<OrderItemCheck> cLs = orderItemCheckMapper.selectList(new QueryWrapper<OrderItemCheck>()
                 .eq("order_item_id", id)
@@ -659,6 +830,8 @@
                 deliveryOrderMapper.updateById(d);
             }
         }
+
+        return oi.getOrderId();
     }
 
     public void deleteCheckInfo(String itemId, String type){

--
Gitblit v1.9.3