[TASK_BATCH_ACIDLIVY] ACI 배송자료 조회 및 출고처리 배치 신규 생성 - AciDlivyJobConfig/Tasklet/Mapper/VO 생성, application-dev.yaml aci.dlivy.api-url 추가

This commit is contained in:
이주일
2026-05-24 21:17:56 +09:00
parent 6cbdc36c71
commit c9f2843a10
8 changed files with 783 additions and 1 deletions

View File

@@ -0,0 +1,263 @@
package com.wb.domainserver.acidlivy;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wb.domainserver.util.dataaccess.DataMap;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.client.RestTemplate;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
// ACI ???? ?? ? ???? ?? Tasklet
// ????: gc_order_shipping(ORDR_STTUS_CD=35) ?? ? ACI API ?? ? BLNO ?? ? ????
@Slf4j
@Component
@RequiredArgsConstructor
public class AciDlivyTasklet implements Tasklet {
private final AciDlivyMapper aciDlivyMapper;
private final PlatformTransactionManager transactionManager;
@Value("${aci.dlivy.api-url}")
private String aciDlivyApiUrl;
@Value("${acikey.userid}")
private String userId;
@Value("${acikey.apikey}")
private String apiKey;
private static final String HDRYCPN_CD = "code22"; // ACI ????? ??
private static final String DLIVY_STTUS_CD = "55"; // ???? ??
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
log.info("[AciDlivyBatch] ?? ??");
// ? ?? ?? ?? (ORDR_STTUS_CD = '35')
List<AciDlivyOrderVO> targetList = aciDlivyMapper.selectAciDlivyTargetList();
log.info("[AciDlivyBatch] ?? ?? ??: {}", targetList.size());
if (targetList.isEmpty()) {
log.info("[AciDlivyBatch] ??? ?? ??? ????.");
return RepeatStatus.FINISHED;
}
int successCnt = 0;
int failCnt = 0;
int skipCnt = 0;
// ???? ?? ???? (REQUIRES_NEW) ? ? ? ??? ?? ?? ?? ???
TransactionTemplate txTemplate = new TransactionTemplate(transactionManager);
txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
for (AciDlivyOrderVO orderVO : targetList) {
try {
// ? ACI ???? API ?? ? BLNO ??
String blno = callAciDlivyApi(orderVO);
if (blno == null) {
log.info("[AciDlivyBatch] ?? - ORDR_DLVY_INNB={}, ORDR_INNB={} (BLNO ?? ?? ?? ??)",
orderVO.getOrdrDlvyInnb(), orderVO.getOrdrInnb());
skipCnt++;
} else {
orderVO.setBlno(blno);
orderVO.setHdrycpnCd(HDRYCPN_CD);
orderVO.setDlivySttusCd(DLIVY_STTUS_CD);
// ? ???? (?? ????)
txTemplate.execute(status -> {
processDelivery(orderVO);
return null;
});
successCnt++;
log.info("[AciDlivyBatch] ???? ?? - ORDR_DLVY_INNB={}, BLNO={}",
orderVO.getOrdrDlvyInnb(), blno);
}
} catch (Exception e) {
log.error("[AciDlivyBatch] ???? ?? - ORDR_DLVY_INNB={}, ??={}",
orderVO.getOrdrDlvyInnb(), e.getMessage(), e);
failCnt++;
}
// ? API ?? ?? ?? (?? 10? ? 1? ??)
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
break;
}
}
log.info("[AciDlivyBatch] ?? ?? - ??: {}, ??: {}, ??: {}", successCnt, failCnt, skipCnt);
return RepeatStatus.FINISHED;
}
/**
* ACI ???? API ??
* Content-Type? text/html? ?? ??? JSON ? String ?? ? ObjectMapper ??
* @return BLNO(?????), ??? null
*/
private String callAciDlivyApi(AciDlivyOrderVO orderVO) {
try {
HttpHeaders headers = new HttpHeaders();
headers.set("UserID", userId);
headers.set("ACIKey", apiKey);
String url = aciDlivyApiUrl + "?searchType=1&orderNumber=" + orderVO.getOrdrInnb();
HttpEntity<Void> entity = new HttpEntity<>(headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
log.debug("[AciDlivyBatch] API ?? ?? (ORDR_INNB={}): {}", orderVO.getOrdrInnb(), response.getBody());
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> body = objectMapper.readValue(response.getBody(), Map.class);
String status = (String) body.get("Status");
log.debug("[AciDlivyBatch] API Status={} (ORDR_INNB={})", status, orderVO.getOrdrInnb());
// ??/????? ?? ?? (?? Status?? ??? ?? ? ???? ?? ?? ??)
if (!"A10".equals(status)) {
log.info("[AciDlivyBatch] API ??? ?? - ORDR_INNB={}, Status={}", orderVO.getOrdrInnb(), status);
return null;
}
// Data ?? (?? Map ?? List<Map> ?? ??)
Object dataObj = body.get("Data");
if (dataObj == null) {
return null;
}
List<Map<String, Object>> dataList;
if (dataObj instanceof List) {
dataList = (List<Map<String, Object>>) dataObj;
} else if (dataObj instanceof Map) {
dataList = List.of((Map<String, Object>) dataObj);
} else {
return null;
}
// OverseaSellerNo == ORDR_DLVY_INNB ?? ? BLNO ??
for (Map<String, Object> item : dataList) {
Object sellerNo = item.get("OverseaSellerNo");
if (sellerNo != null && orderVO.getOrdrDlvyInnb().equals(String.valueOf(sellerNo).trim())) {
Object blno = item.get("BLNO");
if (blno != null && !String.valueOf(blno).isBlank()) {
return String.valueOf(blno).trim();
}
}
}
log.info("[AciDlivyBatch] OverseaSellerNo ?? ?? - ORDR_DLVY_INNB={}", orderVO.getOrdrDlvyInnb());
return null;
} catch (Exception e) {
log.error("[AciDlivyBatch] API ?? ?? - ORDR_INNB={}, ??={}", orderVO.getOrdrInnb(), e.getMessage(), e);
return null;
}
}
/**
* ???? (??: gc_goods_export ? gc_goods_export_item ? gc_order_item_option
* ? gc_order_shipping ? gc_order ? ???? ? ????)
* gc_order_shipping/gc_order ????? ?? ??? ?? ???? ?? ??
*/
private void processDelivery(AciDlivyOrderVO orderVO) {
// ? gc_goods_export ??
int exportCnt = aciDlivyMapper.selectGoodsExportCount(orderVO);
if (exportCnt > 0) {
aciDlivyMapper.updateGoodsExport(orderVO); // selectKey? dlivyInnb ???
} else {
aciDlivyMapper.insertGoodsExport(orderVO); // selectKey? dlivyInnb ???
}
// dlivyCdNb ?? (GC_GOODS_EXPORT ? DLIVY_CD_NB ??? ?? ??)
String dlivyCdNb = "D" + new SimpleDateFormat("yyMMddHH").format(new Date()) + orderVO.getDlivyInnb();
orderVO.setDlivyCdNb(dlivyCdNb);
// ? ?? ?? ?? ?? ?? (gc_order_item_option)
List<DataMap> itemList = aciDlivyMapper.selectAciDlivyTargetItemList(orderVO);
log.debug("[AciDlivyBatch] ?? ?? ?? ?={} (ORDR_DLVY_INNB={})", itemList.size(), orderVO.getOrdrDlvyInnb());
for (DataMap itemMap : itemList) {
Object qyObj = itemMap.get("qy");
if (qyObj == null) continue;
int qy;
try {
qy = Integer.parseInt(String.valueOf(qyObj));
} catch (NumberFormatException e) {
continue;
}
if (qy <= 0) continue;
itemMap.put("sopmalCd", orderVO.getSopmalCd());
itemMap.put("ordrDlvyInnb", orderVO.getOrdrDlvyInnb());
itemMap.put("dlivyInnb", orderVO.getDlivyInnb());
itemMap.put("ordrInnb", orderVO.getOrdrInnb());
itemMap.put("hdrycpnCd", HDRYCPN_CD);
itemMap.put("invcnb", orderVO.getBlno());
itemMap.put("dlivySttusCd", DLIVY_STTUS_CD);
// ? gc_goods_export_item ??
int exportItemCnt = aciDlivyMapper.selectGoodsExportItemCount(itemMap);
if (exportItemCnt > 0) {
aciDlivyMapper.updateGoodsExportItem(itemMap);
} else {
aciDlivyMapper.insertGoodsExportItem(itemMap);
}
// ? gc_order_item_option ?? UPDATE
aciDlivyMapper.updateOrderItemOptionStatus(itemMap);
// ? ?? ?? INSERT (???)
DataMap exportLogMap = new DataMap();
exportLogMap.put("ordrInnb", orderVO.getOrdrInnb());
exportLogMap.put("ordrDlvyInnb", orderVO.getOrdrDlvyInnb());
exportLogMap.put("sopmalCd", orderVO.getSopmalCd());
exportLogMap.put("dlivyInnb", orderVO.getDlivyInnb());
exportLogMap.put("dlivyGoodsCdNb", itemMap.get("ordrGoodsOptnInnb"));
exportLogMap.put("reqstProcsSttusCd", DLIVY_STTUS_CD);
exportLogMap.put("mssageCn", "[ACI ??] ORDR_INNB:" + orderVO.getOrdrInnb() + " ????");
exportLogMap.put("regusr", 0);
aciDlivyMapper.insertGoodsExportLog(exportLogMap);
}
// ? gc_order_shipping ?? UPDATE (?? item_option ?? ???? ?? ??)
aciDlivyMapper.updateOrderShippingStatus(orderVO);
// ? gc_order ?? UPDATE (?? shipping ?? ???? ?? ??)
aciDlivyMapper.updateOrderStatus(orderVO);
// ? ?? ?? INSERT
DataMap orderLogMap = new DataMap();
orderLogMap.put("ordrInnb", orderVO.getOrdrInnb());
orderLogMap.put("sopmalCd", orderVO.getSopmalCd());
orderLogMap.put("logSj", "????(" + dlivyCdNb + ")");
orderLogMap.put("logDtlCn", "[ACI ??] ?? ???? ??");
orderLogMap.put("dlivyCdNb", dlivyCdNb);
orderLogMap.put("logDvCd", "01");
orderLogMap.put("dvCd", "BATCH");
orderLogMap.put("opetrNm", "ACI ??");
orderLogMap.put("qy", 0);
orderLogMap.put("amt", 0);
aciDlivyMapper.insertOrderLog(orderLogMap);
}
}