From 33032d320cb75854def7391026b0401f1bfb8360 Mon Sep 17 00:00:00 2001
From: 黎星凯 <13949086503@163.com>
Date: 星期六, 11 五月 2024 17:26:02 +0800
Subject: [PATCH] 20240511修改: 分发单优化, 分类,物品,分发单明细,采购单明细数据导入

---
 consum-base/src/main/java/com/consum/base/controller/BaseGoodsTemplateController.java |  269 ++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 180 insertions(+), 89 deletions(-)

diff --git a/consum-base/src/main/java/com/consum/base/controller/BaseGoodsTemplateController.java b/consum-base/src/main/java/com/consum/base/controller/BaseGoodsTemplateController.java
index fe31dc4..d19a2f3 100644
--- a/consum-base/src/main/java/com/consum/base/controller/BaseGoodsTemplateController.java
+++ b/consum-base/src/main/java/com/consum/base/controller/BaseGoodsTemplateController.java
@@ -7,7 +7,9 @@
 import java.util.Map;
 import java.util.Optional;
 import java.util.stream.Collectors;
-
+import cn.hutool.core.util.StrUtil;
+import com.alibaba.excel.exception.ExcelAnalysisException;
+import com.consum.base.service.*;
 import org.apache.commons.compress.utils.Lists;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.CollectionUtils;
@@ -17,7 +19,6 @@
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
-
 import com.alibaba.excel.EasyExcelFactory;
 import com.alibaba.excel.context.AnalysisContext;
 import com.alibaba.excel.event.AnalysisEventListener;
@@ -30,11 +31,6 @@
 import com.consum.base.pojo.BaseGoodsTemplateParam;
 import com.consum.base.pojo.excel.ImportGoodsInfoTemplate;
 import com.consum.base.pojo.response.GoodsTemplateVO;
-import com.consum.base.service.BaseCategoryService;
-import com.consum.base.service.BaseGoodsTemplateService;
-import com.consum.base.service.BaseWarehouseService;
-import com.consum.base.service.FinSysTenantService;
-import com.consum.base.service.LWhGoodsService;
 import com.consum.model.po.BaseCategory;
 import com.consum.model.po.BaseGoodsModels;
 import com.consum.model.po.BaseGoodsTemplate;
@@ -77,6 +73,9 @@
     @Autowired
     private BaseCategoryService baseCategoryService;
 
+    @Autowired
+    private BaseGoodsModelsService baseGoodsModelsService;
+
     /**
      * @Description 鏂板鐗╁搧妯℃澘
      * @Author 鍗㈠簡闃�
@@ -101,7 +100,7 @@
         }
         // 鍒ゆ柇鍚屼竴鍒嗙被涓嬬殑鐗╁搧鍚嶇О鏄惁閲嶅
         BaseGoodsTemplate goodsTemplate =
-            this.baseGoodsTemplateService.getByGoodsNameAndCategoryId(param.getGoodsName(), param.getCategoryId());
+                this.baseGoodsTemplateService.getByGoodsNameAndCategoryId(param.getGoodsName(), param.getCategoryId());
         if (goodsTemplate != null) {
             return ResponseValue.error("鐗╁搧鍚嶇О宸插瓨鍦�");
         }
@@ -181,9 +180,9 @@
      *
      * @author 鍗㈠簡闃�
      * @date 2023/10/25
-     *       <p>
-     *       鐗╁搧鐨勭鐢ㄦ垨鍒犻櫎锛屼笉褰卞搷宸茬粡閲囪喘鍏ヨ繃搴撶殑鐗╁搧淇℃伅銆�
-     *       <p>
+     * <p>
+     * 鐗╁搧鐨勭鐢ㄦ垨鍒犻櫎锛屼笉褰卞搷宸茬粡閲囪喘鍏ヨ繃搴撶殑鐗╁搧淇℃伅銆�
+     * <p>
      */
     @PostMapping("/updStatus")
     public ResponseValue updateStatus() {
@@ -204,9 +203,9 @@
      * @Description 鏍规嵁鐗╁搧id鍒犻櫎鐗╁搧
      * @Author 鍗㈠簡闃�
      * @Date 2023/10/25
-     *       <p>
-     *       鐗╁搧鐨勭鐢ㄦ垨鍒犻櫎锛屼笉褰卞搷宸茬粡閲囪喘鍏ヨ繃搴撶殑鐗╁搧淇℃伅銆�
-     *       <p/>
+     * <p>
+     * 鐗╁搧鐨勭鐢ㄦ垨鍒犻櫎锛屼笉褰卞搷宸茬粡閲囪喘鍏ヨ繃搴撶殑鐗╁搧淇℃伅銆�
+     * <p/>
      */
     @DeleteMapping("/del")
     public ResponseValue updateById() {
@@ -249,7 +248,7 @@
     public ResponseValue queryGoodsTemplateByCategoryId(Long agencyId, Long categoryId) {
         // 涓嶉檺鍒舵満鏋�
         List<BaseGoodsTemplate> list = baseGoodsTemplateService.queryGoodsTemplateByCategoryId(null, categoryId,
-            StatesType.NORMAL.getValue(), null);
+                StatesType.NORMAL.getValue(), null);
         if (list == null) {
             return ResponseValue.error("鏌ヨ澶辫触锛�");
         }
@@ -258,10 +257,10 @@
 
     @ApiOperation(value = "璋冩嫧鏌ヨ鏈烘瀯涓嬫墍鏈変粨搴撲笅鐨勫垎绫绘ā鏉夸俊鎭�", notes = "璋冩嫧鏌ヨ鏈烘瀯涓嬫墍鏈変粨搴撲笅鐨勫垎绫绘ā鏉夸俊鎭�")
     @ApiImplicitParams({
-        @ApiImplicitParam(name = "agencyId", value = "鐖剁骇鏈烘瀯id", required = true, dataType = "java.lang.Long",
-            paramType = "query"),
-        @ApiImplicitParam(name = "categoryId", value = "鍒嗙被id", required = true, dataType = "Long",
-            paramType = "query")})
+            @ApiImplicitParam(name = "agencyId", value = "鐖剁骇鏈烘瀯id", required = true, dataType = "java.lang.Long",
+                    paramType = "query"),
+            @ApiImplicitParam(name = "categoryId", value = "鍒嗙被id", required = true, dataType = "Long",
+                    paramType = "query")})
     @GetMapping("/query/warehouse/goods")
     public ResponseValue queryWarehouseGoods(Long agencyId, Long categoryId) {
 
@@ -270,7 +269,7 @@
         map.put("categoryId", categoryId);
 
         List<BaseWarehouse> baseWarehouseList =
-            baseWarehouseService.getBaseWareHouseList(agencyId, StatesType.NORMAL.getValue());
+                baseWarehouseService.getBaseWareHouseList(agencyId, StatesType.NORMAL.getValue());
         if (CollectionUtils.isEmpty(baseWarehouseList)) {
             return ResponseValue.error("鏈烘瀯鏃犱粨搴擄紒");
 
@@ -279,11 +278,11 @@
         map.put("warehouseIdList", warehouseIdList);
 
         String sql =
-            "SELECT DISTINCT bgt.id,bgt.GOODS_NAME, CLASSIFICATION type FROM l_wh_goods g LEFT JOIN base_goods_template bgt ON g.BASE_GOODS_TEMPLATE_ID = bgt.id "
-                + "WHERE WAREHOUSE_TYPE = 0 " + "AND WAREHOUSE_ID in (:warehouseIdList) "
-                + "AND CATEGORY_ID = :categoryId "
-                // 1:闆嗛噰,2:鑷噰
-                + "AND BUY_TYPE =1";
+                "SELECT DISTINCT bgt.id,bgt.GOODS_NAME, CLASSIFICATION type FROM l_wh_goods g LEFT JOIN base_goods_template bgt ON g.BASE_GOODS_TEMPLATE_ID = bgt.id "
+                        + "WHERE WAREHOUSE_TYPE = 0 " + "AND WAREHOUSE_ID in (:warehouseIdList) "
+                        + "AND CATEGORY_ID = :categoryId "
+                        // 1:闆嗛噰,2:鑷噰
+                        + "AND BUY_TYPE =1";
 
         List<GoodsTemplateVO> resultList = Lists.newArrayList();
         List<Map<String, Object>> goodsTemplateList = lWhGoodsService.select(sql, map, new MapperUtil());
@@ -294,82 +293,174 @@
         return ResponseValue.success("鏌ヨ鎴愬姛!", resultList);
     }
 
+    /**
+     * 瀵煎叆鐗╁搧鏁版嵁
+     * @param file
+     * @return
+     */
     @PostMapping("/import")
-    public ResponseValue upload(MultipartFile file) throws IOException {
+    public ResponseValue importData(MultipartFile file) {
         String originalFilename = file.getOriginalFilename();
-        if (!".xls".endsWith(originalFilename)) {
-            return ResponseValue.error("鏂囦欢鏍煎紡鏈夎!");
-        }
+//        if (!".xls".endsWith(originalFilename)) {
+//            return ResponseValue.error("鏂囦欢鏍煎紡鏈夎!");
+//        }
         FinSysTenantUser sysInfo = this.getSysInfo();
         if (sysInfo == null) {
             return ResponseValue.error("褰撳墠鐧诲綍鐢ㄦ埛涓虹┖");
         }
-        EasyExcelFactory.read(file.getInputStream(), ImportGoodsInfoTemplate.class,
-            new AnalysisEventListener<ImportGoodsInfoTemplate>() {
-                List<BaseGoodsTemplateParam> list = Lists.newArrayList();
+        try {
+            EasyExcelFactory.read(file.getInputStream(), ImportGoodsInfoTemplate.class,
+                    new AnalysisEventListener<ImportGoodsInfoTemplate>() {
+                        /**
+                         * 涓绘暟鎹�
+                         */
+                        final List<BaseGoodsTemplateParam> list = Lists.newArrayList();
 
-                @Override
-                public void invoke(ImportGoodsInfoTemplate data, AnalysisContext analysisContext) {
-                    String categoryOne = data.getCategoryOne();
-                    String categoryTwo = data.getCategoryTwo();
-                    String categoryThree = data.getCategoryThree();
-                    String goodsName = data.getGoodsName();
-                    String goodModelName = data.getGoodModelName();
-                    String unit = data.getUnit();
-                    String type = data.getType();
-                    String agencyName = data.getAgencyName();
+                        /**
+                         * 妯″潡鏁版嵁
+                         */
+                        final List<BaseGoodsModels> baseGoodsModelsArrayList = Lists.newArrayList();
 
-                    BaseCategory baseCategory = baseCategoryService.getByCategoryByName(categoryThree);
-                    Long categoryId = baseCategory.getId();
+                        /**
+                         * 璇诲彇鐨勮鍙�
+                         */
+                        Integer rowIndex = 0;
 
-                    Optional<BaseGoodsTemplateParam> optional =
-                        list.stream().filter(item -> item.getCategoryId().equals(categoryId)).findFirst();
+                        /**
+                         * 琛ㄥご淇℃伅
+                         * @param headMap
+                         * @param context
+                         */
+                        @Override
+                        public void invokeHeadMap(Map headMap, AnalysisContext context) {
+                            // 楠岃瘉琛ㄥご鏁伴噺
+                            logger.info("瑙f瀽鍒嗗彂鍗曠殑琛ㄥご闀垮害: {}", headMap.size());
+                            if (headMap.size() != 7) {
+                                throw new ExcelAnalysisException("涓婁紶鐨勬枃浠朵笉绗�!");
+                            }
+                        }
 
-                    if (optional.isPresent()) {
-                        BaseGoodsModels baseGoodsModels = new BaseGoodsModels();
-                        baseGoodsModels.setModelName(goodModelName);
-                        baseGoodsModels.setUnit(unit);
-                        optional.get().getModels().add(baseGoodsModels);
-                    } else {
-                        BaseGoodsTemplateParam baseGoodsTemplate = new BaseGoodsTemplateParam();
-                        baseGoodsTemplate.setCategoryId(categoryId);
-                        baseGoodsTemplate.setGoodsName(goodsName);
-                        baseGoodsTemplate.setStates(1);
-                        FinSysTenant finSysTenant = finSysTenantService.selectByName(agencyName);
-                        baseGoodsTemplate.setAgencyId(finSysTenant.getId());
-                        baseGoodsTemplate.setAgencyName(agencyName);
+                        /**
+                         * 鏁版嵁澶勭悊
+                         * @param data
+                         * @param analysisContext
+                         */
+                        @Override
+                        public void invoke(ImportGoodsInfoTemplate data, AnalysisContext analysisContext) {
+                            rowIndex++;
+                            String categoryOne = data.getCategoryOne();
+                            String categoryTwo = data.getCategoryTwo();
+                            String categoryThree = data.getCategoryThree();
+                            if (StrUtil.isEmpty(categoryThree)) {
+                                throw new ExcelAnalysisException("绗�" + rowIndex + "鏉℃暟鎹紝鍝佺被鍚嶇О涓嶈兘涓虹┖锛�" + categoryThree);
+                            }
+                            String goodsName = data.getGoodsName();
+                            if (StrUtil.isEmpty(goodsName)) {
+                                throw new ExcelAnalysisException("绗�" + rowIndex + "鏉℃暟鎹紝鍝佸悕涓嶈兘涓虹┖锛�" + goodsName);
+                            }
+                            String goodModelName = data.getGoodModelName();
+                            if (StrUtil.isEmpty(goodModelName)) {
+                                throw new ExcelAnalysisException("绗�" + rowIndex + "鏉℃暟鎹紝瑙勬牸鍨嬪彿涓嶈兘涓虹┖锛�" + goodModelName);
+                            }
+                            String unit = data.getUnit();
+                            if (StrUtil.isEmpty(unit)) {
+                                throw new ExcelAnalysisException("绗�" + rowIndex + "鏉℃暟鎹紝鍗曚綅涓嶈兘涓虹┖锛�" + unit);
+                            }
+                            //String type = data.getType();
+                            String agencyName = data.getAgencyName();
+                            if (StrUtil.isEmpty(agencyName)) {
+                                throw new ExcelAnalysisException("绗�" + rowIndex + "鏉℃暟鎹紝鍒涘缓鏈烘瀯涓嶈兘涓虹┖锛�" + agencyName);
+                            }
+                            BaseCategory baseCategory = baseCategoryService.getByCategoryByName(categoryThree);
+                            if (baseCategory == null) {
+                                throw new ExcelAnalysisException("绗�" + rowIndex + "鏉℃暟鎹紝鏈煡璇㈠埌姝ゅ搧绫诲悕绉帮細" + categoryThree);
+                            }
+                            Long categoryId = baseCategory.getId();
+                            // 鍒ゆ柇鍚屼竴鍒嗙被涓嬬殑鐗╁搧鍚嶇О鏄惁閲嶅
+                            BaseGoodsTemplate goodsTemplate = baseGoodsTemplateService.getByGoodsNameAndCategoryId(goodsName, categoryId);
+                            if (goodsTemplate != null) {
+                                // 鏌ョ湅姝ゆ壒娆″拰鏁伴噺
+                                BaseGoodsModels baseGoodsModelsAdd = new BaseGoodsModels();
+                                baseGoodsModelsAdd.setGoodsTemplatesId(goodsTemplate.getId());
+                                baseGoodsModelsAdd.setStates(1);
+                                baseGoodsModelsAdd.setModelName(goodModelName);
+                                baseGoodsModelsAdd.setUnit(unit);
+                                // 鍒ゆ柇鍚屼竴鐗╁搧妯℃澘id涓嬬殑 瑙勬牸鍨嬪彿鍚嶇О鏄惁閲嶅
+                                BaseGoodsModels baseGoodsModels = baseGoodsModelsService.getByModelNameAndGoodsTemplatesId(baseGoodsModelsAdd);
+                                if (baseGoodsModels == null) {
+                                    baseGoodsModelsArrayList.add(baseGoodsModelsAdd);
+                                    return;
+                                } else {
+                                    throw new ExcelAnalysisException("绗�" + rowIndex + "鏉℃暟鎹�" + "鐗╁搧鍚嶇О锛�" + goodsName + "宸插瓨鍦紝瑙勬牸锛�" + goodModelName + "锛屽崟浣嶏細" + unit);
+                                }
+                            }
+                            Optional<BaseGoodsTemplateParam> optional =
+                                    list.stream().filter(item -> item.getCategoryId().equals(categoryId)).findFirst();
+                            Optional<BaseGoodsTemplateParam> optional2 =
+                                    list.stream().filter(item -> item.getGoodsName().equals(goodsName)).findFirst();
+                            if (optional.isPresent() && optional2.isPresent()) {
+                                BaseGoodsModels baseGoodsModels = new BaseGoodsModels();
+                                baseGoodsModels.setModelName(goodModelName);
+                                baseGoodsModels.setUnit(unit);
+                                optional.get().getModels().add(baseGoodsModels);
+                            } else {
+                                BaseGoodsTemplateParam baseGoodsTemplate = new BaseGoodsTemplateParam();
+                                baseGoodsTemplate.setCategoryId(categoryId);
+                                baseGoodsTemplate.setGoodsName(goodsName);
+                                baseGoodsTemplate.setStates(1);
+                                FinSysTenant finSysTenant = finSysTenantService.selectByName(agencyName);
+                                if (finSysTenant == null) {
+                                    throw new ExcelAnalysisException("绗�" + rowIndex + "鏉℃暟鎹紝鏈煡璇㈠埌姝ゆ満鏋勶細" + agencyName);
+                                }
+                                baseGoodsTemplate.setAgencyId(finSysTenant.getId());
+                                baseGoodsTemplate.setAgencyName(agencyName);
+                                List<BaseGoodsModels> models = new ArrayList<>();
+                                BaseGoodsModels baseGoodsModels = new BaseGoodsModels();
+                                baseGoodsModels.setModelName(goodModelName);
+                                baseGoodsModels.setUnit(unit);
+                                models.add(baseGoodsModels);
+                                baseGoodsTemplate.setModels(models);
+                                list.add(baseGoodsTemplate);
+                            }
+                        }
 
-                        List<BaseGoodsModels> models = new ArrayList<>();
-                        BaseGoodsModels baseGoodsModels = new BaseGoodsModels();
-                        baseGoodsModels.setModelName(goodModelName);
-                        baseGoodsModels.setUnit(unit);
-                        models.add(baseGoodsModels);
-                        baseGoodsTemplate.setModels(models);
+                        /**
+                         * 澶勭悊鍚庡鍏ユ暟鎹�
+                         * @param analysisContext
+                         */
+                        @Override
+                        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
+                            // 涓绘暟鎹瓨鍌�
+                            for (BaseGoodsTemplateParam baseGoodsTemplate : list) {
+                                baseGoodsTemplateService.add(baseGoodsTemplate, null);
+                            }
+                            // 妯″潡鏁版嵁瀛樺偍
+                            for (BaseGoodsModels baseGoodsModels : baseGoodsModelsArrayList) {
+                                baseGoodsModelsService.add(baseGoodsModels);
+                            }
+                        }
 
-                        list.add(baseGoodsTemplate);
-                    }
-                }
-
-                @Override
-                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
-                    for (BaseGoodsTemplateParam baseGoodsTemplate : list) {
-                        baseGoodsTemplateService.add(baseGoodsTemplate, null);
-                    }
-                }
-
-                @Override
-                public void onException(Exception exception, AnalysisContext analysisContext) throws Exception {
-                    if (exception instanceof ExcelDataConvertException) {
-                        ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException)exception;
-                        Integer row = excelDataConvertException.getRowIndex() + 1;
-                        Integer column = excelDataConvertException.getColumnIndex() + 1;
-                        throw new RuntimeException("绗�" + row + "琛岋紝绗�" + column + "鍒楄В鏋愬紓甯革紝璇锋纭~鍐�");
-                    } else {
-                        throw new RuntimeException(exception.getMessage());
-                    }
-                }
-            }).sheet(0).doRead();
-
+                        @Override
+                        public void onException(Exception exception, AnalysisContext analysisContext) throws Exception {
+                            if (exception instanceof ExcelDataConvertException) {
+                                ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
+                                Integer row = excelDataConvertException.getRowIndex() + 1;
+                                Integer column = excelDataConvertException.getColumnIndex() + 1;
+                                throw new ExcelAnalysisException("绗�" + row + "琛岋紝绗�" + column + "鍒楄В鏋愬紓甯革紝璇锋纭~鍐�");
+                            } else {
+                                throw new ExcelAnalysisException(exception.getMessage());
+                            }
+                        }
+                    }).sheet(0).doRead();
+        } catch (ExcelAnalysisException e) {
+            return ResponseValue.error(e.getMessage());
+        } catch (RuntimeException e) {
+            e.printStackTrace();
+            return ResponseValue.error("绯荤粺閿欒");
+        }catch (Exception e) {
+            e.printStackTrace();
+            return ResponseValue.error("绯荤粺閿欒");
+        }
         return ResponseValue.success("瀵煎叆鎴愬姛!");
     }
 }

--
Gitblit v1.9.1