瀏覽代碼

添加文件

userName 1 年之前
父節點
當前提交
2460ba7649

+ 144 - 0
xingyun-basedata/src/main/java/com/lframework/xingyun/basedata/excel/product/ProductHeadImportListener.java

@@ -0,0 +1,144 @@
1
+package com.lframework.xingyun.basedata.excel.product;
2
+
3
+import com.alibaba.excel.context.AnalysisContext;
4
+import com.alibaba.excel.event.AnalysisEventListener;
5
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
6
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
7
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
8
+import com.lframework.starter.common.constants.PatternPool;
9
+import com.lframework.starter.common.constants.StringPool;
10
+import com.lframework.starter.common.exceptions.impl.DefaultClientException;
11
+import com.lframework.starter.common.utils.NumberUtil;
12
+import com.lframework.starter.common.utils.RegUtil;
13
+import com.lframework.starter.common.utils.StringUtil;
14
+import com.lframework.starter.web.common.utils.ApplicationUtil;
15
+import com.lframework.starter.web.components.excel.ExcelImportListener;
16
+import com.lframework.starter.web.components.redis.RedisHandler;
17
+import com.lframework.starter.web.utils.IdUtil;
18
+import com.lframework.xingyun.basedata.entity.Product;
19
+import com.lframework.xingyun.basedata.entity.ProductBrand;
20
+import com.lframework.xingyun.basedata.entity.ProductCategory;
21
+import com.lframework.xingyun.basedata.enums.ProductType;
22
+import com.lframework.xingyun.basedata.service.product.ProductBrandService;
23
+import com.lframework.xingyun.basedata.service.product.ProductCategoryService;
24
+import com.lframework.xingyun.basedata.service.product.ProductPurchaseService;
25
+import com.lframework.xingyun.basedata.service.product.ProductRetailService;
26
+import com.lframework.xingyun.basedata.service.product.ProductSaleService;
27
+import com.lframework.xingyun.basedata.service.product.ProductService;
28
+import com.lframework.xingyun.basedata.vo.product.purchase.CreateProductPurchaseVo;
29
+import com.lframework.xingyun.basedata.vo.product.retail.CreateProductRetailVo;
30
+import com.lframework.xingyun.basedata.vo.product.sale.CreateProductSaleVo;
31
+import java.util.ArrayList;
32
+import java.util.HashMap;
33
+import java.util.List;
34
+import java.util.Map;
35
+
36
+import lombok.extern.slf4j.Slf4j;
37
+import org.codehaus.groovy.tools.StringHelper;
38
+import org.springframework.beans.factory.annotation.Autowired;
39
+import org.springframework.boot.autoconfigure.cache.CacheProperties;
40
+
41
+@Slf4j
42
+ public class ProductHeadImportListener extends AnalysisEventListener<Map<Integer, String>> {
43
+  //定义每多少条数据进行数据库保存
44
+  private static final int BATCH_COUNT = 128;
45
+
46
+  private ProductService productService;
47
+  //建一个errMessage集合保存校验有问题的结果
48
+  private List<String> errMessage;
49
+  //用list集合保存解析到的结果
50
+  private List<Map<Integer, Map<Integer, String>>> list;
51
+
52
+
53
+  //重构,把传来的值赋给对应的属性
54
+  public ProductHeadImportListener(ProductService productService ) {
55
+
56
+    this.productService = productService;
57
+    list = new ArrayList<>();
58
+    errMessage = new ArrayList<>();
59
+  }
60
+
61
+  /**
62
+   * 重写invokeHeadMap方法,获去表头,如果有需要获取第一行表头就重写这个方法,不需要则不需要重写
63
+   *
64
+   * @param headMap Excel每行解析的数据为Map<Integer, String>类型,Integer是Excel的列索引,String为Excel的单元格值
65
+   * @param context context能获取一些东西,比如context.readRowHolder().getRowIndex()为Excel的行索引,表头的行索引为0,0之后的都解析成数据
66
+   */
67
+  @Override
68
+  public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
69
+   // logger.info("解析到一条头数据:{}, currentRowHolder: {}", headMap.toString(), context.readRowHolder().getRowIndex());
70
+    Map<Integer, Map<Integer, String>> map = new HashMap<>();
71
+    map.put(context.readRowHolder().getRowIndex(), headMap);
72
+    list.add(map);
73
+  }
74
+
75
+  /**
76
+   * 重写invoke方法获得除Excel第一行表头之后的数据,
77
+   * 如果Excel第二行也是表头,那么也会解析到这里,如果不需要就通过判断context.readRowHolder().getRowIndex()跳过
78
+   *
79
+   * @param data    除了第一行表头外,数据都会解析到这个方法
80
+   * @param context 和上面解释一样
81
+   */
82
+  @Override
83
+  public void invoke(Map<Integer, String> data, AnalysisContext context) {
84
+
85
+    Map<Integer, Map<Integer, String>> map = new HashMap<>();
86
+    map.put(context.readRowHolder().getRowIndex(), data);
87
+    list.add(map);
88
+    // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
89
+    if (list.size() >= BATCH_COUNT) {
90
+      saveData();
91
+      // 存储完成清理 list
92
+      list.clear();
93
+    }
94
+  }
95
+
96
+  /**
97
+   * 解析到最后会进入这个方法,需要重写这个doAfterAllAnalysed方法,然后里面调用自己定义好保存方法
98
+   * @param context
99
+   */
100
+  @Override
101
+  public void doAfterAllAnalysed(AnalysisContext context) {
102
+    // 这里也要保存数据,确保最后遗留的数据也存储到数据库
103
+    saveData();
104
+
105
+  }
106
+
107
+  /**
108
+   * 加上存储数据库
109
+   */
110
+  private void saveData() {
111
+
112
+    //用errMessage集合保存zwmxzService.save()返回校验错误信息,根据自己需要来写就行了
113
+   // errMessage.addAll(productService.saveexcel(list, ztId, year, month, errMessage.size() > 0));
114
+   // errMessage.addAll(productService.saveexcel(list,errMessage.size() > 0));
115
+  }
116
+
117
+
118
+
119
+  public ProductService getProductService() {
120
+    return productService;
121
+  }
122
+
123
+  public void setZwmxzService(ProductService zwmxzService) {
124
+    this.productService = productService;
125
+  }
126
+
127
+  public List<String> getErrMessage() {
128
+    return errMessage;
129
+  }
130
+
131
+  public void setErrMessage(List<String> errMessage) {
132
+    this.errMessage = errMessage;
133
+  }
134
+
135
+  public List<Map<Integer, Map<Integer, String>>> getList() {
136
+    return list;
137
+  }
138
+
139
+  public void setList(List<Map<Integer, Map<Integer, String>>> list) {
140
+    this.list = list;
141
+  }
142
+
143
+
144
+}

+ 145 - 0
xingyun-sc/src/main/java/com/lframework/xingyun/sc/excel/sale/SaleHeadImportListener.java

@@ -0,0 +1,145 @@
1
+package com.lframework.xingyun.sc.excel.sale;
2
+
3
+import com.alibaba.excel.context.AnalysisContext;
4
+import com.alibaba.excel.event.AnalysisEventListener;
5
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
6
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
7
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
8
+import com.lframework.starter.common.constants.PatternPool;
9
+import com.lframework.starter.common.constants.StringPool;
10
+import com.lframework.starter.common.exceptions.impl.DefaultClientException;
11
+import com.lframework.starter.common.utils.NumberUtil;
12
+import com.lframework.starter.common.utils.RegUtil;
13
+import com.lframework.starter.common.utils.StringUtil;
14
+import com.lframework.starter.web.common.utils.ApplicationUtil;
15
+import com.lframework.starter.web.components.excel.ExcelImportListener;
16
+import com.lframework.starter.web.components.redis.RedisHandler;
17
+import com.lframework.starter.web.utils.IdUtil;
18
+import com.lframework.xingyun.basedata.entity.Product;
19
+import com.lframework.xingyun.basedata.entity.ProductBrand;
20
+import com.lframework.xingyun.basedata.entity.ProductCategory;
21
+import com.lframework.xingyun.basedata.enums.ProductType;
22
+import com.lframework.xingyun.basedata.service.product.ProductBrandService;
23
+import com.lframework.xingyun.basedata.service.product.ProductCategoryService;
24
+import com.lframework.xingyun.basedata.service.product.ProductPurchaseService;
25
+import com.lframework.xingyun.basedata.service.product.ProductRetailService;
26
+import com.lframework.xingyun.basedata.service.product.ProductSaleService;
27
+import com.lframework.xingyun.basedata.service.product.ProductService;
28
+import com.lframework.xingyun.basedata.vo.product.purchase.CreateProductPurchaseVo;
29
+import com.lframework.xingyun.basedata.vo.product.retail.CreateProductRetailVo;
30
+import com.lframework.xingyun.basedata.vo.product.sale.CreateProductSaleVo;
31
+import java.util.ArrayList;
32
+import java.util.HashMap;
33
+import java.util.List;
34
+import java.util.Map;
35
+
36
+import com.lframework.xingyun.sc.service.sale.SaleOrderService;
37
+import lombok.extern.slf4j.Slf4j;
38
+import org.codehaus.groovy.tools.StringHelper;
39
+import org.springframework.beans.factory.annotation.Autowired;
40
+import org.springframework.boot.autoconfigure.cache.CacheProperties;
41
+
42
+@Slf4j
43
+ public class SaleHeadImportListener extends AnalysisEventListener<Map<Integer, String>> {
44
+  //定义每多少条数据进行数据库保存
45
+  private static final int BATCH_COUNT = 128;
46
+
47
+  private SaleOrderService saleOrderService;
48
+  //建一个errMessage集合保存校验有问题的结果
49
+  private List<String> errMessage;
50
+  //用list集合保存解析到的结果
51
+  private List<Map<Integer, Map<Integer, String>>> list;
52
+
53
+
54
+  //重构,把传来的值赋给对应的属性
55
+  public SaleHeadImportListener(SaleOrderService saleOrderService ) {
56
+
57
+    this.saleOrderService = saleOrderService;
58
+    list = new ArrayList<>();
59
+    errMessage = new ArrayList<>();
60
+  }
61
+
62
+  /**
63
+   * 重写invokeHeadMap方法,获去表头,如果有需要获取第一行表头就重写这个方法,不需要则不需要重写
64
+   *
65
+   * @param headMap Excel每行解析的数据为Map<Integer, String>类型,Integer是Excel的列索引,String为Excel的单元格值
66
+   * @param context context能获取一些东西,比如context.readRowHolder().getRowIndex()为Excel的行索引,表头的行索引为0,0之后的都解析成数据
67
+   */
68
+  @Override
69
+  public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
70
+   // logger.info("解析到一条头数据:{}, currentRowHolder: {}", headMap.toString(), context.readRowHolder().getRowIndex());
71
+    Map<Integer, Map<Integer, String>> map = new HashMap<>();
72
+    map.put(context.readRowHolder().getRowIndex(), headMap);
73
+    list.add(map);
74
+  }
75
+
76
+  /**
77
+   * 重写invoke方法获得除Excel第一行表头之后的数据,
78
+   * 如果Excel第二行也是表头,那么也会解析到这里,如果不需要就通过判断context.readRowHolder().getRowIndex()跳过
79
+   *
80
+   * @param data    除了第一行表头外,数据都会解析到这个方法
81
+   * @param context 和上面解释一样
82
+   */
83
+  @Override
84
+  public void invoke(Map<Integer, String> data, AnalysisContext context) {
85
+
86
+    Map<Integer, Map<Integer, String>> map = new HashMap<>();
87
+    map.put(context.readRowHolder().getRowIndex(), data);
88
+    list.add(map);
89
+    // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
90
+    if (list.size() >= BATCH_COUNT) {
91
+      saveData();
92
+      // 存储完成清理 list
93
+      list.clear();
94
+    }
95
+  }
96
+
97
+  /**
98
+   * 解析到最后会进入这个方法,需要重写这个doAfterAllAnalysed方法,然后里面调用自己定义好保存方法
99
+   * @param context
100
+   */
101
+  @Override
102
+  public void doAfterAllAnalysed(AnalysisContext context) {
103
+    // 这里也要保存数据,确保最后遗留的数据也存储到数据库
104
+    saveData();
105
+
106
+  }
107
+
108
+  /**
109
+   * 加上存储数据库
110
+   */
111
+  private void saveData() {
112
+
113
+    //用errMessage集合保存zwmxzService.save()返回校验错误信息,根据自己需要来写就行了
114
+   // errMessage.addAll(productService.saveexcel(list, ztId, year, month, errMessage.size() > 0));
115
+    errMessage.addAll(saleOrderService.saveexcel(list,errMessage.size() > 0));
116
+  }
117
+
118
+
119
+
120
+  public SaleOrderService getSaleOrderService() {
121
+    return saleOrderService;
122
+  }
123
+
124
+  public void setSaleOrderService(SaleOrderService saleOrderService) {
125
+    this.saleOrderService = saleOrderService;
126
+  }
127
+
128
+  public List<String> getErrMessage() {
129
+    return errMessage;
130
+  }
131
+
132
+  public void setErrMessage(List<String> errMessage) {
133
+    this.errMessage = errMessage;
134
+  }
135
+
136
+  public List<Map<Integer, Map<Integer, String>>> getList() {
137
+    return list;
138
+  }
139
+
140
+  public void setList(List<Map<Integer, Map<Integer, String>>> list) {
141
+    this.list = list;
142
+  }
143
+
144
+
145
+}