From 7ffd310e52ef6135197379f0ac2c4522780e53b9 Mon Sep 17 00:00:00 2001
From: liujie <1793218484@qq.com>
Date: 星期三, 26 八月 2026 17:42:27 +0800
Subject: [PATCH] 修改bug

---
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TErpGoodsController.java |  354 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 347 insertions(+), 7 deletions(-)

diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TErpGoodsController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TErpGoodsController.java
index ae8fb9a..59069b1 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TErpGoodsController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TErpGoodsController.java
@@ -23,8 +23,11 @@
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.WebUtils;
 import com.ruoyi.framework.web.service.TokenService;
+import com.ruoyi.system.constant.GoodsTypeConstants;
 import com.ruoyi.system.dto.*;
+import com.ruoyi.system.export.TErpGoodsDetailExport;
 import com.ruoyi.system.importExcel.TErpCloudGoodsExcel;
+import com.ruoyi.system.importExcel.TErpFixedGoodsExcel;
 import com.ruoyi.system.importExcel.TErpGoodsExcel;
 import com.ruoyi.system.model.*;
 import com.ruoyi.system.query.PageAddListQuery;
@@ -52,6 +55,7 @@
 import java.time.LocalDateTime;
 import java.util.*;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 /**
  * <p>
@@ -148,6 +152,23 @@
         }
         return R.ok();
     }
+
+    @ApiOperation(value = "平台设置商品排序")
+    @PostMapping(value = "/setGoodsSort")
+    public R<?> setGoodsSort(@RequestBody GoodsSortUpdateDto dto) {
+        SysUser user = tokenService.getLoginUser().getUser();
+        Integer roleType = user.getRoleType();
+        if (roleType == 1) {
+            // 平台
+            TErpGoods goods = erpGoodsService.getById(dto.getId());
+            if(goods!=null){
+                goods.setSort(dto.getSort());
+                erpGoodsService.updateById(goods);
+            }
+        }
+        return R.ok();
+    }
+
 
 
     @ApiOperation(value = "平台审核商品内容")
@@ -272,6 +293,7 @@
         }
         Integer roleType = user.getRoleType();
         TErpGoods goods = new TErpGoods();
+        BeanUtils.copyProperties(dto, goods);
         if (roleType == 4) {
             // 供应商
             TCrmSupplier crmSupplier = crmSupplierService.getOne(Wrappers.lambdaQuery(TCrmSupplier.class)
@@ -305,9 +327,9 @@
         goods.setFormulationSpec(dto.getFormulationSpec());
         goods.setPackingSpec(dto.getPackingSpec());
         goods.setGoodsName(dto.getGoodsName());
-        goods.setIsPrescriptionDrug(null);
+        goods.setIsPrescriptionDrug(dto.getIsPrescriptionDrug());
         goods.setTypeId(dto.getTypeId());
-        goods.setGoodsSpec(dto.getFormulationSpec() + "*" + dto.getPackingSpec());
+        goods.setGoodsSpec(buildGoodsSpec(dto.getFormulationSpec(), dto.getPackingSpec()));
         return R.ok(erpGoodsService.save(goods));
     }
 
@@ -319,13 +341,14 @@
         if (erpGoodsService.isExit(dto.getGoodsIdCode(), dto.getQuasiNumber(), null)) {
             return R.fail("erp商品已存在,商品条码重复");
         }
-        if (erpGoodsService.isExitOne(dto.getGoodsIdCode(), dto.getQuasiNumber(), null)) {
+        if (StringUtils.isNotBlank(dto.getQuasiNumber())
+                && erpGoodsService.isExitOne(dto.getGoodsIdCode(), dto.getQuasiNumber(), null)) {
             return R.fail("erp商品已存在,国药准字号重复");
         }
         Integer roleType = user.getRoleType();
         TErpGoods goods = new TErpGoods();
         BeanUtils.copyProperties(dto, goods);
-        goods.setGoodsSpec(dto.getFormulationSpec() + "*" + dto.getPackingSpec());
+        goods.setGoodsSpec(buildGoodsSpec(dto.getFormulationSpec(), dto.getPackingSpec()));
         if (roleType == 5) {
             // 诊所
             TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class)
@@ -341,7 +364,7 @@
         }
         goods.setCreateId(user.getUserId().toString());
         goods.setGoodsIdCode(dto.getGoodsIdCode());
-        goods.setGoodsSpec(dto.getFormulationSpec() + "*" + dto.getPackingSpec());
+        goods.setGoodsSpec(buildGoodsSpec(dto.getFormulationSpec(), dto.getPackingSpec()));
 //        goods.setClinicPurchasePrice(dto.getSalesAmount());
         if (goods.getIsPrescriptionDrug() != null && goods.getIsPrescriptionDrug() == 3) {
             goods.setIsPrescriptionDrug(null);
@@ -376,7 +399,7 @@
         goods.setCreateId(user.getUserId().toString());
         goods.setGoodsIdCode(dto.getGoodsIdCode());
         goods.setId(dto.getId());
-        goods.setGoodsSpec(dto.getFormulationSpec() + "*" + dto.getPackingSpec());
+        goods.setGoodsSpec(buildGoodsSpec(dto.getFormulationSpec(), dto.getPackingSpec()));
         if (goods.getIsPrescriptionDrug() != null && goods.getIsPrescriptionDrug() == 3) {
             goods.setIsPrescriptionDrug(null);
         }
@@ -580,6 +603,13 @@
         return R.ok(goods);
     }
 
+    private String buildGoodsSpec(String formulationSpec, String packingSpec) {
+        if (StringUtils.isNotBlank(formulationSpec) && StringUtils.isNotBlank(packingSpec)) {
+            return formulationSpec + "*" + packingSpec;
+        }
+        return StringUtils.isNotBlank(packingSpec) ? packingSpec : formulationSpec;
+    }
+
     @ApiOperation(value = "商品管理信息导出--导出")
     @PostMapping("/export")
     @Log(title = "商品管理-导出", businessType = BusinessType.EXPORT)
@@ -613,7 +643,12 @@
         }
 
         List<TErpGoodsVO> pageList = erpGoodsService.listExport(query, user);
-        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TErpGoodsVO.class, pageList);
+        List<TErpGoodsDetailExport> exportList = pageList.stream().map(goods -> {
+            TErpGoodsDetailExport exportItem = new TErpGoodsDetailExport();
+            BeanUtils.copyProperties(goods, exportItem);
+            return exportItem;
+        }).collect(Collectors.toList());
+        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TErpGoodsDetailExport.class, exportList);
         HttpServletResponse response = WebUtils.response();
         assert response != null;
         response.setContentType("application/vnd.ms-excel");
@@ -827,6 +862,311 @@
 
     }
 
+    /**
+     * 供应商固定商品类型导入。旧商品导入接口保留给非固定类型模板使用。
+     */
+    @ApiOperation(value = "供应商固定商品类型导入")
+    @PostMapping("/importFixedGoodsFile")
+    @Log(title = "商品管理-固定类型导入", businessType = BusinessType.IMPORT)
+    public AjaxResult importFixedGoodsFile(
+            @RequestParam(value = "file") MultipartFile file,
+            @RequestParam(value = "typeId") String typeId) {
+        SysUser user = tokenService.getLoginUser().getUser();
+        if (user == null || user.getRoleType() == null || user.getRoleType() != 4) {
+            return AjaxResult.error("仅供应商可以导入固定商品类型");
+        }
+        int sheetIndex = getFixedGoodsSheetIndex(typeId);
+        if (sheetIndex < 0) {
+            return AjaxResult.error("商品类型不支持固定模板导入");
+        }
+        if (file == null || file.isEmpty()) {
+            return AjaxResult.error("请选择要导入的文件");
+        }
+
+        TCrmSupplier supplier = getCurrentSupplier(user);
+        if (supplier == null) {
+            return AjaxResult.error("未找到当前供应商信息");
+        }
+
+        List<String> errors = new ArrayList<>();
+        List<TErpGoods> goodsList = new ArrayList<>();
+        Set<String> barcodeSet = new HashSet<>();
+        ImportParams params = new ImportParams();
+        params.setHeadRows(1);
+        params.setStartSheetIndex(sheetIndex);
+        params.setSheetNum(1);
+
+        try (InputStream inputStream = file.getInputStream()) {
+            List<TErpFixedGoodsExcel> rows = ExcelImportUtil.importExcel(
+                    inputStream, TErpFixedGoodsExcel.class, params);
+            if (rows == null || rows.isEmpty()) {
+                return AjaxResult.error("所选商品类型工作表没有可导入的数据");
+            }
+
+            for (int index = 0; index < rows.size(); index++) {
+                TErpFixedGoodsExcel row = rows.get(index);
+                if (isBlankFixedGoodsRow(row)) {
+                    continue;
+                }
+                String rowName = StringUtils.isBlank(row.getGoodsName())
+                        ? "第" + (index + 2) + "行"
+                        : row.getGoodsName().trim();
+                String validationError = validateFixedGoodsRow(row, typeId);
+                if (validationError != null) {
+                    errors.add(rowName + ":" + validationError);
+                    continue;
+                }
+
+                String goodsIdCode = row.getGoodsIdCode().trim();
+                if (!barcodeSet.add(goodsIdCode)) {
+                    errors.add(rowName + ":商品条码在导入文件中重复");
+                    continue;
+                }
+                if (erpGoodsService.count(new QueryWrapper<TErpGoods>()
+                        .eq("goods_id_code", goodsIdCode)) > 0) {
+                    errors.add(rowName + ":商品条码已存在");
+                    continue;
+                }
+
+                try {
+                    TErpGoods goods = buildFixedGoods(row, typeId, supplier, user);
+                    goodsList.add(goods);
+                } catch (IllegalArgumentException e) {
+                    errors.add(rowName + ":" + e.getMessage());
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            return AjaxResult.error("导入文件解析失败,请确认使用固定商品类型模板");
+        }
+
+        if (goodsList.isEmpty() && errors.isEmpty()) {
+            return AjaxResult.error("所选商品类型工作表没有可导入的数据");
+        }
+        if (!errors.isEmpty()) {
+            return AjaxResult.error("导入失败记录如下:", errors);
+        }
+        erpGoodsService.saveBatch(goodsList);
+        return AjaxResult.success();
+    }
+
+    private int getFixedGoodsSheetIndex(String typeId) {
+        if (GoodsTypeConstants.DRUG_ID.equals(typeId)) {
+            return 0;
+        }
+        if (GoodsTypeConstants.MEDICAL_DEVICE_ID.equals(typeId)) {
+            return 1;
+        }
+        if (GoodsTypeConstants.FOOD_ID.equals(typeId)) {
+            return 2;
+        }
+        return -1;
+    }
+
+    private TCrmSupplier getCurrentSupplier(SysUser user) {
+        TCrmSupplier supplier = crmSupplierService.getOne(
+                Wrappers.lambdaQuery(TCrmSupplier.class)
+                        .eq(TCrmSupplier::getUserId, user.getUserId())
+                        .last("LIMIT 1"));
+        if (supplier == null) {
+            SysUser sysUser = sysUserService.selectUserById(user.getUserId());
+            if (sysUser != null && sysUser.getObjectId() != null) {
+                supplier = crmSupplierService.getById(sysUser.getObjectId());
+            }
+        }
+        return supplier;
+    }
+
+    private String validateFixedGoodsRow(TErpFixedGoodsExcel row, String typeId) {
+        if (StringUtils.isBlank(row.getGoodsName())) {
+            return "商品名称不能为空";
+        }
+        if (StringUtils.isBlank(row.getGoodsIdCode())) {
+            return "商品条形码不能为空";
+        }
+        if (StringUtils.isBlank(row.getManufacturer())) {
+            return "生产厂家不能为空";
+        }
+        if (StringUtils.isBlank(row.getWarningInventory())) {
+            return "预警库存不能为空";
+        }
+        if (StringUtils.isBlank(row.getPackingUnitName())) {
+            return "包装单位不能为空";
+        }
+        if (GoodsTypeConstants.DRUG_ID.equals(typeId)) {
+            if (StringUtils.isBlank(row.getQuasiNumber())) {
+                return "批准文号或国药准字号不能为空";
+            }
+            if (StringUtils.isBlank(row.getProductType())) {
+                return "商品类型不能为空";
+            }
+            if (StringUtils.isBlank(row.getFormulationSpec())) {
+                return "剂型规格不能为空";
+            }
+            if (StringUtils.isBlank(row.getPackingSpec())) {
+                return "包装规格不能为空";
+            }
+            if (StringUtils.isBlank(row.getEffectivePeriod())) {
+                return "有效时间不能为空";
+            }
+        } else if (GoodsTypeConstants.MEDICAL_DEVICE_ID.equals(typeId)) {
+            if (StringUtils.isBlank(row.getDeviceCategory())) {
+                return "器械类别不能为空";
+            }
+            if (StringUtils.isBlank(row.getProductType())) {
+                return "商品类型不能为空";
+            }
+            if (StringUtils.isBlank(row.getProductTechnicalRequirementNumber())) {
+                return "产品技术要求编号不能为空";
+            }
+            if (StringUtils.isBlank(row.getProductionLicenseOrFilingNumber())) {
+                return "生产许可证或备案凭证编号不能为空";
+            }
+            if (StringUtils.isBlank(row.getRegistrantOrFiler())) {
+                return "注册人或备案人不能为空";
+            }
+            if (StringUtils.isBlank(row.getEffectivePeriod())) {
+                return "有效时间不能为空";
+            }
+        } else if (GoodsTypeConstants.FOOD_ID.equals(typeId)) {
+            if (StringUtils.isBlank(row.getProductionLicenseNumber())) {
+                return "生产许可证号不能为空";
+            }
+            if (StringUtils.isBlank(row.getProductType())) {
+                return "商品类型不能为空";
+            }
+            if (StringUtils.isBlank(row.getPackingSpec())) {
+                return "包装规格不能为空";
+            }
+            if (StringUtils.isBlank(row.getProductCategory())) {
+                return "产品类别不能为空";
+            }
+        }
+        return null;
+    }
+
+    private boolean isBlankFixedGoodsRow(TErpFixedGoodsExcel row) {
+        return Stream.of(
+                row.getGoodsName(), row.getCommonName(), row.getGoodsIdCode(), row.getQuasiNumber(),
+                row.getProductType(), row.getManufacturer(), row.getFormulationSpec(), row.getPackingSpec(),
+                row.getDeviceCategory(), row.getProductTechnicalRequirementNumber(),
+                row.getProductionLicenseOrFilingNumber(), row.getRegistrantOrFiler(), row.getStructureComposition(),
+                row.getApplicableScope(), row.getOriginType(), row.getProductionLicenseNumber(),
+                row.getProductCategory(), row.getProductExecutionStandard(), row.getSpecialFoodFlag(),
+                row.getSpecialFood(), row.getGoodsYards(), row.getMaintenanceInterval(), row.getLowPurchaseQuantity(),
+                row.getLowUnitName(), row.getIsPrescriptionDrug(), row.getGoodsSpec(), row.getWarningInventory(),
+                row.getPackingUnitName(), row.getInstructionsUse(), row.getSideEffect(), row.getSimplifiedCode(),
+                row.getDosageForm(), row.getUsageClassification(), row.getProductBrand(),
+                row.getProductBrandAndPrecautions(), row.getPlaceOfOrigin(), row.getIngredient(), row.getTrait(),
+                row.getIndication(), row.getTaboo(), row.getPrecautions(), row.getInteraction(),
+                row.getAdverseReaction(), row.getStorage(), row.getEffectivePeriod(), row.getOtherDescription())
+                .allMatch(StringUtils::isBlank);
+    }
+
+    private TErpGoods buildFixedGoods(
+            TErpFixedGoodsExcel row,
+            String typeId,
+            TCrmSupplier supplier,
+            SysUser user) {
+        TErpGoods goods = new TErpGoods();
+        goods.setSupplierClinicId(supplier.getId());
+        goods.setGoodsSource(1);
+        goods.setCreateId(user.getUserId().toString());
+        goods.setTypeId(typeId);
+        goods.setGoodsName(row.getGoodsName().trim());
+        goods.setGoodsIdCode(row.getGoodsIdCode().trim());
+        goods.setManufacturer(row.getManufacturer().trim());
+        goods.setQuasiNumber(trim(row.getQuasiNumber()));
+        goods.setFormulationSpec(trim(row.getFormulationSpec()));
+        goods.setPackingSpec(trim(row.getPackingSpec()));
+        goods.setCommonName(trim(row.getCommonName()));
+        goods.setProductType(trim(row.getProductType()));
+        goods.setDeviceCategory(trim(row.getDeviceCategory()));
+        goods.setProductTechnicalRequirementNumber(trim(row.getProductTechnicalRequirementNumber()));
+        goods.setProductionLicenseOrFilingNumber(trim(row.getProductionLicenseOrFilingNumber()));
+        goods.setRegistrantOrFiler(trim(row.getRegistrantOrFiler()));
+        goods.setStructureComposition(trim(row.getStructureComposition()));
+        goods.setApplicableScope(trim(row.getApplicableScope()));
+        goods.setOriginType(trim(row.getOriginType()));
+        goods.setProductionLicenseNumber(trim(row.getProductionLicenseNumber()));
+        goods.setProductCategory(trim(row.getProductCategory()));
+        goods.setProductExecutionStandard(trim(row.getProductExecutionStandard()));
+        goods.setSpecialFoodFlag(parseFlag(row.getSpecialFoodFlag(), "是否特殊食品"));
+        goods.setSpecialFood(trim(row.getSpecialFood()));
+        goods.setGoodsYards(trim(row.getGoodsYards()));
+        parseInteger(row.getMaintenanceInterval(), "保养周期");
+        goods.setMaintenanceInterval(trim(row.getMaintenanceInterval()));
+        goods.setLowPurchaseQuantity(parseInteger(row.getLowPurchaseQuantity(), "最低采购数量"));
+        goods.setIsPrescriptionDrug(parseFlag(row.getIsPrescriptionDrug(), "是否为处方药"));
+        goods.setGoodsSpec(StringUtils.isBlank(row.getGoodsSpec())
+                ? buildGoodsSpec(row.getFormulationSpec(), row.getPackingSpec())
+                : row.getGoodsSpec().trim());
+        goods.setWarningInventory(parseInteger(row.getWarningInventory(), "预警库存"));
+        goods.setLowUnitId(findUnitId(row.getLowUnitName(), "最低采购单位"));
+        goods.setPackingUnitId(findUnitId(row.getPackingUnitName(), "包装单位"));
+        goods.setInstructionsUse(trim(row.getInstructionsUse()));
+        goods.setSideEffect(StringUtils.isBlank(row.getSideEffect())
+                ? trim(row.getAdverseReaction()) : row.getSideEffect().trim());
+        goods.setSimplifiedCode(trim(row.getSimplifiedCode()));
+        goods.setDosageForm(trim(row.getDosageForm()));
+        goods.setUsageClassification(trim(row.getUsageClassification()));
+        goods.setProductBrand(StringUtils.isBlank(row.getProductBrand())
+                ? trim(row.getProductBrandAndPrecautions())
+                : row.getProductBrand().trim());
+        goods.setPlaceOfOrigin(trim(row.getPlaceOfOrigin()));
+        goods.setIngredient(trim(row.getIngredient()));
+        goods.setTrait(trim(row.getTrait()));
+        goods.setIndication(trim(row.getIndication()));
+        goods.setTaboo(trim(row.getTaboo()));
+        goods.setPrecautions(trim(row.getPrecautions()));
+        goods.setInteraction(trim(row.getInteraction()));
+        goods.setStorage(trim(row.getStorage()));
+        goods.setEffectivePeriod(trim(row.getEffectivePeriod()));
+        goods.setOtherDescription(trim(row.getOtherDescription()));
+        return goods;
+    }
+
+    private String findUnitId(String unitName, String fieldName) {
+        if (StringUtils.isBlank(unitName)) {
+            return null;
+        }
+        TErpGoodsUnit unit = erpGoodsUnitService.getOne(
+                new LambdaQueryWrapper<TErpGoodsUnit>().eq(TErpGoodsUnit::getUnitName, unitName.trim()));
+        if (unit == null) {
+            throw new IllegalArgumentException(fieldName + "有误");
+        }
+        return unit.getId();
+    }
+
+    private Integer parseInteger(String value, String fieldName) {
+        if (StringUtils.isBlank(value)) {
+            return null;
+        }
+        try {
+            return new BigDecimal(value.trim()).intValueExact();
+        } catch (Exception e) {
+            throw new IllegalArgumentException(fieldName + "有误");
+        }
+    }
+
+    private Integer parseFlag(String value, String fieldName) {
+        if (StringUtils.isBlank(value)) {
+            return null;
+        }
+        String normalized = value.trim();
+        if ("是".equals(normalized) || "1".equals(normalized)) {
+            return 1;
+        }
+        if ("否".equals(normalized) || "2".equals(normalized)) {
+            return 2;
+        }
+        throw new IllegalArgumentException(fieldName + "有误");
+    }
+
+    private String trim(String value) {
+        return StringUtils.isBlank(value) ? null : value.trim();
+    }
+
 
     @ApiOperation(value = "平台设置分佣")
     @PostMapping(value = "/setCommission")

--
Gitblit v1.7.1