1
This commit is contained in:
@@ -2,6 +2,8 @@ package com.ruoyi.web.controller.system;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@@ -376,9 +378,38 @@ public class JDOrderListController extends BaseController
|
||||
}
|
||||
|
||||
/**
|
||||
* 一次性批量更新历史订单:将赔付金额大于0的订单标记为后返到账
|
||||
* 此方法只应执行一次,用于处理历史数据
|
||||
* 列表刷新后:对利润未手动的订单按规则重算,仅结果变化时落库(不解除售价/利润锁定)
|
||||
*/
|
||||
@Log(title = "JD订单同步自动利润", businessType = BusinessType.UPDATE)
|
||||
@PostMapping("/tools/sync-auto-profit")
|
||||
@SuppressWarnings("unchecked")
|
||||
public AjaxResult syncAutoProfit(@RequestBody(required = false) Map<String, Object> body) {
|
||||
if (body == null || !body.containsKey("ids")) {
|
||||
return AjaxResult.error("请传入 ids 数组");
|
||||
}
|
||||
Object raw = body.get("ids");
|
||||
if (!(raw instanceof List)) {
|
||||
return AjaxResult.error("ids 须为数组");
|
||||
}
|
||||
List<?> idList = (List<?>) raw;
|
||||
if (idList.isEmpty()) {
|
||||
Map<String, Object> empty = new HashMap<>(2);
|
||||
empty.put("updated", 0);
|
||||
return AjaxResult.success(empty);
|
||||
}
|
||||
List<Long> ids = new ArrayList<>(idList.size());
|
||||
for (Object o : idList) {
|
||||
if (o == null) {
|
||||
continue;
|
||||
}
|
||||
ids.add(((Number) o).longValue());
|
||||
}
|
||||
int n = jdOrderProfitService.syncAutoProfitIfChanged(ids);
|
||||
Map<String, Object> data = new HashMap<>(2);
|
||||
data.put("updated", n);
|
||||
return AjaxResult.success(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按 ID 批量重算售价(自动从型号配置回填)与利润(清除手动锁定后按规则计算)
|
||||
*/
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.ruoyi.jarvis.service;
|
||||
|
||||
import com.ruoyi.jarvis.domain.JDOrder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 订单利润/售价:按分销标识规则计算并写回订单对象(由列表保存前调用)。
|
||||
*/
|
||||
@@ -14,4 +16,11 @@ public interface IJDOrderProfitService {
|
||||
* 会修改传入的 {@code order}。
|
||||
*/
|
||||
void recalculate(JDOrder order);
|
||||
|
||||
/**
|
||||
* 对「利润未手动锁定」的订单按当前库内数据重算售价/利润字段;仅当计算结果与库中不一致时才 UPDATE。
|
||||
*
|
||||
* @return 实际执行 UPDATE 的条数
|
||||
*/
|
||||
int syncAutoProfitIfChanged(List<Long> ids);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.ruoyi.jarvis.service.impl;
|
||||
|
||||
import com.ruoyi.jarvis.domain.JDOrder;
|
||||
import com.ruoyi.jarvis.domain.ProductJdConfig;
|
||||
import com.ruoyi.jarvis.mapper.JDOrderMapper;
|
||||
import com.ruoyi.jarvis.service.IJDOrderProfitService;
|
||||
import com.ruoyi.jarvis.service.IProductJdConfigService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -9,15 +10,23 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
@Service
|
||||
public class JDOrderProfitServiceImpl implements IJDOrderProfitService {
|
||||
|
||||
private static final double XIANYU_NET_FACTOR = 0.984;
|
||||
private static final double MONEY_EPS = 0.009;
|
||||
|
||||
@Autowired
|
||||
private IProductJdConfigService productJdConfigService;
|
||||
|
||||
@Autowired
|
||||
private JDOrderMapper jdOrderMapper;
|
||||
|
||||
@Override
|
||||
public void recalculate(JDOrder order) {
|
||||
if (order == null) {
|
||||
@@ -116,4 +125,55 @@ public class JDOrderProfitServiceImpl implements IJDOrderProfitService {
|
||||
order.setProfit(BigDecimal.valueOf(netReceipt - cost)
|
||||
.setScale(2, RoundingMode.HALF_UP).doubleValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int syncAutoProfitIfChanged(List<Long> ids) {
|
||||
if (ids == null || ids.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
Set<Long> seen = new HashSet<>();
|
||||
int updated = 0;
|
||||
for (Long id : ids) {
|
||||
if (id == null || !seen.add(id)) {
|
||||
continue;
|
||||
}
|
||||
JDOrder order = jdOrderMapper.selectJDOrderById(id);
|
||||
if (order == null) {
|
||||
continue;
|
||||
}
|
||||
if (order.getProfitManual() != null && order.getProfitManual() == 1) {
|
||||
continue;
|
||||
}
|
||||
String oldType = order.getSellingPriceType();
|
||||
Double oldSp = order.getSellingPrice();
|
||||
Double oldProfit = order.getProfit();
|
||||
|
||||
recalculate(order);
|
||||
|
||||
if (sameNullableString(oldType, order.getSellingPriceType())
|
||||
&& sameMoney(oldSp, order.getSellingPrice())
|
||||
&& sameMoney(oldProfit, order.getProfit())) {
|
||||
continue;
|
||||
}
|
||||
order.getParams().put("applyProfitFields", Boolean.TRUE);
|
||||
updated += jdOrderMapper.updateJDOrder(order);
|
||||
}
|
||||
return updated;
|
||||
}
|
||||
|
||||
private static boolean sameNullableString(String a, String b) {
|
||||
String x = a == null ? "" : a.trim();
|
||||
String y = b == null ? "" : b.trim();
|
||||
return Objects.equals(x, y);
|
||||
}
|
||||
|
||||
private static boolean sameMoney(Double a, Double b) {
|
||||
if (a == null && b == null) {
|
||||
return true;
|
||||
}
|
||||
if (a == null || b == null) {
|
||||
return false;
|
||||
}
|
||||
return Math.abs(a - b) < MONEY_EPS;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user