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 fcd333172..ed5853826 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxCUserBasicInfoServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxCUserBasicInfoServiceImpl.java @@ -2,6 +2,8 @@ package com.iformall.service.impl; import cn.afterturn.easypoi.excel.ExcelImportUtil; import cn.afterturn.easypoi.excel.entity.ImportParams; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.iformall.common.ErrorCode; @@ -27,6 +29,8 @@ import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @@ -35,12 +39,14 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; @Service public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired WxCUserTagsMapper wxCUserTagsMapper; @@ -188,11 +194,37 @@ public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService { phoneCellTwo.setCellValue(entity.getPhone()); nickNameCellTwo.setCellValue(entity.getNickName()); educationCellTwo.setCellValue(entity.getEducation()); - birthdateCellTwo.setCellValue(entity.getBirthdate()); + if (entity.getBirthdate() != null) + birthdateCellTwo.setCellValue(sdf.format(entity.getBirthdate())); addrCellTwo.setCellValue(entity.getAddress()); updateDateCellTwo.setCellValue(sdf.format(entity.getUpdateDate())); createDateCellTwo.setCellValue(sdf.format(entity.getCreateDate())); - tagCellTwo.setCellValue(entity.getTagNames()); + if (entity != null) { + if (entity.getTagId() != null) { + WxCUserTags uTag = wxCUserTagsMapper.selectByPrimaryKey(entity.getTagId()); + if (StringUtils.isNotBlank(uTag.getTags())) { + List ids = JSONObject.parseArray(uTag.getTags(), Long.class); + WxTags wxTagsQ = new WxTags(); + wxTagsQ.setIds(ids); + List tagList = wxTagsMapper.findList(wxTagsQ); + String tagNames = ""; + String tagIds = ""; + List tagIdList = new ArrayList<>(); + for (WxTags wt : tagList) { + tagNames += wt.getName() + "/"; + tagIds += wt.getId() + ","; + tagIdList.add(wt.getId()); + } + if (StringUtils.isNotBlank(tagNames)) { + entity.setTagNames(tagNames.substring(0, tagNames.length() - 1)); + tagCellTwo.setCellValue(entity.getTagNames()); + } + if (StringUtils.isNoneBlank(tagIds)) { + entity.setTagIds(tagIds.substring(0, tagIds.length() - 1)); + } + } + } + } } @@ -253,6 +285,7 @@ public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService { workbook = new XSSFWorkbook(); XSSFSheet sheetTwo = workbook.createSheet("会员信息"); + Row rowtwo = sheetTwo.createRow(0); Cell nameCellTwo = rowtwo.createCell(0); @@ -276,6 +309,75 @@ public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService { updateDateCellTwo.setCellValue("上次活跃时间"); createDateCellTwo.setCellValue("注册时间"); tagCellTwo.setCellValue("标签"); + { + rowtwo = sheetTwo.createRow(1); + nameCellTwo = rowtwo.createCell(0); + sexCellTwo = rowtwo.createCell(1); + phoneCellTwo = rowtwo.createCell(2); + nickNameCellTwo = rowtwo.createCell(3); + educationCellTwo = rowtwo.createCell(4); + birthdateCellTwo = rowtwo.createCell(5); + addrCellTwo = rowtwo.createCell(6); + updateDateCellTwo = rowtwo.createCell(7); + createDateCellTwo = rowtwo.createCell(8); + tagCellTwo = rowtwo.createCell(9); + nameCellTwo.setCellValue("例子1"); + sexCellTwo.setCellValue("保密"); + phoneCellTwo.setCellValue("13900010001"); + nickNameCellTwo.setCellValue("昵称"); + educationCellTwo.setCellValue("本科"); + birthdateCellTwo.setCellValue("2018-09-10"); + addrCellTwo.setCellValue("地址"); + updateDateCellTwo.setCellValue("2018-08-10"); + createDateCellTwo.setCellValue("2018-08-10"); + tagCellTwo.setCellValue("附近住户"); + } + { + rowtwo = sheetTwo.createRow(2); + nameCellTwo = rowtwo.createCell(0); + sexCellTwo = rowtwo.createCell(1); + phoneCellTwo = rowtwo.createCell(2); + nickNameCellTwo = rowtwo.createCell(3); + educationCellTwo = rowtwo.createCell(4); + birthdateCellTwo = rowtwo.createCell(5); + addrCellTwo = rowtwo.createCell(6); + updateDateCellTwo = rowtwo.createCell(7); + createDateCellTwo = rowtwo.createCell(8); + tagCellTwo = rowtwo.createCell(9); + nameCellTwo.setCellValue("例子2"); + sexCellTwo.setCellValue("男"); + phoneCellTwo.setCellValue("13900020002"); + nickNameCellTwo.setCellValue("昵称"); + educationCellTwo.setCellValue("本科"); + birthdateCellTwo.setCellValue("2018-09-10"); + addrCellTwo.setCellValue("地址"); + updateDateCellTwo.setCellValue("2018-08-10"); + createDateCellTwo.setCellValue("2018-08-10"); + tagCellTwo.setCellValue("男/附近住户"); + } + { + rowtwo = sheetTwo.createRow(3); + nameCellTwo = rowtwo.createCell(0); + sexCellTwo = rowtwo.createCell(1); + phoneCellTwo = rowtwo.createCell(2); + nickNameCellTwo = rowtwo.createCell(3); + educationCellTwo = rowtwo.createCell(4); + birthdateCellTwo = rowtwo.createCell(5); + addrCellTwo = rowtwo.createCell(6); + updateDateCellTwo = rowtwo.createCell(7); + createDateCellTwo = rowtwo.createCell(8); + tagCellTwo = rowtwo.createCell(9); + nameCellTwo.setCellValue("例子3"); + sexCellTwo.setCellValue("女"); + phoneCellTwo.setCellValue("13900030003"); + nickNameCellTwo.setCellValue("昵称"); + educationCellTwo.setCellValue("本科"); + birthdateCellTwo.setCellValue("2018-09-10"); + addrCellTwo.setCellValue("地址"); + updateDateCellTwo.setCellValue("2018-08-10"); + createDateCellTwo.setCellValue("2018-08-10"); + tagCellTwo.setCellValue("女/附近住户"); + } FileOutputStream fileOut = new FileOutputStream(file); workbook.write(fileOut); @@ -284,7 +386,7 @@ public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService { downFile(filepath, filename, response, request); FileUtils.forceDelete(file); } catch (Exception e) { - e.printStackTrace(); + logger.error("导出模板:" + e.getMessage()); } } @@ -293,15 +395,19 @@ public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService { @Override public ResultData importTemplate(MultipartFile file, String tenantId) { + final IdWorker idWorker = IdWorker.get(); + try { ImportParams params = new ImportParams(); List datalist = ExcelImportUtil.importExcel(file.getInputStream(), WxCUserBasicInfo.class, params); - final IdWorker idWorker = IdWorker.get(); for (WxCUserBasicInfo userBase : datalist) { + userBase.setTenantId(tenantId); Date curDate = new Date(); - if (StringUtils.isBlank(userBase.getPhone())) + if (StringUtils.isBlank(userBase.getPhone())) { + logger.error("手机号为空", userBase.toString()); continue; + } WxCUserBasicInfo oldUserBase = null; WxCUserBasicInfo userBaseQ = new WxCUserBasicInfo(); userBaseQ.setTenantId(tenantId); @@ -312,7 +418,6 @@ public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService { } if (oldUserBase != null) { - oldUserBase.setTenantId(tenantId); oldUserBase.setName(userBase.getName()); oldUserBase.setSex(userBase.getSex()); oldUserBase.setNickName(userBase.getNickName()); @@ -322,19 +427,31 @@ public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService { oldUserBase.setUpdateDate(userBase.getUpdateDate()); oldUserBase.setCreateDate(userBase.getCreateDate()); } else { - userBase.setId(idWorker.nextId()); - userBase.setTenantId(tenantId); + // 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()); + } } // update 昵称 - WxCUser cUserQ = new WxCUser(); - cUserQ.setTenantId(tenantId); - cUserQ.setPhone(userBase.getPhone()); - List userList = wxCUserMapper.select(cUserQ); - if (userList.size() > 0) { - WxCUser user = userList.get(0); - if (user != null) { - if (user.getNickName() != null) { + if (oldUserBase != null) { + WxCUser cUserQ = new WxCUser(); + cUserQ.setTenantId(tenantId); + cUserQ.setPhone(userBase.getPhone()); + List userList = wxCUserMapper.select(cUserQ); + if (userList.size() > 0) { + WxCUser user = userList.get(0); + if (user != null) { if (oldUserBase != null) { oldUserBase.setNickName(user.getNickName()); } else { @@ -343,11 +460,67 @@ public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService { } } } + if (oldUserBase != null) { wxCUserBasicInfoMapper.updateByPrimaryKeySelective(oldUserBase); } else { wxCUserBasicInfoMapper.insertSelective(userBase); } + // update tags + if (StringUtils.isNotBlank(userBase.getTagNames())) { + // get tag ids + String tagNames = userBase.getTagNames(); + List tagIdList = new ArrayList<>(); + for (String tagName: tagNames.split("/")) { + WxTags wxTags = wxTagsMapper.getByName(tagName); + if (wxTags != null) { + tagIdList.add(Long.valueOf(wxTags.getId())); + } + } + WxCUserTags wxCUserTags = null; + if (oldUserBase != null) { + if (oldUserBase.getTagId() != null) { + wxCUserTags = wxCUserTagsMapper.selectByPrimaryKey(oldUserBase.getTagId()); + } else { + WxCUserTags cUserTagsQ = new WxCUserTags(); + cUserTagsQ.setUserId(userBase.getId()); + cUserTagsQ.setTenantId(userBase.getTenantId()); + List userTagsList = wxCUserTagsMapper.findList(cUserTagsQ); + if (userTagsList.size() > 0) { + wxCUserTags = userTagsList.get(0); + } + } + + if (wxCUserTags != null) { + // have old one, update tagIds + wxCUserTags.setTags(JSON.toJSONString(tagIdList)); + wxCUserTags.setUpdateDate(curDate); + wxCUserTagsMapper.updateByPrimaryKey(wxCUserTags); + } else { + // new one, insert + wxCUserTags = new WxCUserTags(); + wxCUserTags.setId(idWorker.nextId()); + wxCUserTags.setTenantId(tenantId); + wxCUserTags.setUserId(oldUserBase.getId()); + wxCUserTags.setTags(JSON.toJSONString(tagIdList)); + wxCUserTags.setCreateDate(curDate); + wxCUserTags.setUpdateDate(curDate); + wxCUserTagsMapper.insertSelective(wxCUserTags); + } + } else { + // new one, insert + wxCUserTags = new WxCUserTags(); + wxCUserTags.setId(idWorker.nextId()); + wxCUserTags.setTenantId(tenantId); + wxCUserTags.setUserId(userBase.getId()); + wxCUserTags.setTags(JSON.toJSONString(tagIdList)); + wxCUserTags.setCreateDate(curDate); + wxCUserTags.setUpdateDate(curDate); + wxCUserTagsMapper.insertSelective(wxCUserTags); + userBase.setTagId(wxCUserTags.getId()); + wxCUserBasicInfoMapper.updateByPrimaryKeySelective(userBase); + } + } } } catch (Exception e) { diff --git a/mallinkService/src/main/resources/mapper/WxTagsMapper.xml b/mallinkService/src/main/resources/mapper/WxTagsMapper.xml index 2caa4dbe8..483c1d246 100644 --- a/mallinkService/src/main/resources/mapper/WxTagsMapper.xml +++ b/mallinkService/src/main/resources/mapper/WxTagsMapper.xml @@ -22,15 +22,15 @@ - and `name` like concat('%', #{name},'%') + and `name` = #{name} - and `type1` like concat('%', #{type1},'%') + and `type1` = #{type1} - and `type2` like concat('%', #{type2},'%') + and `type2` = #{type2} @@ -71,7 +71,7 @@ select from wx_tags where 1=1 - and `name` like concat('%', #{name},'%') + and `name` = #{name}