From db5da08375951bffe22dbd28d01073366f4888a8 Mon Sep 17 00:00:00 2001 From: "pref_mail@163.com" Date: Fri, 19 Apr 2024 11:19:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=A2=9E=E5=8A=A0=E4=B8=89=E6=96=B9?= =?UTF-8?q?=E7=89=A9=E6=96=99=E7=9A=84=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BasicdataFactoryCategoryEntity.java | 13 ++ .../BasicdataCategoryController.java | 2 +- .../BasicdataFactoryCategoryController.java | 87 ++++++++++---- .../excel/BasicdataCategoryExcel.java | 4 +- .../excel/BasicdataFactoryCategoryExcel.java | 65 ++++++++++ .../BasicdataFactoryCategoryImporter.java | 112 ++++++++++++++++++ 6 files changed, 254 insertions(+), 29 deletions(-) create mode 100644 blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/excel/BasicdataFactoryCategoryExcel.java create mode 100644 blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/excel/BasicdataFactoryCategoryImporter.java diff --git a/blade-service-api/logpm-basicdata-api/src/main/java/com/logpm/basicdata/entity/BasicdataFactoryCategoryEntity.java b/blade-service-api/logpm-basicdata-api/src/main/java/com/logpm/basicdata/entity/BasicdataFactoryCategoryEntity.java index 8731594c6..db4de47b3 100644 --- a/blade-service-api/logpm-basicdata-api/src/main/java/com/logpm/basicdata/entity/BasicdataFactoryCategoryEntity.java +++ b/blade-service-api/logpm-basicdata-api/src/main/java/com/logpm/basicdata/entity/BasicdataFactoryCategoryEntity.java @@ -40,12 +40,25 @@ public class BasicdataFactoryCategoryEntity extends TenantEntity { private String reserve5; + @ApiModelProperty(value = "结算品类id") private Long categoryId; + + @ApiModelProperty(value = "结算品类名称") private String category; + + @ApiModelProperty(value = "一级品") private String firsts; + + @ApiModelProperty(value = "二级品") private String seconds; + + @ApiModelProperty(value = "三级品") private String thirds; + + @ApiModelProperty(value = "品牌名称") private String brand; + + @ApiModelProperty(value = "品牌id") private Long brandId; diff --git a/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/controller/BasicdataCategoryController.java b/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/controller/BasicdataCategoryController.java index d8a5fe187..b536cd638 100644 --- a/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/controller/BasicdataCategoryController.java +++ b/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/controller/BasicdataCategoryController.java @@ -177,7 +177,7 @@ public class BasicdataCategoryController extends BladeController { ExcelUtil.export(response, "品类数据" + DateUtil.time(), "品类数据表", list, BasicdataCategoryExcel.class); } /** - * 导出数据 + * 下载模板 */ @GetMapping("/downTemplate") @ApiOperationSupport(order = 9) diff --git a/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/controller/BasicdataFactoryCategoryController.java b/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/controller/BasicdataFactoryCategoryController.java index 8f9f5e2bf..8d6760c99 100644 --- a/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/controller/BasicdataFactoryCategoryController.java +++ b/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/controller/BasicdataFactoryCategoryController.java @@ -4,6 +4,12 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; import com.logpm.basicdata.dto.BasicdataFactoryCategoryDTO; import com.logpm.basicdata.entity.BasicdataFactoryCategoryEntity; +import com.logpm.basicdata.excel.BasicdataCategoryExcel; +import com.logpm.basicdata.excel.BasicdataCategoryImporter; +import com.logpm.basicdata.excel.BasicdataFactoryCategoryExcel; +import com.logpm.basicdata.excel.BasicdataFactoryCategoryImporter; +import com.logpm.basicdata.service.IBasicdataBrandService; +import com.logpm.basicdata.service.IBasicdataCategoryService; import com.logpm.basicdata.service.IBasicdataFactoryCategoryService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -11,8 +17,15 @@ import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springblade.common.exception.CustomerException; import org.springblade.core.boot.ctrl.BladeController; +import org.springblade.core.excel.util.ExcelUtil; import org.springblade.core.tool.api.R; +import org.springblade.core.tool.utils.DateUtil; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletResponse; +import java.util.ArrayList; +import java.util.List; @Slf4j @RestController @@ -22,8 +35,8 @@ import org.springframework.web.bind.annotation.*; public class BasicdataFactoryCategoryController extends BladeController { private final IBasicdataFactoryCategoryService basicdataFactoryCategoryService; - - + private final IBasicdataBrandService basicdataBrandService; + private final IBasicdataCategoryService basicdataCategoryService; @ResponseBody @PostMapping("/pageList") @@ -31,17 +44,17 @@ public class BasicdataFactoryCategoryController extends BladeController { @ApiOperation(value = "工厂物料列表", notes = "传入basicdataFactoryCategoryDTO") public R pageList(@RequestBody BasicdataFactoryCategoryDTO basicdataFactoryCategoryDTO) { String method = "############pageList: "; - log.info(method+"请求参数{}",basicdataFactoryCategoryDTO); - try{ + log.info(method + "请求参数{}", basicdataFactoryCategoryDTO); + try { IPage pages = basicdataFactoryCategoryService.pageList(basicdataFactoryCategoryDTO); return R.data(pages); - }catch (CustomerException e){ - log.error(e.message,e); - return R.fail(e.code,e.message); - }catch (Exception e){ - log.error(method+"系统异常",e); - return R.fail(500,method+"系统异常"); + } catch (CustomerException e) { + log.error(e.message, e); + return R.fail(e.code, e.message); + } catch (Exception e) { + log.error(method + "系统异常", e); + return R.fail(500, method + "系统异常"); } } @@ -52,15 +65,15 @@ public class BasicdataFactoryCategoryController extends BladeController { @ApiOperation(value = "新增工厂物料", notes = "传入basicdataFactoryCategoryDTO") public R save(@RequestBody BasicdataFactoryCategoryDTO basicdataFactoryCategoryDTO) { String method = "############save: "; - log.info(method+"请求参数{}",basicdataFactoryCategoryDTO); - try{ + log.info(method + "请求参数{}", basicdataFactoryCategoryDTO); + try { return basicdataFactoryCategoryService.saveEntity(basicdataFactoryCategoryDTO); - }catch (CustomerException e){ - log.error(e.message,e); - return R.fail(e.code,e.message); - }catch (Exception e){ - log.error(method+"系统异常",e); - return R.fail(500,method+"系统异常"); + } catch (CustomerException e) { + log.error(e.message, e); + return R.fail(e.code, e.message); + } catch (Exception e) { + log.error(method + "系统异常", e); + return R.fail(500, method + "系统异常"); } } @@ -70,20 +83,42 @@ public class BasicdataFactoryCategoryController extends BladeController { @ApiOperation(value = "移除工厂物料", notes = "传入basicdataFactoryCategoryDTO") public R remove(@RequestBody BasicdataFactoryCategoryDTO basicdataFactoryCategoryDTO) { String method = "############remove: "; - log.info(method+"请求参数{}",basicdataFactoryCategoryDTO); + log.info(method + "请求参数{}", basicdataFactoryCategoryDTO); Long factoryCategoryId = basicdataFactoryCategoryDTO.getFactoryCategoryId(); - try{ + try { return R.status(basicdataFactoryCategoryService.removeById(factoryCategoryId)); - }catch (CustomerException e){ - log.error(e.message,e); - return R.fail(e.code,e.message); - }catch (Exception e){ - log.error(method+"系统异常",e); - return R.fail(500,method+"系统异常"); + } catch (CustomerException e) { + log.error(e.message, e); + return R.fail(e.code, e.message); + } catch (Exception e) { + log.error(method + "系统异常", e); + return R.fail(500, method + "系统异常"); } } + /** + * 下载模板 + */ + @GetMapping("/downTemplate") + @ApiOperationSupport(order = 9) + @ApiOperation(value = "下载导出模板", notes = "下载导出模板") + public void downTemplate(HttpServletResponse response) { + + List list = new ArrayList<>(); + ExcelUtil.export(response, "工厂品类数据" + DateUtil.time(), "工厂品类数据", list, BasicdataFactoryCategoryExcel.class); + } + + + @PostMapping("/import") + @ApiOperationSupport(order = 12) + @ApiOperation(value = "导入品类", notes = "传入excel") + public R importCategory(MultipartFile file) { + BasicdataFactoryCategoryImporter basicdataFactoryCategoryImporter = new BasicdataFactoryCategoryImporter(basicdataFactoryCategoryService,basicdataBrandService,basicdataCategoryService); + ExcelUtil.save(file, basicdataFactoryCategoryImporter, BasicdataFactoryCategoryExcel.class); + return R.success("操作成功"); + } + } diff --git a/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/excel/BasicdataCategoryExcel.java b/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/excel/BasicdataCategoryExcel.java index bc04240ca..0d987f568 100644 --- a/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/excel/BasicdataCategoryExcel.java +++ b/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/excel/BasicdataCategoryExcel.java @@ -29,8 +29,8 @@ import java.io.Serializable; /** * 品类 Excel实体类 * - * @author cyz - * @since 2023-06-02 + * @author chao + * @since 2024-04-19 */ @Data @ColumnWidth(25) diff --git a/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/excel/BasicdataFactoryCategoryExcel.java b/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/excel/BasicdataFactoryCategoryExcel.java new file mode 100644 index 000000000..b11ddf345 --- /dev/null +++ b/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/excel/BasicdataFactoryCategoryExcel.java @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the dreamlu.net developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: Chill 庄骞 (smallchill@163.com) + */ +package com.logpm.basicdata.excel; + + +import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.annotation.write.style.ColumnWidth; +import com.alibaba.excel.annotation.write.style.ContentRowHeight; +import com.alibaba.excel.annotation.write.style.HeadRowHeight; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.io.Serializable; + + +/** + * 工厂品类导入 + * + * @author chao + * @date 2024/04/19 + */ +@Data +@ColumnWidth(25) +@HeadRowHeight(20) +@ContentRowHeight(18) +public class BasicdataFactoryCategoryExcel implements Serializable { + + private static final long serialVersionUID = 1L; + + + @ColumnWidth(20) + @ExcelProperty("品牌") + private String brand; + + @ColumnWidth(20) + @ExcelProperty("结算品类") + private String category; + + @ColumnWidth(20) + @ExcelProperty("一级品") + private String firsts; + + @ColumnWidth(20) + @ExcelProperty("二级品") + private String seconds; + + @ColumnWidth(20) + @ExcelProperty("三级品") + private String thirds; + +} diff --git a/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/excel/BasicdataFactoryCategoryImporter.java b/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/excel/BasicdataFactoryCategoryImporter.java new file mode 100644 index 000000000..f71e8f624 --- /dev/null +++ b/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/excel/BasicdataFactoryCategoryImporter.java @@ -0,0 +1,112 @@ +package com.logpm.basicdata.excel; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.logpm.basicdata.entity.BasicdataBrandEntity; +import com.logpm.basicdata.entity.BasicdataCategoryEntity; +import com.logpm.basicdata.entity.BasicdataFactoryCategoryEntity; +import com.logpm.basicdata.service.IBasicdataBrandService; +import com.logpm.basicdata.service.IBasicdataCategoryService; +import com.logpm.basicdata.service.IBasicdataFactoryCategoryService; +import lombok.AllArgsConstructor; +import org.springblade.core.excel.support.ExcelImporter; +import org.springblade.core.log.exception.ServiceException; +import org.springblade.core.tool.utils.BeanUtil; +import org.springblade.core.tool.utils.ObjectUtil; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.List; + +@AllArgsConstructor +public class BasicdataFactoryCategoryImporter implements ExcelImporter { + + private final IBasicdataFactoryCategoryService basicdataFactoryCategoryService; + private final IBasicdataBrandService basicdataBrandService; + private final IBasicdataCategoryService basicdataCategoryService; + + @Transactional(rollbackFor = RuntimeException.class) + @Override + public void save(List data) { + + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(BasicdataBrandEntity::getIsDeleted, 0); + List list = basicdataBrandService.list(queryWrapper); + + + LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); + lambdaQueryWrapper.eq(BasicdataCategoryEntity::getIsDeleted, 0); + List basicdataCategoryEntities = basicdataCategoryService.list(lambdaQueryWrapper); + + + LambdaQueryWrapper lambdaQueryWrapper1 = new LambdaQueryWrapper<>(); + lambdaQueryWrapper1.eq(BasicdataFactoryCategoryEntity::getIsDeleted, 0); + List allBasicdataFactoryCategoryEntities = basicdataFactoryCategoryService.list(lambdaQueryWrapper1); + + + List basicdataFactoryCategoryEntities = new ArrayList<>(); + for (BasicdataFactoryCategoryExcel datum : data) { + + boolean isBrandFound = list.stream() + .anyMatch(entity -> entity.getBrandName().equals(datum.getBrand())); + + // 根据 isBrandFound 的值进行相应的处理 + if (!isBrandFound) { + // 或者执行其他逻辑,如记录到日志、抛出异常等 + throw new ServiceException(datum.getBrand() + "品牌在系统中不存在!"); + } + + isBrandFound = basicdataCategoryEntities.stream().anyMatch(entity -> entity.getName().equals(datum.getCategory())); + + if (!isBrandFound) { + // 或者执行其他逻辑,如记录到日志、抛出异常等 + throw new ServiceException(datum.getCategory() + "结算品类在系统中不存在!"); + } + + // 需要判断目前增加的三方物料在系统中是否已经存在 + checkAllBasicdataFactoryCategoryEntities(allBasicdataFactoryCategoryEntities, datum); + + + BasicdataFactoryCategoryEntity basicdataFactoryCategoryEntity = BeanUtil.copy(datum, BasicdataFactoryCategoryEntity.class); + + if (ObjectUtil.isNotEmpty(basicdataFactoryCategoryEntity)) { + assert basicdataFactoryCategoryEntity != null; + basicdataFactoryCategoryEntity.setBrandId(list.stream().filter(entity -> entity.getBrandName().equals(basicdataFactoryCategoryEntity.getBrand())).findFirst().get().getId()); + basicdataFactoryCategoryEntity.setCategoryId(basicdataCategoryEntities.stream().filter(entity -> entity.getName().equals(basicdataFactoryCategoryEntity.getCategory())).findFirst().get().getId()); + } + + basicdataFactoryCategoryEntities.add(basicdataFactoryCategoryEntity); + + } + basicdataFactoryCategoryService.saveBatch(basicdataFactoryCategoryEntities); + + } + + /** + * 验证三方物料是否已经存在 + * @param allBasicdataFactoryCategoryEntities + * @param datum + */ + private void checkAllBasicdataFactoryCategoryEntities(List allBasicdataFactoryCategoryEntities, BasicdataFactoryCategoryExcel datum) { + for (BasicdataFactoryCategoryEntity allBasicdataFactoryCategoryEntity : allBasicdataFactoryCategoryEntities) { + if (allBasicdataFactoryCategoryEntity.getBrand().equals(datum.getBrand()) && allBasicdataFactoryCategoryEntity.getCategory().equals(datum.getCategory()) ){ + if(ObjectUtil.isNotEmpty(allBasicdataFactoryCategoryEntity.getFirsts())&& ObjectUtil.isNotEmpty(allBasicdataFactoryCategoryEntity.getSeconds())&& ObjectUtil.isNotEmpty(allBasicdataFactoryCategoryEntity.getThirds())){ + if(allBasicdataFactoryCategoryEntity.getFirsts().equals(datum.getFirsts()) && allBasicdataFactoryCategoryEntity.getSeconds().equals(datum.getSeconds()) && allBasicdataFactoryCategoryEntity.getThirds().equals(datum.getThirds())){ + throw new ServiceException("品牌:" + datum.getBrand() + " 品类:" + datum.getCategory() + "一级品:"+datum.getFirsts()+" 二级品:"+datum.getSeconds()+" 三级品:"+datum.getThirds()+"已存在相同的配置内容"); + } + + }else if(ObjectUtil.isNotEmpty(allBasicdataFactoryCategoryEntity.getFirsts())&& ObjectUtil.isNotEmpty(allBasicdataFactoryCategoryEntity.getSeconds())){ + if (allBasicdataFactoryCategoryEntity.getFirsts().equals(datum.getFirsts()) && allBasicdataFactoryCategoryEntity.getSeconds().equals(datum.getSeconds())){ + throw new ServiceException("品牌:" + datum.getBrand() + " 品类:" + datum.getCategory() + "一级品:"+datum.getFirsts()+" 二级品:"+datum.getSeconds()+"已存在相同的配置内容"); + } + } else if (ObjectUtil.isNotEmpty(allBasicdataFactoryCategoryEntity.getFirsts())) { + if (allBasicdataFactoryCategoryEntity.getFirsts().equals(datum.getFirsts())){ + throw new ServiceException("品牌:" + datum.getBrand() + " 品类:" + datum.getCategory() + "一级品:"+datum.getFirsts()+"已存在相同的配置内容"); + } + } + + } + } + + + } +}