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.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.glxp.api.dao.thrsys.ThirdAliDrugMapper; import com.glxp.api.entity.thrsys.ThirdAliDrug; import com.glxp.api.exception.JsonException; import com.glxp.api.req.thrsys.ThirdAliDrugRequest; import com.glxp.api.service.auth.CustomerService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; @Service @Slf4j public class ThirdAliDrugService extends ServiceImpl { @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() > 100 * 1024 * 1024) { throw new JsonException("上传文件超过100M"); } List 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 == 11) { 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(), 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(); } } } } } } 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 } } int desiredArraySize = Math.max(commaCount + 1, parts.length); String[] filledParts = new String[desiredArraySize]; Arrays.fill(filledParts, ""); int index = 0; for (String part : parts) { if (!part.trim().isEmpty()) { filledParts[index++] = part.trim(); // Trim any surrounding spaces for cleanliness } } return filledParts; } public List filterList(ThirdAliDrugRequest thirdAliDrug) { if (thirdAliDrug == null) { return Collections.emptyList(); } // 分页处理 thirdAliDrug.pageDispose(); return this.baseMapper.filterList(thirdAliDrug); } }