|
|
|
@ -2,7 +2,15 @@ package hw.tagApi.service.service.impl;
|
|
|
|
|
|
|
|
|
|
import hw.tagApi.service.service.IKDocsService;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.http.*;
|
|
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
import org.springframework.web.util.UriComponentsBuilder;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
@ -10,8 +18,15 @@ import java.util.Map;
|
|
|
|
|
|
|
|
|
|
import hw.tagApi.service.constant.ApiConstants;
|
|
|
|
|
import hw.tagApi.service.mapper.HwTagRecordMapper;
|
|
|
|
|
import hw.tagApi.service.domain.HwTagRecord;
|
|
|
|
|
import hw.tagApi.common.utils.poi.TagExcelUtil;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.apache.http.HttpEntity;
|
|
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 云文档服务实现类
|
|
|
|
@ -24,7 +39,108 @@ public class KDocsServiceImpl implements IKDocsService
|
|
|
|
|
{
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(KDocsServiceImpl.class);
|
|
|
|
|
|
|
|
|
|
private HwTagRecordMapper hwTagRecordMapper;
|
|
|
|
|
@Autowired
|
|
|
|
|
private HwTagRecordMapper hwTagRecordMapper;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private CloseableHttpClient httpClient;
|
|
|
|
|
|
|
|
|
|
private static final int BATCH_SIZE = 10;
|
|
|
|
|
private List<HwTagRecord> batchList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String importData(String url) {
|
|
|
|
|
try {
|
|
|
|
|
log.info("开始从金山文档下载Excel文件: {}", url);
|
|
|
|
|
|
|
|
|
|
// 创建HttpGet请求
|
|
|
|
|
HttpGet httpGet = new HttpGet(url);
|
|
|
|
|
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
|
|
|
|
|
httpGet.setHeader("Accept", "*/*");
|
|
|
|
|
httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");
|
|
|
|
|
httpGet.setHeader("Connection", "keep-alive");
|
|
|
|
|
|
|
|
|
|
// 执行请求
|
|
|
|
|
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
|
|
|
|
// 获取响应状态码
|
|
|
|
|
int statusCode = response.getStatusLine().getStatusCode();
|
|
|
|
|
if (statusCode != 200) {
|
|
|
|
|
log.error("从金山文档获取文件失败,响应码: {}", statusCode);
|
|
|
|
|
return ApiConstants.SYSTEM_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取响应实体
|
|
|
|
|
HttpEntity entity = response.getEntity();
|
|
|
|
|
if (entity == null) {
|
|
|
|
|
log.error("无法从金山文档获取文件或文件为空");
|
|
|
|
|
return ApiConstants.SYSTEM_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将响应实体转换为字节数组
|
|
|
|
|
byte[] fileData = EntityUtils.toByteArray(entity);
|
|
|
|
|
|
|
|
|
|
if (fileData.length == 0) {
|
|
|
|
|
log.error("无法从金山文档获取文件或文件为空");
|
|
|
|
|
return ApiConstants.SYSTEM_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 直接使用字节流处理Excel数据
|
|
|
|
|
Map<String, Object> excelResult;
|
|
|
|
|
try (ByteArrayInputStream bis = new ByteArrayInputStream(fileData)) {
|
|
|
|
|
excelResult = TagExcelUtil.parseTagExcel(bis);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取订单信息
|
|
|
|
|
Map<String, String> orderInfo = (Map<String, String>) excelResult.get("orderInfo");
|
|
|
|
|
String orderNo = orderInfo.get("orderNo");
|
|
|
|
|
String batchNo = orderInfo.get("batchNo");
|
|
|
|
|
String operatorId = orderInfo.get("operatorId");
|
|
|
|
|
String processTime = orderInfo.get("processTime");
|
|
|
|
|
|
|
|
|
|
log.info("解析订单信息 - 订单号: {}, 批次号: {}, 操作员: {}", orderNo, batchNo, operatorId);
|
|
|
|
|
|
|
|
|
|
// 获取标签数据列表
|
|
|
|
|
List<Map<String, String>> tagList = (List<Map<String, String>>) excelResult.get("tagList");
|
|
|
|
|
|
|
|
|
|
// 批量处理标签数据
|
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
for (Map<String, String> tagData : tagList) {
|
|
|
|
|
HwTagRecord record = new HwTagRecord();
|
|
|
|
|
record.setOrderCode(orderNo);
|
|
|
|
|
record.setBatchNumber(batchNo);
|
|
|
|
|
record.setOperatorId(operatorId);
|
|
|
|
|
record.setProcessingTime(sdf.parse(processTime));
|
|
|
|
|
|
|
|
|
|
// 设置标签数据
|
|
|
|
|
record.setEpc(tagData.get("EPC"));
|
|
|
|
|
record.settId(tagData.get("TID"));
|
|
|
|
|
record.setTagSequence(tagData.get("序号"));
|
|
|
|
|
|
|
|
|
|
// 添加到批处理列表
|
|
|
|
|
batchList.add(record);
|
|
|
|
|
|
|
|
|
|
// 达到批量大小时执行插入
|
|
|
|
|
if (batchList.size() >= BATCH_SIZE) {
|
|
|
|
|
hwTagRecordMapper.batchInsertHwTagRecord(batchList);
|
|
|
|
|
log.info("批量插入{}条数据成功", batchList.size());
|
|
|
|
|
batchList.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理剩余的数据
|
|
|
|
|
if (!batchList.isEmpty()) {
|
|
|
|
|
hwTagRecordMapper.batchInsertHwTagRecord(batchList);
|
|
|
|
|
log.info("批量插入剩余{}条数据成功", batchList.size());
|
|
|
|
|
batchList.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return getSystemStatus();
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("导入数据失败: {}", e.getMessage(), e);
|
|
|
|
|
return ApiConstants.SYSTEM_ERROR;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getSystemStatus() {
|
|
|
|
@ -52,12 +168,6 @@ public class KDocsServiceImpl implements IKDocsService
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String importData(String data) {
|
|
|
|
|
// TODO: 实现数据导入逻辑
|
|
|
|
|
return getSystemStatus();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String deleteData(String id) {
|
|
|
|
|
// TODO: 实现数据删除逻辑
|
|
|
|
@ -75,7 +185,4 @@ public class KDocsServiceImpl implements IKDocsService
|
|
|
|
|
// TODO: 实现数据导出逻辑
|
|
|
|
|
return getSystemStatus();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|