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 |  280 ++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 244 insertions(+), 36 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 f4ede5c..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
@@ -1,48 +1,80 @@
 package com.consum.base.controller;
 
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+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;
+import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+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;
+import com.alibaba.excel.exception.ExcelDataConvertException;
 import com.consum.base.BaseController;
+import com.consum.base.core.type.StatesType;
 import com.consum.base.core.utils.CommonUtil;
 import com.consum.base.core.utils.MapUtils;
 import com.consum.base.core.utils.MapperUtil;
 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.BaseGoodsTemplateServiceImpl;
-import com.consum.base.service.BaseWarehouseServiceImpl;
-import com.consum.base.service.LWhGoodsService;
-import com.consum.model.po.*;
+import com.consum.model.po.BaseCategory;
+import com.consum.model.po.BaseGoodsModels;
+import com.consum.model.po.BaseGoodsTemplate;
+import com.consum.model.po.BaseWarehouse;
+import com.consum.model.po.FinSysTenant;
+import com.consum.model.po.FinSysTenantUser;
+import com.consum.model.po.SDictData;
 import com.consum.model.vo.BaseGoodsTemplateVo;
 import com.walker.db.page.GenericPager;
 import com.walker.infrastructure.utils.StringUtils;
 import com.walker.web.ResponseValue;
+
+import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiImplicitParam;
 import io.swagger.annotations.ApiImplicitParams;
 import io.swagger.annotations.ApiOperation;
-import org.apache.commons.compress.utils.Lists;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.util.CollectionUtils;
-import org.springframework.web.bind.annotation.*;
-
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
 
 /**
  * @Description 鐗╁搧妯℃澘
  * @Author 鍗㈠簡闃�
  * @Date 2023/10/24
  */
+@Api(value = "鐗╁搧妯℃澘", tags = "鐗╁搧妯℃澘")
 @RestController
 @RequestMapping("/pc/base/goods/template")
 public class BaseGoodsTemplateController extends BaseController {
 
     @Autowired
-    private BaseGoodsTemplateServiceImpl baseGoodsTemplateService;
+    private BaseGoodsTemplateService baseGoodsTemplateService;
 
     @Autowired
     private LWhGoodsService lWhGoodsService;
 
     @Autowired
-    private BaseWarehouseServiceImpl baseWarehouseService;
+    private BaseWarehouseService baseWarehouseService;
+
+    @Autowired
+    private FinSysTenantService finSysTenantService;
+
+    @Autowired
+    private BaseCategoryService baseCategoryService;
+
+    @Autowired
+    private BaseGoodsModelsService baseGoodsModelsService;
 
     /**
      * @Description 鏂板鐗╁搧妯℃澘
@@ -66,8 +98,9 @@
         if (StringUtils.isEmpty(param.getGoodsName())) {
             return ResponseValue.error("鐗╁搧鍚嶇О涓虹┖");
         }
-        //鍒ゆ柇鍚屼竴鍒嗙被涓嬬殑鐗╁搧鍚嶇О鏄惁閲嶅
-        BaseGoodsTemplate goodsTemplate = this.baseGoodsTemplateService.getByGoodsNameAndCategoryId(param.getGoodsName(), param.getCategoryId());
+        // 鍒ゆ柇鍚屼竴鍒嗙被涓嬬殑鐗╁搧鍚嶇О鏄惁閲嶅
+        BaseGoodsTemplate goodsTemplate =
+                this.baseGoodsTemplateService.getByGoodsNameAndCategoryId(param.getGoodsName(), param.getCategoryId());
         if (goodsTemplate != null) {
             return ResponseValue.error("鐗╁搧鍚嶇О宸插瓨鍦�");
         }
@@ -147,8 +180,10 @@
      *
      * @author 鍗㈠簡闃�
      * @date 2023/10/25
+     * <p>
+     * 鐗╁搧鐨勭鐢ㄦ垨鍒犻櫎锛屼笉褰卞搷宸茬粡閲囪喘鍏ヨ繃搴撶殑鐗╁搧淇℃伅銆�
+     * <p>
      */
-    //TODO 鐗╁搧鐨勭鐢ㄦ垨鍒犻櫎锛屼笉褰卞搷宸茬粡閲囪喘鍏ヨ繃搴撶殑鐗╁搧淇℃伅銆�
     @PostMapping("/updStatus")
     public ResponseValue updateStatus() {
         BaseGoodsTemplate goodsTemplate = CommonUtil.getObjFromReqBody(BaseGoodsTemplate.class);
@@ -156,7 +191,7 @@
         CommonUtil.copyProperties(goodsTemplate, param2);
         goodsTemplate = param2;
 
-        if (goodsTemplate == null || goodsTemplate.getId() == null || goodsTemplate.getStates() == null) {
+        if (goodsTemplate.getId() == null || goodsTemplate.getStates() == null) {
             return ResponseValue.error("鍙傛暟閿欒");
         }
 
@@ -168,8 +203,10 @@
      * @Description 鏍规嵁鐗╁搧id鍒犻櫎鐗╁搧
      * @Author 鍗㈠簡闃�
      * @Date 2023/10/25
+     * <p>
+     * 鐗╁搧鐨勭鐢ㄦ垨鍒犻櫎锛屼笉褰卞搷宸茬粡閲囪喘鍏ヨ繃搴撶殑鐗╁搧淇℃伅銆�
+     * <p/>
      */
-    //TODO 鐗╁搧鐨勭鐢ㄦ垨鍒犻櫎锛屼笉褰卞搷宸茬粡閲囪喘鍏ヨ繃搴撶殑鐗╁搧淇℃伅銆�
     @DeleteMapping("/del")
     public ResponseValue updateById() {
         BaseGoodsTemplate goodsTemplate = CommonUtil.getObjFromReqBody(BaseGoodsTemplate.class);
@@ -209,40 +246,43 @@
 
     @GetMapping("/query/goodsTemplate")
     public ResponseValue queryGoodsTemplateByCategoryId(Long agencyId, Long categoryId) {
-        List<BaseGoodsTemplate> list = this.baseGoodsTemplateService.queryGoodsTemplateByCategoryId(agencyId, categoryId, null, null);
+        // 涓嶉檺鍒舵満鏋�
+        List<BaseGoodsTemplate> list = baseGoodsTemplateService.queryGoodsTemplateByCategoryId(null, categoryId,
+                StatesType.NORMAL.getValue(), null);
         if (list == null) {
             return ResponseValue.error("鏌ヨ澶辫触锛�");
         }
         return ResponseValue.success("鏌ヨ鎴愬姛!", list);
     }
 
-
-    @ApiOperation(value = "鏌ヨ鏈烘瀯涓嬮粯璁や粨搴撲笅鐨勫垎绫绘ā鏉夸俊鎭�", notes = "鏌ヨ鏈烘瀯涓嬮粯璁や粨搴撲笅鐨勫垎绫绘ā鏉夸俊鎭�")
+    @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) {
 
-        // TODO 璋冩嫧鏌ヨ浠撳簱鐗╁搧
+        // 璋冩嫧鏌ヨ鎵�鏈変粨搴撶墿鍝佹ā鏉夸俊鎭�
         Map<String, Object> map = new HashMap<>();
         map.put("categoryId", categoryId);
 
-        List<BaseWarehouse> baseWarehouseList = baseWarehouseService.getByAgencyId(agencyId, (short) 1, (short) 1);
+        List<BaseWarehouse> baseWarehouseList =
+                baseWarehouseService.getBaseWareHouseList(agencyId, StatesType.NORMAL.getValue());
         if (CollectionUtils.isEmpty(baseWarehouseList)) {
-            return ResponseValue.error("鏈烘瀯鏃犻粯璁や粨搴擄紒");
+            return ResponseValue.error("鏈烘瀯鏃犱粨搴擄紒");
 
         }
-        map.put("warehouseId", baseWarehouseList.get(0).getId());
+        List<Long> warehouseIdList = baseWarehouseList.stream().map(BaseWarehouse::getId).collect(Collectors.toList());
+        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 = :warehouseId "
-                + "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());
@@ -253,6 +293,174 @@
         return ResponseValue.success("鏌ヨ鎴愬姛!", resultList);
     }
 
+    /**
+     * 瀵煎叆鐗╁搧鏁版嵁
+     * @param file
+     * @return
+     */
+    @PostMapping("/import")
+    public ResponseValue importData(MultipartFile file) {
+        String originalFilename = file.getOriginalFilename();
+//        if (!".xls".endsWith(originalFilename)) {
+//            return ResponseValue.error("鏂囦欢鏍煎紡鏈夎!");
+//        }
+        FinSysTenantUser sysInfo = this.getSysInfo();
+        if (sysInfo == null) {
+            return ResponseValue.error("褰撳墠鐧诲綍鐢ㄦ埛涓虹┖");
+        }
+        try {
+            EasyExcelFactory.read(file.getInputStream(), ImportGoodsInfoTemplate.class,
+                    new AnalysisEventListener<ImportGoodsInfoTemplate>() {
+                        /**
+                         * 涓绘暟鎹�
+                         */
+                        final List<BaseGoodsTemplateParam> list = Lists.newArrayList();
 
+                        /**
+                         * 妯″潡鏁版嵁
+                         */
+                        final List<BaseGoodsModels> baseGoodsModelsArrayList = Lists.newArrayList();
+
+                        /**
+                         * 璇诲彇鐨勮鍙�
+                         */
+                        Integer rowIndex = 0;
+
+                        /**
+                         * 琛ㄥご淇℃伅
+                         * @param headMap
+                         * @param context
+                         */
+                        @Override
+                        public void invokeHeadMap(Map headMap, AnalysisContext context) {
+                            // 楠岃瘉琛ㄥご鏁伴噺
+                            logger.info("瑙f瀽鍒嗗彂鍗曠殑琛ㄥご闀垮害: {}", headMap.size());
+                            if (headMap.size() != 7) {
+                                throw new ExcelAnalysisException("涓婁紶鐨勬枃浠朵笉绗�!");
+                            }
+                        }
+
+                        /**
+                         * 鏁版嵁澶勭悊
+                         * @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);
+                            }
+                        }
+
+                        /**
+                         * 澶勭悊鍚庡鍏ユ暟鎹�
+                         * @param analysisContext
+                         */
+                        @Override
+                        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
+                            // 涓绘暟鎹瓨鍌�
+                            for (BaseGoodsTemplateParam baseGoodsTemplate : list) {
+                                baseGoodsTemplateService.add(baseGoodsTemplate, null);
+                            }
+                            // 妯″潡鏁版嵁瀛樺偍
+                            for (BaseGoodsModels baseGoodsModels : baseGoodsModelsArrayList) {
+                                baseGoodsModelsService.add(baseGoodsModels);
+                            }
+                        }
+
+                        @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