Procházet zdrojové kódy

增加导入和导出模板

tanzhongran před 3 roky
rodič
revize
a133967e86

+ 28 - 0
abi-cloud-qr-platform-common/src/main/java/com/abi/task/common/excel/preperties/MaterialProperty.java

@@ -0,0 +1,28 @@
+package com.abi.task.common.excel.preperties;
+
+
+import com.abi.task.common.excel.common.AbstractExcelProperty;
+import com.abi.task.common.excel.common.ExcelHeadAlias;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+/**
+ * 活动
+ * @author Andy.Tan
+ */
+@Data
+public class MaterialProperty implements AbstractExcelProperty {
+
+    @ExcelHeadAlias(value = "物料类型(必填)(枚举:瓶&罐盖,成品酒)")
+    private String materialType;
+
+    @ExcelHeadAlias(value = "物料号(必填)")
+    private String materialCode;
+
+    @ExcelHeadAlias(value = "物料名称(必填)")
+    private String materialName;
+
+    @ExcelHeadAlias(value = "描述")
+    private String description;
+
+}

+ 29 - 4
abi-cloud-qr-platform-server/src/main/java/com/abi/qms/platform/controller/console/MaterialController.java

@@ -1,18 +1,19 @@
 package com.abi.qms.platform.controller.console;
 
 import com.abi.qms.platform.dto.req.*;
-import com.abi.qms.platform.dto.res.GetMaterialRes;
-import com.abi.qms.platform.dto.res.ListBrandForSelectRes;
-import com.abi.qms.platform.dto.res.ListMaterialForSelectRes;
-import com.abi.qms.platform.dto.res.ListMaterialRes;
+import com.abi.qms.platform.dto.res.*;
 import com.abi.qms.platform.service.MaterialService;
 import com.abi.task.common.api.base.BaseResponse;
+import com.abi.task.common.excel.ExcelInputFactory;
+import com.abi.task.common.excel.preperties.DepartmentProperty;
+import com.abi.task.common.excel.preperties.MaterialProperty;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletResponse;
 import java.util.List;
@@ -97,6 +98,30 @@ public class MaterialController {
         return BaseResponse.create(result);
     }
 
+    @ResponseBody
+    @ApiOperation(value = "导出模板")
+    @GetMapping("downloadExcelModel")
+    public void downloadExcelModel(HttpServletResponse response) throws Exception {
+        ExcelInputFactory.downloadExcelModel("物料模板", response, MaterialProperty.class);
+    }
+
+    @ApiOperation("excel批量导入")
+    @PostMapping("materialExcelImport")
+    public BaseResponse<ExcelImportRes> materialExcelImport(@RequestParam("file") MultipartFile file) throws Exception {
+        List<MaterialProperty> materialPropertyList = ExcelInputFactory.getExcelPojoList(file, MaterialProperty.class);
+        //excel批量导入
+        ExcelImportRes result = materialService.materialExcelImport(materialPropertyList);
+        //包装出参
+        return BaseResponse.create(result);
+    }
+
+
+
+
+
+
+
+
 }
 
 

+ 48 - 0
abi-cloud-qr-platform-server/src/main/java/com/abi/qms/platform/dao/enums/MaterialTypeEnum.java

@@ -0,0 +1,48 @@
+package com.abi.qms.platform.dao.enums;
+
+import com.baomidou.mybatisplus.annotation.EnumValue;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+import java.util.EnumSet;
+import java.util.Set;
+
+/**
+ * 物料类型
+ * @Author AndyTan
+ */
+@Getter
+@AllArgsConstructor
+@JsonFormat(shape = JsonFormat.Shape.OBJECT)
+public enum MaterialTypeEnum {
+
+	//物料类型: 1-瓶&罐盖 2-成品酒
+	COVER(1,"瓶&罐盖"),
+	BEER(2,"成品酒");
+
+	@EnumValue
+	private Integer code;
+	private String name;
+
+	private static final Set<MaterialTypeEnum> ALL = EnumSet.allOf(MaterialTypeEnum.class);
+
+    public static String getName(Integer code) {
+        return ALL.stream()
+                .filter(o -> o.code.equals(code))
+                .map(o -> o.getName())
+                .findAny().orElse(null);
+    }
+
+    public static Integer getCode(String name) {
+        return ALL.stream()
+                .filter(o -> o.name.equals(name))
+                .map(o -> o.getCode())
+                .findAny().orElse(null);
+    }
+
+	public boolean is(Integer code){
+		return getCode().equals(code);
+	}
+
+}

+ 19 - 4
abi-cloud-qr-platform-server/src/main/java/com/abi/qms/platform/service/MaterialService.java

@@ -2,10 +2,11 @@ package com.abi.qms.platform.service;
 
 
 import com.abi.qms.platform.dto.req.*;
-import com.abi.qms.platform.dto.res.GetMaterialRes;
-import com.abi.qms.platform.dto.res.ListBrandForSelectRes;
-import com.abi.qms.platform.dto.res.ListMaterialForSelectRes;
-import com.abi.qms.platform.dto.res.ListMaterialRes;
+import com.abi.qms.platform.dto.res.*;
+import com.abi.task.common.excel.preperties.DepartmentProperty;
+import com.abi.task.common.excel.preperties.MaterialProperty;
+
+import java.util.List;
 
 /**
  * <p>
@@ -56,6 +57,20 @@ public interface MaterialService{
      */
     void disableMaterial(DisableMaterialReq disableMaterialReq);
 
+    /**
+     * 批量导入物料
+     * @param materialPropertyList
+     * @return
+     * @throws Exception
+     */
+    ExcelImportRes materialExcelImport(List<MaterialProperty> materialPropertyList) throws Exception;
+
+    /**
+     * 其他页面下拉选择物料
+     * @param listMaterialForSelectReq
+     * @return
+     * @throws Exception
+     */
     ListMaterialForSelectRes listMaterialForSelect(ListMaterialForSelectReq listMaterialForSelectReq) throws Exception;
 
 

+ 69 - 4
abi-cloud-qr-platform-server/src/main/java/com/abi/qms/platform/service/impl/MaterialServiceImpl.java

@@ -2,24 +2,27 @@ package com.abi.qms.platform.service.impl;
 
 import com.abi.qms.platform.dao.entity.BaseBrand;
 import com.abi.qms.platform.dao.entity.BaseMaterial;
+import com.abi.qms.platform.dao.enums.FactoryTypeEnum;
+import com.abi.qms.platform.dao.enums.MaterialTypeEnum;
 import com.abi.qms.platform.dao.enums.ValidEnum;
 import com.abi.qms.platform.dao.mapper.BaseBrandMapper;
 import com.abi.qms.platform.dao.mapper.BaseMaterialMapper;
 import com.abi.qms.platform.dao.vo.result.MaterialVO;
 import com.abi.qms.platform.dto.req.*;
-import com.abi.qms.platform.dto.res.GetMaterialRes;
-import com.abi.qms.platform.dto.res.ListBrandForSelectRes;
-import com.abi.qms.platform.dto.res.ListMaterialForSelectRes;
-import com.abi.qms.platform.dto.res.ListMaterialRes;
+import com.abi.qms.platform.dto.res.*;
 import com.abi.qms.platform.infrastructure.util.AssertUtil;
 import com.abi.qms.platform.infrastructure.util.PageUtil;
 import com.abi.qms.platform.service.MaterialService;
 import com.abi.task.common.api.exception.BusinessException;
+import com.abi.task.common.api.exception.ErrorCodeEnum;
+import com.abi.task.common.excel.preperties.DepartmentProperty;
+import com.abi.task.common.excel.preperties.MaterialProperty;
 import com.abi.task.common.utils.PojoConverterUtils;
 import com.baomidou.mybatisplus.core.conditions.query.Query;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.redisson.misc.Hash;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -197,8 +200,70 @@ public class MaterialServiceImpl implements MaterialService {
         return res;
     }
 
+    /**
+     * 批量导入物料
+     * @param materialPropertyList
+     * @return
+     * @throws Exception
+     */
+    @Override
+    public ExcelImportRes materialExcelImport(List<MaterialProperty> materialPropertyList) throws Exception {
+        List<ExcelImportRes.FailBean> failList = new ArrayList<>();
+
+        //1-循环导入,有异常则捕获
+        for (int i = 0; i < materialPropertyList.size(); i++) {
+            MaterialProperty materialProperty = materialPropertyList.get(i);
+            try {
+                //1-校验必填与枚举
+                String errorMessage = checkMaterialProperty(materialProperty);
+                if (StringUtils.isNotBlank(errorMessage)) {
+                    throw new BusinessException(ErrorCodeEnum.ERROR_PARAM.getCode(), errorMessage);
+                }
+
+                //2-调用业务代码新增
+                AddMaterialReq req = new AddMaterialReq();
+                req.setMaterialCode(materialProperty.getMaterialCode());
+                req.setMaterialName(materialProperty.getMaterialName());
+                req.setMaterialType(FactoryTypeEnum.getCode(materialProperty.getMaterialType()));
+                req.setDescription(materialProperty.getDescription());
+
+                addMaterial(req);
+            } catch (BusinessException e) {
+                failList.add(new ExcelImportRes.FailBean(i + 2, e.getMessage()));
+            }
+        }
+
+        //2-返回导入结果
+        ExcelImportRes res = new ExcelImportRes();
+        res.setFailedCount(failList.size());
+        res.setSuccessCount(materialPropertyList.size() - res.getFailedCount());
+        res.setFailBeanList(failList);
 
+        return res;
+    }
 
+    private String checkMaterialProperty(MaterialProperty materialProperty) {
+        StringBuffer errorMessage = new StringBuffer();
+        //逐个字段校验
+        if (StringUtils.isBlank(materialProperty.getMaterialType())) {
+            errorMessage.append("物料类型").append("为空,");
+        }
+        if (StringUtils.isNotBlank(materialProperty.getMaterialType())
+                && FactoryTypeEnum.getCode(materialProperty.getMaterialType()) == null) {
+            errorMessage.append("物料类型").append("枚举值不正确,");
+        }
+        if (StringUtils.isBlank(materialProperty.getMaterialCode())) {
+            errorMessage.append("物料号").append("为空,");
+        }
+        if (StringUtils.isBlank(materialProperty.getMaterialName())) {
+            errorMessage.append("物料名称").append("为空,");
+        }
+        if (StringUtils.isBlank(materialProperty.getDescription())) {
+            errorMessage.append("描述").append("为空,");
+        }
+
+        return errorMessage.toString();
+    }
 
 
 }