diff --git a/src/main/java/com/glxp/api/constant/RedissonCacheKey.java b/src/main/java/com/glxp/api/constant/RedissonCacheKey.java index 8036f660..5c13a63c 100644 --- a/src/main/java/com/glxp/api/constant/RedissonCacheKey.java +++ b/src/main/java/com/glxp/api/constant/RedissonCacheKey.java @@ -40,4 +40,5 @@ public interface RedissonCacheKey { String WEB_ADD_CODE = "web_add_code"; String XML_UPLOAD="xml_upload"; + String XML_UPLOAD_ALI="xml_upload_ali"; } diff --git a/src/main/java/com/glxp/api/controller/thrsys/ThirdAliDrugController.java b/src/main/java/com/glxp/api/controller/thrsys/ThirdAliDrugController.java new file mode 100644 index 00000000..125cdfb0 --- /dev/null +++ b/src/main/java/com/glxp/api/controller/thrsys/ThirdAliDrugController.java @@ -0,0 +1,38 @@ +package com.glxp.api.controller.thrsys; + +import com.glxp.api.annotation.CusRedissonAnnotation; +import com.glxp.api.annotation.RepeatSubmit; +import com.glxp.api.common.res.BaseResponse; +import com.glxp.api.common.util.ResultVOUtils; +import com.glxp.api.constant.RedissonCacheKey; +import com.glxp.api.controller.BaseController; +import com.glxp.api.service.thrsys.ThirdAliDrugService; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; + +@RestController +public class ThirdAliDrugController extends BaseController { + + @Resource + private ThirdAliDrugService thirdAliDrugService; + + /** + * 上传文件保存 + * + * @param uuid + * @param file + * @return + */ + @RepeatSubmit() + @CusRedissonAnnotation(cacheName = RedissonCacheKey.XML_UPLOAD_ALI, key = {"#uuid"}, timeOutMsg = "系统正在处理,请勿重复上传") + @PostMapping("/udiwms/aliDrug/txtUpload") + public BaseResponse txtUpload(@RequestParam("uuid") String uuid, @RequestParam("file") MultipartFile file) { + thirdAliDrugService.txtUpload(file); + return ResultVOUtils.successMsg("上传成功!"); + } + +} diff --git a/src/main/java/com/glxp/api/dao/thrsys/ThirdAliDrugMapper.java b/src/main/java/com/glxp/api/dao/thrsys/ThirdAliDrugMapper.java index 4b114078..eefce3ae 100644 --- a/src/main/java/com/glxp/api/dao/thrsys/ThirdAliDrugMapper.java +++ b/src/main/java/com/glxp/api/dao/thrsys/ThirdAliDrugMapper.java @@ -2,8 +2,15 @@ package com.glxp.api.dao.thrsys; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.glxp.api.entity.thrsys.ThirdAliDrug; -import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.*; + +import java.util.List; @Mapper public interface ThirdAliDrugMapper extends BaseMapper { + + int selectDrugsByNameCodes(@Param("nameCode") String nameCode); + + int saveOrUpdateBatch(@Param("list") List list); + } \ No newline at end of file diff --git a/src/main/java/com/glxp/api/entity/thrsys/ThirdAliDrug.java b/src/main/java/com/glxp/api/entity/thrsys/ThirdAliDrug.java index 6028906b..74dfaec5 100644 --- a/src/main/java/com/glxp/api/entity/thrsys/ThirdAliDrug.java +++ b/src/main/java/com/glxp/api/entity/thrsys/ThirdAliDrug.java @@ -95,6 +95,12 @@ public class ThirdAliDrug implements Serializable { @TableField(value = "packLevel") @ApiModelProperty(value = "包装级别") private String packLevel; + /** + * 包装级别 + */ + @TableField(value = "erpId") + @ApiModelProperty(value = "包装级别") + private String erpId; /** * 创建时间 @@ -111,4 +117,19 @@ public class ThirdAliDrug implements Serializable { private Date updateTime; private static final long serialVersionUID = 1L; + + public ThirdAliDrug(String type, String manufacturer, String cpmctymc, String form, String formSpec, String bzgg, String spmc, String nameCode, String packRatio, String packLevel, Date createTime, Date updateTime) { + this.type = type; + this.manufacturer = manufacturer; + this.cpmctymc = cpmctymc; + this.form = form; + this.formSpec = formSpec; + this.bzgg = bzgg; + this.spmc = spmc; + this.nameCode = nameCode; + this.packRatio = packRatio; + this.packLevel = packLevel; + this.createTime = createTime; + this.updateTime = updateTime; + } } \ No newline at end of file diff --git a/src/main/java/com/glxp/api/service/thrsys/ThirdAliDrugService.java b/src/main/java/com/glxp/api/service/thrsys/ThirdAliDrugService.java index 6bb5d777..70746ab4 100644 --- a/src/main/java/com/glxp/api/service/thrsys/ThirdAliDrugService.java +++ b/src/main/java/com/glxp/api/service/thrsys/ThirdAliDrugService.java @@ -1,14 +1,227 @@ 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 javax.annotation.Resource; +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 { + + @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 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; + } + + } diff --git a/src/main/resources/mybatis/mapper/thrsys/ThirdAliDrugMapper.xml b/src/main/resources/mybatis/mapper/thrsys/ThirdAliDrugMapper.xml index 7a3934f4..2ba3fa63 100644 --- a/src/main/resources/mybatis/mapper/thrsys/ThirdAliDrugMapper.xml +++ b/src/main/resources/mybatis/mapper/thrsys/ThirdAliDrugMapper.xml @@ -15,6 +15,7 @@ + @@ -32,6 +33,23 @@ packRatio, packLevel, createTime, + erpId, updateTime + + + + + + INSERT INTO third_ali_drug (`type`, manufacturer, cpmctymc, form, formSpec, bzgg, spmc, nameCode, packRatio, + packLevel, createTime, updateTime,erpId) + VALUES + + (#{item.type}, #{item.manufacturer}, #{item.cpmctymc}, #{item.form}, #{item.formSpec}, #{item.bzgg}, #{item.spmc}, #{item.nameCode}, + #{item.packRatio}, #{item.packLevel}, #{item.createTime}, #{item.updateTime}, #{item.erpId}) + + + \ No newline at end of file