提交 77ab6228 authored 作者: 李秋林's avatar 李秋林

添加工具类:读取excel;更新RedisKey;返回R对象修改;Long对象处理

上级 d30316f4
...@@ -5,14 +5,14 @@ ...@@ -5,14 +5,14 @@
<parent> <parent>
<groupId>com.wangxiaolu</groupId> <groupId>com.wangxiaolu</groupId>
<artifactId>wangxiaolu-promotion-parent</artifactId> <artifactId>wangxiaolu-promotion-parent</artifactId>
<version>0.0.1</version> <version>0.0.2</version>
</parent> </parent>
<groupId>com.wangxiaolu</groupId> <groupId>com.wangxiaolu</groupId>
<artifactId>wangxiaolu-promotion-common</artifactId> <artifactId>wangxiaolu-promotion-common</artifactId>
<version>0.0.1</version> <version>0.0.2</version>
<name>wangxiaolu-promotion-common</name> <name>wangxiaolu-promotion-common</name>
<description>wangxiaolu-promotion-common</description> <description>wangxiaolu-promotion-common</description>
...@@ -113,6 +113,21 @@ ...@@ -113,6 +113,21 @@
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi-ooxml.version}</version>
</dependency>
<!-- 读取大量excel数据时使用 -->
<!-- https://mvnrepository.com/artifact/com.monitorjbl/xlsx-streamer -->
<dependency>
<groupId>com.monitorjbl</groupId>
<artifactId>xlsx-streamer</artifactId>
<version>${xlsx-streamer.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package com.wangxiaolu.promotion.common.excel;
import cn.hutool.core.date.DatePattern;
import com.wangxiaolu.promotion.exception.DataException;
import com.wangxiaolu.promotion.result.basedata.RCode;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;
/**
* @author : liqiulin
* @date : 2024-08-16 15
* @describe :读取Excel
*/
public class ReadExcelUtils {
private Logger logger = LoggerFactory.getLogger(ReadExcelUtils.class);
private Workbook wb;
private Sheet sheet;
private Row row;
public ReadExcelUtils(String filepath) {
if (filepath == null) {
throw new DataException(RCode.READ_FILE_PATH_NULL_ERROR);
}
String ext = filepath.substring(filepath.lastIndexOf("."));
try {
InputStream is = new FileInputStream(filepath);
if (".xls".equals(ext)) {
wb = new HSSFWorkbook(is);
} else if (".xlsx".equals(ext)) {
wb = new XSSFWorkbook(is);
} else {
throw new DataException(RCode.EXCEL_IS_XLS_OR_XLSX_FILE);
}
} catch (FileNotFoundException e) {
logger.error("FileNotFoundException", e);
throw new DataException(RCode.READ_EXCEL_NULL_ERROR);
} catch (IOException e) {
logger.error("IOException", e);
throw new DataException(RCode.READ_EXCEL_NULL_ERROR);
}
}
/**
* 读取Excel表格表头的内容
*
* @return String 表头内容的数组
* @author zengwendong
*/
public String[] readTitle() {
if (wb == null) {
throw new DataException("Workbook对象为空!");
}
sheet = wb.getSheetAt(0);
row = sheet.getRow(0);
// 标题总列数
int colNum = row.getPhysicalNumberOfCells();
System.out.println("colNum:" + colNum);
String[] title = new String[colNum];
for (int i = 0; i < colNum; i++) {
// title[i] = getStringCellValue(row.getCell((short) i));
title[i] = row.getCell(i).getStringCellValue();
}
return title;
}
/**
* 读取Excel数据内容
*
* @return Map 包含单元格数据内容的Map对象
* @author zengwendong
*/
public Map<Integer, List<Object>> readContent(){
if (wb == null) {
throw new DataException("Workbook对象为空!");
}
sheet = wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum();
Map<Integer, List<Object>> content = new HashMap<>(rowNum * 2);
row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells();
// 正文内容应该从第二行开始,第一行为表头的标题
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
int j = 0;
List<Object> cellValues = new ArrayList<>(rowNum * 2);
while (j < colNum) {
Object obj = getCellFormatValue(row.getCell(j));
cellValues.add(obj);
j++;
}
// 如果row没有值,则停止解析
Set vset = new HashSet(cellValues);
if (vset.size() <= 1){
return content;
}
content.put(i, cellValues);
}
return content;
}
/**
* 根据Cell类型设置数据
*
* @param cell
* @return
* @author zengwendong
*/
private Object getCellFormatValue(Cell cell) {
Object cellvalue = "";
//判断是否为null或空串
if (cell == null || cell.toString().trim().equals("")) {
return "";
}
CellType cellType = cell.getCellType();
switch (cellType) {
case STRING:
cellvalue = cell.getStringCellValue();
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
cellvalue = cell.getDateCellValue();
} else {
BigDecimal number = new BigDecimal(cell.getNumericCellValue());
cellvalue = number.toPlainString();
}
break;
default:
return "";
}
return cellvalue;
}
}
\ No newline at end of file
...@@ -16,6 +16,10 @@ public interface RedisKeys { ...@@ -16,6 +16,10 @@ public interface RedisKeys {
* 促销员小程序用户登录信息:token * 促销员小程序用户登录信息:token
*/ */
TEMPORARY_TOKEN("user:login_token:temporary:"), TEMPORARY_TOKEN("user:login_token:temporary:"),
/**
* 组织数据 - 客户类数据 - 经销商
*/
DEALER_HAVE_LIST("user:clientele:dealer_all"),
; ;
String key; String key;
...@@ -42,7 +46,7 @@ public interface RedisKeys { ...@@ -42,7 +46,7 @@ public interface RedisKeys {
@Getter @Getter
enum ExportKeys { enum ExportKeys {
/** /**
* 促销员暂存上报记录单元 * 导出
*/ */
ACTIVITY_REPORTED_PUSH_FEISHU_SHEET("export:activity_feishu:sheet_row_num:sheet-"), ACTIVITY_REPORTED_PUSH_FEISHU_SHEET("export:activity_feishu:sheet_row_num:sheet-"),
FEISHU_TENANT_TOKEN_ACTIVITY_ROBOT_1("export:feishu_tenant_token:activity_robot_1"), FEISHU_TENANT_TOKEN_ACTIVITY_ROBOT_1("export:feishu_tenant_token:activity_robot_1"),
......
...@@ -3,10 +3,15 @@ package com.wangxiaolu.promotion.common.redis.service; ...@@ -3,10 +3,15 @@ package com.wangxiaolu.promotion.common.redis.service;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations; import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@Component @Component
...@@ -89,6 +94,25 @@ public class RedisCache { ...@@ -89,6 +94,25 @@ public class RedisCache {
return JSONObject.parseArray(val); return JSONObject.parseArray(val);
} }
public void putAllHash(String key, Map<Object,Object> values) {
redisTemplate.opsForHash().putAll(key, values);
}
public String getHash(String key,String hashKey){
Object o = redisTemplate.opsForHash().get(key, hashKey);
return o.toString();
}
public Map<Object, Object> getAllHash(String key){
Map<Object, Object> entries = redisTemplate.opsForHash().entries(key);
return entries;
}
public Set<Object> getHashKeys(String key){
Set<Object> keys = redisTemplate.opsForHash().keys(key);
return keys;
}
/** /**
* 获取一个值,并将val json化 * 获取一个值,并将val json化
*/ */
...@@ -101,6 +125,7 @@ public class RedisCache { ...@@ -101,6 +125,7 @@ public class RedisCache {
} }
private String valToJson(Object o) { private String valToJson(Object o) {
return JSONObject.toJSONString(o); return JSONObject.toJSONString(o);
} }
......
package com.wangxiaolu.promotion.common.util; package com.wangxiaolu.promotion.common.util;
import com.wangxiaolu.promotion.exception.DataException;
import com.wangxiaolu.promotion.result.basedata.RCode;
import java.util.Objects;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
...@@ -16,4 +20,12 @@ public class NumberUtils { ...@@ -16,4 +20,12 @@ public class NumberUtils {
Matcher m = pattern.matcher(str); Matcher m = pattern.matcher(str);
return Integer.parseInt(m.replaceAll("").trim()); return Integer.parseInt(m.replaceAll("").trim());
} }
public static boolean notNull(int num){
return num > 0;
}
public static boolean isNull(Long value){
return Objects.isNull(value) || value <= 0L;
}
} }
...@@ -85,4 +85,10 @@ public class R { ...@@ -85,4 +85,10 @@ public class R {
public static R success() { public static R success() {
return new R(); return new R();
} }
public static R fail() {
return new R(RCode.FAILED,null);
}
public static R fail(Object data) {
return new R(RCode.FAILED,data);
}
} }
...@@ -17,6 +17,7 @@ public enum RCode implements StatusCode { ...@@ -17,6 +17,7 @@ public enum RCode implements StatusCode {
FAILED(1001, "请求失败"), FAILED(1001, "请求失败"),
PARAM_ERROR(1002, "参数错误"), PARAM_ERROR(1002, "参数错误"),
RESPONSE_PACK_ERROR(1003, "包装R失败"), RESPONSE_PACK_ERROR(1003, "包装R失败"),
SELECT_PARAMS_ERROR(1004, "查询条件错误"),
/** /**
* 业务统一编码(不分模块) * 业务统一编码(不分模块)
...@@ -28,6 +29,9 @@ public enum RCode implements StatusCode { ...@@ -28,6 +29,9 @@ public enum RCode implements StatusCode {
STATUS_UPDATE_ERROR(2003, "当前状态不可修改"), STATUS_UPDATE_ERROR(2003, "当前状态不可修改"),
DATA_NOT_HAVE_ERROR(2004, "数据不存在"), DATA_NOT_HAVE_ERROR(2004, "数据不存在"),
DATA_TOO_MANY_ERROR(2005, "唯一数据存在多条"), DATA_TOO_MANY_ERROR(2005, "唯一数据存在多条"),
READ_EXCEL_NULL_ERROR(2006, "表格为空或格式错误,请上传正确的文件"),
READ_FILE_PATH_NULL_ERROR(2007, "文件读取失败,未获取到有效地址"),
EXCEL_IS_XLS_OR_XLSX_FILE(2008, "请上传xls或xlsx表格文件"),
/** /**
* user * user
...@@ -78,6 +82,12 @@ public enum RCode implements StatusCode { ...@@ -78,6 +82,12 @@ public enum RCode implements StatusCode {
CLOCK_DATA_NOT_HAVE_ERROR(4022, "打卡记录不存在"), CLOCK_DATA_NOT_HAVE_ERROR(4022, "打卡记录不存在"),
/**
* manage-模块异常
* 5000+
*/
ACTIVITY_PLAN_MONTH_HAS(5000,"本月活动计划已上传,请删除后再次上传"),
/** /**
* 腾讯云 * 腾讯云
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论