From b6fb1c99ad2b683c046805a2dbab77c916f01369 Mon Sep 17 00:00:00 2001 From: furongxin <419481438@qq.com> Date: Wed, 30 Apr 2025 11:48:37 +0800 Subject: [PATCH] =?UTF-8?q?feat(handling-specifications):=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=B7=A5=E5=8E=82=E6=90=AC=E8=BF=90=E8=A7=84=E6=A0=BC?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增导入模板下载和 Excel 导入接口 - 实现搬运规格列表的导入逻辑,支持创建和更新操作 - 添加相关 VO 类和错误码常量 - 优化工厂名称显示,使用简称替代全称 --- .../enums/ErrorCodeConstants.java | 2 + .../HandlingSpecificationsController.java | 81 ++++++++++++---- .../HandlingSpecificationsImportRespVO.java | 24 +++++ .../vo/HandlingSpecificationsImportVO.java | 40 ++++++++ .../admin/staff/StaffController.java | 2 +- .../HandlingSpecificationsService.java | 10 ++ .../HandlingSpecificationsServiceImpl.java | 97 ++++++++++++++++++- 7 files changed, 234 insertions(+), 22 deletions(-) create mode 100644 zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/controller/admin/handlingspecifications/vo/HandlingSpecificationsImportRespVO.java create mode 100644 zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/controller/admin/handlingspecifications/vo/HandlingSpecificationsImportVO.java diff --git a/zn-module-smartfactory/zn-module-smartfactory-api/src/main/java/cn/iocoder/yudao/module/smartfactory/enums/ErrorCodeConstants.java b/zn-module-smartfactory/zn-module-smartfactory-api/src/main/java/cn/iocoder/yudao/module/smartfactory/enums/ErrorCodeConstants.java index e809fdb0..be5fd4ab 100644 --- a/zn-module-smartfactory/zn-module-smartfactory-api/src/main/java/cn/iocoder/yudao/module/smartfactory/enums/ErrorCodeConstants.java +++ b/zn-module-smartfactory/zn-module-smartfactory-api/src/main/java/cn/iocoder/yudao/module/smartfactory/enums/ErrorCodeConstants.java @@ -19,6 +19,7 @@ public interface ErrorCodeConstants { ErrorCode STAFF_NOT_EXISTS = new ErrorCode(1_000_000_005, "员工不存在"); ErrorCode ACCESSORIES_COLLAR_NOT_EXISTS = new ErrorCode(1_000_000_006, "配件费用不存在"); + ErrorCode IMPORT_LIST_IS_EMPTY = new ErrorCode(1_000_000_007, "导入数据不存在"); //打包线模块 ErrorCode PACKAGE_NOT_EXISTS = new ErrorCode(1_000_001_001, "打包线信息不存在"); @@ -38,6 +39,7 @@ public interface ErrorCodeConstants { ErrorCode STAFF_SALARY_EXISTS = new ErrorCode(1_000_004_003, "工资记录已存在"); // ========== 搬运模块 1_000_005_000 ========== ErrorCode CANNOT_FIND_THE_CORRESPONDING_FACTORY_HANDLING_SPECIFICATION = new ErrorCode(1_000_005_001, "找不到对应的工厂搬运规格"); + ErrorCode FACTORY_HANDLING_SPECIFICATION_EXISTS = new ErrorCode(1_000_005_002, "工厂搬运规格已存在"); } diff --git a/zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/controller/admin/handlingspecifications/HandlingSpecificationsController.java b/zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/controller/admin/handlingspecifications/HandlingSpecificationsController.java index 8e8f88d0..9945fc06 100644 --- a/zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/controller/admin/handlingspecifications/HandlingSpecificationsController.java +++ b/zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/controller/admin/handlingspecifications/HandlingSpecificationsController.java @@ -1,33 +1,37 @@ package cn.iocoder.yudao.module.smartfactory.controller.admin.handlingspecifications; -import org.springframework.web.bind.annotation.*; -import javax.annotation.Resource; -import org.springframework.validation.annotation.Validated; -import org.springframework.security.access.prepost.PreAuthorize; -import io.swagger.v3.oas.annotations.tags.Tag; -import io.swagger.v3.oas.annotations.Parameter; -import io.swagger.v3.oas.annotations.Operation; - -import javax.validation.constraints.*; -import javax.validation.*; -import javax.servlet.http.*; -import java.util.*; -import java.io.IOException; - +import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.PageParam; import cn.iocoder.yudao.framework.common.pojo.PageResult; -import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; -import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; - import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; - import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; -import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*; - import cn.iocoder.yudao.module.smartfactory.controller.admin.handlingspecifications.vo.*; +import cn.iocoder.yudao.module.smartfactory.dal.dataobject.factoryinfo.FactoryInfoDO; import cn.iocoder.yudao.module.smartfactory.dal.dataobject.handlingspecifications.HandlingSpecificationsDO; +import cn.iocoder.yudao.module.smartfactory.service.factoryinfo.FactoryInfoService; import cn.iocoder.yudao.module.smartfactory.service.handlingspecifications.HandlingSpecificationsService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.Parameters; +import io.swagger.v3.oas.annotations.tags.Tag; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletResponse; +import javax.validation.Valid; +import java.io.IOException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; +import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT; @Tag(name = "管理后台 - 工厂搬运规格") @RestController @@ -38,6 +42,9 @@ public class HandlingSpecificationsController { @Resource private HandlingSpecificationsService handlingSpecificationsService; + @Resource + private FactoryInfoService factoryInfoService; + @PostMapping("/create") @Operation(summary = "创建工厂搬运规格") @PreAuthorize("@ss.hasPermission('smartfactory:handling-specifications:create')") @@ -99,4 +106,38 @@ public class HandlingSpecificationsController { BeanUtils.toBean(list, HandlingSpecificationsRespVO.class)); } + @GetMapping("/get-import-template") + @Operation(summary = "获得导入用户模板") + public void importTemplate(HttpServletResponse response) throws IOException { + + //获取部门名称 + List factoryInfoDOS = factoryInfoService.getFactoryListByType(); + List factoryName = factoryInfoDOS.stream() + .map(item -> item.getId() + ":" + item.getShortName()) + .collect(Collectors.toList()); + + // 类型选择 + List type = Arrays.asList("0:数量", "1:重量"); + + Map> mapDropDown = new HashMap<>(); + mapDropDown.put(0, factoryName); + mapDropDown.put(2, type); + + // 输出 + ExcelUtils.write(response, "搬运规格列表模板.xlsx", "搬运规格列表", + HandlingSpecificationsImportVO.class, null, + mapDropDown); + } + + @PostMapping("/import") + @Operation(summary = "导入搬运规格列表") + @Parameters({ + @Parameter(name = "file", description = "Excel 文件", required = true), + @Parameter(name = "updateSupport", description = "是否支持更新,默认为 false", example = "true") + }) + public CommonResult importExcel(@RequestParam("file") MultipartFile file, + @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception { + List list = ExcelUtils.read(file, HandlingSpecificationsImportVO.class); + return success(handlingSpecificationsService.importList(list, updateSupport)); + } } diff --git a/zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/controller/admin/handlingspecifications/vo/HandlingSpecificationsImportRespVO.java b/zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/controller/admin/handlingspecifications/vo/HandlingSpecificationsImportRespVO.java new file mode 100644 index 00000000..0c0de01f --- /dev/null +++ b/zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/controller/admin/handlingspecifications/vo/HandlingSpecificationsImportRespVO.java @@ -0,0 +1,24 @@ +package cn.iocoder.yudao.module.smartfactory.controller.admin.handlingspecifications.vo; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Builder; +import lombok.Data; + +import java.util.List; +import java.util.Map; + +@Schema(description = "管理后台 - 用户导入 Response VO") +@Data +@Builder +public class HandlingSpecificationsImportRespVO { + + @Schema(description = "创建成功的用户名数组", requiredMode = Schema.RequiredMode.REQUIRED) + private List createUsernames; + + @Schema(description = "更新成功的用户名数组", requiredMode = Schema.RequiredMode.REQUIRED) + private List updateUsernames; + + @Schema(description = "导入失败的用户集合,key 为用户名,value 为失败原因", requiredMode = Schema.RequiredMode.REQUIRED) + private Map failureUsernames; + +} diff --git a/zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/controller/admin/handlingspecifications/vo/HandlingSpecificationsImportVO.java b/zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/controller/admin/handlingspecifications/vo/HandlingSpecificationsImportVO.java new file mode 100644 index 00000000..4708a6c9 --- /dev/null +++ b/zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/controller/admin/handlingspecifications/vo/HandlingSpecificationsImportVO.java @@ -0,0 +1,40 @@ +package cn.iocoder.yudao.module.smartfactory.controller.admin.handlingspecifications.vo; + +import com.alibaba.excel.annotation.ExcelProperty; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.experimental.Accessors; + +import javax.validation.constraints.NotNull; +import java.math.BigDecimal; + +@Schema(description = "管理后台 - 工厂搬运规格导入 Request VO") +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +@Accessors(chain = false) // 设置 chain = false,避免用户导入有问题 +public class HandlingSpecificationsImportVO { + + @ExcelProperty("厂区名称") + @NotNull(message = "厂区名称不能为空") + private String factoryName; + + @ExcelProperty("规格名称") + @NotNull(message = "规格名称不能为空") + private String specificationsName; + + @ExcelProperty("类型") + @NotNull(message = "类型不能为空") + private String type; + + @ExcelProperty("重量 /(公斤 kg)") + private BigDecimal weight; + + @ExcelProperty("单价/(吨/1000 or 件)") + @NotNull(message = "单价不能为空") + private BigDecimal unitPrice; +} diff --git a/zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/controller/admin/staff/StaffController.java b/zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/controller/admin/staff/StaffController.java index 6f6d742c..f1fb599b 100644 --- a/zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/controller/admin/staff/StaffController.java +++ b/zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/controller/admin/staff/StaffController.java @@ -160,7 +160,7 @@ public class StaffController { //获取部门名称 List factoryInfoDOS = factoryInfoService.getFactoryListByType(); List factoryName = factoryInfoDOS.stream() - .map(item -> item.getId() + ":" + item.getName()) + .map(item -> item.getId() + ":" + item.getShortName()) .collect(Collectors.toList()); //获取工种名称 diff --git a/zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/service/handlingspecifications/HandlingSpecificationsService.java b/zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/service/handlingspecifications/HandlingSpecificationsService.java index 1f569aa5..58c3278f 100644 --- a/zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/service/handlingspecifications/HandlingSpecificationsService.java +++ b/zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/service/handlingspecifications/HandlingSpecificationsService.java @@ -1,6 +1,8 @@ package cn.iocoder.yudao.module.smartfactory.service.handlingspecifications; import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.module.smartfactory.controller.admin.handlingspecifications.vo.HandlingSpecificationsImportRespVO; +import cn.iocoder.yudao.module.smartfactory.controller.admin.handlingspecifications.vo.HandlingSpecificationsImportVO; import cn.iocoder.yudao.module.smartfactory.controller.admin.handlingspecifications.vo.HandlingSpecificationsPageReqVO; import cn.iocoder.yudao.module.smartfactory.controller.admin.handlingspecifications.vo.HandlingSpecificationsSaveReqVO; import cn.iocoder.yudao.module.smartfactory.dal.dataobject.handlingspecifications.HandlingSpecificationsDO; @@ -61,4 +63,12 @@ public interface HandlingSpecificationsService extends IService getHandlingSpecificationsList(@Valid HandlingSpecificationsPageReqVO pageReqVO); + + /** + * 导入列表 + * @param list + * @param updateSupport + * @return + */ + HandlingSpecificationsImportRespVO importList(List list, Boolean updateSupport); } diff --git a/zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/service/handlingspecifications/HandlingSpecificationsServiceImpl.java b/zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/service/handlingspecifications/HandlingSpecificationsServiceImpl.java index 532643ba..ea7d5750 100644 --- a/zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/service/handlingspecifications/HandlingSpecificationsServiceImpl.java +++ b/zn-module-smartfactory/zn-module-smartfactory-biz/src/main/java/cn/iocoder/yudao/module/smartfactory/service/handlingspecifications/HandlingSpecificationsServiceImpl.java @@ -1,8 +1,14 @@ package cn.iocoder.yudao.module.smartfactory.service.handlingspecifications; +import cn.hutool.core.collection.CollUtil; +import cn.iocoder.yudao.framework.common.exception.ServiceException; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; +import cn.iocoder.yudao.framework.datapermission.core.util.DataPermissionUtils; +import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils; +import cn.iocoder.yudao.module.smartfactory.controller.admin.handlingspecifications.vo.HandlingSpecificationsImportRespVO; +import cn.iocoder.yudao.module.smartfactory.controller.admin.handlingspecifications.vo.HandlingSpecificationsImportVO; import cn.iocoder.yudao.module.smartfactory.controller.admin.handlingspecifications.vo.HandlingSpecificationsPageReqVO; import cn.iocoder.yudao.module.smartfactory.controller.admin.handlingspecifications.vo.HandlingSpecificationsSaveReqVO; import cn.iocoder.yudao.module.smartfactory.dal.dataobject.handlingspecifications.HandlingSpecificationsDO; @@ -10,12 +16,18 @@ import cn.iocoder.yudao.module.smartfactory.dal.mysql.handlingspecifications.Han import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; -import java.util.Collections; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; +import static cn.iocoder.yudao.module.smartfactory.enums.ErrorCodeConstants.FACTORY_HANDLING_SPECIFICATION_EXISTS; +import static cn.iocoder.yudao.module.smartfactory.enums.ErrorCodeConstants.IMPORT_LIST_IS_EMPTY; + /** * 工厂搬运规格 Service 实现类 * @@ -66,4 +78,87 @@ public class HandlingSpecificationsServiceImpl extends ServiceImpl list, Boolean updateSupport) { + + if (CollUtil.isEmpty(list)) { + throw exception(IMPORT_LIST_IS_EMPTY); + } + HandlingSpecificationsImportRespVO respVO = HandlingSpecificationsImportRespVO.builder() + .createUsernames(new ArrayList<>()) + .updateUsernames(new ArrayList<>()) + .failureUsernames(new HashMap<>()).build(); + + list.forEach(importUser -> { + if (importUser.getFactoryName() == null) { + return; + } + + // 解析工厂名称数据 格式为 id:name + Long factoryId = Long.valueOf(importUser.getFactoryName().split(":")[0]); + String factoryName = importUser.getFactoryName().split(":")[1]; + + //校验,判断是否有不符合的原因 + try { + if (!updateSupport) { + validateForCreate(factoryId, importUser.getSpecificationsName()); + } + } catch (ServiceException ex) { + respVO.getFailureUsernames().put(factoryName, ex.getMessage()); + return; + } + + HandlingSpecificationsDO updateDo = new HandlingSpecificationsDO(); + // 设置工厂编号 + updateDo.setFactoryId(factoryId); + // 设置规格名称 + updateDo.setSpecificationsName(importUser.getSpecificationsName()); + // 设置重量 + updateDo.setWeight(importUser.getWeight()); + // 设置单价 + updateDo.setUnitPrice(importUser.getUnitPrice()); + + Integer type = null; + if (importUser.getType() != null) { + // 解析类型数据 + type = Integer.valueOf(importUser.getType().split(":")[0]); + updateDo.setType(type); + } + + // 判断如果不存在,在进行插入 + if (updateSupport) { + HandlingSpecificationsDO handlingSpecificationsDO = handlingSpecificationsMapper.selectOne(new LambdaQueryWrapperX() + .eqIfPresent(HandlingSpecificationsDO::getFactoryId, factoryId) + .eqIfPresent(HandlingSpecificationsDO::getSpecificationsName, importUser.getSpecificationsName())); + + updateDo.setId(handlingSpecificationsDO.getId()); + handlingSpecificationsMapper.updateById(updateDo); + respVO.getUpdateUsernames().add(factoryName); + return; + } + + // 插入用户 + handlingSpecificationsMapper.insert(updateDo); + respVO.getCreateUsernames().add(factoryName); + }); + + return respVO; + } + + private void validateForCreate(Long factoryId, String specificationsName) { + + // 关闭数据权限,避免因为没有数据权限,查询不到数据,进而导致唯一校验不正确 + DataPermissionUtils.executeIgnore(() -> { + + // 校验用户名、身份证号是否已经存在 + Long cunt = handlingSpecificationsMapper.selectCount(new LambdaQueryWrapperX() + .eqIfPresent(HandlingSpecificationsDO::getFactoryId, factoryId) + .eqIfPresent(HandlingSpecificationsDO::getSpecificationsName, specificationsName)); + if (cunt > 0) { + throw exception(FACTORY_HANDLING_SPECIFICATION_EXISTS); + } + }); + } + }