From 5524cfff956b81fcd1ebeb40ac57924a38be42d8 Mon Sep 17 00:00:00 2001 From: WangHan <wwh_work@126,com> Date: 星期四, 19 十二月 2024 15:02:17 +0800 Subject: [PATCH] 问题修改 --- consum-base/src/main/java/com/consum/base/controller/BaseGoodsTemplateController.java | 270 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 242 insertions(+), 28 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 ff4f99a..dc5f66b 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,9 +1,15 @@ 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; @@ -12,19 +18,24 @@ 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.BaseGoodsTemplateService; -import com.consum.base.service.BaseWarehouseService; -import com.consum.base.service.LWhGoodsService; +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; @@ -32,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; @@ -41,6 +53,7 @@ * @Author 鍗㈠簡闃� * @Date 2023/10/24 */ +@Api(value = "鐗╁搧妯℃澘", tags = "鐗╁搧妯℃澘") @RestController @RequestMapping("/pc/base/goods/template") public class BaseGoodsTemplateController extends BaseController { @@ -53,6 +66,15 @@ @Autowired private BaseWarehouseService baseWarehouseService; + + @Autowired + private FinSysTenantService finSysTenantService; + + @Autowired + private BaseCategoryService baseCategoryService; + + @Autowired + private BaseGoodsModelsService baseGoodsModelsService; /** * @Description 鏂板鐗╁搧妯℃澘 @@ -76,9 +98,18 @@ if (StringUtils.isEmpty(param.getGoodsName())) { return ResponseValue.error("鐗╁搧鍚嶇О涓虹┖"); } + BaseCategory baseCategory = new BaseCategory(); + baseCategory.setId(param2.getCategoryId()); + BaseCategory baseCategory1 = this.baseCategoryService.get(baseCategory); + if (baseCategory1 == null) { + return ResponseValue.error("鍒嗙被涓嶅瓨鍦�"); + } + if (baseCategory1.getLevels() != 3) { + return ResponseValue.error("鍒嗙被涓嶆纭�"); + } // 鍒ゆ柇鍚屼竴鍒嗙被涓嬬殑鐗╁搧鍚嶇О鏄惁閲嶅 BaseGoodsTemplate goodsTemplate = - this.baseGoodsTemplateService.getByGoodsNameAndCategoryId(param.getGoodsName(), param.getCategoryId()); + this.baseGoodsTemplateService.getByGoodsNameAndCategoryId(param.getGoodsName(), param.getCategoryId()); if (goodsTemplate != null) { return ResponseValue.error("鐗╁搧鍚嶇О宸插瓨鍦�"); } @@ -148,7 +179,15 @@ if (CollectionUtils.isEmpty(models)) { return ResponseValue.error("鐗╁搧鍨嬪彿涓虹┖"); } - + BaseCategory baseCategory = new BaseCategory(); + baseCategory.setId(param2.getCategoryId()); + BaseCategory baseCategory1 = this.baseCategoryService.get(baseCategory); + if (baseCategory1 == null) { + return ResponseValue.error("鍒嗙被涓嶅瓨鍦�"); + } + if (baseCategory1.getLevels() != 3) { + return ResponseValue.error("鍒嗙被涓嶆纭�"); + } int num = this.baseGoodsTemplateService.updateBaseGoodsTemplate(param); return num > 0 ? ResponseValue.success(1) : ResponseValue.error("缂栬緫澶辫触锛�"); } @@ -158,8 +197,10 @@ * * @author 鍗㈠簡闃� * @date 2023/10/25 + * <p> + * 鐗╁搧鐨勭鐢ㄦ垨鍒犻櫎锛屼笉褰卞搷宸茬粡閲囪喘鍏ヨ繃搴撶殑鐗╁搧淇℃伅銆� + * <p> */ - // TODO 鐗╁搧鐨勭鐢ㄦ垨鍒犻櫎锛屼笉褰卞搷宸茬粡閲囪喘鍏ヨ繃搴撶殑鐗╁搧淇℃伅銆� @PostMapping("/updStatus") public ResponseValue updateStatus() { BaseGoodsTemplate goodsTemplate = CommonUtil.getObjFromReqBody(BaseGoodsTemplate.class); @@ -167,7 +208,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("鍙傛暟閿欒"); } @@ -179,8 +220,10 @@ * @Description 鏍规嵁鐗╁搧id鍒犻櫎鐗╁搧 * @Author 鍗㈠簡闃� * @Date 2023/10/25 + * <p> + * 鐗╁搧鐨勭鐢ㄦ垨鍒犻櫎锛屼笉褰卞搷宸茬粡閲囪喘鍏ヨ繃搴撶殑鐗╁搧淇℃伅銆� + * <p/> */ - // TODO 鐗╁搧鐨勭鐢ㄦ垨鍒犻櫎锛屼笉褰卞搷宸茬粡閲囪喘鍏ヨ繃搴撶殑鐗╁搧淇℃伅銆� @DeleteMapping("/del") public ResponseValue updateById() { BaseGoodsTemplate goodsTemplate = CommonUtil.getObjFromReqBody(BaseGoodsTemplate.class); @@ -220,42 +263,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()); @@ -266,4 +310,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