feat(handling-specifications): 添加工厂搬运规格导入功能

- 新增导入模板下载和 Excel 导入接口
- 实现搬运规格列表的导入逻辑,支持创建和更新操作
- 添加相关 VO 类和错误码常量
- 优化工厂名称显示,使用简称替代全称
This commit is contained in:
furongxin 2025-04-30 11:48:37 +08:00
parent 1aeef763b6
commit b6fb1c99ad
7 changed files with 234 additions and 22 deletions

View File

@ -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, "工厂搬运规格已存在");
}

View File

@ -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<FactoryInfoDO> factoryInfoDOS = factoryInfoService.getFactoryListByType();
List<String> factoryName = factoryInfoDOS.stream()
.map(item -> item.getId() + ":" + item.getShortName())
.collect(Collectors.toList());
// 类型选择
List<String> type = Arrays.asList("0:数量", "1:重量");
Map<Integer, List<String>> 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<HandlingSpecificationsImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
@RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
List<HandlingSpecificationsImportVO> list = ExcelUtils.read(file, HandlingSpecificationsImportVO.class);
return success(handlingSpecificationsService.importList(list, updateSupport));
}
}

View File

@ -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<String> createUsernames;
@Schema(description = "更新成功的用户名数组", requiredMode = Schema.RequiredMode.REQUIRED)
private List<String> updateUsernames;
@Schema(description = "导入失败的用户集合key 为用户名value 为失败原因", requiredMode = Schema.RequiredMode.REQUIRED)
private Map<String, String> failureUsernames;
}

View File

@ -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;
}

View File

@ -160,7 +160,7 @@ public class StaffController {
//获取部门名称
List<FactoryInfoDO> factoryInfoDOS = factoryInfoService.getFactoryListByType();
List<String> factoryName = factoryInfoDOS.stream()
.map(item -> item.getId() + ":" + item.getName())
.map(item -> item.getId() + ":" + item.getShortName())
.collect(Collectors.toList());
//获取工种名称

View File

@ -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<HandlingSpecific
* @return
*/
List<HandlingSpecificationsDO> getHandlingSpecificationsList(@Valid HandlingSpecificationsPageReqVO pageReqVO);
/**
* 导入列表
* @param list
* @param updateSupport
* @return
*/
HandlingSpecificationsImportRespVO importList(List<HandlingSpecificationsImportVO> list, Boolean updateSupport);
}

View File

@ -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<HandlingSpeci
return handlingSpecificationsMapper.getHandlingSpecificationsList(pageReqVO);
}
@Override
@Transactional(rollbackFor = Exception.class) // 添加事务异常则回滚所有导入
public HandlingSpecificationsImportRespVO importList(List<HandlingSpecificationsImportVO> 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<HandlingSpecificationsDO>()
.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<HandlingSpecificationsDO>()
.eqIfPresent(HandlingSpecificationsDO::getFactoryId, factoryId)
.eqIfPresent(HandlingSpecificationsDO::getSpecificationsName, specificationsName));
if (cunt > 0) {
throw exception(FACTORY_HANDLING_SPECIFICATION_EXISTS);
}
});
}
}