diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/domain/api/shouman/OrderCreationRequestBody.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/domain/api/shouman/OrderCreationRequestBody.java new file mode 100644 index 000000000..1bdc75743 --- /dev/null +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/domain/api/shouman/OrderCreationRequestBody.java @@ -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 orderContents; + + private Map 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 orderContents, Map 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 void putNonNull(JSONObject json, String key, E value) { + if (value != null) { + json.put(key, value); + } + } +} diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/domain/api/shouman/RequestBody.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/domain/api/shouman/RequestBody.java new file mode 100644 index 000000000..5cd6940fa --- /dev/null +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/domain/api/shouman/RequestBody.java @@ -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 parameters(); + +} diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/domain/job/ShoumanOrderCreationJob.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/domain/job/ShoumanOrderCreationJob.java new file mode 100644 index 000000000..6774b5711 --- /dev/null +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/business/domain/job/ShoumanOrderCreationJob.java @@ -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 countryCodeMap = countryMapper.findAll().stream() + .collect(toMap(Country::getMabangName, Function.identity())); + + List shoumanOrderContents = platformOrderContentMapper.searchShoumanOrderContent(); + log.info("Fetched {} shouman order contents", shoumanOrderContents.size()); + Map> 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 shoumanOrders = new ArrayList<>(); + for (Map.Entry> 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"); + } + +}