package com.glxp.api.service.sync; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONUtil; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.glxp.api.common.res.BaseResponse; import com.glxp.api.common.util.ResultVOUtils; import com.glxp.api.constant.*; import com.glxp.api.dao.basic.*; import com.glxp.api.dao.inout.IoCodeLostMapper; import com.glxp.api.dao.inout.IoCodeRelMapper; import com.glxp.api.dao.purchase.*; import com.glxp.api.dao.thrsys.ThrBusTypeOriginDao; import com.glxp.api.dto.RelaySyncDto; import com.glxp.api.entity.basic.*; import com.glxp.api.entity.inout.*; import com.glxp.api.entity.purchase.*; import com.glxp.api.entity.sync.BasicDownloadStatusEntity; import com.glxp.api.entity.sync.BasicExportStatusEntity; import com.glxp.api.entity.sync.BasicExportStatusTimeEntity; import com.glxp.api.entity.system.SyncDataSetEntity; import com.glxp.api.entity.thrsys.ThrBusTypeOriginEntity; import com.glxp.api.http.sync.SpGetHttpClient; import com.glxp.api.req.sync.SpsSyncDataRequest; import com.glxp.api.res.sync.*; import com.glxp.api.service.basic.IBasicBusTypeChangeService; import com.glxp.api.service.basic.IBasicBussinessTypeService; import com.glxp.api.service.inout.*; import com.glxp.api.service.thrsys.IThrBusTypeOriginService; import com.glxp.api.util.CustomUtil; import com.glxp.api.util.DateUtil; import com.glxp.api.util.RedisUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.TransactionIsolationLevel; import org.springframework.aop.framework.AopContext; import org.springframework.beans.BeanUtils; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.*; import java.util.function.Function; @Slf4j @Service @RequiredArgsConstructor public class HeartService { @Resource BasicExportService basicExportService; @Resource UpBasicService upBasicService; @Resource SpGetHttpClient spGetHttp; @Resource BasicExportTimeService basicExportTimeService; @Resource BasicDownloadService basicDownloadService; @Resource SqlSessionFactory sqlSessionFactory; @Resource IoOrderService orderService; @Resource IoAddInoutService addInoutService; @Resource IoCheckInoutService ioCheckInoutService; @Resource private UpThrDataService upThrDataService; @Resource IoCodeTempService codeTempService; @Resource IoOrderDetailBizService orderDetailBizService; @Resource IoOrderDetailCodeService orderDetailCodeService; @Resource IoOrderDetailResultService orderDetailResultService; @Resource IoOrderInvoiceService orderInvoiceService; private final ApplicationContext applicationContext; //上传最近更新扫码单据 public BaseResponse uploadAllOrder(String syncTime) { SpsSyncDataRequest spsSyncDataRequest = new SpsSyncDataRequest(); if (syncTime == null) { BasicExportStatusTimeEntity basicExportStatusTimeEntity = basicExportTimeService.findByKey(ConstantStatus.SYNC_SCAN_ORDER); if (basicExportStatusTimeEntity == null) { basicExportStatusTimeEntity = new BasicExportStatusTimeEntity(); basicExportStatusTimeEntity.setLastUpdateTime("1949-10-01 09:00:00"); } spsSyncDataRequest.setLastUpdateTime(basicExportStatusTimeEntity.getLastUpdateTime()); } else { spsSyncDataRequest.setLastUpdateTime(syncTime); } SpsSyncOrderResponse spsSyncOrderResponse = upBasicService.upAllOrder(spsSyncDataRequest); if (CollUtil.isEmpty(spsSyncOrderResponse.getOrderEntities()) && CollUtil.isEmpty(spsSyncOrderResponse.getCodeEntities())) { //无数据不记录日志 return ResultVOUtils.success("无数据"); } BasicExportStatusEntity basicExportStatusEntity1 = basicExportService.findByData(ConstantStatus.SYNC_SCAN_ORDER, 0); if (basicExportStatusEntity1 == null) { BasicExportStatusEntity orderStatusEntity = new BasicExportStatusEntity(); orderStatusEntity.setId(CustomUtil.getId()); orderStatusEntity.setIdDatas(ConstantStatus.SYNC_SCAN_ORDER); orderStatusEntity.setType(BasicProcessStatus.NEW_ALL_ORDER); orderStatusEntity.setUpdateTime(new Date()); orderStatusEntity.setStartTime(new Date()); orderStatusEntity.setStatus(ConstantStatus.SYNC_STATUS_WAIT); orderStatusEntity.setScheduleType(BasicProcessStatus.SCHEDULE_NORMAL); basicExportService.insertExportStatus(orderStatusEntity); basicExportStatusEntity1 = orderStatusEntity; } BasicExportStatusTimeEntity basicExportStatusTimeEntity = new BasicExportStatusTimeEntity(); basicExportStatusTimeEntity.setLastUpdateTime(DateUtil.formatDate(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); basicExportStatusTimeEntity.setKey(ConstantStatus.SYNC_SCAN_ORDER); spsSyncOrderResponse.setTaskId(basicExportStatusEntity1.getId()); BaseResponse baseResponse1 = spGetHttp.postAllOrder(spsSyncOrderResponse); if (baseResponse1.getCode() == 20000) { basicExportStatusEntity1.setStatus(ConstantStatus.SYNC_STATUS_SUCCESS); } else { basicExportStatusEntity1.setStatus(ConstantStatus.SYNC_STATUS_FAIL); } basicExportStatusEntity1.setEndTime(new Date()); basicExportTimeService.insertExportStatus(basicExportStatusTimeEntity); //拼接日志 String logs = ""; if (CollUtil.isNotEmpty(spsSyncOrderResponse.getOrderEntities())) logs = logs + "单据:" + spsSyncOrderResponse.getOrderEntities().size() + "条\n"; if (CollUtil.isNotEmpty(spsSyncOrderResponse.getOrderDetailBizEntities())) logs = logs + "单据详情:" + spsSyncOrderResponse.getOrderDetailBizEntities().size() + "条\n"; if (CollUtil.isNotEmpty(spsSyncOrderResponse.getCodeEntities())) logs = logs + "单据条码信息:" + spsSyncOrderResponse.getCodeEntities().size() + "条\n"; basicExportStatusEntity1.setRemark(logs); basicExportStatusEntity1.setUpdateTime(new Date()); basicExportService.updateExportStatus(basicExportStatusEntity1); return ResultVOUtils.success(); } //上传最近更新业务单据 public BaseResponse uploadAllBusOrder(String syncTime) { SpsSyncDataRequest spsSyncDataRequest = new SpsSyncDataRequest(); if (syncTime == null) { BasicExportStatusTimeEntity basicExportStatusTimeEntity = basicExportTimeService.findByKey(ConstantStatus.SYNC_BUS_ORDER); if (basicExportStatusTimeEntity == null) { basicExportStatusTimeEntity = new BasicExportStatusTimeEntity(); basicExportStatusTimeEntity.setLastUpdateTime("1949-10-01 09:00:00"); } spsSyncDataRequest.setLastUpdateTime(basicExportStatusTimeEntity.getLastUpdateTime()); } else { spsSyncDataRequest.setLastUpdateTime(syncTime); } SpsSyncBusOrderResponse spsSyncOrderResponse = upBasicService.upAllBusOrder(spsSyncDataRequest); if (CollUtil.isEmpty(spsSyncOrderResponse.getPurOrderEntities()) && CollUtil.isEmpty(spsSyncOrderResponse.getPurOrderDetailEntities())) { //无数据不记录日志 return ResultVOUtils.success("无数据"); } BasicExportStatusEntity basicExportStatusEntity1 = basicExportService.findByData(ConstantStatus.SYNC_BUS_ORDER, 0); if (basicExportStatusEntity1 == null) { BasicExportStatusEntity orderStatusEntity = new BasicExportStatusEntity(); orderStatusEntity.setId(CustomUtil.getId()); orderStatusEntity.setIdDatas(ConstantStatus.SYNC_BUS_ORDER); orderStatusEntity.setType(BasicProcessStatus.ALL_BUS_ORDER); orderStatusEntity.setUpdateTime(new Date()); orderStatusEntity.setStartTime(new Date()); orderStatusEntity.setStatus(ConstantStatus.SYNC_STATUS_WAIT); orderStatusEntity.setScheduleType(BasicProcessStatus.SCHEDULE_NORMAL); basicExportService.insertExportStatus(orderStatusEntity); basicExportStatusEntity1 = orderStatusEntity; } BasicExportStatusTimeEntity basicExportStatusTimeEntity = new BasicExportStatusTimeEntity(); basicExportStatusTimeEntity.setLastUpdateTime(DateUtil.formatDate(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); basicExportStatusTimeEntity.setKey(ConstantStatus.SYNC_BUS_ORDER); spsSyncOrderResponse.setTaskId(basicExportStatusEntity1.getId()); BaseResponse baseResponse1 = spGetHttp.postAllBusOrder(spsSyncOrderResponse); if (baseResponse1.getCode() == 20000) { basicExportStatusEntity1.setStatus(ConstantStatus.SYNC_STATUS_SUCCESS); } else { basicExportStatusEntity1.setStatus(ConstantStatus.SYNC_STATUS_FAIL); } basicExportStatusEntity1.setEndTime(new Date()); basicExportTimeService.insertExportStatus(basicExportStatusTimeEntity); //拼接日志 String logs = ""; if (CollUtil.isNotEmpty(spsSyncOrderResponse.getPurOrderEntities())) logs = logs + "采购订单单据:" + spsSyncOrderResponse.getPurOrderEntities().size() + "条\n"; if (CollUtil.isNotEmpty(spsSyncOrderResponse.getPurOrderDetailEntities())) logs = logs + "采购订单详情:" + spsSyncOrderResponse.getPurOrderDetailEntities().size() + "条\n"; basicExportStatusEntity1.setRemark(logs); basicExportStatusEntity1.setUpdateTime(new Date()); basicExportService.updateExportStatus(basicExportStatusEntity1); return ResultVOUtils.success(); } private final RedisUtil redisUtil; @Transactional(rollbackFor = Exception.class) public void chooseData(SyncDataSetEntity info, Date syncTime, BasicExportTypeEnum exportType) { Date now = new Date(); String taskId = CustomUtil.getId(); switch (exportType) { case BASIC_DATA: if (needExec(info.getBasicProducts(), info.getBasicCorp(), info.getSupCert())) { uploadData(exportType, taskId, x -> x.getBasicData(info, taskId, now, syncTime)); } break; case OTHER_DATA: if (needExec(info.getUdiCodeLost(), info.getUdiCodeRel())) { uploadData(exportType, taskId, x -> x.getOtherData(info, taskId, now, syncTime)); } break; case DOCUMENT_TYPE_DATA: if (needExec(info.getTypeBus(), info.getTypeScan(), info.getTypeThird())) { uploadData(exportType, taskId, x -> x.getDocumentTypeData(info, taskId, now, syncTime)); } break; case COUNTRY_DI_DATA: } } /** * 获取基础数据模块数据 * * @param info 同步设置 * @param now 当前时间 * @param syncTime 同步时间 -- 传入时不操作exportTime表 * @return */ public SpsSyncBasicDataResponse getBasicData(SyncDataSetEntity info, String taskId, Date now, Date syncTime) { SpsSyncBasicDataResponse dataResponse = null; Map syncTimeMap = new WeakHashMap<>(3); syncTimeMap.put("isNew", true); syncTimeMap.put("oldDate", syncTime); //确认有开启物资字典同步 if (needExec(info.getBasicProducts())) { Map map; if (syncTime == null) { map = basicExportStatusTimeInfo(now, BasicExportStatusTimeEnum.BASIC_PRODUCTS); } else { map = syncTimeMap; } List hospTypeList = basicHospTypeDao.selectList(Wrappers.lambdaQuery(BasicHospTypeEntity.class) .le((boolean) map.get("isNew"), BasicHospTypeEntity::getUpdateTime, now) .between(!(boolean) map.get("isNew"), BasicHospTypeEntity::getUpdateTime , map.get("oldDate"), now) ); if (CollectionUtil.isNotEmpty(hospTypeList)) { dataResponse = new SpsSyncBasicDataResponse(); dataResponse.setHospTypeList(hospTypeList); } List udiRelevanceList = udiRelevanceDao.selectList(Wrappers.lambdaQuery(UdiRelevanceEntity.class) .le((boolean) map.get("isNew"), UdiRelevanceEntity::getUpdateTime, now) .between(!(boolean) map.get("isNew"), UdiRelevanceEntity::getUpdateTime , map.get("oldDate"), now) ); if (CollectionUtil.isNotEmpty(udiRelevanceList)) { if (dataResponse == null) { dataResponse = new SpsSyncBasicDataResponse(); } dataResponse.setUdiRelevanceList(udiRelevanceList); } List productsList = basicProductsDao.selectList(Wrappers.lambdaQuery(BasicProductsEntity.class) .le((boolean) map.get("isNew"), BasicProductsEntity::getUpdateTime, now) .between(!(boolean) map.get("isNew"), BasicProductsEntity::getUpdateTime , map.get("oldDate"), now) ); if (CollectionUtil.isNotEmpty(productsList)) { if (dataResponse == null) { dataResponse = new SpsSyncBasicDataResponse(); } dataResponse.setProductsList(productsList); } List relevanceList = relevanceDao.selectList(Wrappers.lambdaQuery(CompanyProductRelevanceEntity.class) .le((boolean) map.get("isNew"), CompanyProductRelevanceEntity::getUpdateTime, now) .between(!(boolean) map.get("isNew"), CompanyProductRelevanceEntity::getUpdateTime , map.get("oldDate"), now) ); if (CollectionUtil.isNotEmpty(relevanceList)) { if (dataResponse == null) { dataResponse = new SpsSyncBasicDataResponse(); } dataResponse.setRelevanceList(relevanceList); } } //确认有开启往来单位字典同步 if (needExec(info.getBasicCorp())) { Map map; if (syncTime == null) { map = basicExportStatusTimeInfo(now, BasicExportStatusTimeEnum.BASIC_CORP); } else { map = syncTimeMap; } List corpList = corpDao.selectList(Wrappers.lambdaQuery(BasicCorpEntity.class) .le((boolean) map.get("isNew"), BasicCorpEntity::getUpdateTime, now) .between(!(boolean) map.get("isNew"), BasicCorpEntity::getUpdateTime , map.get("oldDate"), now) ); if (CollectionUtil.isNotEmpty(corpList)) { if (dataResponse == null) { dataResponse = new SpsSyncBasicDataResponse(); } dataResponse.setCorpList(corpList); } } //确认有开启首营资质同步 if (info.getSupCert() == 2) { Map map; if (syncTime == null) { map = basicExportStatusTimeInfo(now, BasicExportStatusTimeEnum.SUP_CERT); } else { map = syncTimeMap; } List supCertList = supCertDao.selectList(Wrappers.lambdaQuery(SupCertEntity.class) .le((boolean) map.get("isNew"), SupCertEntity::getUpdateTime, now) .between(!(boolean) map.get("isNew"), SupCertEntity::getUpdateTime , map.get("oldDate"), now) ); if (CollectionUtil.isNotEmpty(supCertList)) { if (dataResponse == null) { dataResponse = new SpsSyncBasicDataResponse(); } dataResponse.setSupCertList(supCertList); } List supCertSetList = supCertSetDao.selectList(Wrappers.lambdaQuery(SupCertSetEntity.class) .le((boolean) map.get("isNew"), SupCertSetEntity::getUpdateTime, now) .between(!(boolean) map.get("isNew"), SupCertSetEntity::getUpdateTime , map.get("oldDate"), now) ); if (CollectionUtil.isNotEmpty(supCertSetList)) { if (dataResponse == null) { dataResponse = new SpsSyncBasicDataResponse(); } dataResponse.setSupCertSetList(supCertSetList); } List supCompanyList = supCompanyDao.selectList(Wrappers.lambdaQuery(SupCompanyEntity.class) .le((boolean) map.get("isNew"), SupCompanyEntity::getUpdateTime, now) .between(!(boolean) map.get("isNew"), SupCompanyEntity::getUpdateTime , map.get("oldDate"), now) ); if (CollectionUtil.isNotEmpty(supCompanyList)) { if (dataResponse == null) { dataResponse = new SpsSyncBasicDataResponse(); } dataResponse.setSupCompanyList(supCompanyList); } List supManufacturerList = supManufacturerDao.selectList(Wrappers.lambdaQuery(SupManufacturerEntity.class) .le((boolean) map.get("isNew"), SupManufacturerEntity::getUpdateTime, now) .between(!(boolean) map.get("isNew"), SupManufacturerEntity::getUpdateTime , map.get("oldDate"), now) ); if (CollectionUtil.isNotEmpty(supManufacturerList)) { if (dataResponse == null) { dataResponse = new SpsSyncBasicDataResponse(); } dataResponse.setSupManufacturerList(supManufacturerList); } List supProductList = supProductDao.selectList(Wrappers.lambdaQuery(SupProductEntity.class) .le((boolean) map.get("isNew"), SupProductEntity::getUpdateTime, now) .between(!(boolean) map.get("isNew"), SupProductEntity::getUpdateTime , map.get("oldDate"), now) ); if (CollectionUtil.isNotEmpty(supProductList)) { if (dataResponse == null) { dataResponse = new SpsSyncBasicDataResponse(); } dataResponse.setSupProductList(supProductList); } } if (dataResponse != null) { dataResponse.setTaskId(taskId); } return dataResponse; } /** * 获取其他数据模块数据 * * @param info 同步设置 * @param now 当前时间 * @param syncTime 同步时间 -- 传入时不操作exportTime表 * @return */ public SpsSyncOtherDataResponse getOtherData(SyncDataSetEntity info, String taskId, Date now, Date syncTime) { SpsSyncOtherDataResponse dataResponse = null; Map syncTimeMap = new WeakHashMap<>(3); syncTimeMap.put("isNew", true); syncTimeMap.put("oldDate", syncTime); //确认有开启udi码补齐同步 if (needExec(info.getUdiCodeLost())) { Map map; if (syncTime == null) { map = basicExportStatusTimeInfo(now, BasicExportStatusTimeEnum.BASIC_PRODUCTS); } else { map = syncTimeMap; } List ioCodeLostList = ioCodeLostMapper.selectList(Wrappers.lambdaQuery(IoCodeLostEntity.class) .le((boolean) map.get("isNew"), IoCodeLostEntity::getUpdateTime, now) .between(!(boolean) map.get("isNew"), IoCodeLostEntity::getUpdateTime , map.get("oldDate"), now) ); if (CollectionUtil.isNotEmpty(ioCodeLostList)) { dataResponse = new SpsSyncOtherDataResponse(); dataResponse.setIoCodeLostList(ioCodeLostList); } } //确认有开启udi关联关系同步 if (needExec(info.getUdiCodeRel())) { Map map; if (syncTime == null) { map = basicExportStatusTimeInfo(now, BasicExportStatusTimeEnum.IO_CODE_REL); } else { map = syncTimeMap; } List ioCodeRelList = ioCodeRelMapper.selectList(Wrappers.lambdaQuery(IoCodeRelEntity.class) .le((boolean) map.get("isNew"), IoCodeRelEntity::getUpdateTime, now) .between(!(boolean) map.get("isNew"), IoCodeRelEntity::getUpdateTime , map.get("oldDate"), now) ); if (CollectionUtil.isNotEmpty(ioCodeRelList)) { if (dataResponse == null) { dataResponse = new SpsSyncOtherDataResponse(); } dataResponse.setIoCodeRelList(ioCodeRelList); } } if (dataResponse != null) { dataResponse.setTaskId(taskId); } return dataResponse; } private final IBasicBussinessTypeService bussinessTypeService; private final IBasicBusTypeChangeService busTypeChangeService; private final IThrBusTypeOriginService thrBusTypeOriginService; /** * 获取单据类型数据 * * @param info 同步设置 * @param now 当前时间 * @param syncTime 同步时间 -- 传入时不操作exportTime表 * @return */ public SpsSyncBusResponse getDocumentTypeData(SyncDataSetEntity info, String taskId, Date now, Date syncTime) { SpsSyncBusResponse dataResponse = null; Map syncTimeMap = new WeakHashMap<>(3); syncTimeMap.put("isNew", true); syncTimeMap.put("oldDate", syncTime); //确认有开启业务单据类型同步 if (needExec(info.getTypeBus())) { Map map; if (syncTime == null) { map = basicExportStatusTimeInfo(now, BasicExportStatusTimeEnum.BASIC_BUSSINESS_TYPE); } else { map = syncTimeMap; } List bussinessTypeEntities = bussinessTypeService.list(Wrappers.lambdaQuery(BasicBussinessTypeEntity.class) .le((boolean) map.get("isNew"), BasicBussinessTypeEntity::getUpdateTime, now) .between(!(boolean) map.get("isNew"), BasicBussinessTypeEntity::getUpdateTime , map.get("oldDate"), now) ); if (CollectionUtil.isNotEmpty(bussinessTypeEntities)) { dataResponse = new SpsSyncBusResponse(); dataResponse.setBussinessTypeEntities(bussinessTypeEntities); } } //确认有开启扫码单据类型同步 if (needExec(info.getTypeScan())) { Map map; if (syncTime == null) { map = basicExportStatusTimeInfo(now, BasicExportStatusTimeEnum.BASIC_BUSTYPE_CHANGE); } else { map = syncTimeMap; } List busTypeChangeList = busTypeChangeService.list(Wrappers.lambdaQuery(BasicBusTypeChangeEntity.class) .le((boolean) map.get("isNew"), BasicBusTypeChangeEntity::getUpdateTime, now) .between(!(boolean) map.get("isNew"), BasicBusTypeChangeEntity::getUpdateTime , map.get("oldDate"), now) ); if (CollectionUtil.isNotEmpty(busTypeChangeList)) { if (dataResponse == null) { dataResponse = new SpsSyncBusResponse(); } dataResponse.setBusTypeChangeEntities(busTypeChangeList); } } //确认有开启第三方单据类型同步 if (needExec(info.getTypeThird())) { Map map; if (syncTime == null) { map = basicExportStatusTimeInfo(now, BasicExportStatusTimeEnum.THR_BUSTYPE_ORIGIN); } else { map = syncTimeMap; } List thrBusTypeOriginEntities = thrBusTypeOriginService.list(Wrappers.lambdaQuery(ThrBusTypeOriginEntity.class) .le((boolean) map.get("isNew"), ThrBusTypeOriginEntity::getUpdateTime, now) .between(!(boolean) map.get("isNew"), ThrBusTypeOriginEntity::getUpdateTime , map.get("oldDate"), now) ); if (CollectionUtil.isNotEmpty(thrBusTypeOriginEntities)) { if (dataResponse == null) { dataResponse = new SpsSyncBusResponse(); } dataResponse.setThrBusTypeOriginEntities(thrBusTypeOriginEntities); } } if (dataResponse != null) { dataResponse.setTaskId(taskId); } return dataResponse; } /** * 判断是否需要执行 由内向外 * * @param set 需要判断的数据 * @return */ private boolean needExec(int... set) { return Arrays.stream(set).filter(i -> i == 1).findAny().isPresent(); } @Transactional(rollbackFor = Exception.class) public void uploadData(BasicExportTypeEnum exportType, String taskId, Function dataMethod) { Integer status = BasicExportStatusEnum.SUCCESS.getCode(); Object data = dataMethod.apply(this); if (data == null) { return; } BaseResponse baseResponse = spGetHttp.postData(exportType, data); if (baseResponse.getCode() != 20000) { status = BasicExportStatusEnum.FAILED.getCode(); } BasicExportStatusEntity exportStatus = BasicExportStatusEntity.builder() .id(taskId) .status(status) .idDatas(exportType.getKey()) .type(exportType.getRemark()) .startTime(new Date()) .endTime(new Date()) .updateTime(new Date()) .receiveStatus(status) .scheduleType(BasicProcessStatus.SCHEDULE_NORMAL) .remark(baseResponse.getMessage()) .build(); applicationContext.getBean(this.getClass()).insertExportStatus(exportStatus); if (baseResponse.getCode() != 20000) { throw new RuntimeException(baseResponse.getMessage()); } } @Transactional(propagation = Propagation.REQUIRES_NEW) public void insertExportStatus(BasicExportStatusEntity exportStatus) { basicExportService.insertExportStatus(exportStatus); } /** * 插入 basicExportStatusTime表 * * @param date 当前时间 * @param exportStatusTimeEnum key枚举 * @return 最后一次同步时间 返回值需与参数 date 比较,判断是否相等 * @throws Exception */ public Map basicExportStatusTimeInfo(Date date, BasicExportStatusTimeEnum exportStatusTimeEnum) { //防止出现同时调用问题 String redisKey = String.format("spsm-sync-task:%s", exportStatusTimeEnum.getKey()); boolean result = redisUtil.setIfAbsent(redisKey, 1, 10); if (!result) { String errorMsg = String.format("syncIdcSps----process------------同步[%s]重复进入", exportStatusTimeEnum.getRemark()); // logger.info(errorMsg); throw new RuntimeException(errorMsg); } boolean isNew = true; BasicExportStatusTimeEntity timeInfo = basicExportTimeService.getOne(Wrappers.lambdaQuery(BasicExportStatusTimeEntity.class) .eq(BasicExportStatusTimeEntity::getKey, exportStatusTimeEnum.getKey())); if (timeInfo == null) { timeInfo = BasicExportStatusTimeEntity.builder() .key(exportStatusTimeEnum.getKey()) .lastUpdateTime(DateUtil.formatDateTime(date)) .remark(exportStatusTimeEnum.getRemark()) .build(); basicExportTimeService.save(timeInfo); } else { if (StrUtil.isNotEmpty(timeInfo.getLastUpdateTime())) { isNew = false; } basicExportTimeService.update(Wrappers.lambdaUpdate(BasicExportStatusTimeEntity.class) .set(BasicExportStatusTimeEntity::getLastUpdateTime, DateUtil.formatDateTime(date)) .eq(BasicExportStatusTimeEntity::getKey, exportStatusTimeEnum.getKey()) ); } redisUtil.del(redisKey); WeakHashMap returnMap = new WeakHashMap<>(10); returnMap.put("isNew", isNew); returnMap.put("oldDate", isNew ? null : timeInfo.getLastUpdateTime()); returnMap.put("id", timeInfo.getId()); return returnMap; } //上传最近更新单据类型 public BaseResponse uploadAllBus(String syncTime) { SpsSyncDataRequest spsSyncDataRequest = new SpsSyncDataRequest(); if (syncTime == null) { BasicExportStatusTimeEntity basicExportStatusTimeEntity = basicExportTimeService.findByKey(ConstantStatus.SYNC_BUS_TYPE); if (basicExportStatusTimeEntity == null) { basicExportStatusTimeEntity = new BasicExportStatusTimeEntity(); basicExportStatusTimeEntity.setLastUpdateTime("1949-10-01 09:00:00"); } spsSyncDataRequest.setLastUpdateTime(basicExportStatusTimeEntity.getLastUpdateTime()); } else { spsSyncDataRequest.setLastUpdateTime(syncTime); } SpsSyncBusResponse spsSyncBusResponse = upBasicService.upAllBus(spsSyncDataRequest); if (CollUtil.isEmpty(spsSyncBusResponse.getBussinessTypeEntities()) && CollUtil.isEmpty(spsSyncBusResponse.getBusTypeChangeEntities()) && CollUtil.isEmpty(spsSyncBusResponse.getThrBusTypeOriginEntities())) { return ResultVOUtils.success("无数据"); } BasicExportStatusEntity basicExportStatusEntity2 = basicExportService.findByData(ConstantStatus.SYNC_BUS_TYPE, 0); if (basicExportStatusEntity2 == null) { BasicExportStatusEntity busTypetatusEntity = new BasicExportStatusEntity(); busTypetatusEntity.setId(CustomUtil.getId()); busTypetatusEntity.setIdDatas(ConstantStatus.SYNC_BUS_TYPE); busTypetatusEntity.setType(BasicProcessStatus.NEW_ALL_BUS); busTypetatusEntity.setUpdateTime(new Date()); busTypetatusEntity.setStartTime(new Date()); busTypetatusEntity.setStatus(0); busTypetatusEntity.setScheduleType(BasicProcessStatus.SCHEDULE_NORMAL); basicExportService.insertExportStatus(busTypetatusEntity); basicExportStatusEntity2 = busTypetatusEntity; } BasicExportStatusTimeEntity basicExportStatusTimeEntity = new BasicExportStatusTimeEntity(); basicExportStatusTimeEntity.setLastUpdateTime(DateUtil.formatDate(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); basicExportStatusTimeEntity.setKey(ConstantStatus.SYNC_BUS_TYPE); spsSyncBusResponse.setTaskId(basicExportStatusEntity2.getId()); BaseResponse baseResponse2 = spGetHttp.postAllBusType(spsSyncBusResponse); if (baseResponse2.getCode() == 20000) { basicExportStatusEntity2.setStatus(1); } else { basicExportStatusEntity2.setStatus(ConstantStatus.SYNC_STATUS_FAIL); } basicExportStatusEntity2.setEndTime(new Date()); basicExportTimeService.insertExportStatus(basicExportStatusTimeEntity); //拼接日志 String logs = ""; if (CollUtil.isNotEmpty(spsSyncBusResponse.getBussinessTypeEntities())) logs = logs + "扫码单据类型:" + spsSyncBusResponse.getBussinessTypeEntities().size() + "条\n"; if (CollUtil.isNotEmpty(spsSyncBusResponse.getBusTypeChangeEntities())) logs = logs + "业务单据类型:" + spsSyncBusResponse.getBusTypeChangeEntities().size() + "条\n"; if (CollUtil.isNotEmpty(spsSyncBusResponse.getThrBusTypeOriginEntities())) logs = logs + "第三方单据类型:" + spsSyncBusResponse.getThrBusTypeOriginEntities().size() + "条\n"; basicExportStatusEntity2.setRemark(logs); basicExportStatusEntity2.setUpdateTime(new Date()); basicExportService.updateExportStatus(basicExportStatusEntity2); return ResultVOUtils.success(); } //上传用户信息 public BaseResponse uploadAllUserData(String syncTime) { SpsSyncDataRequest spsSyncDataRequest = new SpsSyncDataRequest(); if (syncTime == null) { BasicExportStatusTimeEntity basicExportStatusTimeEntity = basicExportTimeService.findByKey(ConstantStatus.SYNC_BASIC_DATA); if (basicExportStatusTimeEntity == null) { basicExportStatusTimeEntity = new BasicExportStatusTimeEntity(); basicExportStatusTimeEntity.setLastUpdateTime("1949-10-01 09:00:00"); } spsSyncDataRequest.setLastUpdateTime(basicExportStatusTimeEntity.getLastUpdateTime()); } else { spsSyncDataRequest.setLastUpdateTime(syncTime); } SpsSyncDataResponse syncDataResponse = upBasicService.uploadAllUserData(spsSyncDataRequest); if (CollUtil.isEmpty(syncDataResponse.getInvWarehouseEntities()) && CollUtil.isEmpty(syncDataResponse.getAuthAdminList()) && CollUtil.isEmpty(syncDataResponse.getDeptEntityList()) && CollUtil.isEmpty(syncDataResponse.getWarehouseBussinessTypeEntities()) && CollUtil.isEmpty(syncDataResponse.getWarehouseUserEntities())) { //数据不记录日志 return ResultVOUtils.success("无数据"); } BasicExportStatusEntity basicExportStatusEntity3 = basicExportService.findByData(ConstantStatus.SYNC_BASIC_DATA, 0); if (basicExportStatusEntity3 == null) { BasicExportStatusEntity basicExportStatusEntity = new BasicExportStatusEntity(); basicExportStatusEntity.setId(CustomUtil.getId()); basicExportStatusEntity.setIdDatas(ConstantStatus.SYNC_BASIC_DATA); basicExportStatusEntity.setType(BasicProcessStatus.NEW_ALL_DATA); basicExportStatusEntity.setUpdateTime(new Date()); basicExportStatusEntity.setStartTime(new Date()); basicExportStatusEntity.setStatus(0); basicExportStatusEntity.setScheduleType(BasicProcessStatus.SCHEDULE_NORMAL); basicExportService.insertExportStatus(basicExportStatusEntity); basicExportStatusEntity3 = basicExportStatusEntity; } BasicExportStatusTimeEntity basicExportStatusTimeEntity = new BasicExportStatusTimeEntity(); basicExportStatusTimeEntity.setLastUpdateTime(DateUtil.formatDate(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); basicExportStatusTimeEntity.setKey(ConstantStatus.SYNC_BASIC_DATA); syncDataResponse.setTaskId(basicExportStatusEntity3.getId()); BaseResponse baseResponse = spGetHttp.postAllUser(syncDataResponse); if (baseResponse.getCode() == 20000) { basicExportStatusEntity3.setStatus(1); } else { basicExportStatusEntity3.setStatus(ConstantStatus.SYNC_STATUS_FAIL); } basicExportStatusEntity3.setEndTime(new Date()); basicExportTimeService.insertExportStatus(basicExportStatusTimeEntity); String logs = ""; if (CollUtil.isNotEmpty(syncDataResponse.getInvWarehouseEntities())) logs = logs + "仓库字典:" + syncDataResponse.getInvWarehouseEntities().size() + "条\n"; if (CollUtil.isNotEmpty(syncDataResponse.getWarehouseUserEntities())) { logs = logs + "仓库用户信息:" + syncDataResponse.getWarehouseUserEntities().size() + "条\n"; } if (CollUtil.isNotEmpty(syncDataResponse.getWarehouseBussinessTypeEntities())) { logs = logs + "仓库单据类型:" + syncDataResponse.getWarehouseBussinessTypeEntities().size() + "条\n"; } if (CollUtil.isNotEmpty(syncDataResponse.getAuthAdminList())) logs = logs + "系统用户信息:" + syncDataResponse.getAuthAdminList().size() + "条\n"; if (CollUtil.isNotEmpty(syncDataResponse.getInvBusUserEntities())) logs = logs + "用户关联单据类型信息:" + syncDataResponse.getInvBusUserEntities().size() + "条\n"; if (CollUtil.isNotEmpty(syncDataResponse.getDeptEntityList())) logs = logs + "部门信息:" + syncDataResponse.getDeptEntityList().size() + "条\n"; if (CollUtil.isNotEmpty(syncDataResponse.getDeptUserEntities())) logs = logs + "部门用户信息:" + syncDataResponse.getDeptUserEntities().size() + "条\n"; if (CollUtil.isNotEmpty(syncDataResponse.getInvSpaces())) logs = logs + "货位信息:" + syncDataResponse.getInvSpaces().size() + "条\n"; basicExportStatusEntity3.setRemark(logs); basicExportStatusEntity3.setUpdateTime(new Date()); basicExportService.updateExportStatus(basicExportStatusEntity3); return ResultVOUtils.success(); } //下载最近更新扫码单据--上级服务 @Transactional(propagation = Propagation.NESTED) public void dlAllOrder() { BaseResponse> baseResponse = spGetHttp.getBasicStatus(BasicProcessStatus.NEW_ALL_ORDER + ""); List pageSimpleResponse = baseResponse != null ? baseResponse.getData() : new ArrayList<>(); if (pageSimpleResponse != null) { List basicExportStatusEntities = pageSimpleResponse; if (basicExportStatusEntities != null && basicExportStatusEntities.size() > 0) { for (BasicExportStatusEntity basicExportStatusEntity : basicExportStatusEntities) { String datas = spGetHttp.getBasicData(basicExportStatusEntity.getId(), BasicProcessStatus.NEW_ALL_ORDER); BaseResponse response = JSONObject.parseObject(datas, new TypeReference>() { }); if (response.getCode() == 20000 && response.getData() != null) { SpsSyncOrderResponse syncDataResponse = JSONObject.parseObject(response.getData(), new TypeReference() { }); BasicDownloadStatusEntity basicDownloadStatusEntity = new BasicDownloadStatusEntity(); basicDownloadStatusEntity.setId(CustomUtil.getId()); basicDownloadStatusEntity.setTaskId(syncDataResponse.getTaskId()); basicDownloadStatusEntity.setStartTime(new Date()); basicDownloadStatusEntity.setIdDatas(ConstantStatus.SYNC_DOWNLOAD_SCAN_ORDER); basicDownloadStatusEntity.setStatus(ConstantStatus.SYNC_STATUS_WAIT); //下载中 basicDownloadStatusEntity.setType(BasicProcessStatus.NEW_ALL_ORDER); basicDownloadStatusEntity.setScheduleType(1); basicDownloadService.insertDownloadStatus(basicDownloadStatusEntity); StringBuffer remark = new StringBuffer(); if (CollUtil.isNotEmpty(syncDataResponse.getOrderEntities())) { try { List orderEntities = syncDataResponse.getOrderEntities(); for (IoOrderEntity orderEntity : orderEntities) { orderEntity.setUpdateTime(null); orderEntity.setFromType(ConstantStatus.FROM_UDISP); orderEntity.setStatus(ConstantStatus.ORDER_STATUS_PROCESS); IoOrderEntity temp = orderService.findByBillNo(orderEntity.getBillNo()); if (temp == null) { orderEntity.setId(null); orderService.insertOrder(orderEntity); insetOrderDb(syncDataResponse, orderEntity); orderEntity = orderService.findByBillNo(orderEntity.getBillNo()); addInoutService.dealProcess(orderEntity); orderEntity = orderService.findByBillNo(orderEntity.getBillNo()); if (orderEntity.getStatus() != ConstantStatus.ORDER_STATS_ERROR && !ioCheckInoutService.checkManual(orderEntity.getBillNo())) { ioCheckInoutService.check(orderEntity.getBillNo()); } } } String logs = ""; if (CollUtil.isNotEmpty(syncDataResponse.getOrderEntities())) logs = logs + "单据:" + syncDataResponse.getOrderEntities().size() + "条\n"; if (CollUtil.isNotEmpty(syncDataResponse.getOrderDetailBizEntities())) logs = logs + "业务详情:" + syncDataResponse.getOrderDetailBizEntities().size() + "条\n"; if (CollUtil.isNotEmpty(syncDataResponse.getOrderDetailCodeEntities())) logs = logs + "单据扫码详情:" + syncDataResponse.getOrderDetailCodeEntities().size() + "条\n"; if (CollUtil.isNotEmpty(syncDataResponse.getOrderDetailResultEntities())) logs = logs + "单据结果详情:" + syncDataResponse.getOrderDetailResultEntities().size() + "条\n"; if (CollUtil.isNotEmpty(syncDataResponse.getCodeEntities())) logs = logs + "单据条码信息:" + syncDataResponse.getCodeEntities().size() + "条\n"; remark.append(logs); } catch (Exception e) { e.printStackTrace(); } } spGetHttp.postBasicStatus(basicExportStatusEntity.getId()); basicDownloadStatusEntity.setUpdateTime(new Date()); basicDownloadStatusEntity.setEndTime(new Date()); basicDownloadStatusEntity.setRemark(remark.toString()); basicDownloadStatusEntity.setStatus(ConstantStatus.SYNC_STATUS_SUCCESS); basicDownloadService.updateDownloadStatus(basicDownloadStatusEntity); } } } } } // 下载最近更新国家库数据-上级服务 @Transactional(propagation = Propagation.NESTED) public void dlAllDiProducts() { BaseResponse> baseResponse = spGetHttp.getBasicStatus(BasicProcessStatus.NEW_ALL_DI + ""); List pageSimpleResponse = baseResponse.getData(); if (pageSimpleResponse != null) { List basicExportStatusEntities = pageSimpleResponse; if (basicExportStatusEntities != null && basicExportStatusEntities.size() > 0) { for (BasicExportStatusEntity basicExportStatusEntity : basicExportStatusEntities) { long curTime = System.currentTimeMillis(); // log.info("--------等待时间------" + curTime); String datas = spGetHttp.getBasicData(basicExportStatusEntity.getId(), BasicProcessStatus.NEW_ALL_DI); // logger.info("--------等待时间1------" + (System.currentTimeMillis() - curTime) / 1000); long curTime2 = System.currentTimeMillis(); BaseResponse response = JSONObject.parseObject(datas, new TypeReference>() { }); // logger.info("--------等待时间2------" + (System.currentTimeMillis() - curTime2) / 1000); if (response.getCode() == 20000 && response.getData() != null) { SpSyncUdiResponse spSyncUdiResponse = response.getData(); BasicDownloadStatusEntity basicDownloadStatusEntity = new BasicDownloadStatusEntity(); basicDownloadStatusEntity.setId(CustomUtil.getId()); basicDownloadStatusEntity.setTaskId(spSyncUdiResponse.getTaskId()); basicDownloadStatusEntity.setStartTime(new Date()); basicDownloadStatusEntity.setIdDatas(ConstantStatus.SYNC_DOWNLOAD_DI_PRODUCTS); basicDownloadStatusEntity.setStatus(ConstantStatus.SYNC_STATUS_WAIT); //下载中 basicDownloadStatusEntity.setType(BasicProcessStatus.NEW_ALL_DI); basicDownloadStatusEntity.setScheduleType(1); basicDownloadService.insertDownloadStatus(basicDownloadStatusEntity); StringBuffer remark = new StringBuffer(); SqlSession batchSession = sqlSessionFactory.openSession(ExecutorType.BATCH, TransactionIsolationLevel.READ_COMMITTED); if (CollUtil.isNotEmpty(spSyncUdiResponse.getUdiCompanyEntities())) { List udiCompanyEntities = spSyncUdiResponse.getUdiCompanyEntities(); UdiCompanyDao mapper = batchSession.getMapper(UdiCompanyDao.class); for (UdiCompanyEntity udiCompanyEntity : udiCompanyEntities) { mapper.insertUdiCompany(udiCompanyEntity); } batchSession.commit(); remark.append("公司信息:").append(spSyncUdiResponse.getUdiCompanyEntities().size()).append("条\n"); } if (CollUtil.isNotEmpty(spSyncUdiResponse.getProductInfoEntityList())) { List productInfoEntities = spSyncUdiResponse.getProductInfoEntityList(); ProductInfoDao mapper = batchSession.getMapper(ProductInfoDao.class); for (ProductInfoEntity productInfoEntity : productInfoEntities) { mapper.insertProductInfo(productInfoEntity); } batchSession.commit(); remark.append("产品信息:").append(spSyncUdiResponse.getProductInfoEntityList().size()).append("条\n"); } batchSession.close(); spGetHttp.postBasicStatus(basicExportStatusEntity.getId()); basicDownloadStatusEntity.setUpdateTime(new Date()); basicDownloadStatusEntity.setEndTime(new Date()); basicDownloadStatusEntity.setRemark(remark.toString()); basicDownloadStatusEntity.setStatus(ConstantStatus.SYNC_STATUS_SUCCESS); basicDownloadService.updateDownloadStatus(basicDownloadStatusEntity); } } } } } private final BasicHospTypeDao basicHospTypeDao; private final UdiRelevanceDao udiRelevanceDao; private final BasicProductsDao basicProductsDao; private final CompanyProductRelevanceDao relevanceDao; private final BasicCorpDao corpDao; private final SupCertDao supCertDao; private final SupCertSetDao supCertSetDao; private final SupCompanyDao supCompanyDao; private final SupManufacturerDao supManufacturerDao; private final SupProductDao supProductDao; /** * 从UDI自助平台拉取 统一方法 * * @param exportType 任务类型 */ @Transactional(rollbackFor = Exception.class) public void pullData(BasicExportTypeEnum exportType) { String data = spGetHttp.pullData(exportType); cn.hutool.json.JSONObject obj = JSONUtil.parseObj(data); Integer code = obj.getInt("code"); if (!code.equals(20000)) { log.error("从UDI自助平台拉取数据错误,报文-----{}", data); return; } //数据内容 String dataStr = obj.getStr("data"); if (StrUtil.isNotBlank(dataStr)) { RelaySyncDto bean = JSONUtil.toBean(dataStr, RelaySyncDto.class); switch (exportType) { case BASIC_DATA: this.insertBasicData(bean.getFileContent()); break; case OTHER_DATA: this.insertOtherData(bean.getFileContent()); break; case DOCUMENT_TYPE_DATA: this.insertDocumentTypeData(bean.getFileContent()); break; case COUNTRY_DI_DATA: break; } String taskId = bean.getTaskId(); if (StrUtil.isNotBlank(taskId)) { //插入下载记录 BasicDownloadStatusEntity basicDownloadStatusEntity = new BasicDownloadStatusEntity(); basicDownloadStatusEntity.setId(CustomUtil.getId()); basicDownloadStatusEntity.setTaskId(taskId); basicDownloadStatusEntity.setStartTime(new Date()); basicDownloadStatusEntity.setUpdateTime(new Date()); basicDownloadStatusEntity.setEndTime(new Date()); basicDownloadStatusEntity.setIdDatas(ConstantStatus.SYNC_DOWNLOAD_BASIC_DATA); basicDownloadStatusEntity.setStatus(ConstantStatus.SYNC_STATUS_SUCCESS); //下载完成 basicDownloadStatusEntity.setType(BasicProcessStatus.BASIC_DATA); basicDownloadStatusEntity.setScheduleType(1); basicDownloadService.insertDownloadStatus(basicDownloadStatusEntity); //通知自助平台任务已完成 spGetHttp.finishTask(taskId); } } } /** * 设置嵌套事物 * * @param content 内容 */ @Transactional(propagation = Propagation.NESTED) public void insertBasicData(String content) { cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(content); List hospTypeList = JSONUtil.toList(jsonObject.getJSONArray(BasicHospTypeEntity.class.getSimpleName()), BasicHospTypeEntity.class); List udiRelevanceList = JSONUtil.toList(jsonObject.getJSONArray(UdiRelevanceEntity.class.getSimpleName()), UdiRelevanceEntity.class); List productsList = JSONUtil.toList(jsonObject.getJSONArray(BasicProductsEntity.class.getSimpleName()), BasicProductsEntity.class); List relevanceList = JSONUtil.toList(jsonObject.getJSONArray(CompanyProductRelevanceEntity.class.getSimpleName()), CompanyProductRelevanceEntity.class); List corpList = JSONUtil.toList(jsonObject.getJSONArray(BasicCorpEntity.class.getSimpleName()), BasicCorpEntity.class); List supCertList = JSONUtil.toList(jsonObject.getJSONArray(SupCertEntity.class.getSimpleName()), SupCertEntity.class); List supCertSetList = JSONUtil.toList(jsonObject.getJSONArray(SupCertSetEntity.class.getSimpleName()), SupCertSetEntity.class); List supCompanyList = JSONUtil.toList(jsonObject.getJSONArray(SupCompanyEntity.class.getSimpleName()), SupCompanyEntity.class); List supManufacturerList = JSONUtil.toList(jsonObject.getJSONArray(SupManufacturerEntity.class.getSimpleName()), SupManufacturerEntity.class); List supProductList = JSONUtil.toList(jsonObject.getJSONArray(SupProductEntity.class.getSimpleName()), SupProductEntity.class); if (CollectionUtil.isNotEmpty(hospTypeList)) { basicHospTypeDao.insertOrUpdateBatch(hospTypeList); } if (CollectionUtil.isNotEmpty(udiRelevanceList)) { udiRelevanceDao.insertOrUpdateBatch(udiRelevanceList); } if (CollectionUtil.isNotEmpty(productsList)) { basicProductsDao.insertOrUpdateBatch(productsList); } if (CollectionUtil.isNotEmpty(relevanceList)) { relevanceDao.insertOrUpdateBatch(relevanceList); } if (CollectionUtil.isNotEmpty(corpList)) { corpDao.insertOrUpdateBatch(corpList); } if (CollectionUtil.isNotEmpty(supCertList)) { supCertDao.insertOrUpdateBatch(supCertList); } if (CollectionUtil.isNotEmpty(supCertSetList)) { supCertSetDao.insertOrUpdateBatch(supCertSetList); } if (CollectionUtil.isNotEmpty(supCompanyList)) { supCompanyDao.insertOrUpdateBatch(supCompanyList); } if (CollectionUtil.isNotEmpty(supManufacturerList)) { supManufacturerDao.insertOrUpdateBatch(supManufacturerList); } if (CollectionUtil.isNotEmpty(supProductList)) { supProductDao.insertOrUpdateBatch(supProductList); } } private final IoCodeLostMapper ioCodeLostMapper; private final IoCodeRelMapper ioCodeRelMapper; /** * 设置嵌套事物 * * @param content 内容 */ @Transactional(propagation = Propagation.NESTED) public void insertOtherData(String content) { cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(content); List ioCodeLostList = JSONUtil.toList(jsonObject.getJSONArray(IoCodeLostEntity.class.getSimpleName()), IoCodeLostEntity.class); List ioCodeRelList = JSONUtil.toList(jsonObject.getJSONArray(IoCodeRelEntity.class.getSimpleName()), IoCodeRelEntity.class); if (CollectionUtil.isNotEmpty(ioCodeLostList)) { ioCodeLostMapper.insertOrUpdateBatch(ioCodeLostList); } if (CollectionUtil.isNotEmpty(ioCodeRelList)) { ioCodeRelMapper.insertOrUpdateBatch(ioCodeRelList); } } private final BasicBussinessTypeDao bussinessTypeDao; private final BasicBusTypeChangeDao busTypeChangeDao; private final ThrBusTypeOriginDao thrBusTypeOriginDao; /** * 设置嵌套事物 * * @param content 内容 */ @Transactional(propagation = Propagation.NESTED) public void insertDocumentTypeData(String content) { cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(content); List bussinessTypeEntities = JSONUtil.toList(jsonObject.getJSONArray(BasicBussinessTypeEntity.class.getSimpleName()), BasicBussinessTypeEntity.class); List busTypeChangeEntities = JSONUtil.toList(jsonObject.getJSONArray(BasicBusTypeChangeEntity.class.getSimpleName()), BasicBusTypeChangeEntity.class); List typeOriginEntities = JSONUtil.toList(jsonObject.getJSONArray(ThrBusTypeOriginEntity.class.getSimpleName()), ThrBusTypeOriginEntity.class); if (CollectionUtil.isNotEmpty(bussinessTypeEntities)) { bussinessTypeDao.insertOrUpdateBatch(bussinessTypeEntities); } if (CollectionUtil.isNotEmpty(busTypeChangeEntities)) { busTypeChangeDao.insertOrUpdateBatch(busTypeChangeEntities); } if (CollectionUtil.isNotEmpty(typeOriginEntities)) { thrBusTypeOriginDao.insertOrUpdateBatch(typeOriginEntities); } } /** * 从UDI自助平台拉取基础数据 */ @Transactional(rollbackFor = Exception.class) public void pullBasicData() { String data = spGetHttp.pullBasicData(); cn.hutool.json.JSONObject obj = JSONUtil.parseObj(data); Integer code = obj.getInt("code"); if (!code.equals(ResultVOUtils.success().getCode())) { log.error("从UDI自助平台拉取基础数据错误,报文-----{}", data); return; } SpsSyncBasicDataResponse bean = obj.get("data", SpsSyncBasicDataResponse.class); this.insertBasicData(bean); String taskId = bean.getTaskId(); if (StrUtil.isNotBlank(taskId)) { //插入下载记录 BasicDownloadStatusEntity basicDownloadStatusEntity = new BasicDownloadStatusEntity(); basicDownloadStatusEntity.setId(CustomUtil.getId()); basicDownloadStatusEntity.setTaskId(taskId); basicDownloadStatusEntity.setStartTime(new Date()); basicDownloadStatusEntity.setUpdateTime(new Date()); basicDownloadStatusEntity.setEndTime(new Date()); basicDownloadStatusEntity.setIdDatas(ConstantStatus.SYNC_DOWNLOAD_BASIC_DATA); basicDownloadStatusEntity.setStatus(ConstantStatus.SYNC_STATUS_SUCCESS); //下载完成 basicDownloadStatusEntity.setType(BasicProcessStatus.BASIC_DATA); basicDownloadStatusEntity.setScheduleType(1); basicDownloadService.insertDownloadStatus(basicDownloadStatusEntity); //通知自助平台任务已完成 spGetHttp.finishTask(taskId); } } /** * 从UDI自助平台拉取其他数据 */ @Transactional(rollbackFor = Exception.class) public void pullOtherData() { String data = spGetHttp.pullOtherData(); cn.hutool.json.JSONObject obj = JSONUtil.parseObj(data); Integer code = obj.getInt("code"); if (!code.equals(ResultVOUtils.success().getCode())) { log.error("从UDI自助平台拉取其他数据,报文-----{}", data); return; } SpsSyncOtherDataResponse bean = obj.get("data", SpsSyncOtherDataResponse.class); this.insertOtherData(bean); String taskId = bean.getTaskId(); if (StrUtil.isNotBlank(taskId)) { //插入下载记录 BasicDownloadStatusEntity basicDownloadStatusEntity = new BasicDownloadStatusEntity(); basicDownloadStatusEntity.setId(CustomUtil.getId()); basicDownloadStatusEntity.setTaskId(taskId); basicDownloadStatusEntity.setStartTime(new Date()); basicDownloadStatusEntity.setUpdateTime(new Date()); basicDownloadStatusEntity.setEndTime(new Date()); basicDownloadStatusEntity.setIdDatas(ConstantStatus.SYNC_DOWNLOAD_OTHER_DATA); basicDownloadStatusEntity.setStatus(ConstantStatus.SYNC_STATUS_SUCCESS); //下载完成 basicDownloadStatusEntity.setType(BasicProcessStatus.OTHER_DATA); basicDownloadStatusEntity.setScheduleType(1); basicDownloadService.insertDownloadStatus(basicDownloadStatusEntity); //通知自助平台任务已完成 spGetHttp.finishTask(taskId); } } /** * 设置嵌套事物 * * @param bean */ @Transactional(propagation = Propagation.NESTED) public void insertBasicData(SpsSyncBasicDataResponse bean) { if (CollectionUtil.isNotEmpty(bean.getHospTypeList())) { basicHospTypeDao.insertOrUpdateBatch(bean.getHospTypeList()); } if (CollectionUtil.isNotEmpty(bean.getUdiRelevanceList())) { udiRelevanceDao.insertOrUpdateBatch(bean.getUdiRelevanceList()); } if (CollectionUtil.isNotEmpty(bean.getProductsList())) { basicProductsDao.insertOrUpdateBatch(bean.getProductsList()); } if (CollectionUtil.isNotEmpty(bean.getRelevanceList())) { relevanceDao.insertOrUpdateBatch(bean.getRelevanceList()); } if (CollectionUtil.isNotEmpty(bean.getCorpList())) { corpDao.insertOrUpdateBatch(bean.getCorpList()); } if (CollectionUtil.isNotEmpty(bean.getSupCertList())) { supCertDao.insertOrUpdateBatch(bean.getSupCertList()); } if (CollectionUtil.isNotEmpty(bean.getSupCertSetList())) { supCertSetDao.insertOrUpdateBatch(bean.getSupCertSetList()); } if (CollectionUtil.isNotEmpty(bean.getSupCompanyList())) { supCompanyDao.insertOrUpdateBatch(bean.getSupCompanyList()); } if (CollectionUtil.isNotEmpty(bean.getSupManufacturerList())) { supManufacturerDao.insertOrUpdateBatch(bean.getSupManufacturerList()); } if (CollectionUtil.isNotEmpty(bean.getSupProductList())) { supProductDao.insertOrUpdateBatch(bean.getSupProductList()); } } /** * 设置嵌套事物 * * @param bean */ @Transactional(propagation = Propagation.NESTED) public void insertOtherData(SpsSyncOtherDataResponse bean) { if (CollectionUtil.isNotEmpty(bean.getIoCodeLostList())) { ioCodeLostMapper.insertOrUpdateBatch(bean.getIoCodeLostList()); } if (CollectionUtil.isNotEmpty(bean.getIoCodeRelList())) { ioCodeRelMapper.insertOrUpdateBatch(bean.getIoCodeRelList()); } } public void insetOrderDb(SpsSyncOrderResponse syncDataResponse, IoOrderEntity orderEntity) { //更新码详情 if (CollUtil.isNotEmpty(syncDataResponse.getOrderDetailCodeEntities())) { List warehouseEntityList = syncDataResponse.getCodeEntities(); for (IoCodeEntity warehouseEntity : warehouseEntityList) { if (warehouseEntity.getOrderId().equals(orderEntity.getBillNo())) { IoCodeTempEntity codeTempEntity = new IoCodeTempEntity(); BeanUtils.copyProperties(warehouseEntity, codeTempEntity); codeTempEntity.setId(null); codeTempService.insert(codeTempEntity); } } } //更新业务详情 if (CollUtil.isNotEmpty(syncDataResponse.getOrderDetailBizEntities())) { List orderDetailBizEntities = syncDataResponse.getOrderDetailBizEntities(); for (IoOrderDetailBizEntity bizEntity : orderDetailBizEntities) { if (bizEntity.getOrderIdFk().equals(orderEntity.getBillNo())) { bizEntity.setId(null); orderDetailBizService.insert(bizEntity); } } } //更新码详情 if (CollUtil.isNotEmpty(syncDataResponse.getOrderDetailCodeEntities())) { List orderDetailCodeEntities = syncDataResponse.getOrderDetailCodeEntities(); for (IoOrderDetailCodeEntity codeEntity : orderDetailCodeEntities) { if (codeEntity.getOrderIdFk().equals(orderEntity.getBillNo())) { codeEntity.setId(null); orderDetailCodeService.insert(codeEntity); } } } //更新结果详情 if (CollUtil.isNotEmpty(syncDataResponse.getOrderDetailResultEntities())) { List orderDetailResultEntities = syncDataResponse.getOrderDetailResultEntities(); for (IoOrderDetailResultEntity detailResultEntity : orderDetailResultEntities) { if (detailResultEntity.getOrderIdFk().equals(orderEntity.getBillNo())) { detailResultEntity.setId(null); orderDetailResultService.insert(detailResultEntity); } } } //更新发票信息 if (CollUtil.isNotEmpty(syncDataResponse.getOrderInvoiceEntities())) { List orderInvoiceEntities = syncDataResponse.getOrderInvoiceEntities(); for (IoOrderInvoiceEntity orderInvoiceEntity : orderInvoiceEntities) { if (orderInvoiceEntity.getOrderIdFk().equals(orderEntity.getBillNo())) { orderInvoiceService.insertInvoice(orderInvoiceEntity); } } } } /** * 同步第三方基础数据 * * @param syncTime 同步时间 */ public BaseResponse uploadThrData(String syncTime) { SpsSyncDataRequest spsSyncDataRequest = new SpsSyncDataRequest(); if (syncTime == null) { BasicExportStatusTimeEntity basicExportStatusTimeEntity = basicExportTimeService.findByKey(ConstantStatus.SYNC_THR_DATA); if (basicExportStatusTimeEntity == null) { basicExportStatusTimeEntity = new BasicExportStatusTimeEntity(); basicExportStatusTimeEntity.setLastUpdateTime("1949-10-01 09:00:00"); } spsSyncDataRequest.setLastUpdateTime(basicExportStatusTimeEntity.getLastUpdateTime()); } else { spsSyncDataRequest.setLastUpdateTime(syncTime); } SpsSyncThrDataResponse spsSyncThrDataResponse = upThrDataService.upThrData(spsSyncDataRequest); if (CollUtil.isEmpty(spsSyncThrDataResponse.getDeptEntities()) && CollUtil.isEmpty(spsSyncThrDataResponse.getInvWarehouseEntities()) && CollUtil.isEmpty(spsSyncThrDataResponse.getCorpEntities())) { //无数据不记录 log.info("无第三方基础数据需要同步"); return ResultVOUtils.success("无数据"); } BasicExportStatusEntity basicExportStatusEntity1 = basicExportService.findByData(ConstantStatus.SYNC_THR_DATA, 0); if (basicExportStatusEntity1 == null) { BasicExportStatusEntity thrDataStatusEntity = new BasicExportStatusEntity(); thrDataStatusEntity.setId(CustomUtil.getId()); thrDataStatusEntity.setIdDatas(ConstantStatus.SYNC_THR_DATA); thrDataStatusEntity.setType(BasicProcessStatus.NEW_ALL_THR_DATA); thrDataStatusEntity.setUpdateTime(new Date()); thrDataStatusEntity.setStartTime(new Date()); thrDataStatusEntity.setStatus(ConstantStatus.SYNC_STATUS_WAIT); thrDataStatusEntity.setScheduleType(BasicProcessStatus.SCHEDULE_NORMAL); basicExportService.insertExportStatus(thrDataStatusEntity); basicExportStatusEntity1 = thrDataStatusEntity; } BasicExportStatusTimeEntity basicExportStatusTimeEntity = new BasicExportStatusTimeEntity(); basicExportStatusTimeEntity.setLastUpdateTime(DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss")); basicExportStatusTimeEntity.setKey(ConstantStatus.SYNC_THR_DATA); spsSyncThrDataResponse.setTaskId(basicExportStatusEntity1.getId()); BaseResponse baseResponse = spGetHttp.postThrData(spsSyncThrDataResponse); if (baseResponse.getCode() == 20000) { basicExportStatusEntity1.setStatus(ConstantStatus.SYNC_STATUS_SUCCESS); } else { basicExportStatusEntity1.setStatus(ConstantStatus.SYNC_STATUS_FAIL); } basicExportStatusEntity1.setEndTime(new Date()); basicExportTimeService.insertExportStatus(basicExportStatusTimeEntity); //拼接日志 StringBuilder logs = new StringBuilder(); if (CollUtil.isNotEmpty(spsSyncThrDataResponse.getDeptEntities())) { logs.append("第三方部门信息:").append(spsSyncThrDataResponse.getDeptEntities().size()).append("条\n"); } if (CollUtil.isNotEmpty(spsSyncThrDataResponse.getInvWarehouseEntities())) { logs.append("第三方仓库信息:").append(spsSyncThrDataResponse.getInvWarehouseEntities().size()).append("条\n"); } if (CollUtil.isNotEmpty(spsSyncThrDataResponse.getCorpEntities())) { logs.append("第三方往来单位信息:").append(spsSyncThrDataResponse.getCorpEntities().size()).append("条\n"); } basicExportStatusEntity1.setRemark(logs.toString()); basicExportStatusEntity1.setUpdateTime(new Date()); basicExportService.updateExportStatus(basicExportStatusEntity1); return ResultVOUtils.success(); } /** * 同步系统设置 * * @param syncTime 同步时间 * @return */ public BaseResponse uploadSysSetting(String syncTime) { SpsSyncDataRequest spsSyncDataRequest = new SpsSyncDataRequest(); if (syncTime == null) { BasicExportStatusTimeEntity basicExportStatusTimeEntity = basicExportTimeService.findByKey(ConstantStatus.SYNC_SYS_SETTING); if (basicExportStatusTimeEntity == null) { basicExportStatusTimeEntity = new BasicExportStatusTimeEntity(); basicExportStatusTimeEntity.setLastUpdateTime("1949-10-01 09:00:00"); } spsSyncDataRequest.setLastUpdateTime(basicExportStatusTimeEntity.getLastUpdateTime()); } else { spsSyncDataRequest.setLastUpdateTime(syncTime); } SpsSyncSysSettingResponse spsSyncSysSettingResponse = upBasicService.upSysSetting(spsSyncDataRequest); if (BeanUtil.isEmpty(spsSyncSysSettingResponse)) { //无数据不记录 log.info("无第系统设置需要同步"); return ResultVOUtils.success("无数据"); } BasicExportStatusEntity basicExportStatusEntity1 = basicExportService.findByData(ConstantStatus.SYNC_SYS_SETTING, 0); if (basicExportStatusEntity1 == null) { BasicExportStatusEntity thrDataStatusEntity = new BasicExportStatusEntity(); thrDataStatusEntity.setId(CustomUtil.getId()); thrDataStatusEntity.setIdDatas(ConstantStatus.SYNC_SYS_SETTING); thrDataStatusEntity.setType(BasicProcessStatus.NEW_ALL_SYS_SETTING); thrDataStatusEntity.setUpdateTime(new Date()); thrDataStatusEntity.setStartTime(new Date()); thrDataStatusEntity.setStatus(ConstantStatus.SYNC_STATUS_WAIT); thrDataStatusEntity.setScheduleType(BasicProcessStatus.SCHEDULE_NORMAL); basicExportService.insertExportStatus(thrDataStatusEntity); basicExportStatusEntity1 = thrDataStatusEntity; } BasicExportStatusTimeEntity basicExportStatusTimeEntity = new BasicExportStatusTimeEntity(); basicExportStatusTimeEntity.setLastUpdateTime(DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss")); basicExportStatusTimeEntity.setKey(ConstantStatus.SYNC_SYS_SETTING); spsSyncSysSettingResponse.setTaskId(basicExportStatusEntity1.getId()); spsSyncSysSettingResponse.setType(basicExportStatusEntity1.getType()); spsSyncSysSettingResponse.setIdDatas(basicExportStatusEntity1.getIdDatas()); BaseResponse baseResponse = spGetHttp.postSysSetting(spsSyncSysSettingResponse); //若模板记录不为空,则将模板文件同步过去 String uploadFileLog = ""; if (CollUtil.isNotEmpty(spsSyncSysSettingResponse.getSystemPDFTemplateEntities())) { // List> list = new ArrayList<>(spsSyncSysSettingResponse.getSystemPDFTemplateEntities().size() * 2); List list = new ArrayList<>(); spsSyncSysSettingResponse.getSystemPDFTemplateEntities().forEach(systemPDFTemplateEntity -> { list.add(systemPDFTemplateEntity.getPath()); }); BaseResponse response = spGetHttp.postTemplateFile(list); if (response.getCode() == 20000) { log.info("模板文件列表上传成功"); uploadFileLog = "\n模板文件列表上传成功"; } else { log.info("模板文件上传失败,响应信息:{}", response.getMessage()); uploadFileLog = "\n模板文件列表上传失败,错误信息:" + response.getMessage(); } } if (baseResponse.getCode() == 20000) { basicExportStatusEntity1.setStatus(ConstantStatus.SYNC_STATUS_SUCCESS); } else { basicExportStatusEntity1.setStatus(ConstantStatus.SYNC_STATUS_FAIL); } basicExportStatusEntity1.setEndTime(new Date()); basicExportTimeService.insertExportStatus(basicExportStatusTimeEntity); basicExportStatusEntity1.setRemark(spsSyncSysSettingResponse.logs() + uploadFileLog); basicExportStatusEntity1.setUpdateTime(new Date()); basicExportService.updateExportStatus(basicExportStatusEntity1); return ResultVOUtils.success(); } /** * 同步第三方产品数据 * * @param syncTime * @return */ public BaseResponse uploadThrProducts(String syncTime) { SpsSyncDataRequest spsSyncDataRequest = new SpsSyncDataRequest(); if (syncTime == null) { BasicExportStatusTimeEntity basicExportStatusTimeEntity = basicExportTimeService.findByKey(ConstantStatus.SYNC_THR_PRODUCTS); if (basicExportStatusTimeEntity == null) { basicExportStatusTimeEntity = new BasicExportStatusTimeEntity(); basicExportStatusTimeEntity.setLastUpdateTime("1949-10-01 09:00:00"); } spsSyncDataRequest.setLastUpdateTime(basicExportStatusTimeEntity.getLastUpdateTime()); } else { spsSyncDataRequest.setLastUpdateTime(syncTime); } SpsSyncThrProductsResponse spsSyncThrProductsResponse = upThrDataService.upThrProducts(spsSyncDataRequest); if (CollUtil.isEmpty(spsSyncThrProductsResponse.getProductsEntities())) { //无数据不记录 log.info("无第三方产品数据需要同步"); return ResultVOUtils.success("无数据"); } BasicExportStatusEntity basicExportStatusEntity1 = basicExportService.findByData(ConstantStatus.SYNC_THR_PRODUCTS, 0); if (basicExportStatusEntity1 == null) { BasicExportStatusEntity thrDataStatusEntity = new BasicExportStatusEntity(); thrDataStatusEntity.setId(CustomUtil.getId()); thrDataStatusEntity.setIdDatas(ConstantStatus.SYNC_THR_PRODUCTS); thrDataStatusEntity.setType(BasicProcessStatus.NEW_ALL_THR_PRODUCTS); thrDataStatusEntity.setUpdateTime(new Date()); thrDataStatusEntity.setStartTime(new Date()); thrDataStatusEntity.setStatus(ConstantStatus.SYNC_STATUS_WAIT); thrDataStatusEntity.setScheduleType(BasicProcessStatus.SCHEDULE_NORMAL); basicExportService.insertExportStatus(thrDataStatusEntity); basicExportStatusEntity1 = thrDataStatusEntity; } BasicExportStatusTimeEntity basicExportStatusTimeEntity = new BasicExportStatusTimeEntity(); basicExportStatusTimeEntity.setLastUpdateTime(DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss")); basicExportStatusTimeEntity.setKey(ConstantStatus.SYNC_THR_PRODUCTS); spsSyncThrProductsResponse.setTaskId(basicExportStatusEntity1.getId()); BaseResponse baseResponse = spGetHttp.postThrProducts(spsSyncThrProductsResponse); if (baseResponse.getCode() == 20000) { basicExportStatusEntity1.setStatus(ConstantStatus.SYNC_STATUS_SUCCESS); } else { basicExportStatusEntity1.setStatus(ConstantStatus.SYNC_STATUS_FAIL); } basicExportStatusEntity1.setEndTime(new Date()); basicExportTimeService.insertExportStatus(basicExportStatusTimeEntity); //拼接日志 StringBuilder logs = new StringBuilder(); if (CollUtil.isNotEmpty(spsSyncThrProductsResponse.getProductsEntities())) { logs.append("第三方产品信息:").append(spsSyncThrProductsResponse.getProductsEntities().size()).append("条\n"); } basicExportStatusEntity1.setRemark(logs.toString()); basicExportStatusEntity1.setUpdateTime(new Date()); basicExportService.updateExportStatus(basicExportStatusEntity1); return ResultVOUtils.success(); } }