diff --git a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/entity/PackageInfoEntity.java b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/entity/PackageInfoEntity.java index befbb9e71..a6c44f1d8 100644 --- a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/entity/PackageInfoEntity.java +++ b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/entity/PackageInfoEntity.java @@ -23,6 +23,8 @@ public class PackageInfoEntity extends BaseEntity { private String tenantCode; @ApiModelProperty(name = "日志id") private String logId; + @ApiModelProperty(name = "订单类型") + private String orderType; @ApiModelProperty(name = "发货单id") private String deliveryNoteId; @ApiModelProperty(name = "标签号") diff --git a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/interceptor/LocalServerLoginAccountsInterceptor.java b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/interceptor/LocalServerLoginAccountsInterceptor.java index d448a14d5..e80507d0f 100644 --- a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/interceptor/LocalServerLoginAccountsInterceptor.java +++ b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/interceptor/LocalServerLoginAccountsInterceptor.java @@ -134,8 +134,10 @@ public class LocalServerLoginAccountsInterceptor extends HandlerInterceptorAdapt request = wrappedRequest; RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); return true; - } + }else{ + returnJson(response, JSONObject.toJSONString(R.fail(1, "缺少参数 companyCode"))); + return false; } return false; } catch (Exception e) { diff --git a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/mapper/DeliveryNoteMapper.java b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/mapper/DeliveryNoteMapper.java index 71869ddbb..e42167b0c 100644 --- a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/mapper/DeliveryNoteMapper.java +++ b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/mapper/DeliveryNoteMapper.java @@ -6,6 +6,8 @@ import com.logpm.factorydata.suofeiya.vo.FactoryAuthVO; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; +import java.util.List; + /** * 发货单 mapper * @@ -16,4 +18,6 @@ import org.apache.ibatis.annotations.Param; public interface DeliveryNoteMapper extends BaseMapper { FactoryAuthVO findFactoryAuth(@Param("companyCode") String companyCode); + + List findFactoryAuthByTenantCode(@Param("tenantId") String tenantId); } diff --git a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/mapper/FactoryNodeOrderMapper.xml b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/mapper/FactoryNodeOrderMapper.xml index 90c7fd14d..d64f69c29 100644 --- a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/mapper/FactoryNodeOrderMapper.xml +++ b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/mapper/FactoryNodeOrderMapper.xml @@ -13,4 +13,13 @@ where logistics_code = #{companyCode} + + diff --git a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/mq/FactoryOrderListener.java b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/mq/FactoryOrderListener.java index 4805c5a9e..be0e73323 100644 --- a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/mq/FactoryOrderListener.java +++ b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/mq/FactoryOrderListener.java @@ -1,39 +1,20 @@ package com.logpm.factorydata.suofeiya.mq; -import cn.hutool.core.bean.BeanUtil; -import cn.hutool.core.collection.CollUtil; -import cn.hutool.core.io.FileUtil; -import cn.hutool.core.util.CharsetUtil; -import cn.hutool.core.util.ObjectUtil; -import cn.hutool.core.util.StrUtil; -import cn.hutool.core.util.URLUtil; -import cn.hutool.json.JSONUtil; -import com.baomidou.mybatisplus.core.toolkit.Wrappers; -import com.logpm.factorydata.enums.SaxStatusEnums; -import com.logpm.factorydata.suofeiya.entity.DeliveryNoteEntity; -import com.logpm.factorydata.suofeiya.entity.FactoryOrderLogEntity; -import com.logpm.factorydata.suofeiya.entity.OrderInfoEntity; -import com.logpm.factorydata.suofeiya.entity.PackageInfoEntity; -import com.logpm.factorydata.suofeiya.service.DeliveryNoteService; -import com.logpm.factorydata.suofeiya.service.FactoryOrderLogService; -import com.logpm.factorydata.suofeiya.service.OrderInfoService; -import com.logpm.factorydata.suofeiya.service.PackageInfoService; -import com.logpm.factorydata.suofeiya.vo.DeliveryNoteVO; -import com.logpm.factorydata.suofeiya.vo.OrderInfoVO; -import com.logpm.factorydata.suofeiya.vo.PackageInfoVO; +import com.logpm.factorydata.suofeiya.service.FactoryDataService; +import com.rabbitmq.client.Channel; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springblade.common.constant.factorydata.FactoryDataConstants; -import org.springblade.core.secure.utils.AuthUtil; import org.springframework.amqp.core.ExchangeTypes; import org.springframework.amqp.rabbit.annotation.Exchange; import org.springframework.amqp.rabbit.annotation.Queue; import org.springframework.amqp.rabbit.annotation.QueueBinding; import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.amqp.support.AmqpHeaders; +import org.springframework.messaging.handler.annotation.Header; import org.springframework.stereotype.Component; -import java.util.ArrayList; -import java.util.List; +import java.io.IOException; /** * 监听索菲亚订单数据 @@ -46,92 +27,25 @@ import java.util.List; @AllArgsConstructor public class FactoryOrderListener { - private final DeliveryNoteService deliveryNoteService; - private final OrderInfoService orderInfoService; - private final PackageInfoService packageInfoService; - private final FactoryOrderLogService logService; + private final FactoryDataService factoryDataService; @RabbitListener(bindings = @QueueBinding( value = @Queue(name = FactoryDataConstants.Mq.Queues.SFY_FACTORY_ORDER), exchange = @Exchange(name = FactoryDataConstants.Mq.Exchanges.FACTORY_ORDER, type = ExchangeTypes.TOPIC), key = FactoryDataConstants.Mq.RoutingKeys.SFY_FACTORY_ORDER - )) -// @Transactional(rollbackFor = Exception.class) - public void factoryOrder(String msg) { + ), ackMode = "MANUAL") + public void factoryOrder(String msg, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) { log.info("处理索菲亚订单:{}", msg); - if (StrUtil.isEmpty(msg)) { - log.error("消息内容为空"); - return; + try { + factoryDataService.factoryOrder(msg); + } catch (Exception e) { + e.printStackTrace(); + log.error("处理索菲亚订单失败: {}", e.getMessage()); } - FactoryOrderLogEntity logEntity = JSONUtil.toBean(msg, FactoryOrderLogEntity.class); - if (ObjectUtil.isEmpty(logEntity)) { - log.error("消息内容为空"); - return; - } - // 去 minio 下载文件到本地,然后解析文件内容为实体对象 - DeliveryNoteVO vo = null; - Long logId = logEntity.getId(); - String logUrl = logEntity.getLogUrl(); - if (StrUtil.isNotEmpty(logUrl)) { - List res = new ArrayList<>(); - FileUtil.readLines(URLUtil.url(logUrl), CharsetUtil.CHARSET_UTF_8, res); - if (CollUtil.isNotEmpty(res)) { - String content = res.get(0); - if (StrUtil.isNotEmpty(content)) { - vo = JSONUtil.toBean(content, DeliveryNoteVO.class); - } - } - } - if (ObjectUtil.isNotNull(vo)) { - // 1 解析数据保存入库 - // 车次号唯一 - String logisticID = vo.getLogisticID(); - List list = deliveryNoteService.list(Wrappers.lambdaQuery() - .eq(DeliveryNoteEntity::getLogisticID, logisticID)); - if (CollUtil.isNotEmpty(list)) { - log.error("车次号{} 数据已经处理过了, logId: {}", logisticID, logId); - return; - } - // 处理额外的字段 - if (CollUtil.isNotEmpty(vo.getOrderExtendFields())) { - vo.setOrderExtendField(JSONUtil.toJsonStr(vo.getOrderExtendFields())); - } - vo.setLogId(ObjectUtil.isNotEmpty(logId) ? logId.toString() : null); - vo.setTenantCode(AuthUtil.getTenantId()); - deliveryNoteService.save(vo); - List orderInfos = vo.getOrderInfo(); - if (CollUtil.isNotEmpty(orderInfos)) { - List infoEntities = new ArrayList<>(); - for (OrderInfoVO orderInfo : orderInfos) { - OrderInfoEntity orderInfoEntity = new OrderInfoEntity(); - BeanUtil.copyProperties(orderInfo, orderInfoEntity); - orderInfoEntity.setLogId(ObjectUtil.isNotEmpty(logId) ? logId.toString() : null); - orderInfoEntity.setDeliveryNoteId(vo.getId().toString()); - orderInfoEntity.setTenantCode(AuthUtil.getTenantId()); - infoEntities.add(orderInfoEntity); - } - orderInfoService.saveBatch(infoEntities); - } - if (CollUtil.isNotEmpty(vo.getPackageInfo())) { - List packageInfoEntities = new ArrayList<>(); - for (PackageInfoVO packageInfo : vo.getPackageInfo()) { - PackageInfoEntity entity = new PackageInfoEntity(); - BeanUtil.copyProperties(packageInfo, entity); - entity.setDeliveryNoteId(vo.getId().toString()); - entity.setLogId(ObjectUtil.isNotEmpty(logId) ? logId.toString() : null); - entity.setTenantCode(AuthUtil.getTenantId()); - packageInfoEntities.add(entity); - } - packageInfoService.saveBatch(packageInfoEntities); - } - // 2 构建暂存单,发送 mq 消息 - FactoryOrderLogEntity logEntity1 = new FactoryOrderLogEntity(); - logEntity1.setSaxStatus(SaxStatusEnums.SUCCESS.getCode()); - logEntity1.setId(logId); - logService.saveOrUpdate(logEntity1); - // 处理暂存单 - deliveryNoteService.buildAdvance(vo); + try { + channel.basicAck(tag, false); + } catch (IOException e) { + throw new RuntimeException(e); } } - } diff --git a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/mq/NodeDataPushListener.java b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/mq/NodeDataPushListener.java index 7c2a2d0bb..3f8fc9079 100644 --- a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/mq/NodeDataPushListener.java +++ b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/mq/NodeDataPushListener.java @@ -1,34 +1,20 @@ package com.logpm.factorydata.suofeiya.mq; -import cn.hutool.core.collection.CollUtil; -import cn.hutool.core.date.DateUtil; -import cn.hutool.core.util.EnumUtil; -import cn.hutool.core.util.ObjectUtil; -import cn.hutool.core.util.StrUtil; -import cn.hutool.http.HttpUtil; -import cn.hutool.json.JSONArray; -import cn.hutool.json.JSONObject; -import cn.hutool.json.JSONUtil; -import com.logpm.factorydata.feign.IFactoryDataClient; -import com.logpm.factorydata.suofeiya.enums.FactoryNodeEnums; -import com.logpm.factorydata.suofeiya.enums.NodeMappingEnums; -import com.logpm.factorydata.suofeiya.enums.NodeNeedEnums; -import com.logpm.factorydata.suofeiya.pros.OldProperties; -import com.logpm.factorydata.vo.PushData; +import com.logpm.factorydata.suofeiya.service.FactoryDataService; +import com.rabbitmq.client.Channel; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springblade.common.constant.WorkNodeEnums; import org.springblade.common.constant.factorydata.FactoryDataConstants; import org.springframework.amqp.core.ExchangeTypes; import org.springframework.amqp.rabbit.annotation.Exchange; import org.springframework.amqp.rabbit.annotation.Queue; import org.springframework.amqp.rabbit.annotation.QueueBinding; import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.amqp.support.AmqpHeaders; +import org.springframework.messaging.handler.annotation.Header; import org.springframework.stereotype.Component; -import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; -import java.util.List; +import java.io.IOException; /** * 监听业务系统推送给节点数据 @@ -41,129 +27,26 @@ import java.util.List; @AllArgsConstructor public class NodeDataPushListener { - private final IFactoryDataClient factoryDataClient; - private final OldProperties oldProperties; + private final FactoryDataService factoryDataService; @RabbitListener(bindings = @QueueBinding( value = @Queue(name = FactoryDataConstants.Mq.Queues.SFY_NODE_DATA_PUSH, durable = "true"), exchange = @Exchange(name = FactoryDataConstants.Mq.Exchanges.NODE_DATA_PUSH, type = ExchangeTypes.TOPIC), key = FactoryDataConstants.Mq.RoutingKeys.SFY_NODE_DATA_PUSH - )) -// @Transactional(rollbackFor = Exception.class) - public void nodeDataPush(String msg) { + ), ackMode = "MANUAL") + public void nodeDataPush(String msg, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) { log.info("接收到节点数据推送:{}", msg); - if (checkData(msg)) { - return; + try { + factoryDataService.nodeDataPush(msg); + } catch (Exception e) { + e.printStackTrace(); + log.error("处理索菲亚订单失败: {}", e.getMessage()); } - JSONObject entries = JSONUtil.parseObj(msg); - // 节点 - String node = entries.getStr("node"); - String main = entries.getStr("main"); - WorkNodeEnums workNodeEnums = EnumUtil.fromString(WorkNodeEnums.class, node); - FactoryNodeEnums factoryNode = NodeMappingEnums.getFactoryByNodeAndStatus(workNodeEnums); - // 2 获取业务数据 - List content = entries.getBeanList("content", PushData.class); - if (!ObjectUtil.equal(workNodeEnums.getCode(), NodeNeedEnums.PLAN_DISTRIBUTION.getCode()) || !ObjectUtil.equal(workNodeEnums.getCode(), NodeNeedEnums.PLAN_BILLOFLADING.getCode())) { - // 推送入库出库数据 - // 按订单号和运单号进行分组 - if (CollUtil.isNotEmpty(content)) { - JSONObject jsons = new JSONObject(); - List packages = new ArrayList<>(); - for (PushData pushData : content) { - String packageCode = pushData.getPackageCode(); - // 新系统自动生成的包条码,不用回传 - if (StrUtil.isNotEmpty(packageCode) && packageCode.startsWith("HT")) { - if (packageCode.length() == 22) { - continue; - } - } - JSONObject js = new JSONObject(); - js.set("paNo", packageCode); - js.set("subinventoryCode", ""); - js.set("locatorCode", ""); - js.set("updateDate", DateUtil.now()); - js.set("paStausNo", "正常"); - js.set("logisticsStatus", factoryNode.getText()); - packages.add(js); - } - jsons.set("packageInfo", new JSONArray(packages)); - if (oldProperties.getEnable()) { - try { - log.info("推送节点数据:{}", JSONUtil.toJsonStr(jsons)); - String post = HttpUtil.post(oldProperties.getPushNodeUrl(), JSONUtil.toJsonStr(jsons)); - log.info("推送结果:{}", post); - } catch (Exception e) { - e.printStackTrace(); - log.error("推送节点数据错误:{}", e); - } - } - } - } else { - // 推送配送单计划 - if (CollUtil.isNotEmpty(content)) { - JSONObject jsons = new JSONObject(); - List packages = new ArrayList<>(); - for (PushData pushData : content) { - String packageCode = pushData.getPackageCode(); - // 新系统自动生成的包条码,不用回传 - if (StrUtil.isNotEmpty(packageCode) && packageCode.startsWith("HT")) { - if (packageCode.length() == 22) { - continue; - } - } - packages.add(pushData.getPackageCode()); - } - if (StrUtil.isNotEmpty(main)) { - JSONObject jsonObject = JSONUtil.parseObj(main); - jsons.set("shipPlanNo", jsonObject.getStr("trainNumber")); - jsons.set("receiver", jsonObject.getStr("receiver")); - jsons.set("receiveAddr", jsonObject.getStr("receiveAddr")); - jsons.set("receiveTel", jsonObject.getStr("receiveTel")); - jsons.set("planDeliveryDate", jsonObject.getStr("planDeliveryDate")); - } - jsons.set("paNo", JSONUtil.toJsonStr(packages)); - jsons.set("remark", ""); - jsons.set("orderExtendFields", new JSONArray()); - if (StrUtil.isNotEmpty(oldProperties.getPushNodePlanUrl())) { - try { - log.info("推送节点数据:{}", JSONUtil.toJsonStr(jsons)); - String post = HttpUtil.post(oldProperties.getPushNodePlanUrl(), JSONUtil.toJsonStr(jsons)); - log.info("推送结果:{}", post); - } catch (Exception e) { - e.printStackTrace(); - log.error("推送节点数据错误:{}", e); - } - } - } + try { + channel.basicAck(tag, false); + } catch (IOException e) { + throw new RuntimeException(e); } - - } - - private boolean checkData(String msg) { - if (StrUtil.isEmpty(msg)) { - return true; - } - if (!msg.contains("brand") || !msg.contains("content") || !msg.contains("node")) { - return true; - } - JSONObject entries = JSONUtil.parseObj(msg); - String node = entries.getStr("node"); - if (StrUtil.isEmpty(node)) { - return true; - } - // 不是志邦需要的节点数据直接不处理 - if (!EnumUtil.contains(NodeNeedEnums.class, node)) { - return true; - } - WorkNodeEnums workNodeEnums = EnumUtil.fromString(WorkNodeEnums.class, node); - if (ObjectUtil.isEmpty(workNodeEnums)) { - return true; - } - List content = entries.getBeanList("content", JSONObject.class); - if (CollUtil.isEmpty(content)) { - return true; - } - return false; } } diff --git a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/pros/FactoryDataSuoFeiYaProperties.java b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/pros/FactoryDataSuoFeiYaProperties.java index d744c2f1c..45cdf1d01 100644 --- a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/pros/FactoryDataSuoFeiYaProperties.java +++ b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/pros/FactoryDataSuoFeiYaProperties.java @@ -16,7 +16,11 @@ public class FactoryDataSuoFeiYaProperties { * 颁布给工厂的认证码 */ private String auth; + /** auth过期时间 */ private Long authTime; + /** 回传配置 */ + private PushProperties push; + } diff --git a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/pros/PushProperties.java b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/pros/PushProperties.java new file mode 100644 index 000000000..95db582a2 --- /dev/null +++ b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/pros/PushProperties.java @@ -0,0 +1,32 @@ +package com.logpm.factorydata.suofeiya.pros; + +import lombok.Data; +import org.springframework.stereotype.Component; + +/** + * 老系统配置 + * @author zhaoqiaobo + * @Date 2024/5/29 + **/ +@Data +@Component +public class PushProperties { + + /** + * 回传索菲亚推送地址 + */ + private String pushHost; + + /** + * 寄存仓库存管理接口 paStatusUrl + */ + private String paStatusUrl; + /** + * 配送单计划接口 shipPlanUrl + */ + private String shipPlanUrl; + + /** 是否回传推送工厂 */ + private Boolean pushEnable = true; + +} diff --git a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/service/DeliveryNoteService.java b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/service/DeliveryNoteService.java index 2a5d1fd1c..0ef116cfb 100644 --- a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/service/DeliveryNoteService.java +++ b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/service/DeliveryNoteService.java @@ -18,4 +18,6 @@ public interface DeliveryNoteService extends BaseService { void buildAdvance(DeliveryNoteVO vo); FactoryAuthVO findFactoryAuth(String companyCode); + + FactoryAuthVO findFactoryAuthByTenantCode(String tenantId); } diff --git a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/service/FactoryDataService.java b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/service/FactoryDataService.java new file mode 100644 index 000000000..cc9e4d6e4 --- /dev/null +++ b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/service/FactoryDataService.java @@ -0,0 +1,14 @@ +package com.logpm.factorydata.suofeiya.service; + +/** + * 索菲亚工厂数据处理 服务类 + * + * @Author zqb + * @Date 2024/4/26 + **/ +public interface FactoryDataService { + + void factoryOrder(String msg); + + void nodeDataPush(String msg); +} diff --git a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/service/impl/DeliveryNoteServiceImpl.java b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/service/impl/DeliveryNoteServiceImpl.java index 9489260c3..8c83e0c84 100644 --- a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/service/impl/DeliveryNoteServiceImpl.java +++ b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/service/impl/DeliveryNoteServiceImpl.java @@ -261,7 +261,27 @@ public class DeliveryNoteServiceImpl extends BaseServiceImpl vos = baseMapper.findFactoryAuthByTenantCode(tenantId); + if (CollUtil.isNotEmpty(vos)) { + FactoryAuthVO factoryAuth = vos.get(0); + bladeRedis.setEx(key, JSONUtil.toJsonStr(factoryAuth), Duration.ofDays(1)); + return factoryAuth; + } } } return null; diff --git a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/service/impl/FactoryDataServiceImpl.java b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/service/impl/FactoryDataServiceImpl.java new file mode 100644 index 000000000..09d450ba4 --- /dev/null +++ b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/service/impl/FactoryDataServiceImpl.java @@ -0,0 +1,335 @@ +package com.logpm.factorydata.suofeiya.service.impl; + +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.util.CharsetUtil; +import cn.hutool.core.util.EnumUtil; +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.core.util.URLUtil; +import cn.hutool.crypto.digest.MD5; +import cn.hutool.http.HttpUtil; +import cn.hutool.json.JSONArray; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.logpm.factorydata.enums.SaxStatusEnums; +import com.logpm.factorydata.suofeiya.entity.DeliveryNoteEntity; +import com.logpm.factorydata.suofeiya.entity.FactoryOrderLogEntity; +import com.logpm.factorydata.suofeiya.entity.OrderInfoEntity; +import com.logpm.factorydata.suofeiya.entity.PackageInfoEntity; +import com.logpm.factorydata.suofeiya.enums.FactoryNodeEnums; +import com.logpm.factorydata.suofeiya.enums.NodeMappingEnums; +import com.logpm.factorydata.suofeiya.enums.NodeNeedEnums; +import com.logpm.factorydata.suofeiya.pros.FactoryDataSuoFeiYaProperties; +import com.logpm.factorydata.suofeiya.pros.OldProperties; +import com.logpm.factorydata.suofeiya.service.DeliveryNoteService; +import com.logpm.factorydata.suofeiya.service.FactoryDataService; +import com.logpm.factorydata.suofeiya.service.FactoryOrderLogService; +import com.logpm.factorydata.suofeiya.service.OrderInfoService; +import com.logpm.factorydata.suofeiya.service.PackageInfoService; +import com.logpm.factorydata.suofeiya.vo.DeliveryNoteVO; +import com.logpm.factorydata.suofeiya.vo.FactoryAuthVO; +import com.logpm.factorydata.suofeiya.vo.OrderInfoVO; +import com.logpm.factorydata.suofeiya.vo.PackageInfoVO; +import com.logpm.factorydata.vo.PushData; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springblade.common.constant.WorkNodeEnums; +import org.springblade.core.secure.utils.AuthUtil; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * 索菲亚工厂数据处理 业务实现类 + * + * @Author zqb + * @Date 2024/4/26 + **/ +@Slf4j +@Service +@AllArgsConstructor +public class FactoryDataServiceImpl implements FactoryDataService { + + private final DeliveryNoteService deliveryNoteService; + private final OrderInfoService orderInfoService; + private final PackageInfoService packageInfoService; + private final FactoryOrderLogService logService; + private final OldProperties oldProperties; + private final FactoryDataSuoFeiYaProperties dataSuoFeiYaProperties; + + @Override + @Transactional(rollbackFor = Exception.class) + public void factoryOrder(String msg) { + if (StrUtil.isEmpty(msg)) { + log.error("消息内容为空"); + return; + } + FactoryOrderLogEntity logEntity = JSONUtil.toBean(msg, FactoryOrderLogEntity.class); + if (ObjectUtil.isEmpty(logEntity)) { + log.error("消息内容为空"); + return; + } + // 去 minio 下载文件到本地,然后解析文件内容为实体对象 + DeliveryNoteVO vo = null; + Long logId = logEntity.getId(); + String logUrl = logEntity.getLogUrl(); + if (StrUtil.isNotEmpty(logUrl)) { + List res = new ArrayList<>(); + FileUtil.readLines(URLUtil.url(logUrl), CharsetUtil.CHARSET_UTF_8, res); + if (CollUtil.isNotEmpty(res)) { + String content = res.get(0); + if (StrUtil.isNotEmpty(content)) { + vo = JSONUtil.toBean(content, DeliveryNoteVO.class); + } + } + } + if (ObjectUtil.isNotNull(vo)) { + // 1 解析数据保存入库 + // 车次号唯一 + String logisticID = vo.getLogisticID(); + List list = deliveryNoteService.list(Wrappers.lambdaQuery() + .eq(DeliveryNoteEntity::getLogisticID, logisticID)); + if (CollUtil.isNotEmpty(list)) { + log.error("车次号{} 数据已经处理过了, logId: {}", logisticID, logId); + return; + } + // 处理额外的字段 + if (CollUtil.isNotEmpty(vo.getOrderExtendFields())) { + vo.setOrderExtendField(JSONUtil.toJsonStr(vo.getOrderExtendFields())); + } + vo.setLogId(ObjectUtil.isNotEmpty(logId) ? logId.toString() : null); + vo.setTenantCode(AuthUtil.getTenantId()); + deliveryNoteService.save(vo); + List orderInfos = vo.getOrderInfo(); + if (CollUtil.isNotEmpty(orderInfos)) { + List infoEntities = new ArrayList<>(); + for (OrderInfoVO orderInfo : orderInfos) { + OrderInfoEntity orderInfoEntity = new OrderInfoEntity(); + BeanUtil.copyProperties(orderInfo, orderInfoEntity); + orderInfoEntity.setLogId(ObjectUtil.isNotEmpty(logId) ? logId.toString() : null); + orderInfoEntity.setDeliveryNoteId(vo.getId().toString()); + orderInfoEntity.setTenantCode(AuthUtil.getTenantId()); + infoEntities.add(orderInfoEntity); + } + orderInfoService.saveBatch(infoEntities); + } + if (CollUtil.isNotEmpty(vo.getPackageInfo())) { + List packageInfoEntities = new ArrayList<>(); + for (PackageInfoVO packageInfo : vo.getPackageInfo()) { + PackageInfoEntity entity = new PackageInfoEntity(); + BeanUtil.copyProperties(packageInfo, entity); + entity.setDeliveryNoteId(vo.getId().toString()); + entity.setLogId(ObjectUtil.isNotEmpty(logId) ? logId.toString() : null); + entity.setTenantCode(AuthUtil.getTenantId()); + packageInfoEntities.add(entity); + } + packageInfoService.saveBatch(packageInfoEntities); + } + // 2 构建暂存单,发送 mq 消息 + FactoryOrderLogEntity logEntity1 = new FactoryOrderLogEntity(); + logEntity1.setSaxStatus(SaxStatusEnums.SUCCESS.getCode()); + logEntity1.setId(logId); + logService.saveOrUpdate(logEntity1); + // 处理暂存单 + deliveryNoteService.buildAdvance(vo); + } + } + + @Override + public void nodeDataPush(String msg) { + if (checkData(msg)) { + return; + } + JSONObject entries = JSONUtil.parseObj(msg); + // 节点 + String node = entries.getStr("node"); + String main = entries.getStr("main"); + WorkNodeEnums workNodeEnums = EnumUtil.fromString(WorkNodeEnums.class, node); + FactoryNodeEnums factoryNode = NodeMappingEnums.getFactoryByNodeAndStatus(workNodeEnums); + // 2 获取业务数据 + List content = entries.getBeanList("content", PushData.class); + Map> packageCodeMap = new HashMap<>(); + if (CollUtil.isNotEmpty(content)) { + List packageCodes = content.stream().map(PushData::getPackageCode).collect(Collectors.toList()); + if (CollUtil.isNotEmpty(packageCodes)) { + List list = packageInfoService.list(Wrappers.lambdaQuery() + .select(PackageInfoEntity::getPaNo, PackageInfoEntity::getOrderType) + .in(PackageInfoEntity::getPaNo, packageCodes) + .eq(PackageInfoEntity::getTenantCode, AuthUtil.getTenantId()) + ); + if (CollUtil.isNotEmpty(list)) { + for (PackageInfoEntity packageInfoEntity : list) { + if (packageCodeMap.containsKey(packageInfoEntity.getOrderType())) { + packageCodeMap.get(packageInfoEntity.getOrderType()).add(packageInfoEntity.getPaNo()); + } else { + Set objects = new HashSet<>(); + objects.add(packageInfoEntity.getPaNo()); + packageCodeMap.put(packageInfoEntity.getOrderType(), objects); + } + } + } + } + } + FactoryAuthVO authVO = deliveryNoteService.findFactoryAuthByTenantCode(AuthUtil.getTenantId()); + if (ObjectUtil.isEmpty(authVO)) { + return; + } + String companyCode = authVO.getCompanyCode(); + String appKey = authVO.getAppKey(); + // 获取毫秒值 + Long time = DateUtil.current(); + if (!ObjectUtil.equal(workNodeEnums.getCode(), NodeNeedEnums.PLAN_DISTRIBUTION.getCode()) || !ObjectUtil.equal(workNodeEnums.getCode(), NodeNeedEnums.PLAN_BILLOFLADING.getCode())) { + // 推送入库出库数据 + // 按订单号和运单号进行分组 + if (CollUtil.isNotEmpty(content)) { + for (Map.Entry> entry : packageCodeMap.entrySet()) { + String key = entry.getKey(); + Set value = entry.getValue(); + JSONObject jsons = new JSONObject(); + List packages = new ArrayList<>(); + for (PushData pushData : content) { + String packageCode = pushData.getPackageCode(); + // 新系统自动生成的包条码,不用回传 + if (StrUtil.isNotEmpty(packageCode) && packageCode.startsWith("HT")) { + if (packageCode.length() == 22) { + continue; + } + } + if (value.contains(packageCode)) { + JSONObject js = new JSONObject(); + js.set("paNo", packageCode); + js.set("subinventoryCode", ""); + js.set("locatorCode", ""); + js.set("updateDate", DateUtil.now()); + js.set("paStausNo", "正常"); + js.set("logisticsStatus", factoryNode.getText()); + packages.add(js); + } + } + jsons.set("packageInfo", new JSONArray(packages)); + jsons.set("orderType", key); + String jsonStr = JSONUtil.toJsonStr(jsons); + if (oldProperties.getEnable()) { + try { + log.info("推送节点数据:{}", jsonStr); + String post = HttpUtil.post(oldProperties.getPushNodeUrl(), jsonStr); + log.info("推送结果:{}", post); + } catch (Exception e) { + e.printStackTrace(); + log.error("推送节点数据错误:{}", e); + } + } + if (dataSuoFeiYaProperties.getPush().getPushEnable()) { + String paStatusUrl = dataSuoFeiYaProperties.getPush().getPaStatusUrl(); + sendFactory(companyCode, jsonStr, time, appKey, paStatusUrl); + } + } + } + } else { + // 推送配送单计划 + if (CollUtil.isNotEmpty(content)) { + for (Map.Entry> entry : packageCodeMap.entrySet()) { + String key = entry.getKey(); + Set value = entry.getValue(); + JSONObject jsons = new JSONObject(); + List packages = new ArrayList<>(); + for (PushData pushData : content) { + String packageCode = pushData.getPackageCode(); + // 新系统自动生成的包条码,不用回传 + if (StrUtil.isNotEmpty(packageCode) && packageCode.startsWith("HT")) { + if (packageCode.length() == 22) { + continue; + } + } + if (value.contains(packageCode)) { + packages.add(pushData.getPackageCode()); + } + } + if (StrUtil.isNotEmpty(main)) { + JSONObject jsonObject = JSONUtil.parseObj(main); + jsons.set("shipPlanNo", jsonObject.getStr("trainNumber")); + jsons.set("receiver", jsonObject.getStr("receiver")); + jsons.set("receiveAddr", jsonObject.getStr("receiveAddr")); + jsons.set("receiveTel", jsonObject.getStr("receiveTel")); + jsons.set("planDeliveryDate", jsonObject.getStr("planDeliveryDate")); + } + jsons.set("paNo", JSONUtil.toJsonStr(packages)); + jsons.set("remark", ""); + jsons.set("orderExtendFields", new JSONArray()); + jsons.set("orderType", key); + String jsonStr = JSONUtil.toJsonStr(jsons); + if (oldProperties.getEnable()) { + try { + log.info("推送节点数据:{}", jsonStr); + String post = HttpUtil.post(oldProperties.getPushNodePlanUrl(), jsonStr); + log.info("推送结果:{}", post); + } catch (Exception e) { + e.printStackTrace(); + log.error("推送节点数据错误:{}", e); + } + } + if (dataSuoFeiYaProperties.getPush().getPushEnable()) { + String paStatusUrl = dataSuoFeiYaProperties.getPush().getShipPlanUrl(); + sendFactory(companyCode, jsonStr, time, appKey, paStatusUrl); + } + } + } + } + } + + private void sendFactory(String companyCode, String jsonStr, Long time, String appKey, String paStatusUrl) { + try { + Map param = new HashMap<>(); + param.put("companyCode", companyCode); + param.put("params", jsonStr); + param.put("timestamp", time); + // 加密 + String digest = MD5.create().digestHex(jsonStr + appKey + time); + param.put("digest", digest); + log.info("推送节点数据:{}", JSONUtil.toJsonStr(param)); + String post = HttpUtil.post(dataSuoFeiYaProperties.getPush().getPushHost() + paStatusUrl, param); + log.info("推送结果:{}", post); + } catch (Exception e) { + e.printStackTrace(); + log.error("推送节点数据错误:{}", e); + } + } + + private boolean checkData(String msg) { + if (StrUtil.isEmpty(msg)) { + return true; + } + if (!msg.contains("brand") || !msg.contains("content") || !msg.contains("node")) { + return true; + } + JSONObject entries = JSONUtil.parseObj(msg); + String node = entries.getStr("node"); + if (StrUtil.isEmpty(node)) { + return true; + } + // 不是志邦需要的节点数据直接不处理 + if (!EnumUtil.contains(NodeNeedEnums.class, node)) { + return true; + } + WorkNodeEnums workNodeEnums = EnumUtil.fromString(WorkNodeEnums.class, node); + if (ObjectUtil.isEmpty(workNodeEnums)) { + return true; + } + List content = entries.getBeanList("content", JSONObject.class); + if (CollUtil.isEmpty(content)) { + return true; + } + return false; + } +} diff --git a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/vo/FactoryAuthVO.java b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/vo/FactoryAuthVO.java index 125d9887b..17a652b16 100644 --- a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/vo/FactoryAuthVO.java +++ b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/vo/FactoryAuthVO.java @@ -16,6 +16,8 @@ public class FactoryAuthVO { private String logisticsCode; @ApiModelProperty(name = "秘钥") private String appKey; + @ApiModelProperty(name = "物流公司") + private String companyCode; @ApiModelProperty(name = "租户") private String tenantCode; @ApiModelProperty(name = "登录账号")