|
|
|
|
@@ -58,14 +58,32 @@ public class SyncErpBillsScheduleService {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private PmFormDataMapper pmFormDataMapper;
|
|
|
|
|
private static Map<String, String> BillOrg_Mapping = MapOf.of(
|
|
|
|
|
//组织机构
|
|
|
|
|
private static final Map<String, String> BILL_ORG_MAPPING = MapOf.of(
|
|
|
|
|
"PRD_PPBOM", "FPrdOrgId",
|
|
|
|
|
"PRD_MO", "FPrdOrgId",
|
|
|
|
|
"SAL_SaleOrder", "FStockOrgId",
|
|
|
|
|
"PRD_PickMtrl", "FStockOrgId",
|
|
|
|
|
"PUR_ReceiveBill", "FStockOrgId",
|
|
|
|
|
"PUR_MRAPP", "FPURCHASEORGID",
|
|
|
|
|
"ka7c19edf9d4b4b39b8cc4a06802163b0", "F_PMSY_PrdOrgId",
|
|
|
|
|
"STK_TransferDirect", "FStockOrgId");
|
|
|
|
|
"STK_TransferDirect", "FStockOrgId",
|
|
|
|
|
"SAL_RETURNSTOCK", "FStockOrgId",
|
|
|
|
|
"PUR_MRB", "FStockOrgId",
|
|
|
|
|
"STK_MisDelivery", "FStockOrgId");
|
|
|
|
|
//仓库信息
|
|
|
|
|
private static final Map<String, String> BILL_STOCK_ID_MAPPING = MapOf.of(
|
|
|
|
|
"PRD_PPBOM", "stock_Id",
|
|
|
|
|
"PRD_MO", "stockId",
|
|
|
|
|
"PRD_PickMtrl", "stockId",
|
|
|
|
|
"SAL_SaleOrder", "stockId",
|
|
|
|
|
"PUR_ReceiveBill", "StockID_Id",
|
|
|
|
|
"PUR_MRAPP", "FPURCHASEORGID",
|
|
|
|
|
"ka7c19edf9d4b4b39b8cc4a06802163b0", "stockId",
|
|
|
|
|
"STK_TransferDirect", "SrcStockId_Id",
|
|
|
|
|
"SAL_RETURNSTOCK", "stockId",
|
|
|
|
|
"PUR_MRB", "stockId",
|
|
|
|
|
"STK_MisDelivery", "stockId");
|
|
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
|
try {
|
|
|
|
|
@@ -74,8 +92,7 @@ public class SyncErpBillsScheduleService {
|
|
|
|
|
MDC.put("requestTime", DateUtil.now());
|
|
|
|
|
LuceneAppender.traceIdTL.set(BaseCode.intToChars(IdUtil.getLongId()));
|
|
|
|
|
//List<SyncFormMapping> list = syncFormMappingServiceImpl.list(new LambdaQueryWrapper<SyncFormMapping>().in(SyncFormMapping::getForm_type, "BD_MATERIAL"));
|
|
|
|
|
List<SyncFormMapping> list = syncFormMappingServiceImpl.list(new LambdaQueryWrapper<SyncFormMapping>().in(SyncFormMapping::getForm_type, "ka7c19edf9d4b4b39b8cc4a06802163b0","PRD_PPBOM","PRD_MO","SAL_SaleOrder","PUR_ReceiveBill","PUR_MRAPP"));
|
|
|
|
|
//List<SyncFormMapping> list = syncFormMappingServiceImpl.list(new LambdaQueryWrapper<SyncFormMapping>().in(SyncFormMapping::getForm_type, "ka7c19edf9d4b4b39b8cc4a06802163b0","PRD_PPBOM","PRD_MO","SAL_SaleOrder","PUR_ReceiveBill","PUR_MRAPP"));
|
|
|
|
|
List<SyncFormMapping> list = syncFormMappingServiceImpl.list(new LambdaQueryWrapper<SyncFormMapping>().in(SyncFormMapping::getForm_type, "ka7c19edf9d4b4b39b8cc4a06802163b0", "PRD_PPBOM", "PRD_MO", "SAL_SaleOrder", "PUR_ReceiveBill", "PUR_MRAPP"));
|
|
|
|
|
for (SyncFormMapping m : list) {
|
|
|
|
|
JSONArray mappingJson = m.getMapping_json();
|
|
|
|
|
String mappingString = JSON.toJSONString(mappingJson);
|
|
|
|
|
@@ -86,148 +103,9 @@ public class SyncErpBillsScheduleService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String manualSync(String formType, String code, String startTime, String endTime) {
|
|
|
|
|
if (StringUtils.isEmpty(formType)) {
|
|
|
|
|
throw new BadRequestException("单据类型不能为空");
|
|
|
|
|
}
|
|
|
|
|
SyncFormMapping syncFormMapping = syncFormMappingServiceImpl.getOne(new LambdaQueryWrapper<SyncFormMapping>().in(SyncFormMapping::getForm_type, formType));
|
|
|
|
|
JSONArray mappingJson = syncFormMapping.getMapping_json();
|
|
|
|
|
String mappingString = JSON.toJSONString(mappingJson);
|
|
|
|
|
return manualSyncData(mappingString, syncFormMapping.getForm_type(), syncFormMapping.getDtl_split(), code, startTime, endTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String manualSyncData(String mappingJson, String formType, Boolean dtlSplit, String code, String start, String end) {
|
|
|
|
|
boolean islock = lock.tryLock();
|
|
|
|
|
Map errorMsg = new HashMap<>();
|
|
|
|
|
try {
|
|
|
|
|
if (islock) {
|
|
|
|
|
//单据同步总数
|
|
|
|
|
List<Object> result = new ArrayList<>();
|
|
|
|
|
// 获取当前日期
|
|
|
|
|
String timeEnd = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
|
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
|
calendar.add(Calendar.DAY_OF_MONTH, -10);
|
|
|
|
|
String timeStart = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
|
|
|
|
|
if (StringUtils.isNotBlank(start) && StringUtils.isNotBlank(end)) {
|
|
|
|
|
timeStart = start;
|
|
|
|
|
timeEnd = end;
|
|
|
|
|
}
|
|
|
|
|
//String filterString = "FUseOrgId='750572'";
|
|
|
|
|
String filterString;
|
|
|
|
|
if (StringUtils.isNotBlank(code)) {
|
|
|
|
|
filterString = "FBillNo = '" + code + "'";
|
|
|
|
|
} else {
|
|
|
|
|
filterString = BillOrg_Mapping.get(formType) + " = '750572' ";
|
|
|
|
|
if ("ka7c19edf9d4b4b39b8cc4a06802163b0".equals(formType)) {
|
|
|
|
|
filterString += "AND FDocumentStatus = 'C' AND F_PMSY_BillStatus = 'A' AND F_PMSY_CreateDate >= '" + timeStart + " 00:00:00' AND F_PMSY_CreateDate <= '" + timeEnd + " 23:59:59' ";
|
|
|
|
|
} else if ("SAL_SaleOrder".equals(formType)) {
|
|
|
|
|
filterString += " AND FDocumentStatus = 'C' AND FCloseStatus ='A' AND FCreateDate >= '" + timeStart + " 00:00:00' and FCreateDate <= '" + timeEnd + " 23:59:59' ";
|
|
|
|
|
} else if ("STK_TransferDirect".equals(formType)||"STK_MisDelivery".equals(formType)||"PUR_MRB".equals(formType)||"SAL_RETURNSTOCK".equals(formType)) {
|
|
|
|
|
filterString += " AND FDocumentStatus ='B' AND FCreateDate >= '" + timeStart + " 00:00:00' and FCreateDate <= '" + timeEnd + " 23:59:59' ";
|
|
|
|
|
} else {
|
|
|
|
|
filterString += " AND FDocumentStatus = 'C' AND FCreateDate >= '" + timeStart + " 00:00:00' and FCreateDate <= '" + timeEnd + " 23:59:59' ";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ErpQuery query = new ErpQuery();
|
|
|
|
|
query.setFilterString(filterString);
|
|
|
|
|
query.setFormId(formType);
|
|
|
|
|
query.setFieldKeys("FID");
|
|
|
|
|
query.setLimit(0);
|
|
|
|
|
String jsonString = JSON.toJSONString(query);
|
|
|
|
|
IdentifyInfo identifyInfo = new IdentifyInfo();
|
|
|
|
|
BeanUtils.copyProperties(erpSec, identifyInfo);
|
|
|
|
|
K3CloudApi cloudApi = new K3CloudApi(identifyInfo);
|
|
|
|
|
List<List<Object>> lists = cloudApi.executeBillQuery(jsonString);
|
|
|
|
|
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");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
ids.add(fid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
OperateParam param = new OperateParam();
|
|
|
|
|
for (String id : ids) {
|
|
|
|
|
try {
|
|
|
|
|
param.setId(id);
|
|
|
|
|
OperatorResult view = cloudApi.view(formType, param);
|
|
|
|
|
RepoStatus status = view.getResult().getResponseStatus();
|
|
|
|
|
if (status.isIsSuccess()) {
|
|
|
|
|
result.add(view.getResult().getResult());
|
|
|
|
|
} else {
|
|
|
|
|
ArrayList<RepoError> errors = status.getErrors();
|
|
|
|
|
errorMsg.put(id, errorMsg);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
errorMsg.put("id","处理ID异常"+e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//物料同步
|
|
|
|
|
SyncFormMapping syncFormMapping = syncFormMappingServiceImpl.getOne(new LambdaQueryWrapper<SyncFormMapping>().in(SyncFormMapping::getForm_type, "BD_MATERIAL"));
|
|
|
|
|
String materialMappingString = JSON.toJSONString(syncFormMapping.getMapping_json());
|
|
|
|
|
Set<String> exitFormDataList = pmFormDataMapper.existFormDataList();
|
|
|
|
|
外部:
|
|
|
|
|
for (int i = 0; i < result.size(); i++) {
|
|
|
|
|
Object r = result.get(i);
|
|
|
|
|
try {
|
|
|
|
|
JSONArray mappingJsonArray = JSONArray.parseArray(mappingJson);
|
|
|
|
|
List<PmFormData> formDataList = formDataService.syncAnalyse(mappingJsonArray, formType, dtlSplit, JSON.toJSONString(r));
|
|
|
|
|
//主单据
|
|
|
|
|
final String mainId=formDataList.stream().filter(rd -> StringUtils.isNotBlank(rd.getCode())).collect(Collectors.toList()).get(0).getId();
|
|
|
|
|
Set<String> materials = formDataList.stream().filter(rd -> StringUtils.isBlank(rd.getCode())).map(PmFormData::getMaterial_id).collect(Collectors.toSet());
|
|
|
|
|
List<MdMeMaterialbase> materialList = iMdMeMaterialbaseService.list(new QueryWrapper<MdMeMaterialbase>().select("material_id").in("material_id", materials));
|
|
|
|
|
Set<String> materialHas = materialList.stream().map(MdMeMaterialbase::getMaterial_id).collect(Collectors.toSet());
|
|
|
|
|
for (String m : materials) {
|
|
|
|
|
if (!materialHas.contains(m)) {
|
|
|
|
|
Boolean materialSync = queryMaterialInfo(materialMappingString, syncFormMapping.getForm_type(), syncFormMapping.getDtl_split(), m, cloudApi);
|
|
|
|
|
if (!materialSync) {
|
|
|
|
|
continue 外部;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (PmFormData f : formDataList) {
|
|
|
|
|
if (exitFormDataList.contains(f.getId() + "$" + f.getForm_type())) {
|
|
|
|
|
continue 外部;
|
|
|
|
|
}
|
|
|
|
|
//单据明细
|
|
|
|
|
if (StringUtils.isBlank(f.getCode())) {
|
|
|
|
|
//除了合格证相关单据,其他都过滤非立库仓库组织
|
|
|
|
|
if (!"PRD_MO".equals(f.getForm_type()) && !"PUR_ReceiveBill".equals(f.getForm_type())) {
|
|
|
|
|
if (StringUtils.isNotBlank(f.getForm_data().getString("SrcStockId_Id"))) {
|
|
|
|
|
if (!"1233925".equals(f.getForm_data().getString("SrcStockId_Id")) && !"1233926".equals(f.getForm_data().getString("SrcStockId_Id"))) {
|
|
|
|
|
continue 外部;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
f.setParent_id(mainId + "$" + formType);
|
|
|
|
|
}
|
|
|
|
|
f.setId(f.getId() + "$" + f.getForm_type());
|
|
|
|
|
formDataService.save(f);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
errorMsg.put("result第"+i+1+"条解析异常:",e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!CollectionUtils.isEmpty(errorMsg)){
|
|
|
|
|
throw new BadRequestException("同步成功"+(result.size()-errorMsg.size())+"条,失败信息:"+errorMsg.toString());
|
|
|
|
|
}else {
|
|
|
|
|
return "同步成功,共计"+result.size()+"条";
|
|
|
|
|
}
|
|
|
|
|
}else {
|
|
|
|
|
throw new BadRequestException("当前同步操作正在执行,稍后再试");
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
throw new BadRequestException(ex.getMessage());
|
|
|
|
|
} finally {
|
|
|
|
|
if (lock.isLocked() && lock.isHeldByCurrentThread()) {
|
|
|
|
|
lock.unlock();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 自动同步
|
|
|
|
|
*/
|
|
|
|
|
public void syncData(String mappingJson, String formType, Boolean dtlSplit) {
|
|
|
|
|
boolean islock = lock.tryLock();
|
|
|
|
|
try {
|
|
|
|
|
@@ -241,7 +119,7 @@ public class SyncErpBillsScheduleService {
|
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
|
calendar.add(Calendar.DAY_OF_MONTH, -10);
|
|
|
|
|
String sevenDaysAgo = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
|
|
|
|
|
String filterString = BillOrg_Mapping.get(formType) + " = '750572' AND FDocumentStatus = 'C' ";
|
|
|
|
|
String filterString = BILL_ORG_MAPPING.get(formType) + " = '750572' AND FDocumentStatus = 'C' ";
|
|
|
|
|
if ("ka7c19edf9d4b4b39b8cc4a06802163b0".equals(formType)) {
|
|
|
|
|
filterString += " AND F_PMSY_BillStatus = 'A' AND F_PMSY_CreateDate >= '" + sevenDaysAgo + " 00:00:00' AND F_PMSY_CreateDate <= '" + today + " 23:59:59' ";
|
|
|
|
|
} else if ("SAL_SaleOrder".equals(formType)) {
|
|
|
|
|
@@ -306,7 +184,7 @@ public class SyncErpBillsScheduleService {
|
|
|
|
|
JSONArray mappingJsonArray = JSONArray.parseArray(mappingJson);
|
|
|
|
|
List<PmFormData> formDataList = formDataService.syncAnalyse(mappingJsonArray, formType, dtlSplit, JSON.toJSONString(r));
|
|
|
|
|
//主单据
|
|
|
|
|
final String mainId=formDataList.stream().filter(rd -> StringUtils.isNotBlank(rd.getCode())).collect(Collectors.toList()).get(0).getId();
|
|
|
|
|
final String mainId = formDataList.stream().filter(rd -> StringUtils.isNotBlank(rd.getCode())).collect(Collectors.toList()).get(0).getId();
|
|
|
|
|
Set<String> materials = formDataList.stream().filter(rd -> StringUtils.isBlank(rd.getCode())).map(PmFormData::getMaterial_id).collect(Collectors.toSet());
|
|
|
|
|
List<MdMeMaterialbase> materialList = iMdMeMaterialbaseService.list(new QueryWrapper<MdMeMaterialbase>().select("material_id").in("material_id", materials));
|
|
|
|
|
Set<String> materialHas = materialList.stream().map(MdMeMaterialbase::getMaterial_id).collect(Collectors.toSet());
|
|
|
|
|
@@ -344,6 +222,177 @@ public class SyncErpBillsScheduleService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 手动同步
|
|
|
|
|
*/
|
|
|
|
|
public String manualSync(String formType, String code, String startTime, String endTime) {
|
|
|
|
|
if (StringUtils.isEmpty(formType)) {
|
|
|
|
|
throw new BadRequestException("单据类型不能为空");
|
|
|
|
|
}
|
|
|
|
|
SyncFormMapping syncFormMapping = syncFormMappingServiceImpl.getOne(new LambdaQueryWrapper<SyncFormMapping>().in(SyncFormMapping::getForm_type, formType));
|
|
|
|
|
JSONArray mappingJson = syncFormMapping.getMapping_json();
|
|
|
|
|
String mappingString = JSON.toJSONString(mappingJson);
|
|
|
|
|
return manualSyncData(mappingString, syncFormMapping.getForm_type(), syncFormMapping.getDtl_split(), code, startTime, endTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 手动同步
|
|
|
|
|
*/
|
|
|
|
|
private String manualSyncData(String mappingJson, String formType, Boolean dtlSplit, String code, String start, String end) {
|
|
|
|
|
boolean islock = lock.tryLock();
|
|
|
|
|
Map errorMsg = new HashMap<>();
|
|
|
|
|
try {
|
|
|
|
|
if (islock) {
|
|
|
|
|
//单据同步总数
|
|
|
|
|
List<Object> result = new ArrayList<>();
|
|
|
|
|
// 获取当前日期
|
|
|
|
|
String timeEnd = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
|
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
|
calendar.add(Calendar.DAY_OF_MONTH, -10);
|
|
|
|
|
String timeStart = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
|
|
|
|
|
if (StringUtils.isNotBlank(start) && StringUtils.isNotBlank(end)) {
|
|
|
|
|
timeStart = start;
|
|
|
|
|
timeEnd = end;
|
|
|
|
|
}
|
|
|
|
|
//String filterString = "FUseOrgId='750572'";
|
|
|
|
|
String filterString;
|
|
|
|
|
if (StringUtils.isNotBlank(code)) {
|
|
|
|
|
filterString = "FBillNo = '" + code + "'";
|
|
|
|
|
} else {
|
|
|
|
|
filterString = BILL_ORG_MAPPING.get(formType) + " = '750572' ";
|
|
|
|
|
if ("ka7c19edf9d4b4b39b8cc4a06802163b0".equals(formType)) {
|
|
|
|
|
filterString += "AND FDocumentStatus = 'C' AND F_PMSY_BillStatus = 'A' AND F_PMSY_CreateDate >= '" + timeStart + " 00:00:00' AND F_PMSY_CreateDate <= '" + timeEnd + " 23:59:59' ";
|
|
|
|
|
} else if ("SAL_SaleOrder".equals(formType)) {
|
|
|
|
|
filterString += " AND FDocumentStatus = 'C' AND FCloseStatus ='A' AND FCreateDate >= '" + timeStart + " 00:00:00' and FCreateDate <= '" + timeEnd + " 23:59:59' ";
|
|
|
|
|
} else if ("STK_TransferDirect".equals(formType)||"STK_MisDelivery".equals(formType)||"PUR_MRB".equals(formType)||"SAL_RETURNSTOCK".equals(formType)) {
|
|
|
|
|
filterString += " AND FDocumentStatus ='B' AND FCreateDate >= '" + timeStart + " 00:00:00' and FCreateDate <= '" + timeEnd + " 23:59:59' ";
|
|
|
|
|
} else {
|
|
|
|
|
filterString += " AND FDocumentStatus = 'C' AND FCreateDate >= '" + timeStart + " 00:00:00' and FCreateDate <= '" + timeEnd + " 23:59:59' ";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ErpQuery query = new ErpQuery();
|
|
|
|
|
query.setFilterString(filterString);
|
|
|
|
|
query.setFormId(formType);
|
|
|
|
|
query.setFieldKeys("FID");
|
|
|
|
|
query.setLimit(0);
|
|
|
|
|
String jsonString = JSON.toJSONString(query);
|
|
|
|
|
IdentifyInfo identifyInfo = new IdentifyInfo();
|
|
|
|
|
BeanUtils.copyProperties(erpSec, identifyInfo);
|
|
|
|
|
K3CloudApi cloudApi = new K3CloudApi(identifyInfo);
|
|
|
|
|
List<List<Object>> lists = cloudApi.executeBillQuery(jsonString);
|
|
|
|
|
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");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
ids.add(fid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
OperateParam param = new OperateParam();
|
|
|
|
|
for (String id : ids) {
|
|
|
|
|
try {
|
|
|
|
|
param.setId(id);
|
|
|
|
|
OperatorResult view = cloudApi.view(formType, param);
|
|
|
|
|
RepoStatus status = view.getResult().getResponseStatus();
|
|
|
|
|
if (status.isIsSuccess()) {
|
|
|
|
|
result.add(view.getResult().getResult());
|
|
|
|
|
} else {
|
|
|
|
|
errorMsg.put(id, errorMsg);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
errorMsg.put("id","处理ID异常"+e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//物料同步
|
|
|
|
|
SyncFormMapping syncFormMapping = syncFormMappingServiceImpl.getOne(new LambdaQueryWrapper<SyncFormMapping>().in(SyncFormMapping::getForm_type, "BD_MATERIAL"));
|
|
|
|
|
String materialMappingString = JSON.toJSONString(syncFormMapping.getMapping_json());
|
|
|
|
|
Set<String> exitFormDataList = pmFormDataMapper.existFormDataList();
|
|
|
|
|
外部:
|
|
|
|
|
for (int i = 0; i < result.size(); i++) {
|
|
|
|
|
Object r = result.get(i);
|
|
|
|
|
try {
|
|
|
|
|
JSONArray mappingJsonArray = JSONArray.parseArray(mappingJson);
|
|
|
|
|
List<PmFormData> formDataList = formDataService.syncAnalyse(mappingJsonArray, formType, dtlSplit, JSON.toJSONString(r));
|
|
|
|
|
//主单据
|
|
|
|
|
List<PmFormData> mainBill = formDataList.stream().filter(rd -> StringUtils.isNotBlank(rd.getCode())).collect(Collectors.toList());
|
|
|
|
|
//主单据Id
|
|
|
|
|
final String mainId = mainBill.get(0).getId();
|
|
|
|
|
//仓库Key
|
|
|
|
|
final String stockId = BILL_STOCK_ID_MAPPING.get(formType);
|
|
|
|
|
//除了合格证相关单据,其他都过滤非立库仓库组织
|
|
|
|
|
if (!"PRD_MO".equals(formType) && !"PUR_ReceiveBill".equals(formType)) {
|
|
|
|
|
//明细单据
|
|
|
|
|
List<PmFormData> detailBills = formDataList.stream()
|
|
|
|
|
.filter(rs -> StringUtils.isBlank(rs.getCode()))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
//仓库信息
|
|
|
|
|
List<PmFormData> detailWithStock = detailBills.stream()
|
|
|
|
|
.filter(rd -> StringUtils.isNotBlank(rd.getForm_data().getString(stockId)))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
//托盘1233925与料箱库1233926
|
|
|
|
|
List<PmFormData> filteredDetails = detailWithStock.stream()
|
|
|
|
|
.filter(rt -> new HashSet<>(Arrays.asList("1233925", "1233926")).contains(rt.getForm_data().getString(stockId)))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
if (ObjectUtils.isNotEmpty(filteredDetails)) {
|
|
|
|
|
mainBill.addAll(filteredDetails);
|
|
|
|
|
} else {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
mainBill = formDataList;
|
|
|
|
|
}
|
|
|
|
|
//检查物料信息,如果不存在则新增物料
|
|
|
|
|
Set<String> materials = mainBill.stream().filter(rd -> StringUtils.isBlank(rd.getCode())).map(PmFormData::getMaterial_id).collect(Collectors.toSet());
|
|
|
|
|
List<MdMeMaterialbase> materialList = iMdMeMaterialbaseService.list(new QueryWrapper<MdMeMaterialbase>().select("material_id").in("material_id", materials));
|
|
|
|
|
Set<String> materialHas = materialList.stream().map(MdMeMaterialbase::getMaterial_id).collect(Collectors.toSet());
|
|
|
|
|
for (String m : materials) {
|
|
|
|
|
if (!materialHas.contains(m)) {
|
|
|
|
|
Boolean materialSync = queryMaterialInfo(materialMappingString, syncFormMapping.getForm_type(), syncFormMapping.getDtl_split(), m, cloudApi);
|
|
|
|
|
if (!materialSync) {
|
|
|
|
|
continue 外部;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (PmFormData f : mainBill) {
|
|
|
|
|
if (exitFormDataList.contains(f.getId() + "$" + f.getForm_type())) {
|
|
|
|
|
continue 外部;
|
|
|
|
|
}
|
|
|
|
|
//单据明细
|
|
|
|
|
if (StringUtils.isBlank(f.getCode())) {
|
|
|
|
|
f.setParent_id(mainId + "$" + formType);
|
|
|
|
|
}
|
|
|
|
|
f.setId(f.getId() + "$" + f.getForm_type());
|
|
|
|
|
formDataService.save(f);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
errorMsg.put("result第"+i+1+"条解析异常:",e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!CollectionUtils.isEmpty(errorMsg)){
|
|
|
|
|
throw new BadRequestException("同步成功"+(result.size()-errorMsg.size())+"条,失败信息:"+errorMsg.toString());
|
|
|
|
|
}else {
|
|
|
|
|
return "同步成功,共计"+result.size()+"条";
|
|
|
|
|
}
|
|
|
|
|
}else {
|
|
|
|
|
throw new BadRequestException("当前同步操作正在执行,稍后再试");
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
throw new BadRequestException(ex.getMessage());
|
|
|
|
|
} finally {
|
|
|
|
|
if (lock.isLocked() && lock.isHeldByCurrentThread()) {
|
|
|
|
|
lock.unlock();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 查询物料信息
|
|
|
|
|
*/
|
|
|
|
|
public Boolean queryMaterialInfo(String mappingJson, String formType, Boolean dtlSplit, String id, K3CloudApi cloudApi) {
|
|
|
|
|
try {
|
|
|
|
|
{
|
|
|
|
|
@@ -389,155 +438,15 @@ public class SyncErpBillsScheduleService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void syncData2(String mappingJson, String formType, Boolean dtlSplit) {
|
|
|
|
|
boolean islock = lock.tryLock();
|
|
|
|
|
try {
|
|
|
|
|
if (islock) {
|
|
|
|
|
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 = "FUseOrgId='750572'";
|
|
|
|
|
//String filterString = BillOrg + "='750572' AND FDocumentStatus='C' AND FCreateDate >= '" + today + " 00:00:00' and FCreateDate <= '" + today + " 23:59:59'";
|
|
|
|
|
//构建查询条件
|
|
|
|
|
// String BillOrg = BillOrg_Mapping.get(formType);
|
|
|
|
|
// String filterString = BillOrg + "='750572' AND FDocumentStatus='C' ";
|
|
|
|
|
// if ("ka7c19edf9d4b4b39b8cc4a06802163b0".equals(formType)) {
|
|
|
|
|
// filterString += " AND F_PMSY_StockOrgId='750572' AND F_PMSY_BillStatus ='A' AND F_PMSY_CreateDate >= '2024-11-01 00:00:00' and F_PMSY_CreateDate <= '2024-12-04 23:59:59' ";
|
|
|
|
|
// }else{
|
|
|
|
|
// filterString += " AND FCreateDate >= '2024-11-01 00:00:00' and FCreateDate <= '2024-12-04 23:59:59' ";
|
|
|
|
|
// }
|
|
|
|
|
ErpQuery query = new ErpQuery();
|
|
|
|
|
query.setFilterString(filterString);
|
|
|
|
|
query.setFormId(formType);
|
|
|
|
|
query.setFieldKeys("FMATERIALID");
|
|
|
|
|
query.setLimit(0);
|
|
|
|
|
String jsonString = JSON.toJSONString(query);
|
|
|
|
|
List<List<Object>> lists = cloudApi.executeBillQuery(jsonString);
|
|
|
|
|
// Set<String> exitFormDataList = pmFormDataMapper.existFormDataList();
|
|
|
|
|
for (List<Object> list : lists) {
|
|
|
|
|
for (Object r : list) {
|
|
|
|
|
String fid = r.toString();
|
|
|
|
|
if (StringUtils.isEmpty(fid)) {
|
|
|
|
|
log.error("单据同步失败,没有找到FID");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
OperateParam param = new OperateParam();
|
|
|
|
|
param.setId(fid);
|
|
|
|
|
OperatorResult view = cloudApi.view(formType, param);
|
|
|
|
|
RepoStatus status = view.getResult().getResponseStatus();
|
|
|
|
|
if (status.isIsSuccess()) {
|
|
|
|
|
List<Object> result = new ArrayList<>();
|
|
|
|
|
result.add(view.getResult().getResult());
|
|
|
|
|
// getData(mappingJson, formType, dtlSplit, result);
|
|
|
|
|
} else {
|
|
|
|
|
ArrayList<RepoError> errors = status.getErrors();
|
|
|
|
|
String errorMsg = errors.stream().map(RepoError::getMessage).collect(Collectors.joining(","));
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("处理ID [{}] 时出现异常: {}", fid, e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
log.error("同步数据时出现异常: {}", ex.getMessage());
|
|
|
|
|
} finally {
|
|
|
|
|
if (lock.isLocked() && lock.isHeldByCurrentThread()) {
|
|
|
|
|
lock.unlock();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void syncDataTest(String mappingJson, String formType, Boolean dtlSplit) {
|
|
|
|
|
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 = "FDocumentStatus='C' AND FCreateDate >= '" + today + " 00:00:00' and FCreateDate <= '" + today + " 23:59:59'";
|
|
|
|
|
//String filterString = " FDocumentStatus='C' AND FCreateDate >= '2024-11-30 00:00:00' and FCreateDate <= '2024-11-30 23:59:59'";
|
|
|
|
|
ErpQuery query = new ErpQuery();
|
|
|
|
|
query.setFilterString(filterString);
|
|
|
|
|
query.setFormId(formType);
|
|
|
|
|
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");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
ids.add(fid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
OperateParam param = new OperateParam();
|
|
|
|
|
Map<String, String> error = new HashMap<>();
|
|
|
|
|
for (String id : ids) {
|
|
|
|
|
param.setId(id);
|
|
|
|
|
OperatorResult view = cloudApi.view(formType, 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);
|
|
|
|
|
}
|
|
|
|
|
List<PmFormData> pmFormDataAll = new ArrayList<>();
|
|
|
|
|
for (Object r : result) {
|
|
|
|
|
//log.info(JSON.toJSONString(r));
|
|
|
|
|
JSONArray mappingJsonArray = JSONArray.parseArray(mappingJson);
|
|
|
|
|
List<PmFormData> formDataList = formDataService.syncAnalyse(mappingJsonArray, formType, dtlSplit, JSON.toJSONString(r));
|
|
|
|
|
formDataService.saveBatch(formDataList);
|
|
|
|
|
if (ObjectUtils.isNotEmpty(formDataList)) {
|
|
|
|
|
pmFormDataAll.addAll(formDataList);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//数据库已存在的数据
|
|
|
|
|
Set<String> existFormDataList = pmFormDataMapper.existFormDataList();
|
|
|
|
|
List<PmFormData> newFormDataList = pmFormDataAll.stream()
|
|
|
|
|
.filter(data -> !existFormDataList.contains(data.getId()))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
if (!newFormDataList.isEmpty()) {
|
|
|
|
|
formDataService.saveBatch(newFormDataList);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
log.error("同步数据时出现异常: {}", ex.getMessage());
|
|
|
|
|
} finally {
|
|
|
|
|
if (lock.isLocked() && lock.isHeldByCurrentThread()) {
|
|
|
|
|
lock.unlock();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理物料信息
|
|
|
|
|
*/
|
|
|
|
|
private void getData(String mappingJson, String formType, Boolean dtlSplit, List<Object> result) {
|
|
|
|
|
String jsonString;
|
|
|
|
|
外部:
|
|
|
|
|
for (Object r : result) {
|
|
|
|
|
try {
|
|
|
|
|
JSONArray mappingJsonArray = JSONArray.parseArray(mappingJson);
|
|
|
|
|
List<PmFormData> formDataList = formDataService.syncAnalyse(mappingJsonArray, formType, dtlSplit, JSON.toJSONString(r));
|
|
|
|
|
// for (String id : formDataList.stream().map(PmFormData::getId).collect(Collectors.toList())) {
|
|
|
|
|
// if (exitFormDataList.contains(id)) {
|
|
|
|
|
// continue 外部;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
if (formDataList != null) {
|
|
|
|
|
PmFormData mainFormData = formDataList.stream().filter(rs1 -> "BD_MATERIAL".equals(rs1.getForm_type())).collect(Collectors.toList()).get(0);
|
|
|
|
|
JSONObject object = mainFormData.getForm_data();
|
|
|
|
|
@@ -562,4 +471,68 @@ public class SyncErpBillsScheduleService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 物料信息同步
|
|
|
|
|
*/
|
|
|
|
|
public void syncMateralData(String mappingJson, String formType, Boolean dtlSplit) {
|
|
|
|
|
boolean islock = lock.tryLock();
|
|
|
|
|
try {
|
|
|
|
|
if (islock) {
|
|
|
|
|
IdentifyInfo identifyInfo = new IdentifyInfo();
|
|
|
|
|
BeanUtils.copyProperties(erpSec, identifyInfo);
|
|
|
|
|
K3CloudApi cloudApi = new K3CloudApi(identifyInfo);
|
|
|
|
|
String filterString = "FUseOrgId='750572'";
|
|
|
|
|
ErpQuery query = new ErpQuery();
|
|
|
|
|
query.setFilterString(filterString);
|
|
|
|
|
query.setFormId(formType);
|
|
|
|
|
query.setFieldKeys("FMATERIALID");
|
|
|
|
|
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");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
ids.add(fid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
OperateParam param = new OperateParam();
|
|
|
|
|
Map<String, String> error = new HashMap<>();
|
|
|
|
|
List<PmFormData> formList = formDataService.list(new LambdaQueryWrapper<PmFormData>().eq(PmFormData::getForm_type, "BD_MATERIAL_1"));
|
|
|
|
|
Set<String> idSet1 = formList.stream().map(PmFormData::getParent_id).collect(Collectors.toSet());
|
|
|
|
|
Set<String> differenceSet = new HashSet<>(ids);
|
|
|
|
|
differenceSet.removeAll(idSet1);
|
|
|
|
|
//数据库已存在的数据
|
|
|
|
|
// Set<String> existFormDataList = pmFormDataMapper.existFormDataList();
|
|
|
|
|
for (String id : differenceSet) {
|
|
|
|
|
param.setId(id);
|
|
|
|
|
OperatorResult view = cloudApi.view(formType, param);
|
|
|
|
|
RepoStatus status = view.getResult().getResponseStatus();
|
|
|
|
|
if (status.isIsSuccess()) {
|
|
|
|
|
JSONArray mappingJsonArray = JSONArray.parseArray(mappingJson);
|
|
|
|
|
List<PmFormData> formDataList = formDataService.syncAnalyse(mappingJsonArray, formType, dtlSplit, JSON.toJSONString(view.getResult().getResult()));
|
|
|
|
|
if (!formDataList.isEmpty()) {
|
|
|
|
|
formDataService.saveBatch(formDataList);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ArrayList<RepoError> errors = status.getErrors();
|
|
|
|
|
String errorMsg = errors.stream().map(RepoError::getMessage).collect(Collectors.joining(","));
|
|
|
|
|
error.put(id, errorMsg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
log.error("同步数据时出现异常: {}", ex.getMessage());
|
|
|
|
|
} finally {
|
|
|
|
|
if (lock.isLocked() && lock.isHeldByCurrentThread()) {
|
|
|
|
|
lock.unlock();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|