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

[会员详情][添加]导入导出

release_toaliyun_real
gongbiao 7 лет назад
Родитель
Сommit
1c1213f8be
4 измененных файлов: 299 добавлений и 7 удалений
  1. +28
    -0
      mallinkAdmin/src/main/java/com/simple/controller/WxCUserBasicInfoController.java
  2. +10
    -4
      mallinkService/src/main/java/com/simple/domain/po/WxCUserBasicInfo.java
  3. +10
    -0
      mallinkService/src/main/java/com/simple/service/WxCUserBasicInfoService.java
  4. +251
    -3
      mallinkService/src/main/java/com/simple/service/impl/WxCUserBasicInfoServiceImpl.java

+ 28
- 0
mallinkAdmin/src/main/java/com/simple/controller/WxCUserBasicInfoController.java Просмотреть файл

@@ -7,6 +7,7 @@ import com.simple.common.Result;
import com.simple.common.ResultData;
import com.simple.domain.dto.WxCUserBasicInfoDto;
import com.simple.domain.po.*;
import com.simple.exception.MallinkException;
import com.simple.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
@@ -16,8 +17,12 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -189,5 +194,28 @@ public class WxCUserBasicInfoController extends BaseController {
return new ResultData(Result.SUCCESS, "查询成功", page);
}

@RequestMapping("/exportData")
public void exportData(HttpServletRequest request, HttpServletResponse response){

wxCUserBasicInfoService.exportData(request,response,getTenantId());

}

@RequestMapping("/exportTemplate")
public void exportTemplate(HttpServletRequest request, HttpServletResponse response){

wxCUserBasicInfoService.exportTemplate(request,response,getTenantId());

}

@Transactional
@RequestMapping("/importTemplate")
public ResultData importTemplate(@RequestParam("file") MultipartFile file){
if (file.isEmpty()) {
throw new MallinkException(500,"上传文件不能为空");
}
return wxCUserBasicInfoService.importTemplate(file,getTenantId());
}


}

+ 10
- 4
mallinkService/src/main/java/com/simple/domain/po/WxCUserBasicInfo.java Просмотреть файл

@@ -1,12 +1,13 @@
package com.simple.domain.po;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import cn.afterturn.easypoi.excel.annotation.Excel;

import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Table(name = "wx_c_user_basic_info")
public class WxCUserBasicInfo implements Serializable {
@@ -43,15 +44,19 @@ public class WxCUserBasicInfo implements Serializable {
/*微信用户绑定的手机号**/
@Excel(name="手机",width = 20,orderNum = "2")
@io.swagger.annotations.ApiModelProperty(value="微信用户绑定的手机号",name="phone")
private String phone;
/*出生日期**/
@Excel(name="生日",width = 20,format="yyyy-MM-dd",orderNum = "3")
@io.swagger.annotations.ApiModelProperty(value="出生日期",name="birthdate")
private Date birthdate;
/*学历**/
@Excel(name="学历",width = 20,orderNum = "4")
@io.swagger.annotations.ApiModelProperty(value="学历",name="education")
private String education;
/*性别:0:保密 1.男 2女**/
@Excel(name="性别",width = 20,replace = { "保密_0", "男_1","女_2"},orderNum = "2")
@io.swagger.annotations.ApiModelProperty(value="性别:0:保密 1.男 2女",name="sex")
private Integer sex;
/*邮箱**/
@@ -75,8 +80,9 @@ public class WxCUserBasicInfo implements Serializable {
/*租户id**/
@io.swagger.annotations.ApiModelProperty(value="租户id",name="tenantId")
private String tenantId;
/*用户姓名**/
@Excel(name="姓名",width = 20)
@io.swagger.annotations.ApiModelProperty(value="用户姓名",name="name")
private String name;


+ 10
- 0
mallinkService/src/main/java/com/simple/service/WxCUserBasicInfoService.java Просмотреть файл

@@ -1,9 +1,13 @@
package com.simple.service;

import com.github.pagehelper.PageInfo;
import com.simple.common.ResultData;
import com.simple.domain.dto.WxCUserBasicInfoDto;
import com.simple.domain.po.WxCUserBasicInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public interface WxCUserBasicInfoService {
@@ -67,4 +71,10 @@ public interface WxCUserBasicInfoService {

PageInfo<Map<String, Object>> queryListMap(WxCUserBasicInfoDto wxCUserBasicInfo, Integer pageNum, Integer pageSize);

void exportData(HttpServletRequest request, HttpServletResponse response, String tenantId);

void exportTemplate(HttpServletRequest request, HttpServletResponse response, String tenantId);

ResultData importTemplate(MultipartFile file, String tenantId);

}

+ 251
- 3
mallinkService/src/main/java/com/simple/service/impl/WxCUserBasicInfoServiceImpl.java Просмотреть файл

@@ -1,18 +1,34 @@
package com.simple.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simple.common.IdWorker;
import com.simple.common.Result;
import com.simple.common.ResultData;
import com.simple.domain.dto.WxCUserBasicInfoDto;
import com.simple.domain.po.WxCUser;
import com.simple.domain.po.WxCUserBasicInfo;
import com.simple.exception.MallinkException;
import com.simple.mapper.WxCUserBasicInfoMapper;
import com.simple.mapper.WxCUserMapper;
import com.simple.service.WxCUserBasicInfoService;
import org.apache.commons.io.FileUtils;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService {
@@ -20,6 +36,8 @@ public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService {
@Autowired
WxCUserBasicInfoMapper wxCUserBasicInfoMapper;

@Autowired
WxCUserMapper wxCUserMapper;

@Override
public PageInfo<WxCUserBasicInfo> listAsPage(WxCUserBasicInfo record, Integer pageIndex, Integer pageSize) {
@@ -83,5 +101,235 @@ public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService {
return PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> wxCUserBasicInfoMapper.findListMap(record));
}

@Override
public void exportData(HttpServletRequest request, HttpServletResponse response, String tenantId) {


WxCUser wxCUser = new WxCUser();
wxCUser.setTenantId(tenantId);
List<WxCUser> userlist = wxCUserMapper.findList(wxCUser);

WxCUserBasicInfoDto basicInfoDto = new WxCUserBasicInfoDto();
basicInfoDto.setTenantId(tenantId);
List<WxCUserBasicInfo> memberlist = wxCUserBasicInfoMapper.list(basicInfoDto);


XSSFWorkbook workbook;
String filepath="./uploads/";
File savefile = new File(filepath);
if (!savefile.exists()) {
savefile.mkdirs();
}
String filename=UUID.randomUUID()+".xlsx";
filepath=filepath+filename;
try {
File file=new File(filepath);
workbook=new XSSFWorkbook();
XSSFSheet sheetOne = workbook.createSheet("微信关注用户");
Row row = sheetOne.createRow(0);
Cell openIdCell = row.createCell(0);
Cell nickNameCell = row.createCell(1);
Cell phoneCell = row.createCell(2);
Cell genderCell = row.createCell(3);
Cell cityCell = row.createCell(4);
Cell provinceCell = row.createCell(5);
Cell languageCell = row.createCell(6);
Cell countryCodeCell = row.createCell(7);
Cell registerIpCell = row.createCell(8);
Cell sceneadressCell = row.createCell(9);

openIdCell.setCellValue("openId");
nickNameCell.setCellValue("昵称");
phoneCell.setCellValue("手机");
genderCell.setCellValue("性别");
cityCell.setCellValue("城市");
provinceCell.setCellValue("省份");
languageCell.setCellValue("语言");
countryCodeCell.setCellValue("国家代码");
registerIpCell.setCellValue("注册IP");
sceneadressCell.setCellValue("场景值");


for(int i=0;i<userlist.size();i++){

row = sheetOne.createRow(i+1);
openIdCell = row.createCell(0);
nickNameCell = row.createCell(1);
phoneCell = row.createCell(2);
genderCell = row.createCell(3);
cityCell = row.createCell(4);
provinceCell = row.createCell(5);
languageCell = row.createCell(6);
countryCodeCell = row.createCell(7);
registerIpCell = row.createCell(8);
sceneadressCell = row.createCell(9);

WxCUser entity = userlist.get(i);
openIdCell.setCellValue(entity.getOpenId());
nickNameCell.setCellValue(entity.getNickName());
phoneCell.setCellValue(entity.getPhone());
genderCell.setCellValue(entity.getGender());
cityCell.setCellValue(entity.getCity());
provinceCell.setCellValue(entity.getProvince());
languageCell.setCellValue(entity.getLanguage());
countryCodeCell.setCellValue(entity.getCountryCode());
registerIpCell.setCellValue(entity.getRegisterIp());
sceneadressCell.setCellValue(entity.getSceneAddress());

}


XSSFSheet sheetTwo = workbook.createSheet("会员信息");
Row rowtwo = sheetTwo.createRow(0);
Cell nameCellTwo = rowtwo.createCell(0);
Cell phoneCellTwo = rowtwo.createCell(1);
Cell birthdateCellTwo = rowtwo.createCell(2);
Cell educationCellTwo = rowtwo.createCell(3);


nameCellTwo.setCellValue("姓名");
phoneCellTwo.setCellValue("手机");
birthdateCellTwo.setCellValue("生日");
educationCellTwo.setCellValue("学历");


for(int i=0;i<memberlist.size();i++){
rowtwo = sheetTwo.createRow(i+1);
nameCellTwo = rowtwo.createCell(0);
phoneCellTwo = rowtwo.createCell(1);
birthdateCellTwo = rowtwo.createCell(2);
educationCellTwo = rowtwo.createCell(3);

WxCUserBasicInfo entity = memberlist.get(i);

nameCellTwo.setCellValue(entity.getName());
phoneCellTwo.setCellValue(entity.getPhone());
birthdateCellTwo.setCellValue(entity.getBirthdate());
educationCellTwo.setCellValue(entity.getEducation());

}


FileOutputStream fileOut = new FileOutputStream(file);
workbook.write(fileOut);
fileOut.close();
workbook.close();
downFile(filepath,filename,response,request);
FileUtils.forceDelete(file);
} catch (Exception e) {
e.printStackTrace();
}


}


public void downFile(String filePath,String filename, HttpServletResponse response,
HttpServletRequest req) throws IOException {
try {
response.reset();
response.setContentType("bin");
String agent = req.getHeader("user-agent");
if (agent.contains("Firefox")) {
response.setHeader("Content-disposition",
"attachment; filename="
+ new String(filename.getBytes("GB2312"),
"ISO-8859-1"));
} else {
response
.setHeader("Content-disposition",
"attachment; filename="
+ java.net.URLEncoder.encode(filename,
"UTF-8"));
}
// 循环取出流中的数据
byte[] b = new byte[1024];
int len;
InputStream inStream = new FileInputStream(filePath);
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void exportTemplate(HttpServletRequest request, HttpServletResponse response, String tenantId) {

XSSFWorkbook workbook;
String filepath="./uploads/";
File savefile = new File(filepath);
if (!savefile.exists()) {
savefile.mkdirs();
}
String filename=UUID.randomUUID()+".xlsx";
filepath=filepath+filename;
try {
File file=new File(filepath);
workbook=new XSSFWorkbook();

XSSFSheet sheetTwo = workbook.createSheet("会员信息");
Row rowtwo = sheetTwo.createRow(0);
Cell nameCellTwo = rowtwo.createCell(0);
Cell phoneCellTwo = rowtwo.createCell(1);
Cell sexCellTwo = rowtwo.createCell(2);
Cell birthdateCellTwo = rowtwo.createCell(3);
Cell educationCellTwo = rowtwo.createCell(4);

nameCellTwo.setCellValue("姓名");
phoneCellTwo.setCellValue("手机");
sexCellTwo.setCellValue("性别");
birthdateCellTwo.setCellValue("生日");
educationCellTwo.setCellValue("学历");

FileOutputStream fileOut = new FileOutputStream(file);
workbook.write(fileOut);
fileOut.close();
workbook.close();
downFile(filepath,filename,response,request);
FileUtils.forceDelete(file);
} catch (Exception e) {
e.printStackTrace();
}

}


@Override
public ResultData importTemplate(MultipartFile file, String tenantId) {

try {

ImportParams params = new ImportParams();
params.setTitleRows(1);
params.setHeadRows(1);
List<WxCUserBasicInfo> datalist = ExcelImportUtil.importExcel(file.getInputStream(),WxCUserBasicInfo.class, params);
WxCUserBasicInfo wxCUserBasicInfo = new WxCUserBasicInfo();
wxCUserBasicInfo.setTenantId(tenantId);
List<WxCUserBasicInfo> list = wxCUserBasicInfoMapper.findList(wxCUserBasicInfo);
final IdWorker idWorker = IdWorker.get();
for(WxCUserBasicInfo user:datalist){
WxCUserBasicInfo tempuser = list.stream().filter(u -> u.getPhone().equals(user.getPhone())).findFirst().get();
if(tempuser!=null){
tempuser.setSex(user.getSex());
tempuser.setEducation(user.getEducation());
tempuser.setBirthdate(user.getBirthdate());
tempuser.setName(user.getName());
wxCUserBasicInfoMapper.updateByPrimaryKeySelective(tempuser);
} else {
user.setId(idWorker.nextId());
wxCUserBasicInfoMapper.insertSelective(user);
}
}

} catch (Exception e) {
throw new MallinkException(500,"模板导入失败");
}

return new ResultData(Result.SUCCESS,"导入成功");
}



}

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