当前位置:网站首页>多线程导入数据并生成错误文件用redis存储
多线程导入数据并生成错误文件用redis存储
2022-07-27 15:54:00 【@淡 定】
导入excel文件:
@Override
@Transactional(rollbackFor = Exception.class)
public Object importShopBlack(MultipartFile file, HttpServletResponse response, long userId) {
try {
//定义总数为0;
int coun = 0;
int fai = 0;
Map<Object, Object> map1 = new HashMap<>();
ArrayList<FwdcBlacklist> succlist = new ArrayList<>();
List<Map<String,String>> errorList = new ArrayList<>();
List<String> shopIdUpdate = new ArrayList<>();
String fileName = file.getOriginalFilename();
if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
return ResultDataUtils.failResult("上传文件格式不正确");
}
InputStream is = file.getInputStream();
Workbook wb = null;
wb = new HSSFWorkbook(is);
Sheet sheet = wb.getSheetAt(0);
if (sheet == null) {
throw new RuntimeException("文件数据错误");
}
coun=sheet.getLastRowNum();
ExecutorService pool=new ThreadPoolExecutor(5,10,
1L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(3),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
final CountDownLatch endGate = new CountDownLatch(coun);
ArrayList<Map<String, String>> maps = new ArrayList<>();
for (int r = 1; r <= sheet.getLastRowNum(); r++) {
Row row = sheet.getRow(r);
if (isRowEmpty(row)) {
continue;
}
//获取excel中数据
Map<String,String> map = new HashMap<>();
map.put("sdmerid",getRowValue(row,0));
map.put("shopName",getRowValue(row,1));
map.put("creditCode",getRowValue(row,2));
map.put("idcard",getRowValue(row,3));
map.put("isOffline",getRowValue(row,4));
map.put("isDraw",getRowValue(row,5));
map.put("isNet",getRowValue(row,6));
maps.add(map);
}
pool.submit(()->{
for (Map<String, String> map : maps) {
try {
//开始判断数据是否正确
//第一步判断是否填写营业执照编号或者身份证号,二者必填其一
if (StrUtil.isEmpty(map.get("creditCode")) && StrUtil.isEmpty(map.get("idcard"))){
map.put("error","营业执照编号或者身份证号必填一项");
errorList.add(map);
continue;
}
//第二步判断是否下线,是否屏蔽金融,是否允许再次入网三项是否填写
if (StrUtil.isEmpty(map.get("isOffline")) || StrUtil.isEmpty(map.get("isDraw")) || StrUtil.isEmpty(map.get("isNet"))){
map.put("error","是否下线,是否屏蔽金融,是否允许再次入网三项必须填写");
errorList.add(map);
continue;
}
//三四五步的查询sql,判断有没有填写商编,没有填写商编不进入下面判断
List<Map<String,Object>> shopDate=new ArrayList<>();
shopDate=fwdcBlacklistMapper.getShopDate(map.get("creditCode"),map.get("idcard"),null);
if (StrUtil.isNotEmpty(map.get("sdmerid"))){
shopDate=fwdcBlacklistMapper.getShopDate(null,null,map.get("sdmerid"));
if (CollUtil.isNotEmpty(shopDate)){
//第四步如果填写身份证判断身份证是否和商编匹配
if (StrUtil.isNotEmpty(map.get("idcard")) && StrUtil.isNotEmpty(map.get("sdmerid")) && !(map.get("idcard").equals(shopDate.get(0).get("cardid")))){
map.put("error","身份证与商编不匹配");
errorList.add(map);
continue;
}
//第三步如果填写营业执照判断营业执照是否和商编匹配
if (StrUtil.isNotEmpty(map.get("creditCode")) && StrUtil.isNotEmpty(map.get("sdmerid")) && !(map.get("creditCode").equals(shopDate.get(0).get("creditcode")))){
map.put("error","营业执照与商编不匹配");
errorList.add(map);
continue;
}
//第五步商编与商户名称匹配
if (StrUtil.isNotEmpty(map.get("shopName")) && StrUtil.isNotEmpty(map.get("sdmerid")) && !(map.get("shopName").equals(shopDate.get(0).get("shopName")))){
map.put("error","商编与商户名称不匹配");
errorList.add(map);
continue;
}
}else {
map.put("error","商编不存在");
errorList.add(map);
continue;
}
}
// errorList.add(map);
//组装屏蔽内容
ArrayList<String> arr = new ArrayList<>();
if (StrUtil.isNotEmpty(map.get("isOffline")) && map.get("isOffline").toString().equals("是")){
arr.add("1");
}
if (StrUtil.isNotEmpty(map.get("isDraw"))&& map.get("isDraw").toString().equals("是")){
arr.add("2");
}
if (StrUtil.isNotEmpty(map.get("isNet"))&& map.get("isNet").toString().equals("否")){
arr.add("3");
}
if(arr == null || arr.size()<= 0){
map.put("error","屏蔽功能不能都无效");
errorList.add(map);
continue;
}
//插入数据
//通过shopid去查询所关联的商户信息,同时该商户不在黑名单表中
if (CollUtil.isNotEmpty(shopDate)){
for (Map<String, Object> selectByShopId : shopDate) {
FwdcBlacklist fwdcBlacklist = new FwdcBlacklist();
fwdcBlacklist.setShopId(Long.valueOf(selectByShopId.get("shopId").toString()));
fwdcBlacklist.setId(IdUtil.getSnowflake().nextIdStr());
fwdcBlacklist.setShieldFunction(String.join(",",arr));
fwdcBlacklist.setRealShieldFunction(String.join(",",arr));
fwdcBlacklist.setAddTime(new Date());
fwdcBlacklist.setAddUserId(userId);
fwdcBlacklist.setCreditCode(selectByShopId.get("creditcode")==null?"":selectByShopId.get("creditcode").toString());
fwdcBlacklist.setIdcard(selectByShopId.get("cardid")==null?"":selectByShopId.get("cardid").toString());
fwdcBlacklist.setSdmerid(selectByShopId.get("sdmerid")==null?"":selectByShopId.get("sdmerid").toString());
fwdcBlacklist.setShopName(selectByShopId.get("shopName")==null?"":selectByShopId.get("shopName").toString());
fwdcBlacklist.setUpdateTime(new Date());
fwdcBlacklist.setUpdateUserId(userId);
succlist.add(fwdcBlacklist);
//添加商户到下线数组中
if(arr.contains("1")){
shopIdUpdate.add(fwdcBlacklist.getShopId().toString());
}
}
}else if (StrUtil.isNotBlank(map.get("idcard"))){
FwdcBlacklist fwdcBlacklist = new FwdcBlacklist();
fwdcBlacklist.setId(IdUtil.getSnowflake().nextIdStr());
fwdcBlacklist.setShieldFunction(String.join(",",arr));
fwdcBlacklist.setRealShieldFunction(String.join(",",arr));
fwdcBlacklist.setAddTime(new Date());
fwdcBlacklist.setAddUserId(userId);
fwdcBlacklist.setIdcard(map.get("idcard"));
fwdcBlacklist.setUpdateTime(new Date());
fwdcBlacklist.setUpdateUserId(userId);
succlist.add(fwdcBlacklist);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
endGate.countDown();
}
}
return errorList;
});
try {
endGate.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pool.shutdown();
String error = JSON.toJSONString(errorList);
//判断失败次数大于0,就生成失败表,不导入数据
if (coun-succlist.size()>0){
//用redis缓存失败数据
String shopBlackErrorFile = "shopBlack"+userId;
redisUtil.set(shopBlackErrorFile, error ); // 没有新增返回true,存在返回false
}else {
// 没有失败的数据则进行数据插入,
for (FwdcBlacklist fwdcBlacklist : succlist) {
baseMapper.insertFwdcBlacklist(fwdcBlacklist);
}
}
//插入suc,失败条数fai
map1.put("suc", coun);
map1.put("fai", coun-succlist.size());
map1.put("addUserId", userId);
return ResultDataUtils.successResult(map1);
} catch (Exception e) {
logger.info("【{}批量导入黑名单商户信息失败!】", "");
return ResultDataUtils.failResult(e.getMessage());
}
}
/** * 获取行列值 **/
public static String getRowValue(Row row, int i) {
if (row.getCell(i) == null) {
return null;
}
row.getCell(i).setCellType(Cell.CELL_TYPE_STRING);
return row.getCell(i) == null || row.getCell(i).toString().isEmpty() ? null
: row.getCell(i).getStringCellValue();
}
导出错误文件:
/** * 黑名单错误信息表导出 * * @param request * @return */
@RequestMapping("/errorFile")
public void errMsgExportExcel(HttpServletRequest request, HttpServletResponse response) {
logger.info("【黑名单错误信息表导入】导出错误信息开始......");
try {
Map<String, Object> map = new HashMap<>();
long startTime = System.currentTimeMillis();
//组装redis键
String addUserId = request.getParameter("addUserId") == null ? null : request.getParameter("addUserId");
String keyRedis="shopBlack"+addUserId;
List<Map> list =null;
if (redisUtil.hasKey(keyRedis)){
list = JSONArray.parseArray(redisUtil.get(keyRedis).toString(), Map.class);
}
HSSFWorkbook wb = new HSSFWorkbook();
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFSheet sheet = wb.createSheet("错误信息表");
for (int i = 0; i <= 22; i++) {
sheet.setColumnWidth(i, 20 * 256);
}
HSSFRow rowNext = null;
HSSFRow row = sheet.createRow(0);
row.setHeight((short) (26.25 * 20));
// 设置列名
row.createCell(0).setCellValue("易生商编");
row.createCell(1).setCellValue("商户名称");
row.createCell(2).setCellValue("营业执照编号");
row.createCell(3).setCellValue("身份证号");
row.createCell(4).setCellValue("是否下线");
row.createCell(5).setCellValue("是否屏蔽金融");
row.createCell(6).setCellValue("是否允许再次入网");
row.createCell(7).setCellValue("错误信息");
if (CollUtil.isNotEmpty(list) && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
rowNext = sheet.createRow(i + 1);
rowNext.setHeight((short) (26.25 * 15));
rowNext.createCell(0).setCellValue(
list.get(i).get("sdmerid") == null || list.get(i).get("sdmerid") == "" ? null
: list.get(i).get("sdmerid").toString());
rowNext.createCell(1).setCellValue(
list.get(i).get("shopName") == null || list.get(i).get("shopName") == "" ? null
: list.get(i).get("shopName").toString());
rowNext.createCell(2).setCellValue(
list.get(i).get("creditCode") == null || list.get(i).get("creditCode") == "" ? null
: list.get(i).get("creditCode").toString());
rowNext.createCell(3).setCellValue(
list.get(i).get("idcard") == null || list.get(i).get("idcard") == "" ? null
: list.get(i).get("idcard").toString());
rowNext.createCell(4).setCellValue(
list.get(i).get("isOffline") == null || list.get(i).get("isOffline") == "" ? null
: list.get(i).get("isOffline").toString());
rowNext.createCell(5).setCellValue(
list.get(i).get("isDraw") == null || list.get(i).get("isDraw") == "" ? null
: list.get(i).get("isDraw").toString());
rowNext.createCell(6).setCellValue(
list.get(i).get("isNet") == null || list.get(i).get("isNet") == "" ? null
: list.get(i).get("isNet").toString());
rowNext.createCell(7).setCellValue(
list.get(i).get("error") == null || list.get(i).get("error") == "" ? null
: list.get(i).get("error").toString());
}
}
redisUtil.del(keyRedis);
response.setContentType("application/vnd.ms-excel;charset=utf-8");
OutputStream os = response.getOutputStream();
response.setHeader("Content-disposition",
"attachment;filename=" + DownChineseEncode.setFileDownHeader(request, response, "错误信息表.xlsx"));
wb.write(os);
os.flush();
os.close();
long endTime = System.currentTimeMillis();
// 删除redis缓存
logger.info("【黑名单错误信息表导入派发】导出错误信息结束,耗时:【{}】", (endTime - startTime));
} catch (Exception e) {
logger.error("【黑名单错误信息表导入派发】导出错误信息异常:【{}】", e);
}
}
边栏推荐
- 【Codeforces】 B. Make it Divisible by 25
- golang worker池
- 知物由学 | APP大瘦身,新一代AAB框架下的安全加固之道
- likeshop外卖点餐系统「100%开源无加密」
- 【单片机】2.1 AT89S52单片机的硬件组成
- Some suggestions for writing original technical articles
- 年终总结模板
- [introduction to database system (Wang Shan)] Chapter 11 concurrency control
- Likeshop takeout ordering system "100% open source without encryption"
- Understand │ what is cross domain? How to solve cross domain problems?
猜你喜欢

Count the six weapons of the domestic interface cooperation platform!

Are those who are absent from the written examination shortlisted for the teacher recruitment interview? Henan Xiangfu: the statistics of individual candidates' scores are wrong

Know things by learning | build a real-time anti plug-in mechanism from 0 to 1 to supplement the offensive and defensive power of mobile games in multiple dimensions

Big gap? Requirements and conditions for candidates with different academic qualifications to take the postgraduate entrance examination

How to develop an online Excel spreadsheet system (Part 1)

Yanrong technology was selected as Beijing's "specialized and innovative" in 2022 to lead hybrid cloud file storage

机器学习——概念理解之IoU

Soul 1: why is es more suitable for complex condition search than MySQL?

【单片机】2.3 AT89S52的CPU

Learn from what you know | Yidun self-developed text real-time clustering technology, and wipe out the same kind of harmful content in social networks
随机推荐
Taishan Office Technology Lecture: word strange paragraph borders
微信小程序 云函数批量删除多条数据 Error: errCode: -502005 database collection not exists
面试好难啊!蚂蚁金服的六轮面试我是强撑过来!差点OUT(面试复盘)
查找表中多余重复记录并删除保留最小一个
Machine learning: IOU of concept understanding
Personal understanding of convolution calculation process of convolution neural network
Count the six weapons of the domestic interface cooperation platform!
Configuration and basic use of vim
[single chip microcomputer] 2.1 hardware composition of AT89S52 single chip microcomputer
Activate genuine win7 ultimate system with slmgr command
MLX90640 红外热成像仪测温传感器模块开发笔记(七)
卷积神经网络——从R-CNN,Fast R-CNN到Faster R-CNN,Mask R-CNN
【cf】#681 A. Kids Seating (Div. 2, based on VK Cup 2019-2020 - Final)
Convolutional neural network -- Translation of yolov2 (yolo9000) papers
2022 safety officer-a certificate examination questions and online simulation examination
帮扶、提振、担当,第六届土巴兔718全民家装节的新价值和意义
Convolutional neural network -- from r-cnn, fast r-cnn to fast r-cnn, mask r-cnn
Knowing things by learning | app slimming down, the way of safety reinforcement under the new generation AAB framework
【Codeforces】 B. Make it Divisible by 25
卷积神经网络——YOLOV2(YOLO9000)论文翻译