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

228 lines
9.9 KiB
Java

package com.glxp.api.service.thrsys;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.glxp.api.exception.JsonException;
import com.glxp.api.service.auth.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glxp.api.dao.thrsys.ThirdAliDrugMapper;
import com.glxp.api.entity.thrsys.ThirdAliDrug;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
@Service
@Slf4j
public class ThirdAliDrugService extends ServiceImpl<ThirdAliDrugMapper, ThirdAliDrug> {
@Resource
private CustomerService customerService;
//解析TXT获取对应的对象列表
@Transactional(rollbackFor = Exception.class)
public void txtUpload(MultipartFile file) {
try {
String fileName = file.getOriginalFilename();
String suffix = FileUtil.getSuffix(fileName);
if (!"txt".equals(suffix)) {
throw new JsonException("仅支持TXT文件类型错误" + suffix);
}
log.info(file.getSize() + "文件长度");
if (file.getSize() > 10 * 1024 * 1024) {
throw new JsonException("上传文件超过10M");
}
List<ThirdAliDrug> thirdAliDrugs = new ArrayList<>();
String customerId = customerService.getCustomerId();
try (BufferedReader br = new BufferedReader(new InputStreamReader(file.getInputStream()))) {
String line;
while ((line = br.readLine()) != null) {
String[] fields = fillEmptyParts(line);
Date createTime = new Date();
if (fields.length == 9) {
ThirdAliDrug medicine = new ThirdAliDrug(
fields[0].trim(),
fields[1].trim(),
fields[2].trim(),
fields[3].trim(),
fields[4].trim(),
fields[5].trim(),
fields[6].trim(),
fields[6].trim(),
fields[7].trim(),
fields[8].trim(),
createTime,
createTime
);
if (StrUtil.isNotEmpty(medicine.getNameCode())) {
int i = this.baseMapper.selectDrugsByNameCodes(medicine.getNameCode());
if (i == 0) {
medicine.setErpId(customerId);
thirdAliDrugs.add(medicine);
if (CollUtil.isNotEmpty(thirdAliDrugs) && thirdAliDrugs.size() >= 500) {
this.baseMapper.saveOrUpdateBatch(thirdAliDrugs);
thirdAliDrugs.clear();
}
}
}
}else
if (fields.length == 10) {
ThirdAliDrug medicine = new ThirdAliDrug(
fields[0].trim(),
fields[1].trim(),
fields[2].trim(),
fields[3].trim(),
fields[4].trim(),
fields[5].trim(),
fields[6].trim(),
fields[7].trim(),
fields[8].trim(),
fields[9].trim(),
createTime,
createTime
);
if (StrUtil.isNotEmpty(medicine.getNameCode())) {
int i = this.baseMapper.selectDrugsByNameCodes(medicine.getNameCode());
if (i == 0) {
medicine.setErpId(customerId);
thirdAliDrugs.add(medicine);
if (CollUtil.isNotEmpty(thirdAliDrugs) && thirdAliDrugs.size() >= 500) {
this.baseMapper.saveOrUpdateBatch(thirdAliDrugs);
thirdAliDrugs.clear();
}
}
}
}else
if (fields.length == 11) {
ThirdAliDrug medicine = new ThirdAliDrug(
fields[0].trim(),
fields[1].trim(),
fields[2].trim(),
fields[3].trim(),
fields[4].trim(),
fields[5].trim(),
fields[7].trim(),
fields[8].trim(),
fields[9].trim(),
fields[10].trim(),
createTime,
createTime
);
if (StrUtil.isNotEmpty(medicine.getNameCode())) {
int i = this.baseMapper.selectDrugsByNameCodes(medicine.getNameCode());
if (i == 0) {
medicine.setErpId(customerId);
thirdAliDrugs.add(medicine);
if (CollUtil.isNotEmpty(thirdAliDrugs) && thirdAliDrugs.size() >= 500) {
this.baseMapper.saveOrUpdateBatch(thirdAliDrugs);
thirdAliDrugs.clear();
}
}
}
}else if (fields.length > 11) {
ThirdAliDrug medicine = new ThirdAliDrug(
fields[0].trim(),
fields[1].trim(),
fields[2].trim(),
fields[3].trim(),
fields[4].trim(),
fields[5].trim(),
fields[7].trim(),
fields[9].trim(),
fields[10].trim(),
fields[11].trim(),
createTime,
createTime
);
if (StrUtil.isNotEmpty(medicine.getNameCode())) {
int i = this.baseMapper.selectDrugsByNameCodes(medicine.getNameCode());
if (i == 0) {
thirdAliDrugs.add(medicine);
if (CollUtil.isNotEmpty(thirdAliDrugs) && thirdAliDrugs.size() >= 500) {
this.baseMapper.saveOrUpdateBatch(thirdAliDrugs);
thirdAliDrugs.clear();
}
}
}
}
}
} catch (IOException e) {
throw new JsonException("上传失败:" + "标识重复");
}
if (CollUtil.isNotEmpty(thirdAliDrugs)) {
this.baseMapper.saveOrUpdateBatch(thirdAliDrugs);
}
} catch (Exception e) {
throw new JsonException("上传失败:" + e.getMessage());
}
}
public static String[] fillEmptyParts(String input) {
// Step 1: Split the input string by commas
String[] parts = input.split(",");
// Step 2: Count the number of commas (which will be one less than the number of parts we want)
// But we need to account for leading/trailing commas as well, so we use a different approach
int commaCount = 0;
boolean inEmptyPart = true; // To handle leading commas
for (char c : input.toCharArray()) {
if (c == ',') {
commaCount++;
inEmptyPart = true; // Next part will be empty until we find non-comma characters
} else if (inEmptyPart && c != ' ') {
inEmptyPart = false; // We found the start of a non-empty part
}
}
// If the input ends with a comma, we need an extra empty part
// But we also need to handle the case where the input is just commas (e.g., ",,,")
// So we use Math.max to ensure we at least have one part if the input is non-empty
int desiredArraySize = Math.max(commaCount + 1, parts.length);
// Step 3: Create a new array with the desired size and fill it with empty strings
String[] filledParts = new String[desiredArraySize];
Arrays.fill(filledParts, "");
// Step 4: Copy non-empty parts from the original array to the new array
int index = 0;
for (String part : parts) {
if (!part.trim().isEmpty()) {
filledParts[index++] = part.trim(); // Trim any surrounding spaces for cleanliness
}
// Note: We don't increment index for empty parts because those are already represented by empty strings in filledParts
}
// Note: At this point, if there were more commas than non-empty parts, the remaining elements in filledParts will be empty strings.
// If there were leading/trailing commas or multiple consecutive commas, those will also be represented by empty strings.
return filledParts;
}
}