parent
3cf248f835
commit
b1238b9ce5
@ -0,0 +1,112 @@ |
||||
package com.bweb.excelListener; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.fastjson.JSON; |
||||
import com.bweb.model.ImportCouponModel; |
||||
import com.hai.common.utils.DateUtil; |
||||
import com.hai.entity.HighCoupon; |
||||
import com.hai.entity.HighCouponCode; |
||||
import com.hai.model.UserInfoModel; |
||||
import com.hai.service.HighCouponCodeService; |
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import javax.annotation.Resource; |
||||
import java.util.*; |
||||
import java.util.stream.Collectors; |
||||
|
||||
/** |
||||
* @Auther: 胡锐 |
||||
* @Description: 导入监听器 |
||||
* @Date: 2021/3/20 21:03 |
||||
*/ |
||||
public class ImportCouponListener extends AnalysisEventListener<ImportCouponModel> { |
||||
|
||||
List<ImportCouponModel> errorData = new ArrayList<>(); |
||||
List<HighCouponCode> successData = new ArrayList<>(); |
||||
List<HighCouponCode> allCouponCode = new ArrayList<>(); |
||||
HighCouponCode highCouponCode; |
||||
HighCoupon highCoupon; |
||||
UserInfoModel userInfoModel; |
||||
|
||||
private HighCouponCodeService highCouponCodeService; |
||||
|
||||
/** |
||||
* @Author 胡锐 |
||||
* @Description 解析到的每一条数据都会调用此方法 |
||||
* @Date 2021/3/20 21:08 |
||||
**/ |
||||
@Override |
||||
public void invoke(ImportCouponModel importCouponModel, AnalysisContext analysisContext) { |
||||
if (StringUtils.isBlank(importCouponModel.getSalesCode()) |
||||
|| StringUtils.isBlank(importCouponModel.getSalesEndTime()) |
||||
|| StringUtils.isBlank(importCouponModel.getUseEndTime()) ) { |
||||
importCouponModel.setErrorMessage("有必填项未填写"); |
||||
errorData.add(importCouponModel); |
||||
return; |
||||
} |
||||
|
||||
List<HighCouponCode> collect = allCouponCode.stream().filter(o -> o.getSalesCode().equals(importCouponModel.getSalesCode())).collect(Collectors.toList()); |
||||
if (collect != null && collect.size() > 0) { |
||||
importCouponModel.setErrorMessage("销售码已存在"); |
||||
errorData.add(importCouponModel); |
||||
return; |
||||
} |
||||
|
||||
highCouponCode = new HighCouponCode(); |
||||
highCouponCode.setCouponId(highCoupon.getCompanyId()); |
||||
highCouponCode.setMerchantId(highCoupon.getMerchantId()); |
||||
highCouponCode.setSalesCode(importCouponModel.getSalesCode()); |
||||
|
||||
// 销售截止时间
|
||||
Calendar salesEndTime = Calendar.getInstance(); |
||||
salesEndTime.setTime(DateUtil.format(importCouponModel.getSalesEndTime(), "yyyy/MM/dd")); |
||||
salesEndTime.set(Calendar.HOUR_OF_DAY, 23); |
||||
salesEndTime.set(Calendar.MINUTE, 59); |
||||
salesEndTime.set(Calendar.SECOND, 59); |
||||
highCouponCode.setSalesEndTime(salesEndTime.getTime()); |
||||
|
||||
// 使用截止时间
|
||||
Calendar useEndTime = Calendar.getInstance(); |
||||
useEndTime.setTime(DateUtil.format(importCouponModel.getUseEndTime(), "yyyy/MM/dd")); |
||||
useEndTime.set(Calendar.HOUR_OF_DAY, 23); |
||||
useEndTime.set(Calendar.MINUTE, 59); |
||||
useEndTime.set(Calendar.SECOND, 59); |
||||
highCouponCode.setUseEndTime(useEndTime.getTime()); |
||||
|
||||
highCouponCode.setStatus(1); |
||||
highCouponCode.setOperatorId(userInfoModel.getSecUser().getId()); |
||||
highCouponCode.setOperatorName(userInfoModel.getSecUser().getUserName()); |
||||
highCouponCode.setCreateTime(new Date()); |
||||
successData.add(highCouponCode); |
||||
allCouponCode.add(highCouponCode); |
||||
} |
||||
|
||||
/** |
||||
* @Author 胡锐 |
||||
* @Description 所有数据解析完成了 都会来调用此方法 |
||||
* @Date 2021/3/20 21:08 |
||||
**/ |
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext analysisContext) { |
||||
if (successData.size() > 0) { |
||||
highCouponCodeService.insertList(successData); |
||||
} |
||||
this.successData.clear(); |
||||
this.allCouponCode.clear(); |
||||
} |
||||
|
||||
public List<ImportCouponModel> getErrorData() { |
||||
return errorData; |
||||
} |
||||
|
||||
public void initData(HighCoupon highCoupon, UserInfoModel userInfoModel, HighCouponCodeService highCouponCodeService){ |
||||
this.highCoupon = highCoupon; |
||||
this.userInfoModel = userInfoModel; |
||||
this.highCouponCodeService = highCouponCodeService; |
||||
this.allCouponCode = this.highCouponCodeService.getCouponCodeList(new HashMap<>()); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,57 @@ |
||||
package com.bweb.model; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
import com.alibaba.excel.annotation.format.DateTimeFormat; |
||||
|
||||
/** |
||||
* @Auther: 胡锐 |
||||
* @Description: 卡卷导入模型 |
||||
* @Date: 2021/3/20 21:03 |
||||
*/ |
||||
public class ImportCouponModel { |
||||
|
||||
@ExcelProperty("卡券销售码") |
||||
private String salesCode; |
||||
|
||||
@ExcelProperty("销售有效期") |
||||
@DateTimeFormat("yyyy/MM/dd") |
||||
private String salesEndTime; |
||||
|
||||
@ExcelProperty("使用有效期") |
||||
private String useEndTime; |
||||
|
||||
// 错误消息
|
||||
private String errorMessage; |
||||
|
||||
public String getSalesCode() { |
||||
return salesCode; |
||||
} |
||||
|
||||
public void setSalesCode(String salesCode) { |
||||
this.salesCode = salesCode; |
||||
} |
||||
|
||||
public String getSalesEndTime() { |
||||
return salesEndTime; |
||||
} |
||||
|
||||
public void setSalesEndTime(String salesEndTime) { |
||||
this.salesEndTime = salesEndTime; |
||||
} |
||||
|
||||
public String getUseEndTime() { |
||||
return useEndTime; |
||||
} |
||||
|
||||
public void setUseEndTime(String useEndTime) { |
||||
this.useEndTime = useEndTime; |
||||
} |
||||
|
||||
public String getErrorMessage() { |
||||
return errorMessage; |
||||
} |
||||
|
||||
public void setErrorMessage(String errorMessage) { |
||||
this.errorMessage = errorMessage; |
||||
} |
||||
} |
@ -0,0 +1,34 @@ |
||||
package common; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.fastjson.JSON; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @Auther: 胡锐 |
||||
* @Description: |
||||
* @Date: 2021/3/20 20:51 |
||||
*/ |
||||
public class DemoDataListener extends AnalysisEventListener<ExcelModel> { |
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(DemoDataListener.class); |
||||
|
||||
List<ExcelModel> list = new ArrayList<>(); |
||||
|
||||
@Override |
||||
public void invoke(ExcelModel excelModel, AnalysisContext analysisContext) { |
||||
System.out.println(JSON.toJSONString(excelModel)); |
||||
list.add(excelModel); |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext analysisContext) { |
||||
LOGGER.info("所有数据解析完成!"); |
||||
System.out.println("所有数据解析完成"); |
||||
} |
||||
} |
@ -0,0 +1,33 @@ |
||||
package common; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
||||
/** |
||||
* @Auther: 胡锐 |
||||
* @Description: |
||||
* @Date: 2021/3/20 20:26 |
||||
*/ |
||||
public class ExcelModel { |
||||
|
||||
@ExcelProperty("卡券编码") |
||||
private String key; |
||||
|
||||
@ExcelProperty("商品编码") |
||||
private String name; |
||||
|
||||
public String getKey() { |
||||
return key; |
||||
} |
||||
|
||||
public void setKey(String key) { |
||||
this.key = key; |
||||
} |
||||
|
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
public void setName(String name) { |
||||
this.name = name; |
||||
} |
||||
} |
@ -0,0 +1,44 @@ |
||||
package common; |
||||
|
||||
import com.BWebApplication; |
||||
import com.alibaba.excel.EasyExcel; |
||||
import com.alibaba.fastjson.JSON; |
||||
import com.hai.entity.SecRegion; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
||||
import org.springframework.test.context.web.WebAppConfiguration; |
||||
|
||||
import java.io.FileOutputStream; |
||||
import java.io.OutputStreamWriter; |
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @Auther: 胡锐 |
||||
* @Description: |
||||
* @Date: 2021/3/20 20:26 |
||||
*/ |
||||
@RunWith(SpringJUnit4ClassRunner.class) |
||||
@SpringBootTest(classes = BWebApplication.class) |
||||
@WebAppConfiguration |
||||
public class ExcelTest { |
||||
|
||||
|
||||
@Test |
||||
public void test(){ |
||||
try { |
||||
|
||||
List<ExcelModel> list = new ArrayList<>(); |
||||
EasyExcel.read("F:\\卡券列表记录.xlsx", ExcelModel.class, new DemoDataListener()).sheet().doRead(); |
||||
|
||||
|
||||
}catch (Exception e){ |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue