| | |
| | | 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.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.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; |
| | |
| | | |
| | | @Autowired |
| | | private BaseCategoryService baseCategoryService; |
| | | |
| | | @Autowired |
| | | private BaseGoodsModelsService baseGoodsModelsService; |
| | | |
| | | /** |
| | | * @Description 新增物品模板 |
| | |
| | | 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("当前登录用户为空"); |
| | | } |
| | | try { |
| | | EasyExcelFactory.read(file.getInputStream(), ImportGoodsInfoTemplate.class, |
| | | new AnalysisEventListener<ImportGoodsInfoTemplate>() { |
| | | List<BaseGoodsTemplateParam> list = Lists.newArrayList(); |
| | | /** |
| | | * 主数据 |
| | | */ |
| | | 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("解析分发单的表头长度: {}", 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(); |
| | | String type = data.getType(); |
| | | 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(); |
| | | |
| | | if (optional.isPresent()) { |
| | | 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); |
| | |
| | | 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); |
| | | } |
| | | } |
| | | |
| | |
| | | ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException)exception; |
| | | Integer row = excelDataConvertException.getRowIndex() + 1; |
| | | Integer column = excelDataConvertException.getColumnIndex() + 1; |
| | | throw new RuntimeException("第" + row + "行,第" + column + "列解析异常,请正确填写"); |
| | | throw new ExcelAnalysisException("第" + row + "行,第" + column + "列解析异常,请正确填写"); |
| | | } else { |
| | | throw new RuntimeException(exception.getMessage()); |
| | | 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("导入成功!"); |
| | | } |
| | | } |