From fae8a9349499cd045bc73e8fd22aeae52618a46c Mon Sep 17 00:00:00 2001
From: WangHan <wwh_work@126,com>
Date: 星期四, 19 十二月 2024 10:02:02 +0800
Subject: [PATCH] 采购明细查询修改,查询本级及以下

---
 consum-base/src/main/java/com/consum/base/controller/BaseGoodsTemplateController.java |  290 +++++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 195 insertions(+), 95 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 7a5e781..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
@@ -6,7 +6,10 @@
 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;
@@ -16,23 +19,18 @@
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
-
-import com.alibaba.excel.EasyExcel;
+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.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;
@@ -45,6 +43,7 @@
 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;
@@ -54,6 +53,7 @@
  * @Author 鍗㈠簡闃�
  * @Date 2023/10/24
  */
+@Api(value = "鐗╁搧妯℃澘", tags = "鐗╁搧妯℃澘")
 @RestController
 @RequestMapping("/pc/base/goods/template")
 public class BaseGoodsTemplateController extends BaseController {
@@ -72,6 +72,9 @@
 
     @Autowired
     private BaseCategoryService baseCategoryService;
+
+    @Autowired
+    private BaseGoodsModelsService baseGoodsModelsService;
 
     /**
      * @Description 鏂板鐗╁搧妯℃澘
@@ -97,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("鐗╁搧鍚嶇О宸插瓨鍦�");
         }
@@ -177,8 +180,10 @@
      *
      * @author 鍗㈠簡闃�
      * @date 2023/10/25
+     * <p>
+     * 鐗╁搧鐨勭鐢ㄦ垨鍒犻櫎锛屼笉褰卞搷宸茬粡閲囪喘鍏ヨ繃搴撶殑鐗╁搧淇℃伅銆�
+     * <p>
      */
-    // TODO 鐗╁搧鐨勭鐢ㄦ垨鍒犻櫎锛屼笉褰卞搷宸茬粡閲囪喘鍏ヨ繃搴撶殑鐗╁搧淇℃伅銆�
     @PostMapping("/updStatus")
     public ResponseValue updateStatus() {
         BaseGoodsTemplate goodsTemplate = CommonUtil.getObjFromReqBody(BaseGoodsTemplate.class);
@@ -186,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("鍙傛暟閿欒");
         }
 
@@ -198,8 +203,10 @@
      * @Description 鏍规嵁鐗╁搧id鍒犻櫎鐗╁搧
      * @Author 鍗㈠簡闃�
      * @Date 2023/10/25
+     * <p>
+     * 鐗╁搧鐨勭鐢ㄦ垨鍒犻櫎锛屼笉褰卞搷宸茬粡閲囪喘鍏ヨ繃搴撶殑鐗╁搧淇℃伅銆�
+     * <p/>
      */
-    // TODO 鐗╁搧鐨勭鐢ㄦ垨鍒犻櫎锛屼笉褰卞搷宸茬粡閲囪喘鍏ヨ繃搴撶殑鐗╁搧淇℃伅銆�
     @DeleteMapping("/del")
     public ResponseValue updateById() {
         BaseGoodsTemplate goodsTemplate = CommonUtil.getObjFromReqBody(BaseGoodsTemplate.class);
@@ -239,42 +246,43 @@
 
     @GetMapping("/query/goodsTemplate")
     public ResponseValue queryGoodsTemplateByCategoryId(Long agencyId, Long categoryId) {
-        // TODO 涓嶉檺鍒舵満鏋�
-        List<BaseGoodsTemplate> list =
-            this.baseGoodsTemplateService.queryGoodsTemplateByCategoryId(null, 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 "
-                // TODO 涓存椂瑙e喅
-                /*+ "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());
@@ -285,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 (!originalFilename.endsWith(".xls")) {
-            return ResponseValue.error("鏂囦欢鏍煎紡鏈夎!");
-        }
+//        if (!".xls".endsWith(originalFilename)) {
+//            return ResponseValue.error("鏂囦欢鏍煎紡鏈夎!");
+//        }
         FinSysTenantUser sysInfo = this.getSysInfo();
         if (sysInfo == null) {
             return ResponseValue.error("褰撳墠鐧诲綍鐢ㄦ埛涓虹┖");
         }
-        EasyExcel.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();
-
-        return ResponseValue.success("瀵煎叆鎴愬姛!", 1);
+                        @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