配件下载excel模板、导入数据

This commit is contained in:
xiaofajia 2024-11-29 17:09:31 +08:00
parent a718b7537c
commit ab67214d92
6 changed files with 288 additions and 13 deletions

View File

@ -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";
}

View File

@ -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<RepairWaresExcelVO> 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<RepairWaresExcelRespVO> importData(@RequestParam("file") MultipartFile file,
@RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception{
List<RepairWaresExcelVO> list = ExcelUtils.read(file, RepairWaresExcelVO.class);
return success(waresService.importData(list, updateSupport));
}
}

View File

@ -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<RepairWares> {
* @return java.util.List<cn.iocoder.yudao.module.project.entity.RepairWares>
**/
List<RepairWares> listByIds(List<String> idList);
/**
* 导入配件数据
*
* @author 小李
* @date 16:20 2024/11/29
**/
RepairWaresExcelRespVO importData(List<RepairWaresExcelVO> list, Boolean updateSupport);
}

View File

@ -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<RepairWaresMapper, Repai
@Resource
private RepairWaresMapper waresMapper;
@Resource
@Lazy
private BaseWarehouseService warehouseService;
@Resource
@Lazy
private BaseTypeService typeService;
/**
* 保存配件库
@ -98,7 +114,7 @@ public class RepairWaresServiceImpl extends ServiceImpl<RepairWaresMapper, Repai
* @date 18:03 2024/9/25
**/
@Override
public RepairWaresRespVO getWaresByName(String name){
public RepairWaresRespVO getWaresByName(String name) {
return waresMapper.getWaresByName(name);
}
@ -127,4 +143,70 @@ public class RepairWaresServiceImpl extends ServiceImpl<RepairWaresMapper, Repai
return baseMapper.selectBatchIds(idList);
}
/**
* 导入配件数据
*
* @author 小李
* @date 16:20 2024/11/29
**/
@Override
@Transactional(rollbackFor = Exception.class)
public RepairWaresExcelRespVO importData(List<RepairWaresExcelVO> list, Boolean updateSupport) {
// 参数校验
if (CollUtil.isEmpty(list)) {
throw exception0(500, "导入配件数据不能为空!");
}
// 取所有的仓库变成mapkey是名称value是id
List<BaseWarehouse> warehouses = warehouseService.list();
Map<String, String> warehousesMap = warehouses.stream().collect(Collectors.toMap(BaseWarehouse::getName, BaseWarehouse::getId));
// 取所有的配件类型变成mapkey是名称value是id
List<BaseType> types = typeService.list(new LambdaQueryWrapper<BaseType>().eq(BaseType::getType, "02"));
Map<String, String> 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> repairWares = baseMapper.selectList(new LambdaQueryWrapper<RepairWares>().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;
}
}

View File

@ -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<String> createNames;
/** 更新成功的配件名称集合 */
private List<String> updateNames;
/** 导入失败的配件信息key是配件名称value是失败原因 */
private Map<String, String> failureNames;
}

View File

@ -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;
}