xuhy
2026-02-04 139ab6c2f107e4e9b9ada2ffefb2849b4c4f2bd2
ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TErpGoodsController.java
@@ -20,9 +20,11 @@
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.WebUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.dto.*;
import com.ruoyi.system.importExcel.TErpCloudGoodsExcel;
import com.ruoyi.system.importExcel.TErpGoodsExcel;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.PageAddListQuery;
@@ -304,6 +306,7 @@
        goods.setPackingSpec(dto.getPackingSpec());
        goods.setGoodsName(dto.getGoodsName());
        goods.setIsPrescriptionDrug(null);
        goods.setTypeId(dto.getTypeId());
        goods.setGoodsSpec(dto.getFormulationSpec() + "*" + dto.getPackingSpec());
        return R.ok(erpGoodsService.save(goods));
    }
@@ -481,7 +484,7 @@
        if (dto.getState() == 1 && goods.getClinicPurchasePrice() == null) {
            return R.fail("操作失败,请设置供应商分佣比例后再启用");
        }
        if (dto.getState() == 1 && goods.getStateUser() == 1 && user.getRoleType() == 4) {
        if (dto.getState() == 1 && Objects.nonNull(goods.getStateUser()) && goods.getStateUser() == 1 && user.getRoleType() == 4) {
            return R.fail("操作失败,平台禁用商品不可启用");
        }
        if (user.getRoleType() == 1) {
@@ -835,9 +838,104 @@
            TErpGoods goods = erpGoodsService.getById(dto.getId());
            goods.setClinicPurchasePrice(BigDecimal.valueOf(dto.getClinicPurchasePrice()).setScale(2, RoundingMode.HALF_UP));
            goods.setPlatformCommissionPrice(BigDecimal.valueOf(dto.getPlatformCommissionPrice()).setScale(2, RoundingMode.HALF_UP));
            goods.setRetailPrice(dto.getRetailPrice());
            goods.setSalesAmount(dto.getRetailPrice());
            erpGoodsService.updateById(goods);
        }
        return R.ok();
    }
    @Log(title = "云仓商品关联导入模板-导出", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "云仓商品关联导入模板")
    @PostMapping("/cloudGoodsImportTemplate")
    public void cloudGoodsImportTemplate() {
        List<TErpCloudGoodsExcel> pageList = new ArrayList<>();
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TErpCloudGoodsExcel.class, pageList);
        HttpServletResponse response = WebUtils.response();
        assert response != null;
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        ServletOutputStream outputStream = null;
        try {
            String fileName = URLEncoder.encode("云仓商品关联导入模板.xls", "utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @Log(title = "云仓商品导入-导入", businessType = BusinessType.IMPORT)
    @ApiOperation(value = "云仓商品信息导入",notes = "cloudWarehouse:云仓类型 1=旺店通")
    @PostMapping("/importCloudGoodsFile")
    public AjaxResult importCloudGoodsFile(@RequestParam(value = "file") MultipartFile file,
                                           @RequestParam(value = "cloudWarehouse") Integer cloudWarehouse) {
        ImportParams params = new ImportParams();
        params.setHeadRows(1); //表头行数
        InputStream inputStream = null;
        ArrayList<String> names = new ArrayList<>();
        ArrayList<TErpGoods> tErpGoods = new ArrayList<>();
        try {
            inputStream = file.getInputStream();
            List<TErpCloudGoodsExcel> erpCloudGoodsExcels = ExcelImportUtil.importExcel(inputStream, TErpCloudGoodsExcel.class, params);
            Set<String> collect = erpCloudGoodsExcels.stream().map(TErpCloudGoodsExcel::getCloudSpecCode).collect(Collectors.toSet());
            if (collect.size() != erpCloudGoodsExcels.size()) {
                return AjaxResult.error("导入数据中商品规格编码重复");
            }
            for (TErpCloudGoodsExcel erpCloudGoodsExcel : erpCloudGoodsExcels) {
                // 判空
                if (StringUtils.isBlank(erpCloudGoodsExcel.getGoodsName())) {
                    names.add("云仓商品名称不能为空");
                    continue;
                }
                if (StringUtils.isBlank(erpCloudGoodsExcel.getCloudGoodsCode())) {
                    names.add("云仓商品编码不能为空");
                    continue;
                }
                if (StringUtils.isBlank(erpCloudGoodsExcel.getCloudSpecCode())) {
                    names.add("云仓商品规格编码不能为空");
                    continue;
                }
                TErpGoods erpGoods = erpGoodsService.getOne(Wrappers.lambdaQuery(TErpGoods.class)
                        .eq(TErpGoods::getGoodsName, erpCloudGoodsExcel.getGoodsName())
                        .last("LIMIT 1"));
                if (Objects.isNull(erpGoods)) {
                    names.add(erpCloudGoodsExcel.getGoodsName() + " :信息有误,未查询到匹配得商品。");
                    continue;
                }
                erpGoods.setCloudWarehouse(cloudWarehouse);
                erpGoods.setCloudGoodsCode(erpCloudGoodsExcel.getCloudGoodsCode());
                erpGoods.setCloudSpecCode(erpCloudGoodsExcel.getCloudSpecCode());
                tErpGoods.add(erpGoods);
            }
            erpGoodsService.updateBatchById(tErpGoods);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("记录导入失败!");
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                throw new ServiceException(e.getMessage());
            }
        }
        if (!names.isEmpty()) {
            return AjaxResult.error("导入失败记录如下:", names);
        }
        return AjaxResult.success();
    }
}