feature : new Mabang Job to retrieve orders.

pull/6221/head
Gauthier LO 2023-07-06 16:15:48 +02:00
parent cdf5ce3cb6
commit 28ec71f50a
4 changed files with 76 additions and 34 deletions

View File

@ -33,8 +33,9 @@ public class OrderListRawStream implements NetworkDataStream<OrderListResponse>
if (currentResponse.getDataCount() == 0) {
return null;
}
began = true;
toSend.setCursor(currentResponse.getNextCursor());
toSend.nextPage();
began = true;
return currentResponse;
}
@ -49,8 +50,8 @@ public class OrderListRawStream implements NetworkDataStream<OrderListResponse>
throw new IllegalStateException("Calling hasNext before begin");
}
// still has page left, true
if (toSend.getPage() <= currentResponse.getTotalPage()) {
log.info("page: {}/{}, has next", toSend.getPage(), currentResponse.getTotalPage());
if (currentResponse.getHasNext()) {
log.info("page: {}, has next", toSend.getPage());
return true;
}
// no page left, false
@ -72,6 +73,7 @@ public class OrderListRawStream implements NetworkDataStream<OrderListResponse>
log.info("Sending request for page {}.", toSend.getPage());
this.currentResponse = new OrderListRequest(toSend).send();
toSend.setCursor(currentResponse.getNextCursor());
toSend.nextPage();
return this.currentResponse;
}

View File

@ -18,11 +18,13 @@ public class OrderListRequestBody implements RequestBody {
private LocalDateTime endDate;
// 1.Normal 2.Abnormal 3.All
private final static String CAN_SEND = "3";
private String cursor = "";
private Integer page = 1;
private boolean hasNext = true;
@Override
public String api() {
return "order-get-order-list";
return "order-get-order-list-new";
}
@Override
@ -35,19 +37,29 @@ public class OrderListRequestBody implements RequestBody {
putNonNull(json, datetimeType.text() + "End", endDate, formatter::format);
}
putNonNull(json, "canSend", CAN_SEND);
putNonNull(json, "page", page);
putNonNull(json, "cursor", cursor);
return json;
}
void nextPage() {
setPage(this.page + 1);
String getCursor() {
return cursor;
}
void setCursor(String cursor) {
this.cursor = cursor;
}
int getPage() {
return page;
}
void setPage(int page) {
this.page = page;
}
void nextPage() {
this.page += 1;
}
boolean getHasNext() { return hasNext; }
void setHasNext(boolean hasNext) {
this.hasNext = hasNext;
}
public OrderListRequestBody setStatus(OrderStatus status) {
this.status = status;
return this;
@ -73,8 +85,8 @@ public class OrderListRequestBody implements RequestBody {
return this;
}
public OrderListRequestBody setPage(int page) {
this.page = page;
public OrderListRequestBody setPage(String cursor) {
this.cursor = cursor;
return this;
}

View File

@ -5,15 +5,25 @@ import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.business.domain.api.mabang.Response;
import static cn.hutool.core.util.StrUtil.trim;
/**
* Immutable object
*/
@Slf4j
public class OrderListResponse extends Response {
/**
* total page number
* Response message
*/
private final int pageCount;
private String message;
/**
* Has next page
*/
private boolean hasNext;
/**
*
*/
private String nextCursor;
/**
* Total data number
*/
@ -25,9 +35,11 @@ public class OrderListResponse extends Response {
private final JSONObject rawData;
OrderListResponse(Code code, int pageCount, int dataCount, JSONArray data, JSONObject rawData) {
OrderListResponse(Code code, String message, String hasNext, String nextCursor, int dataCount, JSONArray data, JSONObject rawData) {
super(code);
this.pageCount = pageCount;
setMessage(message);
setHasNext(hasNext);
setNextCursor(nextCursor);
this.dataCount = dataCount;
this.data = data;
this.rawData = rawData;
@ -44,19 +56,37 @@ public class OrderListResponse extends Response {
public static OrderListResponse parse(JSONObject json) throws OrderListRequestErrorException {
log.debug("Constructing a response by json.");
String code = json.getString("code");
String message = json.getString("message");
if (code.equals(Code.ERROR.value))
throw new OrderListRequestErrorException(json.getString("message"));
throw new OrderListRequestErrorException(message);
JSONObject data = json.getJSONObject("data");
int pageCount = Integer.parseInt(data.getString("pageCount"));
int dataCount = Integer.parseInt(data.getString("dataCount"));
String hasNext = data.getString("hasNext");
String nextCursor = data.getString("nextCursor");
int dataCount = Integer.parseInt(data.getString("total"));
JSONArray realData = data.getJSONArray("data");
log.info("Constructed response: data contained {}, total page {}, total data {}", realData.size(), pageCount, dataCount);
return new OrderListResponse(Code.SUCCESS, pageCount, dataCount, realData, json);
log.info("Constructed response: data contained {}, total data {}", realData.size(), dataCount);
return new OrderListResponse(Code.SUCCESS, message, hasNext, nextCursor, dataCount, realData, json);
}
public int getTotalPage() {
return pageCount;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = trim(message);
}
public boolean getHasNext() {
return hasNext;
}
public void setHasNext(String hasNext) {
this.hasNext = hasNext.equals("true");
}
public String getNextCursor() {
return this.nextCursor;
}
public void setNextCursor(String nextCursor) {
this.nextCursor = nextCursor;
}
public JSONArray getData() {
@ -74,7 +104,6 @@ public class OrderListResponse extends Response {
@Override
public String toString() {
return "OrderListResponse{" +
"pageCount=" + pageCount +
", dataCount=" + dataCount +
'}';
}

View File

@ -74,10 +74,10 @@ public class RetrieveOrderListJob implements Job {
// sent request for newly paid orders
OrderListRequestBody body = new OrderListRequestBody();
body.setDatetimeType(DateType.PAID)
.setStartDate(begin)
.setEndDate(end)
.setStatus(OrderStatus.AllUnshipped);
body.setDatetimeType(DateType.PAID);
body.setStartDate(begin);
body.setEndDate(end);
body.setStatus(OrderStatus.AllUnshipped);
OrderListRawStream rawStream = new OrderListRawStream(body);
// get data in json array format
List<OrderListResponse> rawStreamAll = rawStream.all();
@ -118,11 +118,10 @@ public class RetrieveOrderListJob implements Job {
// Query orders that updated in a certain duration of time in the past.
OrderListRequestBody updatedOrderBody = new OrderListRequestBody();
updatedOrderBody
.setStartDate(begin)
.setEndDate(end)
.setDatetimeType(DateType.UPDATE)
.setStatus(OrderStatus.Pending);
updatedOrderBody.setStartDate(begin);
updatedOrderBody.setEndDate(end);
updatedOrderBody.setDatetimeType(DateType.UPDATE);
updatedOrderBody.setStatus(OrderStatus.Pending);
OrderListRawStream rawStream = new OrderListRawStream(updatedOrderBody);
OrderListStream stream = new OrderListStream(rawStream);