|
|
@@ -1,14 +1,20 @@
|
|
|
package com.etotem.cfc.service;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
-import com.etotem.cfc.entity.*;
|
|
|
-import com.etotem.cfc.mapper.*;
|
|
|
+import com.etotem.cfc.entity.ProductOrder;
|
|
|
+import com.etotem.cfc.entity.SupplySettlement;
|
|
|
+import com.etotem.cfc.entity.SupplySettlementDetail;
|
|
|
+import com.etotem.cfc.entity.SupplySystem;
|
|
|
+import com.etotem.cfc.mapper.ProductOrderMapper;
|
|
|
+import com.etotem.cfc.mapper.SupplySettlementDetailMapper;
|
|
|
+import com.etotem.cfc.mapper.SupplySettlementMapper;
|
|
|
+import com.etotem.cfc.mapper.SupplySystemMapper;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.math.BigDecimal;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
|
|
|
@@ -16,10 +22,7 @@ import java.util.List;
|
|
|
public class SupplySettlementService extends ServiceImpl<SupplySettlementMapper, SupplySettlement> {
|
|
|
|
|
|
@Resource
|
|
|
- private SupplySettlementItemMapper supplySettlementItemMapper;
|
|
|
-
|
|
|
- @Resource
|
|
|
- private SupplierMapper supplierMapper;
|
|
|
+ private SupplySettlementDetailMapper supplySettlementDetailMapper;
|
|
|
|
|
|
@Resource
|
|
|
private SupplySystemMapper supplySystemMapper;
|
|
|
@@ -27,100 +30,89 @@ public class SupplySettlementService extends ServiceImpl<SupplySettlementMapper,
|
|
|
@Resource
|
|
|
private ProductOrderMapper productOrderMapper;
|
|
|
|
|
|
- public List<SupplySettlement> getSettlementsBySupplier(Long supplierId) {
|
|
|
+ public Page<SupplySettlement> list(Long systemId, String status, int page, int size) {
|
|
|
+ Page<SupplySettlement> pageParam = new Page<>(page, size);
|
|
|
LambdaQueryWrapper<SupplySettlement> wrapper = new LambdaQueryWrapper<>();
|
|
|
- wrapper.eq(SupplySettlement::getSupplierId, supplierId)
|
|
|
- .orderByDesc(SupplySettlement::getCreatedAt);
|
|
|
- return this.list(wrapper);
|
|
|
+ if (systemId != null) {
|
|
|
+ wrapper.eq(SupplySettlement::getSystemId, systemId);
|
|
|
+ }
|
|
|
+ if (status != null && !status.isEmpty()) {
|
|
|
+ wrapper.eq(SupplySettlement::getStatus, status);
|
|
|
+ }
|
|
|
+ wrapper.orderByDesc(SupplySettlement::getId);
|
|
|
+ return this.page(pageParam, wrapper);
|
|
|
}
|
|
|
|
|
|
- public List<SupplySettlement> getSettlementsBySystem(Long supplySystemId) {
|
|
|
- LambdaQueryWrapper<SupplySettlement> wrapper = new LambdaQueryWrapper<>();
|
|
|
- wrapper.eq(SupplySettlement::getSupplySystemId, supplySystemId)
|
|
|
- .orderByDesc(SupplySettlement::getCreatedAt);
|
|
|
- return this.list(wrapper);
|
|
|
+ public SupplySettlement detail(Long id) {
|
|
|
+ return this.getById(id);
|
|
|
}
|
|
|
|
|
|
- public List<SupplySettlementItem> getSettlementItems(Long settlementId) {
|
|
|
- LambdaQueryWrapper<SupplySettlementItem> wrapper = new LambdaQueryWrapper<>();
|
|
|
- wrapper.eq(SupplySettlementItem::getSettlementId, settlementId);
|
|
|
- return supplySettlementItemMapper.selectList(wrapper);
|
|
|
+ public List<SupplySettlementDetail> detailItems(Long settlementId) {
|
|
|
+ LambdaQueryWrapper<SupplySettlementDetail> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(SupplySettlementDetail::getSettlementId, settlementId);
|
|
|
+ return supplySettlementDetailMapper.selectList(wrapper);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Calculate and create a settlement for a completed order.
|
|
|
- * Called when an order is marked as completed/confirmed.
|
|
|
- */
|
|
|
@Transactional
|
|
|
- public SupplySettlement createSettlementForOrder(Long orderId) {
|
|
|
- ProductOrder order = productOrderMapper.selectById(orderId);
|
|
|
- if (order == null || order.getSupplierId() == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
+ public SupplySettlement createSettlement(Long systemId, Date periodStart, Date periodEnd, String remark) {
|
|
|
+ SupplySystem system = supplySystemMapper.selectById(systemId);
|
|
|
+ if (system == null) return null;
|
|
|
|
|
|
- Supplier supplier = supplierMapper.selectById(order.getSupplierId());
|
|
|
- if (supplier == null || supplier.getSupplySystemId() == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
+ // Query all completed orders in the period for this system
|
|
|
+ LambdaQueryWrapper<ProductOrder> orderWrapper = new LambdaQueryWrapper<>();
|
|
|
+ orderWrapper.eq(ProductOrder::getSupplySystemId, systemId)
|
|
|
+ .ge(ProductOrder::getPaidAt, periodStart)
|
|
|
+ .le(ProductOrder::getPaidAt, periodEnd)
|
|
|
+ .ne(ProductOrder::getStatus, "CANCELLED");
|
|
|
+ List<ProductOrder> orders = productOrderMapper.selectList(orderWrapper);
|
|
|
|
|
|
- SupplySystem system = supplySystemMapper.selectById(supplier.getSupplySystemId());
|
|
|
- if (system == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
+ if (orders.isEmpty()) return null;
|
|
|
|
|
|
- BigDecimal totalAmount = BigDecimal.valueOf(order.getTotalAmount()).divide(BigDecimal.valueOf(100));
|
|
|
- BigDecimal platformRate = system.getPlatformProfitRate() != null
|
|
|
- ? system.getPlatformProfitRate() : BigDecimal.ZERO;
|
|
|
- BigDecimal platformProfit = totalAmount.multiply(platformRate);
|
|
|
- BigDecimal supplierIncome = totalAmount.subtract(platformProfit);
|
|
|
- BigDecimal commissionAmount = BigDecimal.valueOf(
|
|
|
- order.getSupplyCommission() != null ? order.getSupplyCommission() : 0)
|
|
|
- .divide(BigDecimal.valueOf(100));
|
|
|
+ int totalSales = 0;
|
|
|
+ int platformProfit = 0;
|
|
|
|
|
|
SupplySettlement settlement = new SupplySettlement();
|
|
|
- settlement.setSupplierId(order.getSupplierId());
|
|
|
- settlement.setSupplySystemId(supplier.getSupplySystemId());
|
|
|
- settlement.setOrderId(orderId);
|
|
|
- settlement.setTotalAmount(totalAmount);
|
|
|
- settlement.setPlatformProfit(platformProfit);
|
|
|
- settlement.setSupplierIncome(supplierIncome);
|
|
|
- settlement.setCommissionAmount(commissionAmount);
|
|
|
- settlement.setStatus("PENDING");
|
|
|
+ settlement.setSystemId(systemId);
|
|
|
+ settlement.setPeriodStart(periodStart);
|
|
|
+ settlement.setPeriodEnd(periodEnd);
|
|
|
+ settlement.setStatus("pending");
|
|
|
+ settlement.setRemark(remark);
|
|
|
settlement.setCreatedAt(new Date());
|
|
|
- settlement.setUpdatedAt(new Date());
|
|
|
- this.save(settlement);
|
|
|
|
|
|
- SupplySettlementItem item = new SupplySettlementItem();
|
|
|
- item.setSettlementId(settlement.getId());
|
|
|
- item.setProductId(order.getProductId());
|
|
|
- item.setProductName(order.getProductName());
|
|
|
- item.setQuantity(order.getQuantity());
|
|
|
- item.setUnitPrice(totalAmount.divide(BigDecimal.valueOf(order.getQuantity()), BigDecimal.ROUND_HALF_UP));
|
|
|
- item.setSubtotal(totalAmount);
|
|
|
- item.setCommissionRate(supplier.getCommissionRate());
|
|
|
- item.setCommissionAmount(commissionAmount);
|
|
|
- item.setCreatedAt(new Date());
|
|
|
- supplySettlementItemMapper.insert(item);
|
|
|
+ for (ProductOrder order : orders) {
|
|
|
+ int orderAmount = order.getTotalAmount() != null ? order.getTotalAmount() : 0;
|
|
|
+ int profitRate = system.getPlatformProfitRate() != null ? system.getPlatformProfitRate().multiply(java.math.BigDecimal.valueOf(10)).intValue() : 0;
|
|
|
+ int orderProfit = (orderAmount * profitRate) / 1000;
|
|
|
+
|
|
|
+ totalSales += orderAmount;
|
|
|
+ platformProfit += orderProfit;
|
|
|
+
|
|
|
+ SupplySettlementDetail detail = new SupplySettlementDetail();
|
|
|
+ detail.setSettlementId(settlement.getId());
|
|
|
+ detail.setOrderId(order.getId());
|
|
|
+ detail.setOrderNo(order.getOrderNo());
|
|
|
+ detail.setProductName(order.getProductName());
|
|
|
+ detail.setAmount(orderAmount);
|
|
|
+ detail.setPlatformFee(orderProfit);
|
|
|
+ detail.setSupplierId(order.getSupplierId());
|
|
|
+ detail.setPayout(orderAmount - orderProfit);
|
|
|
+ supplySettlementDetailMapper.insert(detail);
|
|
|
+ }
|
|
|
|
|
|
- return settlement;
|
|
|
- }
|
|
|
+ settlement.setTotalSales(totalSales);
|
|
|
+ settlement.setPlatformProfit(platformProfit);
|
|
|
+ settlement.setSupplierPayout(totalSales - platformProfit);
|
|
|
+ this.save(settlement);
|
|
|
|
|
|
- @Transactional
|
|
|
- public boolean confirmSettlement(Long id) {
|
|
|
- SupplySettlement settlement = this.getById(id);
|
|
|
- if (settlement == null) return false;
|
|
|
- settlement.setStatus("SETTLED");
|
|
|
- settlement.setSettlementDate(new Date());
|
|
|
- settlement.setUpdatedAt(new Date());
|
|
|
- return this.updateById(settlement);
|
|
|
+ return settlement;
|
|
|
}
|
|
|
|
|
|
@Transactional
|
|
|
- public boolean cancelSettlement(Long id) {
|
|
|
+ public boolean confirm(Long id) {
|
|
|
SupplySettlement settlement = this.getById(id);
|
|
|
if (settlement == null) return false;
|
|
|
- settlement.setStatus("CANCELLED");
|
|
|
- settlement.setUpdatedAt(new Date());
|
|
|
+ settlement.setStatus("settled");
|
|
|
+ settlement.setSettledAt(new Date());
|
|
|
return this.updateById(settlement);
|
|
|
}
|
|
|
}
|