You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
udi-spms-java/src/main/java/com/glxp/api/service/thrsys/ThrOrdersDlService.java

388 lines
20 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.glxp.api.service.thrsys;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.glxp.api.common.res.BaseResponse;
import com.glxp.api.constant.BasicProcessStatus;
import com.glxp.api.constant.Constant;
import com.glxp.api.entity.sync.SyncDataSetEntity;
import com.glxp.api.entity.thrsys.*;
import com.glxp.api.httpClient.serviceClient.impl.ErpOrderClientImpl;
import com.glxp.api.httpClient.utils.HttpOkClient;
import com.glxp.api.req.thrsys.FilterThrOrderDetailRequest;
import com.glxp.api.req.thrsys.FilterThrOrderRequest;
import com.glxp.api.req.thrsys.PostThrOrderRequest;
import com.glxp.api.req.thrsys.ThrOrderExportRequest;
import com.glxp.api.res.PageSimpleResponse;
import com.glxp.api.res.thrsys.ThrOrderResponse;
import com.glxp.api.service.sync.SyncDataSetService;
import com.glxp.api.util.RedisUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class ThrOrdersDlService {
// @Value("${SPSYNC_IP}")
// private String spsSyncUrl;
@Resource
ThrOrderService thrOrderService;
@Resource
ThrOrderDetailService thrOrderDetailService;
@Resource
ThrSystemDetailService thrSystemDetailService;
@Resource
RedisUtil redisUtil;
@Resource
private ThrOrderImportLogService thrOrderImportLogService;
@Resource
ThrOrderExportLogService thrOrderExportLogService;
@Resource
ThrOrderImportDetailService thrOrderImportDetailService;
@Resource
private ErpOrderClientImpl erpOrderClientImpl;
@Resource
SyncDataSetService syncDataSetService;
@Resource
HttpOkClient httpOkClient;
@Resource
private IThrBusTypeOriginService iThrBusTypeOriginService;
public String getIpUrl() {
SyncDataSetEntity syncDataSetEntity = syncDataSetService.findSet();
return syncDataSetEntity.getSyncIp();
}
@Async
public void importOrders(String genKey, String action, FilterThrOrderRequest filterThrProductsRequest) {
ThrOrderImportLogEntity thrOrderImportLogEntity = thrOrderImportLogService.selectByGenKey(genKey);
ThrSystemDetailEntity thrSystemDetailEntity = iThrBusTypeOriginService.findSysByAction(action, "orderQueryUrl");
if (thrSystemDetailEntity == null || !thrSystemDetailEntity.getEnabled()) {
// WebSocketServer.sendInfo("业务单据查询接口未设置!", "sid");
return;
}
if (thrSystemDetailEntity.getThirdSysFk() == null) {
// WebSocketServer.sendInfo("业务单据查询接口未设置!", "sid");
return;
}
int page = 1;
int limit = 100;
while (page != -1) {
page = getOrders(page, limit, thrSystemDetailEntity.getThirdSysFk(), thrOrderImportLogEntity, action, filterThrProductsRequest);
}
thrOrderImportLogService.importThrOrder(genKey);
redisUtil.set(Constant.dlThrProducts, "false");
// WebSocketServer.sendInfo("业务单据信息下载已完成,请刷新查看!", "sid");
thrOrderImportLogEntity.setStatus(BasicProcessStatus.UDIINFO_IMPORT_SUCCESS);
thrOrderImportLogService.updateImportLog(thrOrderImportLogEntity);
}
@Async
public void importSelectOrders(String genKey, List<ThrOrderResponse> erpOrderResponses, String thirdSys) {
ThrOrderImportLogEntity thrOrderImportLogEntity = thrOrderImportLogService.selectByGenKey(genKey);
if (erpOrderResponses != null && erpOrderResponses.size() > 0) {
for (ThrOrderResponse erpOrderResponse : erpOrderResponses) {
ThrOrderEntity thrOrderEntity = new ThrOrderEntity();
BeanUtils.copyProperties(erpOrderResponse, thrOrderEntity);
ThrOrderEntity originEntity = thrOrderService.findByUnique(thrOrderEntity.getBillNo(), thrOrderEntity.getThirdSysFk());
if (originEntity != null) {
thrOrderService.deleteById(originEntity.getId() + "");
thrOrderDetailService.deleteByOrderIdFk(originEntity.getId() + "");
}
thrOrderService.insertThrOrder(thrOrderEntity);
List<ThrOrderDetailEntity> thrOrderDetailEntities = new ArrayList<>();
for (ThrOrderResponse.SubErpOrder subPurchase : erpOrderResponse.getSubErpOrders()) {
ThrOrderDetailEntity thrOrderDetailEntity = new ThrOrderDetailEntity();
thrOrderDetailEntity.setUpdateTime(new Date());
BeanUtils.copyProperties(subPurchase, thrOrderDetailEntity);
BeanUtils.copyProperties(subPurchase, thrOrderDetailEntity);
thrOrderDetailEntity.setThirdSysFk(thirdSys);
thrOrderDetailEntity.setUpdateTime(new Date());
thrOrderDetailEntities.add(thrOrderDetailEntity);
}
thrOrderDetailService.insertThrOrderDetails(thrOrderDetailEntities);
}
}
redisUtil.set(Constant.dlThrProducts, "false");
// WebSocketServer.sendInfo("业务单据信息下载已完成,请刷新查看!", "sid");
thrOrderImportLogEntity.setStatus(BasicProcessStatus.UDIINFO_IMPORT_SUCCESS);
thrOrderImportLogService.updateImportLog(thrOrderImportLogEntity);
}
@Async
public void genExcel(String genKey, ThrOrderExportRequest thrOrderExportRequest) {
ThrOrderExportLogEntity thrOrderExportLogEntity = thrOrderExportLogService.selectByGenKey(genKey);
List<List<String>> excelData = new ArrayList<>();
List<String> head = new ArrayList<>();
head.add("单据号");
head.add("单据日期");
head.add("往来单位ID");
head.add("往来单位名称");
head.add("单据类型");
head.add("单据状态");
head.add("产品ID");
head.add("产品名称");
head.add("规格型号");
head.add("批次号");
head.add("生产日期");
head.add("失效日期");
head.add("订单数量");
head.add("实际数量");
head.add("原单据类型");
excelData.add(head);
//todo改功能有关表还未设计
// BussinessTypeEntity bussinessTypeEntity = bussinessTypeService.findBTByAction(thrOrderExportRequest.getBillAction());
//
// //选中导出
// if (thrOrderExportRequest.getErpOrderResponses() != null && thrOrderExportRequest.getErpOrderResponses().size() > 0) {
// BussinessTypeFilterRequest bussinessTypeFilterRequest = new BussinessTypeFilterRequest();
// bussinessTypeFilterRequest.setEnabled(true);
// List<BussinessTypeEntity> bussinessTypeEntities = bussinessTypeService.filterList(bussinessTypeFilterRequest);
// List<ErpOrderResponse> erpOrderResponses = thrOrderExportRequest.getErpOrderResponses();
// for (ErpOrderResponse erpOrderResponse : erpOrderResponses) {
// List<ErpOrderResponse.SubErpOrder> subErpOrders = erpOrderResponse.getSubErpOrders();
// for (ErpOrderResponse.SubErpOrder subErpOrder : subErpOrders) {
// List<String> rows = new ArrayList<>();
// rows.add(erpOrderResponse.getBillNo());
// rows.add(erpOrderResponse.getBilldate());
//// BasicUnitMaintainEntity basicUnitMaintainEntity = basicUnitMaintainService.selectByThirdId(erpOrderResponse.getCorpId(), bussinessTypeEntity.getThirdSysFk());
//// if (basicUnitMaintainEntity != null)
//// rows.add(basicUnitMaintainEntity.getErpId());
//// else
// rows.add(erpOrderResponse.getCorpId());
//
// rows.add(erpOrderResponse.getCorpName());
// rows.add(getName(bussinessTypeEntities, erpOrderResponse.getBillType()));
// rows.add(erpOrderResponse.getBillFlag());
// rows.add(subErpOrder.getProductId());
// rows.add(subErpOrder.getProductName());
// rows.add(subErpOrder.getSpec());
// rows.add(subErpOrder.getBatchNo());
// rows.add(subErpOrder.getProductDate());
// rows.add(subErpOrder.getExpireDate());
// rows.add(subErpOrder.getCount() + "");
// rows.add(subErpOrder.getReCount() + "");
// rows.add(erpOrderResponse.getOriginType());
// excelData.add(rows);
// }
//
// }
//
// } else {//一键导出
//
// BasicThirdSysDetailEntity basicThirdSysDetailEntity = thrSystemDetailService.selectByKey("orderQueryUrl", bussinessTypeEntity.getThirdSysFk());
//
// if (basicThirdSysDetailEntity == null || basicThirdSysDetailEntity.getValue() == null) {
// thrOrderExportLogEntity.setStatus(BasicProcessStatus.UDIINFO_IMPORT_FAIL);
// thrOrderExportLogEntity.setRemark("业务单据接口地址未定义");
// thrOrderExportLogService.updateThrOrderExportLog(thrOrderExportLogEntity);
// return;
// }
// if (!basicThirdSysDetailEntity.getEnabled() || basicThirdSysDetailEntity.getThirdSysFk() == null) {
// thrOrderExportLogEntity.setStatus(BasicProcessStatus.UDIINFO_IMPORT_FAIL);
// thrOrderExportLogEntity.setRemark("第三方系统业务单据接口服务未启用");
// thrOrderExportLogService.updateThrOrderExportLog(thrOrderExportLogEntity);
// return;
// }
//
//
// if (basicThirdSysDetailEntity.getFromType() == 0) {
// FilterThrOrderRequest filterThrOrderRequest = new FilterThrOrderRequest();
// filterThrOrderRequest.setThirdSysFk(bussinessTypeEntity.getThirdSysFk());
// BeanUtils.copyProperties(thrOrderExportRequest, filterThrOrderRequest);
// List<List<String>> datas = exportThrOrders(filterThrOrderRequest);
// excelData.addAll(datas);
// } else {
// FilterThrOrderRequest filterThrOrderRequest = new FilterThrOrderRequest();
// BeanUtils.copyProperties(thrOrderExportRequest, filterThrOrderRequest);
// List<List<String>> genDatas = genExcelData(filterThrOrderRequest);
// if (genDatas != null && genDatas.size() > 0) {
// excelData.addAll(genDatas);
// }
// }
//
// }
// String sheetName = "业务单据";
// new ExcelUtil().exportExcel(excelData, sheetName, thrOrderExportLogEntity.getFilePath(), 20);
// thrOrderExportLogEntity.setStatus(BasicProcessStatus.UDIINFO_IMPORT_SUCCESS);
thrOrderExportLogService.updateThrOrderExportLog(thrOrderExportLogEntity);
}
@Async
public void uploadSmp(String genKey, ThrOrderExportRequest thrOrderExportRequest) {
ThrOrderExportLogEntity thrOrderExportLogEntity = thrOrderExportLogService.selectByGenKey(genKey);
PostThrOrderRequest postThrOrderRequest = new PostThrOrderRequest();
//选中导出
if (thrOrderExportRequest.getErpOrderResponses() != null && thrOrderExportRequest.getErpOrderResponses().size() > 0) {
List<ThrOrderResponse> erpOrderResponses = thrOrderExportRequest.getErpOrderResponses();
postThrOrderRequest.setDatas(erpOrderResponses);
} else {//一键导出
List<ThrOrderResponse> erpOrderResponses = new ArrayList<>();
ThrSystemDetailEntity thrSystemDetailEntity = thrSystemDetailService.selectByKey("orderQueryUrl", thrOrderExportRequest.getThirdSysFk());
if (thrSystemDetailEntity == null || thrSystemDetailEntity.getValue() == null) {
thrOrderExportLogEntity.setStatus(BasicProcessStatus.UDIINFO_IMPORT_FAIL);
thrOrderExportLogEntity.setRemark("往来单位接口地址未定义");
thrOrderExportLogService.updateThrOrderExportLog(thrOrderExportLogEntity);
return;
}
if (!thrSystemDetailEntity.getEnabled()) {
thrOrderExportLogEntity.setStatus(BasicProcessStatus.UDIINFO_IMPORT_FAIL);
thrOrderExportLogEntity.setRemark("第三方接口往来单位服务接口未启用");
thrOrderExportLogService.updateThrOrderExportLog(thrOrderExportLogEntity);
return;
}
if (thrSystemDetailEntity.getFromType() == 0) {
FilterThrOrderRequest filterThrOrderRequest = new FilterThrOrderRequest();
BeanUtils.copyProperties(thrOrderExportRequest, filterThrOrderRequest);
List<ThrOrderResponse> datas = getThrOrders(filterThrOrderRequest);
erpOrderResponses.addAll(datas);
} else {
FilterThrOrderRequest filterThrOrderRequest = new FilterThrOrderRequest();
BeanUtils.copyProperties(thrOrderExportRequest, filterThrOrderRequest);
List<ThrOrderEntity> thrOrderEntities = thrOrderService.filterThrOrder(filterThrOrderRequest);
for (ThrOrderEntity thrOrderEntity : thrOrderEntities) {
ThrOrderResponse erpOrderResponse = new ThrOrderResponse();
BeanUtils.copyProperties(thrOrderEntity, erpOrderResponse);
FilterThrOrderDetailRequest filterThrOrderDetailRequest = new FilterThrOrderDetailRequest();
filterThrOrderDetailRequest.setOrderIdFk(thrOrderEntity.getId() + "");
List<ThrOrderDetailEntity> thrOrderDetailEntities = thrOrderDetailService.filterThrOrderDetailDetail(filterThrOrderDetailRequest);
if (thrOrderDetailEntities != null) {
List<ThrOrderResponse.SubErpOrder> subErpOrders = new ArrayList<>();
for (ThrOrderDetailEntity thrOrderDetailEntity : thrOrderDetailEntities) {
ThrOrderResponse.SubErpOrder subErpOrder = new ThrOrderResponse.SubErpOrder();
BeanUtils.copyProperties(thrOrderDetailEntity, subErpOrder);
subErpOrders.add(subErpOrder);
}
erpOrderResponse.setSubErpOrders(subErpOrders);
}
erpOrderResponses.add(erpOrderResponse);
}
}
postThrOrderRequest.setDatas(erpOrderResponses);
}
//toDo 上傳SMP
postThrOrderRequest.setGenKey(genKey);
postThrOrderRequest.setThirdSys(thrOrderExportRequest.getThirdSysFk());
// SystemParamConfigEntity systemParamConfigEntity = systemParamConfigService.selectByParamKey("smp_service_ip");
// if (systemParamConfigEntity != null) {
String response = httpOkClient.uCloudPost(getIpUrl() + "/udiwms/thrsys/postOrderDetail", postThrOrderRequest);
BaseResponse baseResponse = JSONObject.parseObject(response, new TypeReference<BaseResponse>() {
});
if (baseResponse.getCode() == 20000) {
thrOrderExportLogEntity.setStatus(BasicProcessStatus.UDIINFO_IMPORT_SUCCESS);
} else {
thrOrderExportLogEntity.setStatus(BasicProcessStatus.UDIINFO_IMPORT_FAIL);
}
// } else {
// thrOrderExportLogEntity.setRemark("SMP服务IP地址未定义");
// thrOrderExportLogEntity.setStatus(BasicProcessStatus.UDIINFO_IMPORT_FAIL);
// }
thrOrderExportLogEntity.setStatus(BasicProcessStatus.UDIINFO_IMPORT_SUCCESS);
thrOrderExportLogService.updateThrOrderExportLog(thrOrderExportLogEntity);
}
public int getOrders(int page, int limit, String thirdSys, ThrOrderImportLogEntity
thrOrderImportLogEntity, String action, FilterThrOrderRequest filterThrOrderRequest) {
if (filterThrOrderRequest == null)
filterThrOrderRequest = new FilterThrOrderRequest();
filterThrOrderRequest.setPage(page);
filterThrOrderRequest.setLimit(limit);
filterThrOrderRequest.setBillAction(action);
FilterThrOrderRequest filterOrderRequest = new FilterThrOrderRequest();
BeanUtils.copyProperties(filterThrOrderRequest, filterOrderRequest);
BaseResponse<PageSimpleResponse<ThrOrderResponse>> responseBaseResponse = erpOrderClientImpl.getThrOrderResponse(filterOrderRequest);
if (responseBaseResponse != null && responseBaseResponse.getCode() == 20000) {
List<ThrOrderResponse> erpOrderResponses = responseBaseResponse.getData().getList();
if (erpOrderResponses != null && erpOrderResponses.size() > 0) {
for (ThrOrderResponse erpOrderResponse : erpOrderResponses) {
ThrOrderEntity thrOrderEntity = new ThrOrderEntity();
BeanUtils.copyProperties(erpOrderResponse, thrOrderEntity);
thrOrderEntity.setThirdSysFk(thirdSys);
thrOrderEntity.setUpdateTime(new Date());
thrOrderService.insertThrOrder(thrOrderEntity);
List<ThrOrderImportDetailEntity> thrOrderDetailEntities = new ArrayList<>();
for (ThrOrderResponse.SubErpOrder subPurchase : erpOrderResponse.getSubErpOrders()) {
ThrOrderImportDetailEntity thrOrderDetailEntity = new ThrOrderImportDetailEntity();
BeanUtils.copyProperties(subPurchase, thrOrderDetailEntity);
BeanUtils.copyProperties(erpOrderResponse, thrOrderDetailEntity);
thrOrderDetailEntity.setGenKeyFk(thrOrderImportLogEntity.getGenKey());
thrOrderDetailEntity.setThirdSysFk(thirdSys);
thrOrderDetailEntity.setUpdateTime(new Date());
thrOrderDetailEntities.add(thrOrderDetailEntity);
}
thrOrderImportDetailService.insertOrderImportDetails(thrOrderDetailEntities);
}
page++;
if (page * limit < responseBaseResponse.getData().getTotal()) {
return page;
}
}
} else {
thrOrderImportLogEntity.setStatus(BasicProcessStatus.UDIINFO_IMPORT_FAIL);
if (responseBaseResponse != null) {
thrOrderImportLogEntity.setRemark(responseBaseResponse.getMessage());
} else {
thrOrderImportLogEntity.setRemark("第三方服务出错!");
}
thrOrderImportLogService.updateImportLog(thrOrderImportLogEntity);
return -1;
}
return -1;
}
public List<ThrOrderResponse> getThrOrders(FilterThrOrderRequest filterThrOrderRequest) {
ThrSystemDetailEntity thrSystemDetailEntity = thrSystemDetailService.selectByKey("orderQueryUrl", filterThrOrderRequest.getThirdSysFk());
int page = 1;
int limit = 100;
List<ThrOrderResponse> erpOrderResponseList = new ArrayList<>();
while (true) {
List<ThrOrderResponse> datas = getThrOrders(page, limit, thrSystemDetailEntity.getValue(), filterThrOrderRequest);
if (datas != null && datas.size() >= limit) {
erpOrderResponseList.addAll(datas);
page++;
} else {
if (datas != null) {
erpOrderResponseList.addAll(datas);
}
break;
}
}
return erpOrderResponseList;
}
public List<ThrOrderResponse> getThrOrders(int page, int limit, String orderQueryUrl, FilterThrOrderRequest
filterThrOrderRequest) {
FilterThrOrderRequest filterOrderRequest = new FilterThrOrderRequest();
BeanUtils.copyProperties(filterThrOrderRequest, filterOrderRequest);
filterOrderRequest.setPage(page);
filterOrderRequest.setLimit(limit);
BaseResponse<PageSimpleResponse<ThrOrderResponse>> responseBaseResponse = erpOrderClientImpl.getThrOrderResponse(filterOrderRequest);
if (responseBaseResponse != null && responseBaseResponse.getCode() == 20000) {
List<ThrOrderResponse> erpOrderResponses = responseBaseResponse.getData().getList();
if (erpOrderResponses != null && erpOrderResponses.size() > 0) {
return erpOrderResponses;
}
}
return null;
}
}