diff --git a/dl-module-repair/src/main/java/cn/iocoder/yudao/common/RepairDictConstants.java b/dl-module-repair/src/main/java/cn/iocoder/yudao/common/RepairDictConstants.java new file mode 100644 index 00000000..76af5320 --- /dev/null +++ b/dl-module-repair/src/main/java/cn/iocoder/yudao/common/RepairDictConstants.java @@ -0,0 +1,19 @@ +package cn.iocoder.yudao.common; + +/** + * 维修相关字典枚举 + * + * @author 小李 + * @date 15:48 2024/11/29 +**/ +public interface RepairDictConstants { + + /** 单位 */ + String REPAIR_UNIT = "repair_unit"; + + /** 配件来源 */ + String WARES_DATA_FORM = "repair_data_form"; + + /** 配件属性 */ + String WARES_ATTRIBUTE = "repair_attribute"; +} diff --git a/dl-module-repair/src/main/java/cn/iocoder/yudao/module/project/controller/admin/RepairWaresController.java b/dl-module-repair/src/main/java/cn/iocoder/yudao/module/project/controller/admin/RepairWaresController.java index be4b75ce..8aa15b45 100644 --- a/dl-module-repair/src/main/java/cn/iocoder/yudao/module/project/controller/admin/RepairWaresController.java +++ b/dl-module-repair/src/main/java/cn/iocoder/yudao/module/project/controller/admin/RepairWaresController.java @@ -1,22 +1,29 @@ package cn.iocoder.yudao.module.project.controller.admin; import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; import cn.iocoder.yudao.module.project.entity.RepairWares; import cn.iocoder.yudao.module.project.service.RepairWaresService; -import cn.iocoder.yudao.module.project.vo.RepairWaresPageReqVO; -import cn.iocoder.yudao.module.project.vo.RepairWaresRespVO; -import cn.iocoder.yudao.module.project.vo.RepairWaresSaveReqVO; +import cn.iocoder.yudao.module.project.vo.*; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.Parameters; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; +import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; +import java.math.BigDecimal; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; @Tag(name = "管理后台 - 配件库") @@ -135,4 +142,56 @@ public class RepairWaresController { return success(waresService.getAllTypeList()); } + /** + * 下载配件导入模板 + * + * @author 小李 + * @date 15:35 2024/11/29 + **/ + @GetMapping("/get-import-template") + @Operation(summary = "下载配件导入模板") + public void getImportTemplate(HttpServletResponse response) throws Exception { + List list = Collections.singletonList( + RepairWaresExcelVO + .builder() + .barCode("123") + .code("123") + .name("模板数据") + .model("123") + .price(new BigDecimal("100")) + .purPrice(new BigDecimal("100")) + .type("模板分类") + .unit("01") + .warehouse("模板仓库") + .miniStock(new BigDecimal("0")) + .maxStock(new BigDecimal("10")) + .stock(new BigDecimal("5")) +// .attribute("yc") + .carModel("模板车型") +// .dataForm("bd") + .remark("模板") + .build() + ); + ExcelUtils.write(response, "配件导入模板.xls", "配件信息", RepairWaresExcelVO.class, list); + } + + /** + * 导入配件数据 + * + * @author 小李 + * @date 16:20 2024/11/29 + * @param file Excel 文件 + * @param updateSupport 是否支持更新 + **/ + @PostMapping("/import-data") + @Operation(summary = "导入配件数据") + @Parameters({ + @Parameter(name = "file", description = "Excel 文件", required = true), + @Parameter(name = "updateSupport", description = "是否支持更新,默认为 false", example = "true") + }) + public CommonResult importData(@RequestParam("file") MultipartFile file, + @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception{ + List list = ExcelUtils.read(file, RepairWaresExcelVO.class); + return success(waresService.importData(list, updateSupport)); + } } \ No newline at end of file diff --git a/dl-module-repair/src/main/java/cn/iocoder/yudao/module/project/service/RepairWaresService.java b/dl-module-repair/src/main/java/cn/iocoder/yudao/module/project/service/RepairWaresService.java index cc123d5c..a7ab5e90 100644 --- a/dl-module-repair/src/main/java/cn/iocoder/yudao/module/project/service/RepairWaresService.java +++ b/dl-module-repair/src/main/java/cn/iocoder/yudao/module/project/service/RepairWaresService.java @@ -1,9 +1,7 @@ package cn.iocoder.yudao.module.project.service; import cn.iocoder.yudao.module.project.entity.RepairWares; -import cn.iocoder.yudao.module.project.vo.RepairWaresPageReqVO; -import cn.iocoder.yudao.module.project.vo.RepairWaresRespVO; -import cn.iocoder.yudao.module.project.vo.RepairWaresSaveReqVO; +import cn.iocoder.yudao.module.project.vo.*; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.IService; @@ -80,4 +78,12 @@ public interface RepairWaresService extends IService { * @return java.util.List **/ List listByIds(List idList); + + /** + * 导入配件数据 + * + * @author 小李 + * @date 16:20 2024/11/29 + **/ + RepairWaresExcelRespVO importData(List list, Boolean updateSupport); } \ No newline at end of file diff --git a/dl-module-repair/src/main/java/cn/iocoder/yudao/module/project/service/impl/RepairWaresServiceImpl.java b/dl-module-repair/src/main/java/cn/iocoder/yudao/module/project/service/impl/RepairWaresServiceImpl.java index 62aa262a..c5d51ad4 100644 --- a/dl-module-repair/src/main/java/cn/iocoder/yudao/module/project/service/impl/RepairWaresServiceImpl.java +++ b/dl-module-repair/src/main/java/cn/iocoder/yudao/module/project/service/impl/RepairWaresServiceImpl.java @@ -1,23 +1,31 @@ package cn.iocoder.yudao.module.project.service.impl; +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; +import cn.iocoder.yudao.module.conf.entity.BaseType; +import cn.iocoder.yudao.module.conf.entity.BaseWarehouse; +import cn.iocoder.yudao.module.conf.service.BaseTypeService; +import cn.iocoder.yudao.module.conf.service.BaseWarehouseService; import cn.iocoder.yudao.module.project.entity.RepairWares; import cn.iocoder.yudao.module.project.mapper.RepairWaresMapper; import cn.iocoder.yudao.module.project.service.RepairWaresService; -import cn.iocoder.yudao.module.project.vo.RepairWaresPageReqVO; -import cn.iocoder.yudao.module.project.vo.RepairWaresRespVO; -import cn.iocoder.yudao.module.project.vo.RepairWaresSaveReqVO; +import cn.iocoder.yudao.module.project.vo.*; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.apache.commons.lang3.StringUtils; +import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; -import java.util.Arrays; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.stream.Collectors; + +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception0; /** * 配件库 Service 实现类 @@ -31,6 +39,14 @@ public class RepairWaresServiceImpl extends ServiceImpl list, Boolean updateSupport) { + // 参数校验 + if (CollUtil.isEmpty(list)) { + throw exception0(500, "导入配件数据不能为空!"); + } + + // 取所有的仓库变成map,key是名称,value是id + List warehouses = warehouseService.list(); + Map warehousesMap = warehouses.stream().collect(Collectors.toMap(BaseWarehouse::getName, BaseWarehouse::getId)); + + // 取所有的配件类型变成map,key是名称,value是id + List types = typeService.list(new LambdaQueryWrapper().eq(BaseType::getType, "02")); + Map typesMap = types.stream().collect(Collectors.toMap(BaseType::getName, BaseType::getId)); + + // 遍历,逐个创建 or 更新 + RepairWaresExcelRespVO result = RepairWaresExcelRespVO.builder().createNames(new ArrayList<>()) + .updateNames(new ArrayList<>()).failureNames(new LinkedHashMap<>()).build(); + list.forEach(item -> { + // 验证是否存在,用selectList防止本来就有多个导致的报错 + List repairWares = baseMapper.selectList(new LambdaQueryWrapper().eq(RepairWares::getName, item.getName())); + if (CollUtil.isEmpty(repairWares)) { + RepairWares wares = BeanUtils.toBean(item, RepairWares.class); + if (ObjectUtil.isNotEmpty(item.getType()) && ObjectUtil.isNotEmpty(typesMap.get(item.getType()))) { + wares.setType(typesMap.get(item.getType())); + } + if (ObjectUtil.isNotEmpty(item.getWarehouse()) && ObjectUtil.isNotEmpty(warehousesMap.get(item.getWarehouse()))) { + wares.setWarehouse(warehousesMap.get(item.getWarehouse())); + } + baseMapper.insert(wares); + result.getCreateNames().add(item.getName()); + return; + } + // 存在看是否可以修改 + if (!updateSupport) { + result.getFailureNames().put(item.getName(), "不允许修改!"); + return; + } + // 可以修改,看是否有多个相同名称的配件 + if (repairWares.size() > 1) { + result.getFailureNames().put(item.getName(), "存在多个相同名称的配件!"); + return; + } + // 更新 + RepairWares wares = repairWares.get(0); + RepairWares updateWares = BeanUtils.toBean(item, RepairWares.class); + updateWares.setId(wares.getId()); + if (ObjectUtil.isNotEmpty(item.getType()) && ObjectUtil.isNotEmpty(typesMap.get(item.getType()))) { + updateWares.setType(typesMap.get(item.getType())); + } + if (ObjectUtil.isNotEmpty(item.getWarehouse()) && ObjectUtil.isNotEmpty(warehousesMap.get(item.getWarehouse()))) { + updateWares.setWarehouse(warehousesMap.get(item.getWarehouse())); + } + baseMapper.updateById(updateWares); + result.getUpdateNames().add(item.getName()); + }); + return result; + } + } \ No newline at end of file diff --git a/dl-module-repair/src/main/java/cn/iocoder/yudao/module/project/vo/RepairWaresExcelRespVO.java b/dl-module-repair/src/main/java/cn/iocoder/yudao/module/project/vo/RepairWaresExcelRespVO.java new file mode 100644 index 00000000..71ab82af --- /dev/null +++ b/dl-module-repair/src/main/java/cn/iocoder/yudao/module/project/vo/RepairWaresExcelRespVO.java @@ -0,0 +1,29 @@ +package cn.iocoder.yudao.module.project.vo; + +import lombok.Builder; +import lombok.Data; + +import java.util.List; +import java.util.Map; + +/** + * 导入配件数据后的响应 + * + * @author 小李 + * @date 16:22 2024/11/29 +**/ +@Data +@Builder +public class RepairWaresExcelRespVO { + + /** 导入成功的配件名称集合 */ + private List createNames; + + /** 更新成功的配件名称集合 */ + private List updateNames; + + /** 导入失败的配件信息,key是配件名称,value是失败原因 */ + private Map failureNames; + + +} diff --git a/dl-module-repair/src/main/java/cn/iocoder/yudao/module/project/vo/RepairWaresExcelVO.java b/dl-module-repair/src/main/java/cn/iocoder/yudao/module/project/vo/RepairWaresExcelVO.java new file mode 100644 index 00000000..6df5ba3b --- /dev/null +++ b/dl-module-repair/src/main/java/cn/iocoder/yudao/module/project/vo/RepairWaresExcelVO.java @@ -0,0 +1,80 @@ +package cn.iocoder.yudao.module.project.vo; + +import cn.iocoder.yudao.common.RepairDictConstants; +import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat; +import cn.iocoder.yudao.framework.excel.core.convert.DictConvert; +import com.alibaba.excel.annotation.ExcelProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.experimental.Accessors; + +import java.math.BigDecimal; + +/** + * 导入、导出excel用的配件VO + * + * @author 小李 + * @date 15:38 2024/11/29 +**/ +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +@Accessors(chain = false) +public class RepairWaresExcelVO { + + @ExcelProperty("条形码") + private String barCode; + + @ExcelProperty("商品编码") + private String code; + + @ExcelProperty("商品名称") + private String name; + + @ExcelProperty("规格型号") + private String model; + + @ExcelProperty("售价") + private BigDecimal price; + + @ExcelProperty("进价") + private BigDecimal purPrice; + + @ExcelProperty("所属分类") + private String type; + + @ExcelProperty(value = "计量单位", converter = DictConvert.class) + @DictFormat(RepairDictConstants.REPAIR_UNIT) + private String unit; + + @ExcelProperty("默认仓库") + private String warehouse; + + @ExcelProperty("最低库存") + private BigDecimal miniStock; + + @ExcelProperty("最高库存") + private BigDecimal maxStock; + + @ExcelProperty("当前库存") + private BigDecimal stock; + + /** 好像找不到这个字典 */ +// @ExcelProperty(value = "配件属性", converter = DictConvert.class) +// @DictFormat(RepairDictConstants.WARES_ATTRIBUTE) +// private String attribute; + + @ExcelProperty("适用车型") + private String carModel; + + /** 好像找不到这个字典 */ +// @ExcelProperty(value = "配件来源", converter = DictConvert.class) +// @DictFormat(RepairDictConstants.WARES_DATA_FORM) +// private String dataForm; + + @ExcelProperty("备注") + private String remark; +}