Commit ade277a2 by zhangyuxiao

导入会员信息

parent 818c5b00
package com.xiaomai.cases.polar.enums;
import org.apache.commons.lang3.StringUtils;
/**
* @Author zxy
* @Description
* @Date 2024/7/2 17:48
*/
public enum ExcelImportEnum {
MEMBER_INFO("memberInfo",3,0,1);
/**业务 */
private String value;
/**行开始下标(从0开始) */
private int rowStartIndex;
/**姓名列坐标 */
private int cellNametIndex;
/**手机号列坐标 */
private int cellPhoneIndex;
ExcelImportEnum(String value, int rowStartIndex, int cellNametIndex, int cellPhoneIndex) {
this.value = value;
this.rowStartIndex = rowStartIndex;
this.cellNametIndex = cellNametIndex;
this.cellPhoneIndex = cellPhoneIndex;
}
ExcelImportEnum(String value, int rowStartIndex, int cellPhoneIndex) {
this.value = value;
this.rowStartIndex = rowStartIndex;
this.cellPhoneIndex = cellPhoneIndex;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public int getRowStartIndex() {
return rowStartIndex;
}
public void setRowStartIndex(int rowStartIndex) {
this.rowStartIndex = rowStartIndex;
}
public int getCellNametIndex() {
return cellNametIndex;
}
public void setCellNametIndex(int cellNametIndex) {
this.cellNametIndex = cellNametIndex;
}
public int getCellPhoneIndex() {
return cellPhoneIndex;
}
public void setCellPhoneIndex(int cellPhoneIndex) {
this.cellPhoneIndex = cellPhoneIndex;
}
/**
*根据value获取对应枚举
* value找不对应的枚举,则返回一个默认值
* @param value
* @return
}
*/
public static int getRowIndexByValue(String value) {
for (ExcelImportEnum excelImportBusinessEnum : ExcelImportEnum.values()) {
if (StringUtils.equals(excelImportBusinessEnum.getValue(), value)) {
return excelImportBusinessEnum.rowStartIndex;
}
}
return 0;
}
public static int getNameCellIndexByValue(String value) {
for (ExcelImportEnum excelImportBusinessEnum : ExcelImportEnum.values()) {
if (StringUtils.equals(excelImportBusinessEnum.getValue(), value)) {
return excelImportBusinessEnum.cellNametIndex;
}
}
return 0;
}
public static int getPhoneCellIndexByValue(String value) {
for (ExcelImportEnum excelImportBusinessEnum : ExcelImportEnum.values()) {
if (StringUtils.equals(excelImportBusinessEnum.getValue(), value)) {
return excelImportBusinessEnum.cellPhoneIndex;
}
}
return 1;
}
}
package com.xiaomai.cases.polar.importExcel;
import com.alibaba.fastjson.JSONObject;
import com.xiaomai.client.DataUserInfo;
import com.xiaomai.enums.ApiModule;
import com.xiaomai.enums.RequestType;
import com.xiaomai.enums.Terminal;
import com.xiaomai.utils.XMBaseTest;
/**
* @Author zxy
* @Description
* @Date 2024/7/2 17:31
*/
public class ImportTools extends XMBaseTest {
//创建导入
public JSONObject createImportTask(String taskType,String resourceId,String taskDesc,String memo,DataUserInfo...userInfos){
dataApi.setApiModule(ApiModule.Polar_ImportTask)
.setApiName("API_createImportTask")
.setTerminal(Terminal.B);
super.beforeDataRequest(userInfos);
JSONObject object = new JSONObject();
object.put("operatorId",dataApi.getLoginInfo().getAdminId());
object.put("tenantId",dataApi.getLoginInfo().getStudioId());
object.put("resourceId",resourceId);
object.put("tenantType","STUDIO");
object.put("taskType",taskType);
object.put("taskDesc",taskDesc);
object.put("memo",memo);
dataApi.doRequest(RequestType.JSON,dataparams,object.toString(),dataheadrs).assetsSuccess(true);
return dataApi.getBodyInJSON();
}
//获取导入任务
public JSONObject getTask(String id,DataUserInfo...userInfos){
dataApi.setApiModule(ApiModule.Polar_ImportTask)
.setApiName("API_getTask")
.setTerminal(Terminal.B);
super.beforeDataRequest(userInfos);
JSONObject object = new JSONObject();
object.put("id",id);
dataApi.doRequest(RequestType.JSON,dataparams,object.toString(),dataheadrs).assetsSuccess(true);
return dataApi.getBodyInJSON();
}
//任务中心-导入记录
public JSONObject pageImportTask(DataUserInfo...userInfos){
dataApi.setApiModule(ApiModule.Polar_ImportTask)
.setApiName("API_pageImportTask")
.setTerminal(Terminal.B);
super.beforeDataRequest(userInfos);
JSONObject object = new JSONObject();
object.put("operatorId",dataApi.getLoginInfo().getAdminId());
object.put("tenantId",dataApi.getLoginInfo().getStudioId());
object.put("tenantType","STUDIO");
object.put("current",0);
object.put("size",10);
dataApi.doRequest(RequestType.JSON,dataparams,object.toString(),dataheadrs).assetsSuccess(true);
return dataApi.getBodyInJSON();
}
//获取导入资源
public JSONObject getImportResource(String resourceId,DataUserInfo...userInfos){
dataApi.setApiModule(ApiModule.Polar_ImportTask)
.setApiName("API_getImportResource")
.setTerminal(Terminal.B);
super.beforeDataRequest(userInfos);
JSONObject object = new JSONObject();
object.put("tenantId",dataApi.getLoginInfo().getStudioId());
object.put("tenantType","STUDIO");
object.put("resourceId",resourceId);
dataApi.doRequest(RequestType.JSON,dataparams,object.toString(),dataheadrs).assetsSuccess(true);
return dataApi.getBodyInJSON();
}
}
package com.xiaomai.cases.polar.importExcel;
import com.alibaba.fastjson.JSONObject;
import com.xiaomai.cases.polar.enums.ExcelImportEnum;
import com.xiaomai.cases.polar.utils.Entity.SignatureVO;
import com.xiaomai.cases.polar.utils.ModifyExcelContentUtil;
import com.xiaomai.cases.polar.utils.ResourceUtil;
import com.xiaomai.enums.ApiModule;
import com.xiaomai.enums.LoginAccount;
import com.xiaomai.enums.RequestType;
import com.xiaomai.enums.Terminal;
import com.xiaomai.utils.XMBaseTest;
import com.xiaomai.utils.XMJSONPath;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* @Author zxy
* @Description
* @Date 2024/7/2 17:37
*/
public class TestImportMember extends XMBaseTest {
ImportTools importTools = new ImportTools();
@BeforeMethod
public void beforeTest(){
xmAppApi.setApiModule(ApiModule.Polar_ImportTask)
.setApiName("API_webTokenWithAccessUrl")
.setLoginUser(LoginAccount.GYM_PROD)
.setTerminal(Terminal.B);
dal.setCase_owner("zyx")
.setCase_name(Thread.currentThread().getStackTrace()[1].getFileName().split("\\.")[0]);
super.beforeTest();
}
@Test(description = "导入会员信息")
public void testImportMember()throws IOException, InterruptedException{
//导入任务列表
int total = Integer.parseInt(importTools.pageImportTask().getJSONObject("result").getString("total"));
//1.更新excel文档
String filePathName = "excelFile/memberInfo.xls";
ModifyExcelContentUtil.modifyExcel(filePathName, "会员信息导入", ExcelImportEnum.MEMBER_INFO);
Thread.sleep(1000);
//2、获取签名
JSONObject object = new JSONObject();
object.put("tenantId", xmAppApi.getLoginInfo().getBrandId());
object.put("tenantType", "BRAND");
object.put("resourceName",filePathName);
object.put("resourceNameToMD5",false);
object.put("bizCode","IMPORT_TASK");
xmAppApi.doRequest(RequestType.JSON, params, object.toString(), headers).assetsSuccess(true);
JSONObject result1 = xmAppApi.getBodyInJSON();
String resourceId = XMJSONPath.readPath(result1, "$.result.resourceId");
String accessId = XMJSONPath.readPath(result1, "$.result.accessId");
String policy = XMJSONPath.readPath(result1, "$.result.policy");
String signature = XMJSONPath.readPath(result1, "$.result.signature");
String key = XMJSONPath.readPath(result1, "$.result.key");
String callback = XMJSONPath.readPath(result1, "$.result.callback");
String host = XMJSONPath.readPath(result1, "$.result.host");
String time = XMJSONPath.readPath(result1, "$.result.extAttrs");
JSONObject time1 = JSONObject.parseObject(time);
String url = XMJSONPath.readPath(result1, "$.result.url");
//3.腾讯云上传
InputStream inputStream = new FileInputStream(filePathName);
SignatureVO signatureVO = new SignatureVO();
signatureVO.setAlgorithm("sha1");
signatureVO.setKey(key);
signatureVO.setAccessId(accessId);
signatureVO.setPolicy(policy);
signatureVO.setSignature(signature);
signatureVO.setCallback(callback);
signatureVO.setExtAttrs(time1.getString("q-key-time"));
signatureVO.setHost(host);
signatureVO.setResourceId(Long.valueOf(resourceId));
String result = String.valueOf(ResourceUtil.uploadFileContentToTencent(inputStream, filePathName, signatureVO, 5000,"multipart/form-data"));
Assert.assertEquals(result, "true");
//4.创建任务
JSONObject result2 = importTools.createImportTask("MEMBER",resourceId,"REPLACE_DUPLICATES","");
String recordId = XMJSONPath.readPath(result2, "$.result");
//5.获取上传进度
Thread.sleep(6000);
importTools.getTask(recordId);
Assert.assertEquals(XMJSONPath.readPath(dataApi.getApi_response(), "$.result.state"), "SUCCESS");
Assert.assertEquals(XMJSONPath.readPath(dataApi.getApi_response(), "$.result.successRowNum"), "1");
//上传成功后导入记录列表新增一条数据
JSONObject res = importTools.pageImportTask().getJSONObject("result");
Assert.assertEquals(Integer.parseInt(res.getString("total")),total+1);
Assert.assertEquals(res.getJSONArray("records").getJSONObject(0).getString("resourceId"),resourceId);
Assert.assertEquals(res.getJSONArray("records").getJSONObject(0).getString("state"),"SUCCESS");
//获取导入资源-下载导入文件
String url1 = importTools.getImportResource(resourceId).getString("result");
Assert.assertEquals(url1,url);
}
}
package com.xiaomai.cases.polar.utils;
import com.alibaba.fastjson.JSONArray;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;
/**
* @Author zxy
* @Description
* @Date 2024/7/2 17:57
*/
public class CommonUtil {
/**
* 把字符串解析成JSONArray
*
* @param str
* @return
*/
public static JSONArray JsonArray2Str(String str) {
JSONArray jsonArray = JSONArray.parseArray(str);
return jsonArray;
}
/**
* 获取手机号,以1111开头
*
* @param random
* @return
*/
public static Long getRandomPhone(Random random) {
Integer randomNum = random.nextInt(100000000);
return 11111000000L + randomNum;
}
/**
* @param mark 打标,比如获取的是员工:员工,校长;获取的是学员:_杜同学,同学来自线索导入
* @return 学员姓名
*/
public static String getRandomName(String mark) {
StringBuilder sb = new StringBuilder();
String firstName = "杜张王李赵彭柏孙谢钮徐薛劳周程钟郭竺杨黄苏艾楼陈曹蒋牛";
String nameMiddle = "ABCDEFGHIJKLMNOPQRSTUVWXTZ";
String nameNum = "零一二三四五六七八九十";
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(firstName.length());
sb.append(firstName, randomInt, randomInt + 1);
randomInt = randomGenerator.nextInt(nameMiddle.length());
sb.append(nameMiddle, randomInt, randomInt + 1);
randomInt = randomGenerator.nextInt(nameNum.length());
sb.append(nameNum, randomInt, randomInt + 1);
if (mark != null && mark.length() != 0) {
sb.append(mark);
}
return sb.toString();
}
/**
* 保留2位小数
*
* @param d
* @return
*/
public static String formatDouble2(double d) {
// 新方法,如果不需要四舍五入,可以使用RoundingMode.DOWN
return new BigDecimal(d).setScale(2, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
}
/**
* 保留2位小数
*
* @param d
* @return
*/
public static Double formatDouble(double d) {
// 新方法,如果不需要四舍五入,可以使用RoundingMode.DOWN
return new BigDecimal(d).setScale(2, RoundingMode.HALF_UP).stripTrailingZeros().doubleValue();
}
}
package com.xiaomai.cases.polar.utils.Entity;
/**
* @Author zxy
* @Description
* @Date 2024/7/3 10:32
*/
public class SignatureVO {
//资源ID
private Long resourceId;
//接入ID
private String accessId;
//访问策略
private String policy;
//签名
private String signature;
//对象路径
private String key;
//接入点
private String host;
//过期时间
private String expire;
private String extAttrs;
//回调地址
private String callback;
private String algorithm;
public Long getResourceId() {
return resourceId;
}
public void setResourceId(Long resourceId) {
this.resourceId = resourceId;
}
public String getAccessId() {
return accessId;
}
public void setAccessId(String accessId) {
this.accessId = accessId;
}
public String getPolicy() {
return policy;
}
public void setPolicy(String policy) {
this.policy = policy;
}
public String getSignature() {
return signature;
}
public void setSignature(String signature) {
this.signature = signature;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getExpire() {
return expire;
}
public void setExpire(String expire) {
this.expire = expire;
}
public String getCallback() {
return callback;
}
public void setCallback(String callback) {
this.callback = callback;
}
public String getExtAttrs() {
return extAttrs;
}
public void setExtAttrs(String extAttrs) {
this.extAttrs = extAttrs;
}
public String getAlgorithm() {
return algorithm;
}
public void setAlgorithm(String algorithm) {
this.algorithm = algorithm;
}
}
package com.xiaomai.cases.polar.utils;
import com.xiaomai.cases.polar.enums.ExcelImportEnum;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.*;
import java.util.Random;
/**
* @Author zxy
* @Description
* @Date 2024/7/2 17:47
*/
public class ModifyExcelContentUtil {
private static final Logger logger = Logger.getLogger(ModifyExcelContentUtil.class);
/**
* 修改excel中的姓名和手机号信息
* @param fileUrl
* @param sheetName
* @param importBusinessEnum 业务类型
*/
public static void modifyExcel(String fileUrl, String sheetName, ExcelImportEnum importBusinessEnum){
int rowIndex=0;
int nameCellIndex=0;
int phoneCellIndex=1;
Long now = System.currentTimeMillis();
Random random = new Random(now);
String name="";
rowIndex=importBusinessEnum.getRowStartIndex();
nameCellIndex=importBusinessEnum.getCellNametIndex();
phoneCellIndex=importBusinessEnum.getCellPhoneIndex();
name = getRandomName("导入线索");
try {
InputStream inputStream = new FileInputStream(fileUrl);
//根据指定的文件输入流导入Excel从而产生Workbook对象
Workbook workbook = new HSSFWorkbook(inputStream);
//根据sheetname获取Excel文档表单
Sheet sheet = workbook.getSheet(sheetName);
//获取第rowIndex行
Row row = sheet.getRow(rowIndex);
Long phone = CommonUtil.getRandomPhone(random);
int nameCount = 1;
while(row != null && rowIndex < 5010) {
Cell phoneCell = row.getCell(phoneCellIndex);
Cell nameCell = row.getCell(nameCellIndex);
if (StringUtils.isBlank(nameCell.getStringCellValue())) {
break;
}
phoneCell.setCellValue(phone.toString());
String realName = name + nameCount;
//String realName = getRandomName("") + nameCount;
nameCell.setCellValue(realName);
nameCount ++;
phone ++;
rowIndex ++;
row = sheet.getRow(rowIndex);
}
OutputStream outputStream = new FileOutputStream(fileUrl);
workbook.write(outputStream);
outputStream.flush();
logger.info("-------------------Excel信息修改成功------------------");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
* @param mark 学员姓名打标:比如 在读导入、线索导入;也可以不写
* @return 学员姓名
*/
private static String getRandomName(String mark) {
StringBuilder sb = new StringBuilder();
String firstName = "杜张王李赵彭柏孙谢钮徐薛劳周程钟郭竺杨黄苏艾楼陈曹蒋牛";
String nameMiddle="ABCDEFGHIJKLMNOPQRSTUVWXTZ";
String nameNum="零一二三四五六七八九十";
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(firstName.length());
sb.append(firstName, randomInt, randomInt+1);
randomInt = randomGenerator.nextInt(nameMiddle.length());
sb.append(nameMiddle, randomInt, randomInt+1);
randomInt = randomGenerator.nextInt(nameNum.length());
sb.append(nameNum, randomInt, randomInt+1);
sb.append("同学_"+mark);
return sb.toString();
}
}
...@@ -10,6 +10,8 @@ public class ApiModule { ...@@ -10,6 +10,8 @@ public class ApiModule {
public static String Polar_Setting ="polar_setting";//通用设置 public static String Polar_Setting ="polar_setting";//通用设置
public static String Polar_Applet ="polar_applet";//专属小程序 public static String Polar_Applet ="polar_applet";//专属小程序
public static String Polar_Clue ="polar_clue";//crm public static String Polar_Clue ="polar_clue";//crm
public static String Polar_ImportTask ="polar_importTask";//导入
public static String Polar_Homepage ="polar_homePage";//首页 public static String Polar_Homepage ="polar_homePage";//首页
public static String Polar_Member ="polar_member";//会员管理 public static String Polar_Member ="polar_member";//会员管理
public static String Polar_Course ="polar_course";//会员管理 public static String Polar_Course ="polar_course";//会员管理
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment