优化 新增产品类型导入

This commit is contained in:
yf 2025-10-23 09:04:55 +08:00
parent 9df51bddda
commit 3d7fa5e802
4 changed files with 101 additions and 6 deletions

View File

@ -1,5 +1,6 @@
package cn.hangtag.module.oms.controller.admin.productinfo.vo;
import cn.hangtag.framework.excel.core.annotations.ExcelColumnSelect;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.annotation.write.style.HeadFontStyle;
@ -23,12 +24,12 @@ import org.apache.poi.ss.usermodel.IndexedColors;
@Accessors(chain = false) // 保持 chain = false 避免导入数据异常
public class ProductInfoExcelVO {
@Schema(description = "产品编码")
@Schema(description = "*产品编码")
@ExcelProperty("*产品编码")
@HeadFontStyle(color = Font.COLOR_RED)
private String code;
@Schema(description = "产品名称", requiredMode = Schema.RequiredMode.REQUIRED)
@Schema(description = "*产品编码", requiredMode = Schema.RequiredMode.REQUIRED)
@ExcelProperty("*产品名称")
@HeadFontStyle(color = Font.COLOR_RED)
private String name;
@ -56,6 +57,11 @@ public class ProductInfoExcelVO {
@ExcelProperty("材质(颜色)")
private String material;
@Schema(description = "品类")
@ExcelColumnSelect(functionName = "ExcelSelectProductTypeFunction")
@ExcelProperty("品类(尺码唛,价钱卡,纸盒,主唛,挂卡,洗水唛)")
private String productType;
@Schema(description = "品牌名称")
@ExcelProperty("品牌名称")
private String brandName;

View File

@ -0,0 +1,35 @@
package cn.hangtag.module.oms.service.productinfo;
import cn.hangtag.framework.excel.core.function.ExcelColumnSelectFunction;
import cn.hangtag.framework.mybatis.core.dataobject.BaseDO;
import cn.hangtag.module.oms.base.dal.dataobject.producttype.ProductTypeDO;
import cn.hangtag.module.oms.base.dal.mysql.producttype.ProductTypeMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Component
public class ExcelSelectProductTypeFunction implements ExcelColumnSelectFunction {
@Resource
private ProductTypeMapper productTypeMapper;
@Override
public String getName() {
return "ExcelSelectProductTypeFunction";
}
@Override
public List<String> getOptions() {
List<ProductTypeDO> dos = productTypeMapper.selectList(new LambdaQueryWrapper<ProductTypeDO>().eq(BaseDO::getDeleted, false)
.orderByDesc(ProductTypeDO::getSort));
List<String> labels = new ArrayList<>();
for (ProductTypeDO aDo : dos) {
labels.add(aDo.getLabel());
}
return labels;
}
}

View File

@ -301,8 +301,11 @@ public class ProductInfoServiceImpl implements ProductInfoService {
for (int i = 0; i < list.size(); i++) {
ProductInfoExcelVO excelVO = list.get(i);
// 判断是否为空行 下拉导致很多空行
if (isAllFieldsEmpty(excelVO)) {
continue;
}
ProductInfoDO productInfo = new ProductInfoDO();
String code = excelVO.getCode();
if(FuncUtil.isNotEmpty(code)){
try {
@ -313,10 +316,23 @@ public class ProductInfoServiceImpl implements ProductInfoService {
}else {
code = productInfo.getCode();
}
if(FuncUtil.isEmpty(code)){
return ""+(i+1)+"行编码为空:"+code;
}
productInfo.setName(excelVO.getName());
String brandName = excelVO.getBrandName();
productInfo.setCode(code);
productInfo.setTemplateType("2");
productInfo.setSpecSizeWidth(FuncUtil.toDouble(excelVO.getSpecWidth()));
productInfo.setSpecSizeHeight(FuncUtil.toDouble(excelVO.getSpecHeight()));
productInfo.setSpecSizeThk(FuncUtil.toDouble(excelVO.getSpecThickness()));
productInfo.setSpecMaterial(excelVO.getMaterial());
productInfo.setRemark(excelVO.getRemarks());
Long productTypeId = toProductTypeId(excelVO.getProductType());
productInfo.setProductTypeId(productTypeId);
if(FuncUtil.isNotEmpty(brandName)){
Long brandId = brandIdCache.get(brandName);
if(FuncUtil.isNotEmpty(brandId)){
@ -328,7 +344,6 @@ public class ProductInfoServiceImpl implements ProductInfoService {
brandIdCache.put(brandName,brandDO.getId());
}
}
}
newList.add(productInfo);
}
@ -337,7 +352,18 @@ public class ProductInfoServiceImpl implements ProductInfoService {
return "";
}
public boolean isAllFieldsEmpty(ProductInfoExcelVO excelVO) {
if (excelVO == null) {
return true;
}
//
if(FuncUtil.isEmpty(excelVO.getCode())
&& FuncUtil.isEmpty(excelVO.getName())
){
return true;
}
return false;
}
private List<ProductInfoRespVO> wrapperRespVO(List<ProductInfoDO> list){
List<ProductInfoRespVO> resList = new ArrayList<>();
list.forEach(productInfoDO -> {
@ -450,6 +476,25 @@ public class ProductInfoServiceImpl implements ProductInfoService {
}
return vo;
}
private static LFUCache<String,Long> ProductTypeIdCache = CacheUtil.newLFUCache(10000, 1000 * 60 * 60);
private Long toProductTypeId(String name){
if(FuncUtil.isEmpty(name)){
return null;
}
Long s = ProductTypeIdCache.get(name);
if (FuncUtil.isEmpty(s)) {
LambdaQueryWrapper<ProductTypeDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(ProductTypeDO::getLabel,name);
lambdaQueryWrapper.last("limit 1");
ProductTypeDO productTypeDO = productTypeMapper.selectOne(lambdaQueryWrapper);
if(FuncUtil.isNotEmpty(productTypeDO)){
s = productTypeDO.getId();
ProductTypeIdCache.put(name, productTypeDO.getId());
}
}
return s;
}
private void initProductTypeNameCache(Set<Long> ids){
if(FuncUtil.isNotEmpty(ids)){
List<ProductTypeDO> dos = productTypeMapper.selectBatchIds(ids);

View File

@ -111,8 +111,17 @@
<Icon icon="ep:upload" class="mr-5px"/>
excel导入
</el-button>
<ProductInfoExcelImport ref="excelImportRef" @success="importSuccess" @download-template="downloadTemplate"/>
<ProductInfoExcelImport ref="excelImportRef" @success="importSuccess" @download-template="downloadTemplate"/>
</div>
<el-button class="ml-2"
type="primary"
plain
@click="downloadTemplate"
v-hasPermi="['oms:product-info:export']"
>
<Icon icon="ep:download" class="mr-5px"/>
下载模板
</el-button>
</el-form-item>
</el-form>