|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
package com.ms.api.service.impl;
|
|
|
|
|
|
|
|
|
|
import com.ms.api.consts.AppConst;
|
|
|
|
|
import com.ms.api.consts.OrderConst;
|
|
|
|
|
import com.ms.api.consts.PurchaseOrderConst;
|
|
|
|
|
import com.ms.api.consts.StatusConst;
|
|
|
|
@ -7,6 +8,8 @@ import com.ms.api.dto.dsapi.request.*;
|
|
|
|
|
import com.ms.api.dto.dsapi.response.*;
|
|
|
|
|
import com.ms.api.dto.dsorder.*;
|
|
|
|
|
import com.ms.api.service.*;
|
|
|
|
|
import com.ms.api.tool.CommonTool;
|
|
|
|
|
import com.ms.api.tool.PurchaseTool;
|
|
|
|
|
import com.ms.dal.entity.*;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.apache.http.client.utils.DateUtils;
|
|
|
|
@ -16,6 +19,10 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
import java.util.regex.MatchResult;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
|
|
|
|
@ -72,6 +79,9 @@ public class DistributionOrderServiceImpl implements DistributionOrderService {
|
|
|
|
|
@Autowired
|
|
|
|
|
private DsApiService dsApiService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private ShopService shopService;
|
|
|
|
|
|
|
|
|
|
private void batchCreateDistributionOrders(Long shopId, BatchCreateDistributionOrdersRequestDTO request) {
|
|
|
|
|
List<PlatformOrderDTO> platformOrders = request.getPlatformOrders();
|
|
|
|
|
|
|
|
|
@ -399,12 +409,211 @@ public class DistributionOrderServiceImpl implements DistributionOrderService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buildAndUpdateNewPurchaseOrderRemark(OpOrder opOrder) {
|
|
|
|
|
OpOrderAmountDetail amountDetail = opOrderAmountDetailService.getByOrderId(opOrder.getOrderId());
|
|
|
|
|
OpOrderRedpackInfo redpackInfo = opOrderRedpackInfoService.getByOrderId(opOrder.getOrderId());
|
|
|
|
|
private HashMap<String, String> buildAndUpdateNewPurchaseOrderRemark(OpOrder opOrder) {
|
|
|
|
|
HashMap<String, String> result = new HashMap<>();
|
|
|
|
|
PurchaseSetting purchaseSetting = purchaseSettingService.getByShopId(opOrder.getShopId());
|
|
|
|
|
List<PurchaseOrder> purchaseOrders = purchaseOrderService.getListByOrderId(opOrder.getShopId(), opOrder.getOpOrderId().toString(), StatusConst.normal,false);
|
|
|
|
|
int orderPayment = opOrder.getOrderTotalAmount().intValue() + opOrder.getPostAmount().intValue() + opOrder.getCouponAmount().intValue() + amountDetail.getTalentCouponAmount() + redpackInfo.getPlatformRedpackAmount() + redpackInfo.getTalentRedpackAmount();
|
|
|
|
|
List<PurchaseOrder> purchaseOrders = purchaseOrderService.getListByOrderId(opOrder.getShopId(), opOrder.getOpOrderId().toString(), StatusConst.normal,true);
|
|
|
|
|
if (purchaseSetting.getIsAppendShopNote() == 0 && purchaseSetting.getIsAppendShopRemark() == 0 && purchaseSetting.getShopRemarkFlag() > 0) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
String newShopNote = buildAndUpdateNewShopNote(opOrder.getShopId(), opOrder.getOrderId(), purchaseSetting.getAppendShopNoteTpl(), purchaseOrders);
|
|
|
|
|
String newSellerWords = buildAndUpdateNewSellerWords(opOrder.getShopId(), opOrder.getOrderId(), purchaseSetting.getAppendShopRemarkTpl(), purchaseOrders, purchaseSetting.getShopRemarkFlag());
|
|
|
|
|
result.put("shopNote", newShopNote);
|
|
|
|
|
result.put("sellerWords", newSellerWords);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String buildAndUpdateNewSellerWords(Long shopId, String orderId, String remarkTpl, List<PurchaseOrder> purchaseOrders, Integer shopRemarkFlag) {
|
|
|
|
|
OpOrder opOrder = opOrderService.selectByOrderId(orderId);
|
|
|
|
|
String oldSellerWords = opOrder.getSellerWords();
|
|
|
|
|
String newSellerWords = "";
|
|
|
|
|
if (StringUtils.isBlank(remarkTpl)) {
|
|
|
|
|
return oldSellerWords;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String purchaseOrderRemark = buildPurchaseOrderRemark(shopId, remarkTpl, purchaseOrders);
|
|
|
|
|
|
|
|
|
|
String appChineseName = "";
|
|
|
|
|
Pattern p = Pattern.compile("【(" + appChineseName + "|0.0):[^】]*】");
|
|
|
|
|
Matcher m = p.matcher(oldSellerWords);
|
|
|
|
|
|
|
|
|
|
StringBuffer newShopNoteBuffer = new StringBuffer(oldSellerWords.length());
|
|
|
|
|
while (m.find()) {
|
|
|
|
|
m.appendReplacement(newShopNoteBuffer, purchaseOrderRemark);
|
|
|
|
|
}
|
|
|
|
|
m.appendTail(newShopNoteBuffer);
|
|
|
|
|
newSellerWords = newShopNoteBuffer.toString();
|
|
|
|
|
|
|
|
|
|
if (newSellerWords.equals(oldSellerWords)) {
|
|
|
|
|
return newSellerWords;
|
|
|
|
|
}
|
|
|
|
|
newSellerWords = newSellerWords.replaceAll(appChineseName, "0.0");
|
|
|
|
|
if (newSellerWords.equals(oldSellerWords) && shopRemarkFlag.equals(opOrder.getSellerRemarkStars())) {
|
|
|
|
|
return newSellerWords;
|
|
|
|
|
}
|
|
|
|
|
opOrderExtService.updateOpOrderShopNote(shopId, orderId, newSellerWords);
|
|
|
|
|
return newSellerWords;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String buildAndUpdateNewShopNote(Long shopId, String orderId, String remarkTpl, List<PurchaseOrder> purchaseOrders) {
|
|
|
|
|
OpOrderExt opOrderExt = opOrderExtService.getByOrderId(orderId);
|
|
|
|
|
String oldShopNote = "";
|
|
|
|
|
String newShopNote = "";
|
|
|
|
|
if (opOrderExt != null) {
|
|
|
|
|
oldShopNote = opOrderExt.getShopNote();
|
|
|
|
|
}
|
|
|
|
|
if (StringUtils.isBlank(remarkTpl)) {
|
|
|
|
|
return oldShopNote;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String purchaseOrderRemark = buildPurchaseOrderRemark(shopId, remarkTpl, purchaseOrders);
|
|
|
|
|
|
|
|
|
|
String appChineseName = "";
|
|
|
|
|
Pattern p = Pattern.compile("【(" + appChineseName + "|0.0):[^】]*】");
|
|
|
|
|
Matcher m = p.matcher(oldShopNote);
|
|
|
|
|
|
|
|
|
|
StringBuffer newShopNoteBuffer = new StringBuffer(oldShopNote.length());
|
|
|
|
|
while (m.find()) {
|
|
|
|
|
m.appendReplacement(newShopNoteBuffer, purchaseOrderRemark);
|
|
|
|
|
}
|
|
|
|
|
m.appendTail(newShopNoteBuffer);
|
|
|
|
|
newShopNote = newShopNoteBuffer.toString();
|
|
|
|
|
|
|
|
|
|
if (newShopNote.equals(oldShopNote)) {
|
|
|
|
|
return newShopNote;
|
|
|
|
|
}
|
|
|
|
|
newShopNote = newShopNote.replaceAll(appChineseName, "0.0");
|
|
|
|
|
opOrderExtService.updateOpOrderShopNote(shopId, orderId, newShopNote);
|
|
|
|
|
return newShopNote;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String buildPurchaseOrderRemark(Long shopId, String appendRemarkTpl, List<PurchaseOrder> purchaseOrders) {
|
|
|
|
|
Shop shop = shopService.getDetailById(shopId);
|
|
|
|
|
List<String> purchaseOrderRemarks = new ArrayList<>();
|
|
|
|
|
Pattern p = Pattern.compile(PurchaseTool.getRemarkTplRegPattern());
|
|
|
|
|
Matcher m = p.matcher(appendRemarkTpl);
|
|
|
|
|
List<String> fields = new ArrayList<>();
|
|
|
|
|
while (m.find()) {
|
|
|
|
|
fields.add(m.group(2));
|
|
|
|
|
}
|
|
|
|
|
boolean needProfit = false;
|
|
|
|
|
boolean needProfitRatio = false;
|
|
|
|
|
if (fields.contains(PurchaseOrderConst.TOTAL_PROFIT_FIELD)) {
|
|
|
|
|
needProfit = true;
|
|
|
|
|
}
|
|
|
|
|
if (fields.contains(PurchaseOrderConst.PROFIT_RATIO_FIELD)) {
|
|
|
|
|
needProfitRatio = true;
|
|
|
|
|
}
|
|
|
|
|
PurchaseOrder firstPurchaseOrder = purchaseOrders.iterator().next();
|
|
|
|
|
|
|
|
|
|
String profitText = "";
|
|
|
|
|
String profitRatioText = "";
|
|
|
|
|
if (needProfit || needProfitRatio) {
|
|
|
|
|
HashMap<String, Double> resuslt = getOrderProfitAndRatioByOrderId(firstPurchaseOrder.getOrderId());
|
|
|
|
|
profitText = "(总利润 ¥" + resuslt.get("orderProfitSum") + ")";
|
|
|
|
|
profitRatioText = "(利润率" + resuslt.get("orderTotalProfitRate") + "%)";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (PurchaseOrder purchaseOrder: purchaseOrders) {
|
|
|
|
|
if (StringUtils.isBlank(purchaseOrder.getPurchaseOrderSn())) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Matcher matchRes = p.matcher(appendRemarkTpl);
|
|
|
|
|
StringBuffer resString = new StringBuffer();
|
|
|
|
|
while (matchRes.find()) {
|
|
|
|
|
String result = generatePurchaseOrderRemark(matchRes.toMatchResult(), purchaseOrder, shop, profitText, profitRatioText);
|
|
|
|
|
matchRes.appendReplacement(resString, result);
|
|
|
|
|
}
|
|
|
|
|
matchRes.appendTail(resString);
|
|
|
|
|
purchaseOrderRemarks.add(resString.toString());
|
|
|
|
|
}
|
|
|
|
|
if (purchaseOrderRemarks.isEmpty()) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
String appChineseName = "";
|
|
|
|
|
return "【" + appChineseName + ":" + String.join(" / ", purchaseOrderRemarks) + "】";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String generatePurchaseOrderRemark(MatchResult result , PurchaseOrder purchaseOrder, Shop shop, String profitText, String profitRatioText) {
|
|
|
|
|
String prefix = result.group(1);
|
|
|
|
|
String field = result.group(2);
|
|
|
|
|
String suffix = result.group(3);
|
|
|
|
|
String value = "";
|
|
|
|
|
|
|
|
|
|
int totalPurchaseNum = 0;
|
|
|
|
|
for (PurchaseOrderItem item: purchaseOrder.getItems()) {
|
|
|
|
|
totalPurchaseNum += item.getPurchaseNum();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (field) {
|
|
|
|
|
case PurchaseOrderConst.PURCHASE_PLATFORM2_FIELD:
|
|
|
|
|
value = PurchaseOrderConst.getPurchasePlatformName(purchaseOrder.getPurchasePlatform());
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PurchaseOrderConst.PURCHASE_ORDER_SN_FIELD:
|
|
|
|
|
value = purchaseOrder.getPurchaseOrderSn();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PurchaseOrderConst.PURCHASE_ORDER_BUYER:
|
|
|
|
|
value = purchaseOrder.getPurchaseOrderBuyer();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PurchaseOrderConst.LOGISTICS_INFO_FIELD:
|
|
|
|
|
value = purchaseOrder.getPurchaseOrderLogisticsName() + "#" + purchaseOrder.getPurchaseOrderWaybillCode();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PurchaseOrderConst.PURCHASE_PAYMENT_FIELD:
|
|
|
|
|
value = "¥" + purchaseOrder.getPurchaseOrderPayment();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PurchaseOrderConst.PURCHASE_ORDER_SELLER_FIELD:
|
|
|
|
|
value = purchaseOrder.getPurchaseOrderSeller();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PurchaseOrderConst.PURCHASE_ORDER_NUM_FIELD:
|
|
|
|
|
value = totalPurchaseNum > 0 ? ("采购数量*" + totalPurchaseNum) : "";
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PurchaseOrderConst.SHOP_NAME_FIELD:
|
|
|
|
|
value = shop.getShopName();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PurchaseOrderConst.TOTAL_PROFIT_FIELD:
|
|
|
|
|
value = profitText;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PurchaseOrderConst.PROFIT_RATIO_FIELD:
|
|
|
|
|
value = profitRatioText;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
value = "";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return prefix + value + suffix;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private HashMap<String, Double> getOrderProfitAndRatioByOrderId(String orderId) {
|
|
|
|
|
OpOrder opOrder = opOrderService.selectByOrderId(orderId);
|
|
|
|
|
double orderTotalAmount = CommonTool.correctCentToYuan(opOrder.getOrderTotalAmount());
|
|
|
|
|
double freightPrice = CommonTool.correctCentToYuan(opOrder.getPostAmount());
|
|
|
|
|
double orderPayment = orderTotalAmount + freightPrice;
|
|
|
|
|
|
|
|
|
|
OpOrderAmountDetail amountDetail = opOrderAmountDetailService.getByOrderId(orderId);
|
|
|
|
|
double postInsuranceAmount = CommonTool.correctCentToYuan(amountDetail.getPostInsuranceAmount());
|
|
|
|
|
double platformCostAmount = CommonTool.correctCentToYuan(amountDetail.getPlatformCostAmount());
|
|
|
|
|
|
|
|
|
|
Map<String, Double> purchasePaymentInfo = purchaseOrderService.getPaymentInfoByOrderId(orderId);
|
|
|
|
|
Double purchasePayment = purchasePaymentInfo.get("purchaseOrderPayment");
|
|
|
|
|
Double orderCommission = purchasePaymentInfo.get("purchaseOrderCommission");
|
|
|
|
|
|
|
|
|
|
double orderProfitSum = orderPayment + platformCostAmount + orderCommission - purchasePayment - postInsuranceAmount;
|
|
|
|
|
double orderTotalProfitRate = orderPayment > 0 ? orderProfitSum / orderPayment * 100 : 0;
|
|
|
|
|
|
|
|
|
|
HashMap<String, Double> result = new HashMap<>();
|
|
|
|
|
result.put("orderProfitSum", orderProfitSum);
|
|
|
|
|
result.put("orderTotalProfitRate", orderTotalProfitRate);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean checkPurchaseOrderStatusCanOutStorage(PurchaseOrderRequestDTO purchaseOrderInfo, OpOrder opOrder) {
|
|
|
|
|