涉及无限极分类实现
- ProductExcel.java
package io.renren.modules.product.excel;
import cn.afterturn.easypoi.excel.annotation.Excel;
import io.renren.modules.product.dto.ProductBarDTO;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.poi.hpsf.Decimal;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
@Data
public class ProductExcel {
// 动态下拉框字段:dict,addressList = true
// @Excel(name = "学生类型",dict = "level",addressList = true,width = 20)
// private String type;
@Excel(name="商品名称",width = 20)
private String name;
public String getName(){//过滤空格--因为可能读取excel下面的空行,null必须处理;
if (name != null){
return name.trim();
}
return null;
}
@Excel(name = "商品简称",width = 20)
private String shortName;
@Excel(name = "商品规格",width = 20)
private String spec;
@Excel(name = "商品重量",width = 20)
private String weight;
@Excel(name = "商品主条码",width = 20)
private String barCode;
public String getBarCode(){// 过滤空格;
if (barCode != null){
return barCode.trim();
}
return null;
}
@Excel(name = "分类",width = 20,dict = "categoryName",addressList = true)
private String categoryName;
private Long categoryId;
public String getCategoryName(){//过滤空格,过滤开头的一个或多个"|--"
if (categoryName != null){
String cateName = categoryName.trim();
String lastStr = cateName.replaceFirst("^(\\|--)*", "");
return lastStr;
}
return null;
}
@Excel(name = "商品单位",width = 20,dict = "unitName",addressList = true)
private String unitName;
private Long unitId;
@Excel(name = "经营方式",width = 20,dict = "manageName",addressList = true)
private String manageName;
private Long manageType;
@Excel(name="销项税率",width = 20)
private Integer saleTaxRate;
@Excel(name = "可负库存销售",width = 20,replace = { "是_1", "否_0"}, addressList = true)// 是,否
private Integer negativeStockSale;
@Excel(name = "销售价",width = 20)
private BigDecimal price;
@Excel(name="进货价",width = 20)
private BigDecimal purchasePrice;
@Excel(name="进项税率",width = 20)
private Integer purchaseTaxRate;
@Excel(name = "会员价",width = 20)
private BigDecimal memberPrice;
@Excel(name = "是否会员折扣",width = 20,replace = { "是_1", "否_0"}, addressList = true)//0:否 1:是
private Integer isMemberDiscount;
@Excel(name = "是否微信端售卖",width = 20,replace = { "是_1", "否_0"}, addressList = true)// 0:否 1:是
private Integer isWxSale;
@Excel(name = "微信端价",width = 20)
private BigDecimal wxPrice;
@Excel(name = "导游提成",width = 20)
private BigDecimal guideCommission;
@Excel(name="商品多条码",width = 60)
private String moreBarCode;
public String getMoreBarCode(){//过滤空格
if (moreBarCode != null){
return moreBarCode.trim();
}
return null;
}
@Excel(name="多条码对应规格",width = 60)
private String moreSpec;
public String getMoreSpec(){//过滤空格
if (moreSpec != null){
return moreSpec.trim();
}
return null;
}
private Integer isMultiBar;
private String pinyinCode;
private Integer status;
@ApiModelProperty(value = "产品多条码")
private List<ProductBarDTO> productBarList;
}
- productExcelHandler.java
package io.renren.modules.product.handler;
import cn.afterturn.easypoi.handler.inter.IExcelDictHandler;
import io.renren.modules.product.dto.ProductCategoryDTO;
import io.renren.modules.product.dto.ProductCategoryExportModelDTO;
import io.renren.modules.product.dto.ProductUnitDTO;
import io.renren.modules.product.service.ProductCategoryService;
import io.renren.modules.product.service.ProductUnitService;
import io.renren.modules.settings.dto.SettingManageTypeDTO;
import io.renren.modules.settings.service.SettingManageTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class productExcelHandler implements IExcelDictHandler {
@Autowired
private ProductUnitService productUnitService;
@Autowired
private SettingManageTypeService settingManageTypeService;
@Autowired
private ProductCategoryService productCategoryService;
public static productExcelHandler productExcelHandler;
public productExcelHandler(){}
@PostConstruct
public void init(){
productExcelHandler = this;
productExcelHandler.productUnitService = this.productUnitService;
productExcelHandler.settingManageTypeService = this.settingManageTypeService;
productExcelHandler.productCategoryService = this.productCategoryService;
}
@Override
public List<Map> getList(String dict) {
System.out.println("-----" + dict);
List<Map> list = new ArrayList<>();
Map<String, String> dictMap = new HashMap<>();
if ("level".equals(dict)){
dictMap.put("dictKey", "0");
dictMap.put("dictValue", "严重瞌睡");
list.add(dictMap);
dictMap = new HashMap<>();
dictMap.put("dictKey", "1");
dictMap.put("dictValue", "小B");
list.add(dictMap);
dictMap = new HashMap<>();
dictMap.put("dictKey", "2");
dictMap.put("dictValue", "深度富有");
list.add(dictMap);
} else if ("unitName".equals(dict)){
List<ProductUnitDTO> proUnitList = productExcelHandler.productUnitService.list(null);
System.out.println("------lists-----" + proUnitList);
for (ProductUnitDTO i : proUnitList){
dictMap = new HashMap<>();
dictMap.put("dictKey", i.getId().toString());
dictMap.put("dictValue", i.getName());
list.add(dictMap);
}
} else if ("manageName".equals(dict)){
List<SettingManageTypeDTO> manageTypeList = productExcelHandler.settingManageTypeService.list(null);
for (SettingManageTypeDTO i : manageTypeList){
dictMap = new HashMap<>();
dictMap.put("dictKey", i.getId().toString());
dictMap.put("dictValue", i.getName());
list.add(dictMap);
}
} else if ("categoryName".equals(dict)){
Map<String, Object> params = new HashMap<>();
List<ProductCategoryExportModelDTO> cateList = productExcelHandler.productCategoryService.plainTreeList(params);
for (ProductCategoryExportModelDTO i : cateList){
dictMap = new HashMap<>();
dictMap.put("dictKey", i.getId().toString());
int level = i.getLevel();
String name = fullStr( level,i.getName());
dictMap.put("dictValue", name);
list.add(dictMap);
}
}
return list;
}
/*****
* original,左边扩充n个"-"
* @param n
* @param original
* @return
*/
private String fullStr(int n,String original){
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append("|--");
}
sb.append(original);
return sb.toString();
}
// 导出用到--没发现怎么用到的呢?????
@Override
public String toName(String dict, Object obj, String name, Object value) {
if ("level".equals(dict)) {
int level = Integer.parseInt(value.toString());
switch (level) {
case 0:
return "严重瞌睡";
case 1:
return "小B";
case 2:
return "深度富有";
}
}
// if ("unitName".equals(dict)){
//
// }
return null;
}
// 导入用到
@Override
public String toValue(String dict, Object obj, String name, Object value) {
if ("level".equals(dict)) {
String valueStr = String.valueOf(value);
switch (valueStr) {
case "严重瞌睡":
return "0";
case "小B":
return "1";
case "深度富有":
return "2";
}
}
return null;
}
}
- https://www.cnblogs.com/super-chao/p/16955065.html 实测有用
- easpoi的api文档:http://doc.wupaas.com/docs/easypoi/easypoi-1c2cp5rf3hnqv