UserMessengeController.java
6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package zzsy.controller;
/**
* Created by 周伟鹏 on 2018/10/16.
*/
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import zzsy.dao.UserMessengeRepository;
import zzsy.entity.UserMessenge;
import zzsy.utils.ErrorEnum;
import zzsy.utils.ResponseBean;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.regex.Pattern;
/**
* @Auther: Administrator
* @Date: 2018/10/16 16:19
* @Description:
*/
@RestController
@RequestMapping(value = "/userApi")
public class UserMessengeController {
@Autowired
UserMessengeRepository userMessengeService;
@PostMapping("save")
public ResponseBean save(@RequestParam(name = "name") String name, @RequestParam(name = "phone")String phone, @RequestParam(name = "company")String company,@RequestParam(name = "videoId")Integer videoId,
@RequestParam(name = "videoTitle") String videoTitle, @RequestParam(name = "videoUrl") String videoUrl){
ResponseBean responseBean = new ResponseBean();
try {
String REGEX_PHONE = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
boolean isPhone = Pattern.matches(REGEX_PHONE, phone);
if(isPhone){
UserMessenge userMessenge = new UserMessenge();
userMessenge.setPhone(phone);
userMessenge.setVideoId(videoId);
Example<UserMessenge> example = Example.of(userMessenge);
List<UserMessenge> userMessengeList = userMessengeService.findAll(example);
if(userMessengeList.isEmpty()){
userMessenge.setName(name);
userMessenge.setCompany(company);
userMessenge.setVideoTitle(videoTitle);
userMessenge.setVideoUrl(videoUrl);
userMessengeService.save(userMessenge);
}else {
userMessenge.setId(userMessengeList.get(0).getId());
userMessenge.setName(name);
userMessenge.setCompany(company);
userMessenge.setVideoTitle(videoTitle);
userMessenge.setVideoUrl(videoUrl);
userMessengeService.saveAndFlush(userMessenge);
}
}else{
responseBean = new ResponseBean(ErrorEnum.PHONE_ERROR.getErrorCode(),ErrorEnum.PHONE_ERROR.getErrorMessage());
}
}catch (DataIntegrityViolationException e){
responseBean = new ResponseBean(ErrorEnum.PHONE_REPEAT.getErrorCode(),ErrorEnum.PHONE_REPEAT.getErrorMessage());
}
return responseBean;
}
@RequestMapping("messenge")
public ResponseBean findAll() {
ResponseBean responseBean = new ResponseBean();
responseBean.setData(userMessengeService.findAll());
return responseBean;
}
@RequestMapping("excel")
public void getExcel(HttpServletResponse response) {
List<UserMessenge> userMessengeList = userMessengeService.findAll();
String fileName = "正在上演用户信息表";
XSSFWorkbook wb = new XSSFWorkbook();
ServletOutputStream out = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
XSSFSheet sheetlist = wb.createSheet(fileName);
XSSFRow row = sheetlist.createRow(0);//行
XSSFCell cell = row.createCell(0);//列
cell.setCellValue("填写时间");
cell = row.createCell(1);//列
cell.setCellValue("视频名称");
cell = row.createCell(2);//列
cell.setCellValue("视频链接");
cell = row.createCell(3);//列
cell.setCellValue("姓名");
cell = row.createCell(4);//列
cell.setCellValue("电话");
cell = row.createCell(5);//列
cell.setCellValue("公司");
cell = row.createCell(6);//列
cell.setCellValue("视频id");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
for (int i = 0; i < userMessengeList.size(); i++) {
row = sheetlist.createRow((short) (i + 1));//行
cell = row.createCell(0);//列
cell.setCellValue(dateFormat.format(userMessengeList.get(i).getInsertDate()));
cell = row.createCell(1);//列
cell.setCellValue(userMessengeList.get(i).getVideoTitle());
cell = row.createCell(2);//列
cell.setCellValue(userMessengeList.get(i).getVideoUrl());
cell = row.createCell(3);//列
cell.setCellValue(userMessengeList.get(i).getName());
cell = row.createCell(4);//列
cell.setCellValue(userMessengeList.get(i).getPhone());
cell = row.createCell(5);//列
cell.setCellValue(userMessengeList.get(i).getCompany());
cell = row.createCell(6);//列
cell.setCellValue(userMessengeList.get(i).getVideoId());
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
wb.write(os);
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xlsx").getBytes(), "iso-8859-1"));
out = response.getOutputStream();
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (final Exception e) {
e.printStackTrace();
try {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
} catch (Exception e1) {
e1.printStackTrace();
}
} finally {
try {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}