package com.gs.xiaomi.service;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.gs.xiaomi.dto.BCS101Request;
|
import com.gs.xiaomi.dto.BCS101Response;
|
import com.gs.xiaomi.dto.SnListItemDto;
|
import com.gs.xiaomi.entity.DeliveryMain;
|
import com.gs.xiaomi.entity.SnListItem;
|
import com.gs.xiaomi.util.SnListItemConverter;
|
import lombok.RequiredArgsConstructor;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.io.IOException;
|
import java.util.List;
|
|
@Service
|
@Transactional(rollbackFor = Exception.class)
|
@RequiredArgsConstructor
|
public class BCS101Service {
|
|
private static final Logger log = LoggerFactory.getLogger(BCS101Service.class);
|
|
private final BCS101ApiService bcs101ApiService;
|
private final DeliveryMainService deliveryMainService;
|
private final SnListItemService snListItemService;
|
|
/**
|
* 根据送货单号获取BCS101条码数据并持久化
|
*
|
* @param asn 送货单号
|
* @throws Exception 处理异常
|
*/
|
public void syncBCS101DataByAsn(String asn) throws Exception {
|
log.info("【BCS101数据同步】开始同步送货单: {}", asn);
|
|
// 查询送货单主表信息
|
LambdaQueryWrapper<DeliveryMain> queryWrapper = new LambdaQueryWrapper<>();
|
queryWrapper.eq(DeliveryMain::getZzasn, asn);
|
DeliveryMain deliveryMain = deliveryMainService.getOne(queryWrapper, false);
|
|
if (deliveryMain == null) {
|
log.error("【BCS101数据同步失败】送货单不存在: {}", asn);
|
throw new RuntimeException("送货单不存在: " + asn);
|
}
|
|
// 创建BCS101请求参数
|
BCS101Request request = new BCS101Request();
|
request.setSupplierId(String.valueOf(Integer.parseInt(deliveryMain.getHubLifnr())));
|
request.setDocNo(deliveryMain.getZzasn());
|
request.setDocType("ASNGR");
|
request.setPageNo(1);
|
request.setPageSize(1000);
|
|
// 调用API获取原始JSON字符串
|
String bcs101Data;
|
try {
|
bcs101Data = bcs101ApiService.getBCS101Data(request);
|
log.debug("【BCS101数据同步】原始响应: {}", bcs101Data);
|
} catch (IOException e) {
|
log.error("【BCS101数据同步失败】调用API异常, 送货单: {}, 异常: {}", asn, e.getMessage(), e);
|
throw new RuntimeException("调用BCS101接口失败: " + e.getMessage(), e);
|
}
|
|
// 解析JSON为BCS101Response对象
|
ObjectMapper objectMapper = new ObjectMapper();
|
try {
|
BCS101Response response = objectMapper.readValue(bcs101Data, BCS101Response.class);
|
|
// 检查响应是否成功
|
if (response.isSuccess()) {
|
// 获取snList数据
|
List<SnListItemDto> snList = response.getBody().getSnList();
|
|
if (snList != null && !snList.isEmpty()) {
|
log.info("【BCS101数据同步】获取到 {} 条SN数据,开始持久化...", snList.size());
|
|
// 转换DTO为Entity,并设置关联信息
|
List<SnListItem> entityList = SnListItemConverter.toEntityList(
|
snList,
|
deliveryMain.getId(), // deliveryMainId - 送货单主表ID
|
deliveryMain.getZzasn() // zzasn - 送货单号
|
);
|
|
// 先删除该送货单已有的SN数据(避免重复)
|
snListItemService.lambdaUpdate()
|
.eq(SnListItem::getZzasn, deliveryMain.getZzasn())
|
.remove();
|
|
// 批量保存到数据库
|
boolean saved = snListItemService.saveBatch(entityList);
|
|
if (saved) {
|
log.info("【BCS101数据同步成功】成功保存 {} 条SN数据到数据库, 送货单: {}", entityList.size(), asn);
|
} else {
|
log.error("【BCS101数据同步失败】保存SN数据失败, 送货单: {}", asn);
|
throw new RuntimeException("保存SN数据失败");
|
}
|
} else {
|
log.warn("【BCS101数据同步】响应中没有SN数据, 送货单: {}", asn);
|
}
|
} else {
|
log.error("【BCS101数据同步失败】接口调用失败, 送货单: {}, 错误: {}", asn, response.getErrorDesc());
|
throw new RuntimeException("BCS101接口调用失败: " + response.getErrorDesc());
|
}
|
|
} catch (JsonProcessingException e) {
|
log.error("【BCS101数据同步失败】JSON解析异常, 送货单: {}, 异常: {}", asn, e.getMessage(), e);
|
throw new RuntimeException("JSON解析失败: " + e.getMessage(), e);
|
}
|
}
|
}
|