feat: clear logistic channel name

pull/8040/head
Gauthier LO 2024-05-29 15:28:34 +02:00
parent bdf8bc4a1c
commit f54660d001
4 changed files with 174 additions and 0 deletions

View File

@ -0,0 +1,15 @@
package org.jeecg.modules.business.domain.api.mabang.dochangeorder;
import org.jeecg.modules.business.domain.api.mabang.Request;
public class EditLogisticRequest extends Request {
public EditLogisticRequest(EditLogisticRequestBody body) {
super(body);
}
@Override
public ChangeOrderResponse send() {
String jsonString = rawSend().getBody();
return ChangeOrderResponse.parse(jsonString);
}
}

View File

@ -0,0 +1,47 @@
package org.jeecg.modules.business.domain.api.mabang.dochangeorder;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.apache.commons.lang3.tuple.Pair;
import org.jeecg.modules.business.domain.api.mabang.RequestBody;
import java.util.HashSet;
@Data
public class EditLogisticRequestBody implements RequestBody {
private String platformOrderId;
private String logisticChannelName;
/**
* 1 : edit, 2: delete, 3: add
*/
private Integer type;
public EditLogisticRequestBody(String platformOrderId, String logisticChannelName, int type) {
this.platformOrderId = platformOrderId;
this.logisticChannelName = logisticChannelName;
this.type = type;
}
@Override
public String api() {
return "order-do-change-order";
}
@Override
public JSONObject parameters() {
JSONObject json = new JSONObject();
putNonNull(json, "platformOrderId", platformOrderId);
putNonNull(json, "myLogisticsId", logisticChannelName);
putNonNull(json, "type", type);
return json;
}
private <E> void putNonNull(JSONObject json, String key, E value) {
if (value != null) {
json.put(key, value);
}
}
}

View File

@ -0,0 +1,49 @@
package org.jeecg.modules.business.domain.api.mabang.dochangeorder;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.modules.business.domain.api.mabang.Response;
public class ChangeOrderResponse extends Response {
private final String message;
/**
* Erp order number
*/
private final String orderId;
private ChangeOrderResponse(Code status, String message, String orderId) {
super(status);
this.message = message;
this.orderId = orderId;
}
public static ChangeOrderResponse parse(String json) {
JSONObject jsonObject = JSON.parseObject(json);
String code = jsonObject.getString("code");
String message = jsonObject.getString("message");
if (code.equals("200")) {
JSONObject data = jsonObject.getJSONObject("data");
String orderId = data.getString("orderId");
return new ChangeOrderResponse(Code.SUCCESS, message, orderId);
} else {
return new ChangeOrderResponse(Code.ERROR, message, null);
}
}
public String getMessage() {
return message;
}
public String getOrderId() {
return orderId;
}
@Override
public String toString() {
return "ChangeOrderResponse{" +
"message='" + message + '\'' +
", orderId='" + orderId + '\'' +
'}';
}
}

View File

@ -0,0 +1,63 @@
package org.jeecg.modules.business.domain.job;
import lombok.extern.slf4j.Slf4j;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.jeecg.modules.business.domain.api.mabang.dochangeorder.*;
import org.jeecg.modules.business.domain.api.mabang.getorderlist.Order;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
@Slf4j
public class ClearLogisticChannelJob implements Job {
private static final Integer DEFAULT_NUMBER_OF_THREADS = 10;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
JobDataMap jobDataMap = context.getMergedJobDataMap();
String parameter = ((String) jobDataMap.get("parameter"));
List<String> platformOrderIds = new ArrayList<>();
if (parameter != null) {
try {
JSONObject jsonObject = new JSONObject(parameter);
if (!jsonObject.isNull("platformOrderIds")) {
JSONArray orderIds = jsonObject.getJSONArray("platformOrderIds");
if(orderIds == null) {
throw new RuntimeException("Empty parameter");
}
for(int i = 0; i < orderIds.length(); i++) {
platformOrderIds.add(orderIds.get(i).toString());
}
}
else {
throw new RuntimeException("platformOrderIds parameter is mandatory.");
}
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
ExecutorService executor = Executors.newFixedThreadPool(DEFAULT_NUMBER_OF_THREADS);
List<CompletableFuture<Boolean>> clearLogisticFutures = platformOrderIds.stream()
.map(orderId -> CompletableFuture.supplyAsync(() -> {
ClearLogisticRequestBody body = new ClearLogisticRequestBody(orderId);
ClearLogisticRequest request = new ClearLogisticRequest(body);
ClearLogisticResponse response = request.send();
return response.success();
}, executor))
.collect(Collectors.toList());
List<Boolean> clearResults = clearLogisticFutures.stream().map(CompletableFuture::join).collect(Collectors.toList());
long clearSuccessCount = clearResults.stream().filter(b -> b).count();
log.info("{}/{} logistic channel names cleared successfully.", clearSuccessCount, platformOrderIds.size());
}
}