diff --git a/mallinkService/src/main/java/com/iformall/domain/po/WxCUserBasicInfo.java b/mallinkService/src/main/java/com/iformall/domain/po/WxCUserBasicInfo.java index 524ed1aff..d8cbc5860 100644 --- a/mallinkService/src/main/java/com/iformall/domain/po/WxCUserBasicInfo.java +++ b/mallinkService/src/main/java/com/iformall/domain/po/WxCUserBasicInfo.java @@ -105,7 +105,7 @@ public class WxCUserBasicInfo implements Serializable { @Transient private String tagIds; @Transient - @Excel(name="标签",width = 20,orderNum = "9") + @Excel(name="标签",width = 20,orderNum = "10") private String tagNames; @Transient private long count; diff --git a/mallinkService/src/main/java/com/iformall/domain/vo/CUserBaseInfoT.java b/mallinkService/src/main/java/com/iformall/domain/vo/CUserBaseInfoT.java new file mode 100644 index 000000000..be7a7f561 --- /dev/null +++ b/mallinkService/src/main/java/com/iformall/domain/vo/CUserBaseInfoT.java @@ -0,0 +1,142 @@ +package com.iformall.domain.vo; + +import cn.afterturn.easypoi.excel.annotation.Excel; + +import javax.persistence.Transient; +import javax.validation.constraints.NotNull; +import java.io.Serializable; + +public class CUserBaseInfoT implements Serializable { + /**用户姓名**/ + @Excel(name="姓名",width = 20,orderNum = "1") + @NotNull + @io.swagger.annotations.ApiModelProperty(value="用户姓名",name="name") + private String name; + /**性别:0:保密 1.男 2女**/ + @Excel(name="性别",width = 20,replace = { "保密_0", "男_1","女_2"},orderNum = "2") + @NotNull + @io.swagger.annotations.ApiModelProperty(value="性别:0:保密 1.男 2女",name="sex") + private Integer sex; + /**微信用户绑定的手机号**/ + @Excel(name="手机号",width = 20,orderNum = "3") + @io.swagger.annotations.ApiModelProperty(value="微信用户绑定的手机号",name="phone") + @NotNull + private String phone; + /**用户昵称**/ + @Excel(name="微信昵称",width = 20,orderNum = "4") + @io.swagger.annotations.ApiModelProperty(value="用户昵称",name="nickName") + private String nickName; + /**学历**/ + @Excel(name="学历",width = 20,orderNum = "5") + @NotNull + @io.swagger.annotations.ApiModelProperty(value="学历",name="education") + private String education; + /**出生日期**/ + @Excel(name="生日",width = 20,orderNum = "6") + @NotNull + @io.swagger.annotations.ApiModelProperty(value="出生日期",name="birthdate") + private String birthdate; + /**地址**/ + @Excel(name="地址",width = 20,orderNum = "7") + @io.swagger.annotations.ApiModelProperty(value="地址",name="address") + private String address; + + /**更新时间**/ + @Excel(name="上次活跃时间",width = 20, orderNum = "8") + @io.swagger.annotations.ApiModelProperty(value="更新时间",name="updateDate") + private String updateDate; + + /**创建时间**/ + @Excel(name="注册时间",width = 20, orderNum = "9") + @io.swagger.annotations.ApiModelProperty(value="创建时间",name="createDate") + private String createDate; + + @Transient + @Excel(name="标签",width = 20,orderNum = "10") + private String tagNames; + + @NotNull + public String getName() { + return name; + } + + public void setName(@NotNull String name) { + this.name = name; + } + + @NotNull + public Integer getSex() { + return sex; + } + + public void setSex(@NotNull Integer sex) { + this.sex = sex; + } + + @NotNull + public String getPhone() { + return phone; + } + + public void setPhone(@NotNull String phone) { + this.phone = phone; + } + + public String getNickName() { + return nickName; + } + + public void setNickName(String nickName) { + this.nickName = nickName; + } + + @NotNull + public String getEducation() { + return education; + } + + public void setEducation(@NotNull String education) { + this.education = education; + } + + @NotNull + public String getBirthdate() { + return birthdate; + } + + public void setBirthdate(@NotNull String birthdate) { + this.birthdate = birthdate; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public String getUpdateDate() { + return updateDate; + } + + public void setUpdateDate(String updateDate) { + this.updateDate = updateDate; + } + + public String getCreateDate() { + return createDate; + } + + public void setCreateDate(String createDate) { + this.createDate = createDate; + } + + public String getTagNames() { + return tagNames; + } + + public void setTagNames(String tagNames) { + this.tagNames = tagNames; + } +} 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 ed5853826..53a7746f1 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxCUserBasicInfoServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxCUserBasicInfoServiceImpl.java @@ -1,12 +1,17 @@ package com.iformall.service.impl; import cn.afterturn.easypoi.excel.ExcelImportUtil; +import cn.afterturn.easypoi.excel.annotation.Excel; import cn.afterturn.easypoi.excel.entity.ImportParams; +import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult; +import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult; +import cn.afterturn.easypoi.handler.impl.ExcelDataHandlerDefaultImpl; +import cn.afterturn.easypoi.handler.inter.IExcelDataHandler; +import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler; 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; import com.iformall.common.IdWorker; import com.iformall.common.Result; import com.iformall.common.ResultData; @@ -15,9 +20,9 @@ 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; import com.iformall.domain.vo.CUserTagVo; -import com.iformall.exception.MallinkException; import com.iformall.mapper.WxCUserBasicInfoMapper; import com.iformall.mapper.WxCUserMapper; import com.iformall.mapper.WxCUserTagsMapper; @@ -35,14 +40,14 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; +import javax.persistence.Transient; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.validation.constraints.NotNull; import java.io.*; +import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.UUID; +import java.util.*; @Service public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService { @@ -391,18 +396,92 @@ public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService { } + private class UserExcelHandler extends ExcelDataHandlerDefaultImpl { + @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); + } + + } + + /* + private class UserVerifyHandler implements IExcelVerifyHandler { + @Override + public ExcelVerifyHandlerResult verifyHandler(CUserBaseInfoT cUserBaseInfoT) { + if (StringUtils.isBlank(cUserBaseInfoT.getPhone())) { + return new ExcelVerifyHandlerResult(cUserBaseInfoT); + } + return null; + } + } + */ + + + private WxCUserBasicInfo InitUserBaseInfo(CUserBaseInfoT uBase, String tenantId) { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + + WxCUserBasicInfo userBase = new WxCUserBasicInfo(); + userBase.setTenantId(tenantId); + userBase.setName(uBase.getName()); + userBase.setSex(uBase.getSex()); + if (StringUtils.isBlank(uBase.getPhone())) { + return null; + } + userBase.setPhone(uBase.getPhone()); + userBase.setNickName(uBase.getNickName()); + userBase.setEducation(uBase.getEducation()); + try { + userBase.setBirthdate(sdf.parse(uBase.getBirthdate())); + } catch (ParseException e) { + logger.error("生日: " + e.getMessage()); + } + userBase.setAddress(uBase.getAddress()); + try { + userBase.setUpdateDate(sdf.parse(uBase.getUpdateDate())); + } catch (ParseException e) { + logger.error("上次活跃时间: " + e.getMessage()); + } + try { + userBase.setCreateDate(sdf.parse(uBase.getCreateDate())); + } catch (ParseException e) { + logger.error("注册时间: " + e.getMessage()); + } + userBase.setTagNames(uBase.getTagNames()); + + return userBase; + } + @Override public ResultData importTemplate(MultipartFile file, String tenantId) { final IdWorker idWorker = IdWorker.get(); + ImportParams params = new ImportParams(); + IExcelDataHandler handler = new UserExcelHandler(); + handler.setNeedHandlerFields(new String[]{"姓名", "性别", "手机号", "微信昵称", "学历", "生日", "地址", "上次活跃时间", "注册时间", "标签"});// 注意这里对应的是excel的列名。也就是对象上指定的列名。 + params.setDataHandler(handler); + //params.setVerifyHandler(); + // 需要验证 + params.setNeedVerfiy(true); + try { - ImportParams params = new ImportParams(); - List datalist = ExcelImportUtil.importExcel(file.getInputStream(), WxCUserBasicInfo.class, params); - for (WxCUserBasicInfo userBase : datalist) { - userBase.setTenantId(tenantId); + ExcelImportResult datalist = ExcelImportUtil.importExcelMore(file.getInputStream(), CUserBaseInfoT.class, params); + List successList = datalist.getList(); + List failList = datalist.getFailList(); + logger.info("验证通过的数量:" + successList.size()); + logger.info("验证未通过的数量:" + failList.size()); + for(CUserBaseInfoT uBase: successList) { + WxCUserBasicInfo userBase = InitUserBaseInfo(uBase, tenantId); + if (userBase == null) { + continue; + } + Date curDate = new Date(); if (StringUtils.isBlank(userBase.getPhone())) { logger.error("手机号为空", userBase.toString()); @@ -471,7 +550,7 @@ public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService { // get tag ids String tagNames = userBase.getTagNames(); List tagIdList = new ArrayList<>(); - for (String tagName: tagNames.split("/")) { + for (String tagName : tagNames.split("/")) { WxTags wxTags = wxTagsMapper.getByName(tagName); if (wxTags != null) { tagIdList.add(Long.valueOf(wxTags.getId())); @@ -522,11 +601,16 @@ public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService { } } } - } catch (Exception e) { - throw new MallinkException(ErrorCode.MEM_IMPORT_ERR.getCode(), e.getMessage()); + logger.error(e.getMessage()); } + /* + for (WxCUserBasicInfo userBase : datalist) { + userBase.setTenantId(tenantId); + } + } + */ return new ResultData(Result.SUCCESS, "导入成功"); }