Просмотр исходного кода

[会员导入][修改]:异步快速返回前端结果

release_toaliyun_real
Stormeye Wu 7 лет назад
Родитель
Сommit
551968c3e2
2 измененных файлов: 129 добавлений и 81 удалений
  1. +117
    -0
      mallinkAdmin/src/main/java/com/iformall/controller/AsyncTask.java
  2. +12
    -81
      mallinkAdmin/src/main/java/com/iformall/controller/WxCUserBasicInfoController.java

+ 117
- 0
mallinkAdmin/src/main/java/com/iformall/controller/AsyncTask.java Просмотреть файл

@@ -0,0 +1,117 @@
package com.iformall.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.afterturn.easypoi.handler.impl.ExcelDataHandlerDefaultImpl;
import cn.afterturn.easypoi.handler.inter.IExcelDataHandler;
import com.iformall.domain.po.MallUserInfo;
import com.iformall.domain.po.WxTags;
import com.iformall.domain.vo.CUserBaseInfoT;
import com.iformall.service.WxCUserBasicInfoService;
import com.iformall.service.WxTagsService;
import com.iformall.shiro.UserSession;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Component
public class AsyncTask {
private final Logger logger = LoggerFactory.getLogger(this.getClass());

@Autowired
private WxCUserBasicInfoService wxCUserBasicInfoService;

@Autowired
private WxTagsService wxTagsService;

@Autowired
StringRedisTemplate stringRedisTemplate;

private class UserExcelHandler extends ExcelDataHandlerDefaultImpl<CUserBaseInfoT> {
@Override
public Object importHandler(CUserBaseInfoT obj, String name, Object value) {
if (value == null) {
value = "";
}
System.out.println(name + " + " + value.toString());
return super.importHandler(obj, name, value);
}

}

@Async
public void importExcelData(MultipartFile file, MallUserInfo user, String importKey, String tenantId) {
ImportParams params = new ImportParams();
// 需要验证
params.setImportFields(new String[]{"姓名", "性别", "手机号", "微信昵称", "学历", "生日", "地址", "上次活跃时间", "注册时间", "标签", "成长值"});
IExcelDataHandler<CUserBaseInfoT> handler = new AsyncTask.UserExcelHandler();
handler.setNeedHandlerFields(new String[] { "手机号" });
params.setNeedVerify(true);

ExcelImportResult<CUserBaseInfoT> datalist = null;

try {
datalist = ExcelImportUtil.importExcelMore(file.getInputStream(), CUserBaseInfoT.class, params);
} catch (Exception e) {
stringRedisTemplate.opsForHash().put(importKey, "allCount", "1");
stringRedisTemplate.opsForHash().put(importKey, "allSuccessCount", "0");
stringRedisTemplate.opsForHash().put(importKey, "processCount", "0");
stringRedisTemplate.opsForHash().put(importKey, "failCount", "1");
logger.error(e.getMessage());
return;
}

if(datalist == null) {
logger.error("导入模板失败: 模板数据解析失败");
stringRedisTemplate.opsForHash().put(importKey, "allCount", "1");
stringRedisTemplate.opsForHash().put(importKey, "allSuccessCount", "0");
stringRedisTemplate.opsForHash().put(importKey, "processCount", "0");
stringRedisTemplate.opsForHash().put(importKey, "failCount", "1");
return;
}

List<CUserBaseInfoT> successList = datalist.getList();
List<CUserBaseInfoT> failList = datalist.getFailList();

int total = successList.size() + failList.size();
int all_success = successList.size();
int all_fail = failList.size();

//添加到redis里
stringRedisTemplate.opsForHash().put(importKey, "allCount", "" + total);
stringRedisTemplate.opsForHash().put(importKey, "allSuccessCount", "" + all_success);
stringRedisTemplate.opsForHash().put(importKey, "processCount", "0");
stringRedisTemplate.opsForHash().put(importKey, "failCount", "" + all_fail);

logger.info("验证通过的数量: " + successList.size());
logger.info("验证未通过的数量: " + failList.size());

WxTags wxTagsQ = new WxTags();
List<WxTags> tagList = wxTagsService.findList(wxTagsQ);

try {
successList.parallelStream().forEach(uBase -> {
// 异步无法获取到原有的session,所以再注入一下session
Session session = SecurityUtils.getSubject().getSession();
session.setAttribute(UserSession.userInfo, user);
session.setAttribute(UserSession.userId, user.getId());
session.setAttribute(UserSession.tenantId, tenantId);

wxCUserBasicInfoService.importOneMem(tenantId, importKey, tagList, uBase);
});
} catch (Exception e) {
stringRedisTemplate.opsForHash().put(importKey, "allCount", "" + all_fail);
e.printStackTrace();
logger.error("导入模板失败:"+e.getMessage());
}
}
}

+ 12
- 81
mallinkAdmin/src/main/java/com/iformall/controller/WxCUserBasicInfoController.java Просмотреть файл

@@ -1,10 +1,5 @@
package com.iformall.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.afterturn.easypoi.handler.impl.ExcelDataHandlerDefaultImpl;
import cn.afterturn.easypoi.handler.inter.IExcelDataHandler;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
@@ -13,19 +8,14 @@ import com.iformall.common.ErrorCode;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.*;
import com.iformall.domain.vo.CUserBaseInfoT;
import com.iformall.enums.EnumAssignTagsTrigger;
import com.iformall.exception.MallinkException;
import com.iformall.service.*;
import com.iformall.service.impl.WxCUserBasicInfoServiceImpl;
import com.iformall.shiro.UserSession;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -38,6 +28,7 @@ import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@@ -69,12 +60,14 @@ public class WxCUserBasicInfoController extends BaseController {
@Autowired
private WxLevelConfigService wxLevelConfigService;

@Autowired
private WxTagsService wxTagsService;


@Autowired
StringRedisTemplate stringRedisTemplate;

@Autowired
private AsyncTask asyncTask;

private void setUserInfoLevel(WxCUserBasicInfo info) {

if (info.getPoins() == null || info.getPoins() == 0) {
@@ -265,17 +258,7 @@ public class WxCUserBasicInfoController extends BaseController {

}

private class UserExcelHandler extends ExcelDataHandlerDefaultImpl<CUserBaseInfoT> {
@Override
public Object importHandler(CUserBaseInfoT obj, String name, Object value) {
if (value == null) {
value = "";
}
System.out.println(name + " + " + value.toString());
return super.importHandler(obj, name, value);
}

}

@RequestMapping("/importTemplate")
public ResultData importTemplate(@RequestParam("file") MultipartFile file) {
@@ -287,80 +270,28 @@ public class WxCUserBasicInfoController extends BaseController {
final MallUserInfo user = getUser();
String userId = "" + user.getId();
String importKey = importMemPrev + userId;
final String tenantId = getTenantId();

//查询当前用户得到的值是否为空,为空继续,不为空,返回模板正在导入
Boolean allCount = stringRedisTemplate.opsForHash().hasKey(importKey, "allCount");
if (allCount) {
return new ResultData(Result.SUCCESS, "模板正在导入");
}

stringRedisTemplate.opsForHash().putIfAbsent(importKey, "allCount", 0 + "");
stringRedisTemplate.opsForHash().putIfAbsent(importKey, "allSuccessCount", 0 + "");
stringRedisTemplate.opsForHash().putIfAbsent(importKey, "processCount", "");
stringRedisTemplate.opsForHash().putIfAbsent(importKey, "failCount", "");

asyncTask.importExcelData(file, user, importKey, tenantId);

ImportParams params = new ImportParams();
// 需要验证
params.setImportFields(new String[]{"姓名", "性别", "手机号", "微信昵称", "学历", "生日", "地址", "上次活跃时间", "注册时间", "标签", "成长值"});
IExcelDataHandler<CUserBaseInfoT> handler = new UserExcelHandler();
handler.setNeedHandlerFields(new String[] { "手机号" });
params.setNeedVerify(true);

ExcelImportResult<CUserBaseInfoT> datalist = null;
try {
datalist = ExcelImportUtil.importExcelMore(file.getInputStream(), CUserBaseInfoT.class, params);
} catch (Exception e) {
stringRedisTemplate.opsForHash().put(importKey, "allCount", "1");
stringRedisTemplate.opsForHash().put(importKey, "allSuccessCount", "0");
stringRedisTemplate.opsForHash().put(importKey, "processCount", "0");
stringRedisTemplate.opsForHash().put(importKey, "failCount", "1");
logger.error(e.getMessage());
return new ResultData(ErrorCode.MEM_IMPORT_ERR);
}

if(datalist == null) {
logger.error("导入模板失败: 模板数据解析失败");
return new ResultData(ErrorCode.MEM_IMPORT_ERR);
}

List<CUserBaseInfoT> successList = datalist.getList();
List<CUserBaseInfoT> failList = datalist.getFailList();

int total = successList.size() + failList.size();
int all_success = successList.size();
int all_fail = failList.size();

//添加到redis里
stringRedisTemplate.opsForHash().put(importKey, "allCount", "" + total);
stringRedisTemplate.opsForHash().put(importKey, "allSuccessCount", "" + all_success);
stringRedisTemplate.opsForHash().put(importKey, "processCount", "0");
stringRedisTemplate.opsForHash().put(importKey, "failCount", "" + all_fail);
stringRedisTemplate.expire(importKey,30,TimeUnit.MINUTES);
return new ResultData(Result.SUCCESS, "模板正在导入");
}

logger.info("验证通过的数量: " + successList.size());
logger.info("验证未通过的数量: " + failList.size());

WxTags wxTagsQ = new WxTags();
List<WxTags> tagList = wxTagsService.findList(wxTagsQ);

final String tenantId = getTenantId();

try {
successList.parallelStream().forEach(uBase -> {
// 异步无法获取到原有的session,所以再注入一下session
Session session = SecurityUtils.getSubject().getSession();
session.setAttribute(UserSession.userInfo, user);
session.setAttribute(UserSession.userId, user.getId());
session.setAttribute(UserSession.tenantId, tenantId);

wxCUserBasicInfoService.importOneMem(tenantId, importKey, tagList, uBase);
});
} catch (Exception e) {
stringRedisTemplate.opsForHash().put(importKey, "allCount", "" + all_fail);
e.printStackTrace();
logger.error("导入模板失败:"+e.getMessage());
}
stringRedisTemplate.expire(importKey,30,TimeUnit.MINUTES);
return new ResultData(Result.SUCCESS, "模板正在导入");
}

@GetMapping("/queryTemplateCount")
public ResultData queryTemplateCount() {


Загрузка…
Отмена
Сохранить