From 428696b1d05e4b82920c6eefe7bd90948a455296 Mon Sep 17 00:00:00 2001 From: "hongxi.lian" Date: Mon, 16 Oct 2023 09:43:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=80=92=E6=95=B05=E4=B8=AA=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wms/enums/team/ErrorCodeConstants.java | 19 ++++ .../transactiontype/ErrorCodeConstants.java | 8 +- .../wms/controller/team/TeamController.java | 49 ++++++++- .../controller/team/vo/TeamImportExcelVo.java | 54 ++++++++++ .../module/wms/convert/team/TeamConvert.java | 6 ++ .../module/wms/dal/mysql/team/TeamMapper.java | 4 + .../module/wms/service/team/TeamService.java | 4 + .../wms/service/team/TeamServiceImpl.java | 99 ++++++++++++++++++- .../TransactiontypeServiceImpl.java | 3 +- 9 files changed, 239 insertions(+), 7 deletions(-) create mode 100644 win-module-wms/win-module-wms-api/src/main/java/com/win/module/wms/enums/team/ErrorCodeConstants.java create mode 100644 win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/controller/team/vo/TeamImportExcelVo.java diff --git a/win-module-wms/win-module-wms-api/src/main/java/com/win/module/wms/enums/team/ErrorCodeConstants.java b/win-module-wms/win-module-wms-api/src/main/java/com/win/module/wms/enums/team/ErrorCodeConstants.java new file mode 100644 index 00000000..3d6e6c80 --- /dev/null +++ b/win-module-wms/win-module-wms-api/src/main/java/com/win/module/wms/enums/team/ErrorCodeConstants.java @@ -0,0 +1,19 @@ +package com.win.module.wms.enums.team; + +import com.win.framework.common.exception.ErrorCode; + +/** + * System 错误码枚举类 + * + * system 系统,使用 1-002-000-000 段 + */ +public interface ErrorCodeConstants { + + + ErrorCode TEAM_NOT_EXISTS = new ErrorCode(1_000_058_000, "班组不存在"); + + ErrorCode TEAM_CODE_EXISTSNO = new ErrorCode(1_000_058_001, "code已存在"); + + ErrorCode TEAM_AVAIABLE_NOT_EXISTS = new ErrorCode(1_000_058_002, "是否可用不存在"); + +} diff --git a/win-module-wms/win-module-wms-api/src/main/java/com/win/module/wms/enums/transactiontype/ErrorCodeConstants.java b/win-module-wms/win-module-wms-api/src/main/java/com/win/module/wms/enums/transactiontype/ErrorCodeConstants.java index 1c9f91fb..8640e464 100644 --- a/win-module-wms/win-module-wms-api/src/main/java/com/win/module/wms/enums/transactiontype/ErrorCodeConstants.java +++ b/win-module-wms/win-module-wms-api/src/main/java/com/win/module/wms/enums/transactiontype/ErrorCodeConstants.java @@ -9,12 +9,12 @@ import com.win.framework.common.exception.ErrorCode; */ public interface ErrorCodeConstants { - ErrorCode NOT_EXISTS = new ErrorCode(1_0_000_000, "仓库不存在"); - ErrorCode TRANSACTIONTYPE_CODE_EXISTS = new ErrorCode(1_0_000_001, "code已存在"); + ErrorCode TRANSACTIONTYPE_NOT_EXISTS = new ErrorCode(1_049_000_000, "事务类型不存在"); + ErrorCode TRANSACTIONTYPE_CODE_EXISTS = new ErrorCode(1_049_000_001, "code已存在"); - ErrorCode CODE_EXISTSNO = new ErrorCode(1_039_000_002, "code已存在"); + ErrorCode CODE_EXISTSNO = new ErrorCode(1_049_000_002, "code已存在"); - ErrorCode TRANSACTIONTYPE_AVAIABLE_NOT_EXISTS = new ErrorCode(1_040_000_003, "是否可用不存在"); + ErrorCode TRANSACTIONTYPE_AVAIABLE_NOT_EXISTS = new ErrorCode(1_049_000_003, "是否可用不存在"); } diff --git a/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/controller/team/TeamController.java b/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/controller/team/TeamController.java index 70991f83..6f9bf333 100644 --- a/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/controller/team/TeamController.java +++ b/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/controller/team/TeamController.java @@ -1,6 +1,9 @@ package com.win.module.wms.controller.team; +import com.win.framework.dict.core.util.DictFrameworkUtils; import com.win.module.wms.controller.team.vo.*; +import com.win.module.wms.enums.DictTypeConstants; +import io.swagger.v3.oas.annotations.Parameters; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import org.springframework.validation.annotation.Validated; @@ -11,6 +14,8 @@ import io.swagger.v3.oas.annotations.Operation; import javax.validation.*; import javax.servlet.http.*; +import java.time.LocalDateTime; +import java.time.ZoneOffset; import java.util.*; import java.io.IOException; @@ -23,10 +28,10 @@ import com.win.framework.excel.core.util.ExcelUtils; import com.win.framework.operatelog.core.annotations.OperateLog; import static com.win.framework.operatelog.core.enums.OperateTypeEnum.*; -import com.win.module.wms.controller.team.vo.*; import com.win.module.wms.dal.dataobject.team.TeamDO; import com.win.module.wms.convert.team.TeamConvert; import com.win.module.wms.service.team.TeamService; +import org.springframework.web.multipart.MultipartFile; @Tag(name = "管理后台 - 班组") @RestController @@ -98,5 +103,47 @@ public class TeamController { List datas = TeamConvert.INSTANCE.convertList02(list); ExcelUtils.write(response, "班组.xls", "数据", TeamExcelVO.class, datas); } + @GetMapping("/get-import-template") + @Operation(summary = "获得导入班组信息模板") + public void importTemplate(HttpServletResponse response) throws IOException { + // 手动创建导出 demo + List list = Arrays.asList( + ); + // 手动创建导出 demo + Map mapDropDown = new HashMap<>(); + String[] available = DictFrameworkUtils.dictTypeDictDataValue(DictTypeConstants.TRUE_FALSE); + mapDropDown.put(4, available); + // 输出 + + // 输出 + ExcelUtils.write(response, "班组导入模板.xls", "班组列表", TeamImportExcelVo.class, list,mapDropDown); + } + + + @PostMapping("/import") + @Operation(summary = "导入班组信息") + @Parameters({ + @Parameter(name = "file", description = "Excel 文件", required = true), + @Parameter(name = "mode", description = "导入模式1更新2追加3覆盖", example = "1"), + @Parameter(name = "updatePart", description = "部分更新,默认为 true", example = "true") + }) + @PreAuthorize("@ss.hasPermission('wms:team:import')") + public CommonResult> importExcel(HttpServletResponse response, + @RequestParam("file") MultipartFile file, + @RequestParam(value = "mode") Integer mode, + @RequestParam(value = "updatePart", required = false, defaultValue = "false") Boolean updatePart) throws Exception { + List list = ExcelUtils.read(file, TeamImportExcelVo.class); + List errorList = teamService.importTeamList(list, mode, updatePart); + Map returnMap = new HashMap<>(); + returnMap.put("errorCount", errorList.size()); + if(!errorList.isEmpty()) { + String url = ExcelUtils.writeLocalFile("班组导入错误数据" + LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")) + ".xls", "错误列表", errorList); + returnMap.put("errorFile", url); + } + return success(returnMap); + } + + + } diff --git a/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/controller/team/vo/TeamImportExcelVo.java b/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/controller/team/vo/TeamImportExcelVo.java new file mode 100644 index 00000000..538fd11f --- /dev/null +++ b/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/controller/team/vo/TeamImportExcelVo.java @@ -0,0 +1,54 @@ +package com.win.module.wms.controller.team.vo; +import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.annotation.write.style.ColumnWidth; +import com.win.framework.excel.core.annotations.DictFormat; +import com.win.framework.excel.core.convert.DictConvert; +import com.win.module.wms.enums.DictTypeConstants; +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 org.springframework.format.annotation.DateTimeFormat; + +import javax.validation.constraints.NotNull; +import java.time.LocalDateTime; + +import static com.win.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +@Accessors(chain = false) // 设置 chain = false,避免用户导入有问题 +public class TeamImportExcelVo { + + + @ExcelProperty("代码") + private String code; + + @ExcelProperty("名称") + private String name; + + @ExcelProperty("描述") + private String description; + + @ExcelProperty("成员") + private String members; + + @ExcelProperty(value = "生效时间") + @ColumnWidth(value = 16) + private LocalDateTime activeTime; + + @ExcelProperty(value = "失效时间") + @ColumnWidth(value = 16) + private LocalDateTime expireTime; + + @ExcelProperty("备注") + private String remark; + + @ExcelProperty(value = "是否为空", converter = DictConvert.class) + @DictFormat(DictTypeConstants.TRUE_FALSE) + private String available; +} diff --git a/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/convert/team/TeamConvert.java b/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/convert/team/TeamConvert.java index 6b907bdb..e5a2920e 100644 --- a/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/convert/team/TeamConvert.java +++ b/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/convert/team/TeamConvert.java @@ -8,6 +8,10 @@ import com.win.module.wms.controller.team.vo.TeamCreateReqVO; import com.win.module.wms.controller.team.vo.TeamExcelVO; import com.win.module.wms.controller.team.vo.TeamRespVO; import com.win.module.wms.controller.team.vo.TeamUpdateReqVO; +import com.win.module.wms.controller.team.vo.TeamImportExcelVo; + +import com.win.module.wms.controller.transactiontype.vo.TransactiontypeImportExcelVo; +import com.win.module.wms.dal.dataobject.transactiontype.TransactiontypeDO; import org.mapstruct.Mapper; import org.mapstruct.factory.Mappers; import com.win.module.wms.dal.dataobject.team.TeamDO; @@ -34,4 +38,6 @@ public interface TeamConvert { List convertList02(List list); + TeamDO convert(TeamImportExcelVo bean); + } diff --git a/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/dal/mysql/team/TeamMapper.java b/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/dal/mysql/team/TeamMapper.java index d2ca4b38..e5831a21 100644 --- a/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/dal/mysql/team/TeamMapper.java +++ b/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/dal/mysql/team/TeamMapper.java @@ -8,6 +8,7 @@ import com.win.framework.mybatis.core.mapper.BaseMapperX; import com.win.module.wms.controller.team.vo.TeamExportReqVO; import com.win.module.wms.controller.team.vo.TeamPageReqVO; import com.win.module.wms.dal.dataobject.team.TeamDO; +import com.win.module.wms.dal.dataobject.warehouse.WarehouseDO; import org.apache.ibatis.annotations.Mapper; /** @@ -18,6 +19,9 @@ import org.apache.ibatis.annotations.Mapper; @Mapper public interface TeamMapper extends BaseMapperX { + default TeamDO selectByCode(String code) { + return selectOne(TeamDO::getCode, code); + } default PageResult selectPage(TeamPageReqVO reqVO) { return selectPage(reqVO, new LambdaQueryWrapperX() .eqIfPresent(TeamDO::getCode, reqVO.getCode()) diff --git a/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/service/team/TeamService.java b/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/service/team/TeamService.java index 6d00949a..24ffa395 100644 --- a/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/service/team/TeamService.java +++ b/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/service/team/TeamService.java @@ -7,6 +7,8 @@ import com.win.module.wms.controller.team.vo.TeamCreateReqVO; import com.win.module.wms.controller.team.vo.TeamExportReqVO; import com.win.module.wms.controller.team.vo.TeamPageReqVO; import com.win.module.wms.controller.team.vo.TeamUpdateReqVO; +import com.win.module.wms.controller.team.vo.TeamImportExcelVo; +import com.win.module.wms.controller.transactiontype.vo.TransactiontypeImportExcelVo; import com.win.module.wms.dal.dataobject.team.TeamDO; import com.win.framework.common.pojo.PageResult; @@ -71,4 +73,6 @@ public interface TeamService { */ List getTeamList(TeamExportReqVO exportReqVO); + List importTeamList(List importTeam, Integer mode, boolean updatePart); + } diff --git a/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/service/team/TeamServiceImpl.java b/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/service/team/TeamServiceImpl.java index e8583d43..f2b6f020 100644 --- a/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/service/team/TeamServiceImpl.java +++ b/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/service/team/TeamServiceImpl.java @@ -1,11 +1,21 @@ package com.win.module.wms.service.team; +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.StrUtil; +import com.google.common.annotations.VisibleForTesting; +import com.win.framework.common.exception.ServiceException; +import com.win.framework.datapermission.core.util.DataPermissionUtils; import com.win.module.wms.controller.team.vo.TeamCreateReqVO; import com.win.module.wms.controller.team.vo.TeamExportReqVO; import com.win.module.wms.controller.team.vo.TeamPageReqVO; import com.win.module.wms.controller.team.vo.TeamUpdateReqVO; +import com.win.module.wms.controller.team.vo.TeamImportExcelVo; +import com.win.module.wms.convert.team.TeamConvert; +import com.win.module.wms.dal.dataobject.team.TeamDO; import org.springframework.stereotype.Service; import javax.annotation.Resource; + +import org.springframework.transaction.interceptor.TransactionAspectSupport; import org.springframework.validation.annotation.Validated; import java.util.*; @@ -17,7 +27,9 @@ import com.win.module.wms.convert.team.TeamConvert; import com.win.module.wms.dal.mysql.team.TeamMapper; import static com.win.framework.common.exception.util.ServiceExceptionUtil.exception; -import static com.win.module.wms.enums.ErrorCodeConstants.*; +import static com.win.module.system.enums.ErrorCodeConstants.USER_IMPORT_LIST_IS_EMPTY; + +import static com.win.module.wms.enums.team.ErrorCodeConstants.*; /** * 班组 Service 实现类 @@ -83,4 +95,89 @@ public class TeamServiceImpl implements TeamService { return teamMapper.selectList(exportReqVO); } + public List importTeamList(List teams, Integer mode, boolean updatePart) { + if (CollUtil.isEmpty(teams)) { + throw exception(USER_IMPORT_LIST_IS_EMPTY); + } + List errorList = new ArrayList<>(); + teams.forEach(team -> { + // 校验,判断是否有不符合的原因 + try { + if(mode != null){ + validateTeamForCreateOrUpdate(null,team.getCode(),team.getAvailable()); + } + } catch (ServiceException ex) { + errorList.add(team); + return; + } + + // 判断如果不存在,在进行插入 + TeamDO existTeam = teamMapper.selectByCode(team.getCode()); + if (existTeam == null&& mode != 3) { + teamMapper.insert(TeamConvert.INSTANCE.convert(team)); + } + else if (existTeam != null && mode != 2) {// 如果存在,判断是否允许更新 + TeamDO teamDO = TeamConvert.INSTANCE.convert(team); + teamDO.setId(existTeam.getId()); + teamMapper.updateById(teamDO); + } + }); + //错误不为空并非部分更新,手工回滚 + if(!errorList.isEmpty() && !updatePart) { + TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); + } + return errorList; + } + + private void validateTeamForCreateOrUpdate(Long id, String code, String available) { + // 关闭数据权限,避免因为没有数据权限,查询不到数据,进而导致唯一校验不正确 + DataPermissionUtils.executeIgnore(() -> { + validateTeampExists(id); + // 校验code唯一 + validateCodeExists(id,code); + validateAvailableExists(available); + + }); + } + @VisibleForTesting + private void validateTeampExists(Long id) { + if (id == null) { + return; + } + TeamDO team = teamMapper.selectById(id); + if (team == null) { + throw exception(TEAM_NOT_EXISTS); + } + } + + @VisibleForTesting + private void validateCodeExists(Long id,String code) { + if (code.isEmpty()) { + throw exception(TEAM_CODE_EXISTSNO); + } + if (StrUtil.isBlank(code)) { + return; + } + TeamDO team = teamMapper.selectByCode(code); + if (team == null) { + return; + } + // 如果 id 为空,说明不用比较是否为相同 id 的用户 + if (id == null) { + throw exception(TEAM_CODE_EXISTSNO); + } + if (!team.getId().equals(id)) { + throw exception(TEAM_CODE_EXISTSNO); + } + } + @VisibleForTesting + private void validateAvailableExists(String available) { + if (available == null) { + throw exception(TEAM_AVAIABLE_NOT_EXISTS); + } + } + + + + } diff --git a/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/service/transactiontype/TransactiontypeServiceImpl.java b/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/service/transactiontype/TransactiontypeServiceImpl.java index c04ba3b3..50d9aaf1 100644 --- a/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/service/transactiontype/TransactiontypeServiceImpl.java +++ b/win-module-wms/win-module-wms-biz/src/main/java/com/win/module/wms/service/transactiontype/TransactiontypeServiceImpl.java @@ -29,6 +29,7 @@ import static com.win.framework.common.exception.util.ServiceExceptionUtil.excep import static com.win.module.system.enums.ErrorCodeConstants.USER_IMPORT_LIST_IS_EMPTY; import static com.win.module.wms.enums.ErrorCodeConstants.*; import static com.win.module.wms.enums.transactiontype.ErrorCodeConstants.*; +import static com.win.module.wms.enums.transactiontype.ErrorCodeConstants.TRANSACTIONTYPE_NOT_EXISTS; /** @@ -71,7 +72,7 @@ public class TransactiontypeServiceImpl implements TransactiontypeService { private void validateTransactiontypeExists(Long id) { if (transactiontypeMapper.selectById(id) == null) { - throw exception(NOT_EXISTS); + throw exception(TRANSACTIONTYPE_NOT_EXISTS); } }