mirror of https://github.com/jeecgboot/jeecg-boot
Add job to create ShoumanOrder
parent
023046416d
commit
28ac56d2b7
|
@ -0,0 +1,110 @@
|
|||
package org.jeecg.modules.business.domain.api.shouman;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.Data;
|
||||
import org.jeecg.modules.business.entity.Country;
|
||||
import org.jeecg.modules.business.entity.ShoumanOrderContent;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Data
|
||||
public class OrderCreationRequestBody implements RequestBody {
|
||||
|
||||
private List<ShoumanOrderContent> orderContents;
|
||||
|
||||
private Map<String, Country> countryMap;
|
||||
|
||||
private final static String DEFAULT_SPLIT = ";";
|
||||
private final static String LINE_BREAK = "\n";
|
||||
private final static String CUSTOM = "定制";
|
||||
private final static String QUOTE = ":";
|
||||
private final static String WIA = "维亚智通";
|
||||
|
||||
public OrderCreationRequestBody(List<ShoumanOrderContent> orderContents, Map<String, Country> countryMap) {
|
||||
this.orderContents = orderContents;
|
||||
this.countryMap = countryMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String path() {
|
||||
return "/order/openapi/orders/add";
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject parameters() {
|
||||
JSONObject json = new JSONObject();
|
||||
// TODO: 2023/11/29 Change to real address
|
||||
ShoumanOrderContent anyContent = orderContents.get(0);
|
||||
putNonNull(json, "address", anyContent.getPostcode());
|
||||
putNonNull(json, "addressee", anyContent.getRecipient());
|
||||
putNonNull(json, "city", anyContent.getCity());
|
||||
String countryName = anyContent.getCountry();
|
||||
Country country = countryMap.get(countryName);
|
||||
putNonNull(json, "country", country.getNameZh());
|
||||
putNonNull(json, "countryCode", country.getCode());
|
||||
putNonNull(json, "orderId", anyContent.getPlatformOrderId());
|
||||
JSONArray outboundInfos = new JSONArray();
|
||||
BigDecimal totalPrice = BigDecimal.ZERO;
|
||||
for (ShoumanOrderContent content : orderContents) {
|
||||
JSONObject contentJson = new JSONObject();
|
||||
putNonNull(contentJson, "productName", content.getProductName());
|
||||
putNonNull(contentJson, "customerId", content.getPlatformOrderNumber());
|
||||
BigDecimal price = content.getPrice();
|
||||
putNonNull(contentJson, "price", price.toString());
|
||||
totalPrice = totalPrice.add(price);
|
||||
putNonNull(contentJson, "theImagePath", content.getImageUrl());
|
||||
putNonNull(contentJson, "comment", generateRemark(content.getRemark(), content.getCustomizationData(),
|
||||
content.getContentRecRegex(), content.getContentExtRegex()));
|
||||
putNonNull(contentJson, "sku", content.getSku());
|
||||
putNonNull(contentJson, "outboundNumder", content.getQuantity()); // Typo intended
|
||||
outboundInfos.add(contentJson);
|
||||
}
|
||||
putNonNull(json, "totalPrice", totalPrice.toString());
|
||||
putNonNull(json, "outboundInfos", outboundInfos);
|
||||
return json;
|
||||
}
|
||||
|
||||
private String generateRemark(String baseRemark, String customizationData, String contentRecRegex, String contentExtRegex) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String[] baseRemarks = baseRemark.split(DEFAULT_SPLIT);
|
||||
for (String remark : baseRemarks) {
|
||||
sb.append(remark)
|
||||
.append(LINE_BREAK);
|
||||
}
|
||||
|
||||
String[] strings = customizationData.split(DEFAULT_SPLIT);
|
||||
for (int i = 0; i < strings.length; i++) {
|
||||
String string = strings[i];
|
||||
if (string.matches(contentRecRegex)) {
|
||||
String trimmed = string.trim();
|
||||
String content = trimmed.split(contentExtRegex)[1];
|
||||
sb.append(CUSTOM)
|
||||
.append(i + 1)
|
||||
.append(QUOTE)
|
||||
.append(content)
|
||||
.append(LINE_BREAK);
|
||||
}
|
||||
}
|
||||
Calendar instance = Calendar.getInstance();
|
||||
// Add date (format MM-dd) and company name at the end
|
||||
sb.append(WIA)
|
||||
.append("(")
|
||||
.append(instance.get(Calendar.MONTH) + 1) // Starts with 0, so must add 1
|
||||
.append("-")
|
||||
.append(instance.get(Calendar.DAY_OF_MONTH))
|
||||
.append(")");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private <E> void putNonNull(JSONObject json, String key, E value) {
|
||||
if (value != null) {
|
||||
json.put(key, value);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package org.jeecg.modules.business.domain.api.shouman;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface RequestBody {
|
||||
|
||||
/**
|
||||
* API Path
|
||||
*
|
||||
* @return path
|
||||
*/
|
||||
String path();
|
||||
|
||||
Map<String, Object> parameters();
|
||||
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package org.jeecg.modules.business.domain.job;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.modules.business.domain.api.shouman.OrderCreationRequestBody;
|
||||
import org.jeecg.modules.business.entity.Country;
|
||||
import org.jeecg.modules.business.entity.Shouman.ShoumanOrder;
|
||||
import org.jeecg.modules.business.entity.ShoumanOrderContent;
|
||||
import org.jeecg.modules.business.mapper.CountryMapper;
|
||||
import org.jeecg.modules.business.mapper.PlatformOrderContentMapper;
|
||||
import org.jeecg.modules.business.service.IShoumanOrderService;
|
||||
import org.quartz.Job;
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.quartz.JobExecutionException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.util.stream.Collectors.toMap;
|
||||
|
||||
@Slf4j
|
||||
public class ShoumanOrderCreationJob implements Job {
|
||||
|
||||
@Autowired
|
||||
private PlatformOrderContentMapper platformOrderContentMapper;
|
||||
@Autowired
|
||||
private IShoumanOrderService shoumanOrderService;
|
||||
@Autowired
|
||||
private CountryMapper countryMapper;
|
||||
|
||||
@Override
|
||||
public void execute(JobExecutionContext context) throws JobExecutionException {
|
||||
log.info("Started Shouman order creation job");
|
||||
Map<String, Country> countryCodeMap = countryMapper.findAll().stream()
|
||||
.collect(toMap(Country::getMabangName, Function.identity()));
|
||||
|
||||
List<ShoumanOrderContent> shoumanOrderContents = platformOrderContentMapper.searchShoumanOrderContent();
|
||||
log.info("Fetched {} shouman order contents", shoumanOrderContents.size());
|
||||
Map<String, List<ShoumanOrderContent>> groupedByPlatformOrderId = shoumanOrderContents
|
||||
.stream()
|
||||
.collect(Collectors.groupingBy(ShoumanOrderContent::getPlatformOrderId));
|
||||
log.info("After grouping by PlatformOrderId, {} Shouman Orders should be created", groupedByPlatformOrderId.size());
|
||||
|
||||
log.info("Started constructing Shouman request bodies");
|
||||
List<ShoumanOrder> shoumanOrders = new ArrayList<>();
|
||||
for (Map.Entry<String, List<ShoumanOrderContent>> entry : groupedByPlatformOrderId.entrySet()) {
|
||||
OrderCreationRequestBody requestBody = new OrderCreationRequestBody(entry.getValue(), countryCodeMap);
|
||||
ShoumanOrder shoumanOrder = new ShoumanOrder();
|
||||
shoumanOrder.setOrderJson(requestBody.parameters().toJSONString());
|
||||
shoumanOrder.setPlatformOrderId(entry.getKey());
|
||||
shoumanOrder.setCreateBy("shouman job");
|
||||
shoumanOrders.add(shoumanOrder);
|
||||
}
|
||||
log.info("Finished constructing Shouman request bodies");
|
||||
|
||||
log.info("{} shouman orders to be inserted into DB", shoumanOrders.size());
|
||||
shoumanOrderService.saveBatch(shoumanOrders);
|
||||
|
||||
log.info("Finished Shouman order creation job");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue