add:增加ERP单据定时同步功能。
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
package org.nl.wms.system_manage.service.quartz.task;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.kingdee.bos.webapi.entity.*;
|
||||
import com.kingdee.bos.webapi.sdk.K3CloudApi;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.nl.common.utils.BaseCode;
|
||||
import org.nl.common.utils.IdUtil;
|
||||
import org.nl.config.lucene.LuceneAppender;
|
||||
import org.nl.wms.dispatch_manage.task.service.ISchBaseTaskService;
|
||||
import org.nl.wms.external_system.acs.service.WmsToAcsService;
|
||||
import org.nl.wms.external_system.erp.dto.ErpQuery;
|
||||
import org.nl.wms.external_system.erp.dto.ErpSec;
|
||||
import org.nl.wms.pm_manage.form_data.service.IPmFormDataService;
|
||||
import org.nl.wms.pm_manage.form_data.service.dao.PmFormData;
|
||||
import org.nl.wms.sync_manage.service.form_mapping.dao.SyncFormMapping;
|
||||
import org.nl.wms.sync_manage.service.form_mapping.impl.SyncFormMappingServiceImpl;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2023/3/22 17:14
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class SyncErpBillsScheduleService {
|
||||
|
||||
private ReentrantLock lock = new ReentrantLock();
|
||||
@Autowired
|
||||
ErpSec erpSec;
|
||||
@Autowired
|
||||
private ISchBaseTaskService iSchBaseTaskService;
|
||||
@Autowired
|
||||
private WmsToAcsService wmsToAcsService;
|
||||
|
||||
@Autowired
|
||||
private SyncFormMappingServiceImpl syncFormMappingServiceImpl;
|
||||
|
||||
@Autowired
|
||||
private IPmFormDataService formDataService;
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
MDC.put("requestMethod", "TaskScheduleService#run");
|
||||
MDC.put("requestIp", "127.0.0.1");
|
||||
MDC.put("requestTime", DateUtil.now());
|
||||
LuceneAppender.traceIdTL.set(BaseCode.intToChars(IdUtil.getLongId()));
|
||||
List<SyncFormMapping> list = syncFormMappingServiceImpl.list();
|
||||
list.forEach(this::syncData);
|
||||
} finally {
|
||||
LuceneAppender.traceIdTL.remove();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void syncData(SyncFormMapping syncFormMapping) {
|
||||
boolean islock = lock.tryLock();
|
||||
try {
|
||||
if (islock) {
|
||||
List<Object> result = new ArrayList<>();
|
||||
IdentifyInfo identifyInfo = new IdentifyInfo();
|
||||
BeanUtils.copyProperties(erpSec, identifyInfo);
|
||||
K3CloudApi cloudApi = new K3CloudApi(identifyInfo);
|
||||
String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
|
||||
String filterString = "FCreateDate >= '" + today + " 00:00:00' and FCreateDate <= '" + today + " 23:59:59'";
|
||||
ErpQuery query = new ErpQuery();
|
||||
query.setFilterString(filterString);
|
||||
query.setFormId(syncFormMapping.getForm_type());
|
||||
query.setFieldKeys("FID");
|
||||
query.setLimit(0);
|
||||
String jsonString = JSON.toJSONString(query);
|
||||
List<List<Object>> lists = cloudApi.executeBillQuery(jsonString);
|
||||
//log.info("同步ERP结果" + lists.size());
|
||||
Set<String> ids = new HashSet<>();
|
||||
for (List<Object> list : lists) {
|
||||
for (Object r : list) {
|
||||
String fid = r.toString();
|
||||
if (StringUtils.isEmpty(fid)) {
|
||||
log.error("单据同步失败,没有找到FID");
|
||||
throw new RuntimeException("单据同步失败,没有找到FID");
|
||||
}
|
||||
ids.add(fid);
|
||||
}
|
||||
}
|
||||
OperateParam param = new OperateParam();
|
||||
Map<String, String> error = new HashMap<>();
|
||||
for (String id : ids) {
|
||||
param.setId(id);
|
||||
OperatorResult view = cloudApi.view(syncFormMapping.getForm_type(), param);
|
||||
RepoStatus status = view.getResult().getResponseStatus();
|
||||
if (status.isIsSuccess()) {
|
||||
result.add(view.getResult().getResult());
|
||||
} else {
|
||||
ArrayList<RepoError> errors = status.getErrors();
|
||||
String errorMsg = errors.stream().map(RepoError::getMessage).collect(Collectors.joining(","));
|
||||
error.put(id, errorMsg);
|
||||
}
|
||||
}
|
||||
if (!CollectionUtils.isEmpty(error)) {
|
||||
log.error("ERP单据同步同步失败:{}", error);
|
||||
}
|
||||
for (Object r : result) {
|
||||
List<PmFormData> pmFormDatas = formDataService.syncAnalyse(syncFormMapping, JSON.toJSONString(r));
|
||||
formDataService.saveBatch(pmFormDatas);
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.error("定时任务执行异常:" + ex.getMessage());
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
if (lock.isLocked() && lock.isHeldByCurrentThread()) {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user