diff --git a/mallinkAdmin/src/main/java/com/iformall/controller/WxCUserBasicInfoController.java b/mallinkAdmin/src/main/java/com/iformall/controller/WxCUserBasicInfoController.java index b3870c09e..8629100aa 100644 --- a/mallinkAdmin/src/main/java/com/iformall/controller/WxCUserBasicInfoController.java +++ b/mallinkAdmin/src/main/java/com/iformall/controller/WxCUserBasicInfoController.java @@ -39,6 +39,8 @@ import static com.iformall.domain.po.WxCouponOrder.Field.CreateDate_DESC; public class WxCUserBasicInfoController extends BaseController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); + private String importMemPrev = "importmem/"; + @Autowired private WxCUserBasicInfoService wxCUserBasicInfoService; @@ -223,10 +225,11 @@ public class WxCUserBasicInfoController extends BaseController { } @RequestMapping("/exportData") - public void exportData(HttpServletRequest request, HttpServletResponse response) { + public void exportData(@ModelAttribute WxCUserBasicInfo basicInfo, HttpServletRequest request, HttpServletResponse response) { logger.debug("[" + getIpAddr() + "] WxCUserBasicInfoController::exportData"); - - wxCUserBasicInfoService.exportData(request, response, getTenantId()); + if(basicInfo == null) basicInfo = new WxCUserBasicInfo(); + basicInfo.setTenantId(getTenantId()); + wxCUserBasicInfoService.exportData(basicInfo, request, response); } @@ -242,27 +245,28 @@ public class WxCUserBasicInfoController extends BaseController { public ResultData importTemplate(@RequestParam("file") MultipartFile file) { logger.debug("[" + getIpAddr() + "] WxCUserBasicInfoController::importTemplate"); if (file.isEmpty()) { - throw new MallinkException(500, "上传文件不能为空"); + throw new MallinkException(Result.ERROR, "上传文件不能为空"); } //得到当前用户ID - String userId = getUser().getId().toString(); + String userId = "" + getUser().getId(); + String importKey = importMemPrev + userId; //查询当前用户得到的值是否为空,为空继续,不为空,返回模板正在导入 - Boolean allCount = stringRedisTemplate.opsForHash().hasKey(userId, "allCount"); + Boolean allCount = stringRedisTemplate.opsForHash().hasKey(importKey, "allCount"); if (allCount) { return new ResultData(Result.SUCCESS, "模板正在导入"); } - stringRedisTemplate.opsForHash().putIfAbsent(userId, "allCount", 0 + ""); - stringRedisTemplate.opsForHash().putIfAbsent(userId, "allSuccessCount", 0 + ""); - stringRedisTemplate.opsForHash().putIfAbsent(userId, "successCount", ""); - stringRedisTemplate.opsForHash().putIfAbsent(userId, "failCount", ""); - wxCUserBasicInfoService.importTemplate(file, getTenantId(), userId); - stringRedisTemplate.expire(userId,30,TimeUnit.MINUTES); + stringRedisTemplate.opsForHash().putIfAbsent(importKey, "allCount", 0 + ""); + stringRedisTemplate.opsForHash().putIfAbsent(importKey, "allSuccessCount", 0 + ""); + stringRedisTemplate.opsForHash().putIfAbsent(importKey, "successCount", ""); + stringRedisTemplate.opsForHash().putIfAbsent(importKey, "failCount", ""); + wxCUserBasicInfoService.importTemplate(file, getTenantId(), importKey); + stringRedisTemplate.expire(importKey,30,TimeUnit.MINUTES); return new ResultData(Result.SUCCESS, "模板正在导入"); } @GetMapping("/queryTemplateCount") public ResultData queryTemplateCount() { - String importKey = "import-" + getUser().getId(); + String importKey = importMemPrev + getUser().getId(); Map entries = stringRedisTemplate.opsForHash().entries(importKey); logger.info(JSONArray.toJSONString(entries) + ">>>>>>>>>>>>>>>>>>>>>1"); if (entries.size() < 4) { diff --git a/mallinkService/src/main/java/com/iformall/service/WxCUserBasicInfoService.java b/mallinkService/src/main/java/com/iformall/service/WxCUserBasicInfoService.java index 2fa33087a..0c62edd06 100644 --- a/mallinkService/src/main/java/com/iformall/service/WxCUserBasicInfoService.java +++ b/mallinkService/src/main/java/com/iformall/service/WxCUserBasicInfoService.java @@ -99,11 +99,11 @@ public interface WxCUserBasicInfoService { long findCountByScore(WxCUserBasicInfoDto dto); - void exportData(HttpServletRequest request, HttpServletResponse response, String tenantId); + void exportData(WxCUserBasicInfo basicInfo, HttpServletRequest request, HttpServletResponse response); void exportTemplate(HttpServletRequest request, HttpServletResponse response, String tenantId); - ResultData importTemplate(MultipartFile file, String tenantId, String userId); + ResultData importTemplate(MultipartFile file, String tenantId, String importKey); long findCount(WxCUserBasicInfoDto dto); diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxCUserBasicInfoServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxCUserBasicInfoServiceImpl.java index ff36a341e..d8d84c6aa 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxCUserBasicInfoServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxCUserBasicInfoServiceImpl.java @@ -8,12 +8,12 @@ import cn.afterturn.easypoi.handler.inter.IExcelDataHandler; import com.alibaba.fastjson.JSONObject; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; +import com.iformall.common.ErrorCode; import com.iformall.common.IdWorker; import com.iformall.common.ResultData; import com.iformall.domain.dto.WxCUserBasicInfoDto; import com.iformall.domain.po.WxCUser; import com.iformall.domain.po.WxCUserBasicInfo; -import com.iformall.domain.po.WxCUserTags; import com.iformall.domain.po.WxTags; import com.iformall.domain.vo.CUserBaseInfoT; import com.iformall.domain.vo.CUserBaseVo; @@ -153,10 +153,8 @@ public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService { @Override - public void exportData(HttpServletRequest request, HttpServletResponse response, String tenantId) { - WxCUserBasicInfo basicInfoQ = new WxCUserBasicInfo(); - basicInfoQ.setTenantId(tenantId); - List memberlist = wxCUserBasicInfoMapper.findVoList(basicInfoQ); + public void exportData(WxCUserBasicInfo basicInfo, HttpServletRequest request, HttpServletResponse response) { + List memberlist = wxCUserBasicInfoMapper.findVoList(basicInfo); XSSFWorkbook workbook; String filepath = Constant.fileDirectory; @@ -514,31 +512,55 @@ public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService { @Async @Override - public ResultData importTemplate(MultipartFile file, String tenantId, String userId) { + public ResultData importTemplate(MultipartFile file, String tenantId, String importKey) { final IdWorker idWorker = IdWorker.get(); - String importKey = "import-" + userId; - - //默认添加到redis里 - stringRedisTemplate.opsForHash().put(importKey, "allCount", ""); - stringRedisTemplate.opsForHash().put(importKey, "allSuccessCount", "0"); - stringRedisTemplate.opsForHash().put(importKey, "successCount", "0"); - stringRedisTemplate.opsForHash().put(importKey, "failCount", "0"); - ImportParams params = new ImportParams(); // 需要验证 params.setImportFields(new String[]{"姓名", "性别", "手机号", "微信昵称", "学历", "生日", "地址", "上次活跃时间", "注册时间", "标签", "成长值"}); + IExcelDataHandler handler = new UserExcelHandler(); + handler.setNeedHandlerFields(new String[] { "手机号" }); params.setNeedVerify(true); + ExcelImportResult datalist = null; try { - ExcelImportResult datalist = ExcelImportUtil.importExcelMore(file.getInputStream(), CUserBaseInfoT.class, params); - List successList = datalist.getList(); - List failList = datalist.getFailList(); + 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, "successCount", "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 successList = datalist.getList(); + List failList = datalist.getFailList(); + + int total = successList.size() + failList.size(); + int all_success = successList.size(); + int all_fail = failList.size(); + + WxTags wxTagsQ = new WxTags(); + List tagList = wxTagsMapper.findList(wxTagsQ); + + try { + //添加到redis里 + stringRedisTemplate.opsForHash().put(importKey, "allCount", "" + total); + stringRedisTemplate.opsForHash().put(importKey, "allSuccessCount", "" + all_success); + stringRedisTemplate.opsForHash().put(importKey, "successCount", "0"); + stringRedisTemplate.opsForHash().put(importKey, "failCount", "" + all_fail); + logger.info("验证通过的数量: " + successList.size()); logger.info("验证未通过的数量: " + failList.size()); - successList.parallelStream().forEach(uBase->{ + successList.parallelStream().forEach(uBase -> { if (StringUtils.isBlank(uBase.getPhone())) { logger.error("手机号为空", uBase.toString()); @@ -546,6 +568,7 @@ public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService { } WxCUserBasicInfo userBase = InitUserBaseInfo(uBase, tenantId); if (userBase == null) { + logger.error("userBase为null", uBase.toString()); return; } @@ -568,24 +591,12 @@ public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService { oldUserBase.setUpdateDate(new Date()); oldUserBase.setCreateDate(userBase.getCreateDate()); } else { - // check c_user 是否存在 - WxCUser cUserQ = new WxCUser(); - cUserQ.setTenantId(tenantId); - cUserQ.setPhone(userBase.getPhone()); - List cUsers = wxCUserMapper.findList(cUserQ); - if (cUsers.size() > 0) { - WxCUser wUser = cUsers.get(0); - if (wUser != null) { - userBase.setId(wUser.getId()); - userBase.setNickName(wUser.getNickName()); - } - } else { - userBase.setId(idWorker.nextId()); - } + userBase.setId(idWorker.nextId()); } // update 昵称 - if (oldUserBase != null) { + { + // check c_user 是否存在 WxCUser cUserQ = new WxCUser(); cUserQ.setTenantId(tenantId); cUserQ.setPhone(userBase.getPhone()); @@ -594,8 +605,13 @@ public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService { WxCUser user = userList.get(0); if (user != null) { if (oldUserBase != null) { - oldUserBase.setNickName(user.getNickName()); + if(oldUserBase != null) { + oldUserBase.setNickName(user.getNickName()); + } + userBase.setId(user.getId()); + userBase.setNickName(user.getNickName()); } else { + userBase.setNickName(user.getNickName()); } } @@ -603,55 +619,66 @@ public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService { } if (oldUserBase != null) { + logger.info("update user base: " + oldUserBase.getId()); wxCUserBasicInfoMapper.updateByPrimaryKeySelective(oldUserBase); } else { - wxCUserBasicInfoMapper.insertSelective(userBase); + logger.info("insert user base: " + userBase.getId()); + try { + wxCUserBasicInfoMapper.insertSelective(userBase); + } catch (Exception e) { + logger.error(e.getMessage()); + return; + } } // update tags - if (StringUtils.isNotBlank(userBase.getTagNames())) { - // get tag ids - String tagNames = userBase.getTagNames(); - List tagIdList = new ArrayList<>(); - for (String tagName : tagNames.split("/")) { - List wxTagsList = wxTagsMapper.getByName(tagName); - if (wxTagsList.size()==1) { - WxTags wxTags = wxTagsList.get(0); - tagIdList.add(Long.valueOf(wxTags.getId())); + try { + if (StringUtils.isNotBlank(userBase.getTagNames())) { + // get tag ids + String tagNames = userBase.getTagNames(); + List tagIdList = new ArrayList<>(); + for (String tagName : tagNames.split("/")) { + for(WxTags tag: tagList) { + if(tagName.equals(tag.getName())) { + tagIdList.add(Long.valueOf(tag.getId())); + break; + } + } } - } - if (oldUserBase != null){ - wxCUserTagsService.assignTags(tagIdList,oldUserBase); - }else{ - wxCUserTagsService.assignTags(tagIdList,userBase); + if (oldUserBase != null){ + wxCUserTagsService.assignTags(tagIdList,oldUserBase); + }else{ + wxCUserTagsService.assignTags(tagIdList,userBase); + } } - } - //触发自动打标签 - if (oldUserBase != null) { - wxCUserTagsService.triggerAssignTags(EnumAssignTagsTrigger.ASSIGN_TAGS_TRIGGER_IMPORT,oldUserBase); - } - else{ - wxCUserTagsService.triggerAssignTags(EnumAssignTagsTrigger.ASSIGN_TAGS_TRIGGER_IMPORT,userBase); - } - - // 成长值 - if (StringUtils.isNotBlank(uBase.getPoins())) { + //触发自动打标签 if (oldUserBase != null) { - wxScoreRulesService.addScore2(EnumScoreType.MEM_IMPORT, oldUserBase, uBase.getPoins()); - } else { - wxScoreRulesService.addScore2(EnumScoreType.MEM_IMPORT, userBase, uBase.getPoins()); + wxCUserTagsService.triggerAssignTags(EnumAssignTagsTrigger.ASSIGN_TAGS_TRIGGER_IMPORT,oldUserBase); + } + else{ + wxCUserTagsService.triggerAssignTags(EnumAssignTagsTrigger.ASSIGN_TAGS_TRIGGER_IMPORT,userBase); + } + + // 成长值 + if (StringUtils.isNotBlank(uBase.getPoins())) { + if (oldUserBase != null) { + wxScoreRulesService.addScore2(EnumScoreType.MEM_IMPORT, oldUserBase, uBase.getPoins()); + } else { + wxScoreRulesService.addScore2(EnumScoreType.MEM_IMPORT, userBase, uBase.getPoins()); + } } + } catch (Exception e) { + logger.error(e.getMessage()); + logger.error("打标签+成长值失败--继续导入"); + return; } //记数 - stringRedisTemplate.opsForHash().increment(userId,"successCount",1); + stringRedisTemplate.opsForHash().increment(importKey,"successCount",1); }); } catch (Exception e) { - stringRedisTemplate.opsForHash().put(importKey, "allCount", "1"); - stringRedisTemplate.opsForHash().put(importKey, "allSuccessCount", "0"); - stringRedisTemplate.opsForHash().put(importKey, "successCount", "0"); - stringRedisTemplate.opsForHash().put(importKey, "failCount", "1"); + stringRedisTemplate.opsForHash().put(importKey, "allCount", "" + all_fail); e.printStackTrace(); logger.error("导入模板失败:"+e.getMessage()); }