DataCountController.java
8.45 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package com.viontech.keliu.controller;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.fastjson.JSON;
import com.viontech.keliu.dao.DataCountDao;
import com.viontech.keliu.vo.MallVo;
import com.viontech.keliu.vo.PersonVo;
import com.viontech.keliu.vo.ResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Created with IntelliJ IDEA.
*
* @author: zhuhai
* Date: 2022-10-31
* Time: 21:00
*/
@RestController
public class DataCountController {
@Autowired
private DataCountDao dataCountDao;
private static final Logger logger = LoggerFactory.getLogger(DataCountController.class);
public DataCountController() {
}
@GetMapping({"/personGroup"})
public Object getPersonGroup(@RequestParam("countdate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date countdate, @RequestParam("accountid") Integer accountid) {
List<ResultVo> resultVos = new ArrayList();
try {
List<PersonVo> personVos = this.dataCountDao.get_person(countdate, accountid);
List<MallVo> mallVos = this.dataCountDao.get_malls(accountid);
resultVos = this.getPersonList(mallVos, personVos);
} catch (Exception ex) {
logger.info("抓拍数据分组出错{}",ex.getMessage());
}
HashMap<String, Object> result = new HashMap();
if (resultVos.size() > 0) {
List<ResultVo> resultVoList = resultVos.stream().sorted(Comparator.comparing(ResultVo::getCount_person).reversed()).collect(Collectors.toList());
result.put("msg_code", 200);
result.put("msg_info", "成功");
result.put("data", resultVoList);
} else {
result.put("msg_code", 506);
result.put("msg_info", "数据为空");
result.put("data", resultVos);
}
return result;
}
@GetMapping({"/personDownload"})
public void exportExcel(@RequestParam Integer accountid, @RequestParam("countdate") Date countdate, HttpServletResponse response) throws IOException {
List<PersonVo> personVos = this.dataCountDao.get_person(countdate, accountid);
List<MallVo> mallVos = this.dataCountDao.get_malls(accountid);
List<ResultVo> resultVos = this.getPersonList(mallVos, personVos);
try {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("抓拍分组-" + DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(LocalDateTime.now()), "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
((ExcelWriterBuilder) EasyExcel.write(response.getOutputStream(), ResultVo.class).head(this.titleHead())).autoCloseStream(Boolean.FALSE).sheet("模板").doWrite(resultVos);
} catch (IOException var9) {
response.reset();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
Map<String, String> map = new HashMap(2);
map.put("status", "failure");
map.put("message", "下载文件失败" + var9.getMessage());
response.getWriter().println(JSON.toJSONString(map));
}
}
public List<ResultVo> getPersonList(List<MallVo> mallVos, List<PersonVo> personVos) {
List<ResultVo> resultVos = new ArrayList();
Iterator iterator = mallVos.iterator();
while(iterator.hasNext()) {
MallVo mall = (MallVo) iterator.next();
ResultVo resultVo = new ResultVo();
resultVo.setMall_name(mall.getName());
List<PersonVo> tempList = personVos.stream().filter((p) -> p.getMall_id().equals(mall.getId())).collect(Collectors.toList());
if (tempList.size() == 0) {
resultVo.setClerk_num("-");
resultVo.setCount_person("-");
resultVo.setCustomer_num("-");
resultVo.setCustomer_adult("-");
resultVo.setCustomer_child("-");
resultVo.setCustomer_alone("-");
resultVo.setCustomer_group("-");
resultVos.add(resultVo);
} else {
resultVo.setCount_person(String.valueOf(tempList.size()));
Integer clerkNum = (tempList.stream().filter((t) -> t.getPerson_type().equals(1)).collect(Collectors.toList())).size();
resultVo.setClerk_num(String.valueOf(clerkNum));
List<PersonVo> customerList = (tempList.stream().filter((t) -> t.getPerson_type().equals(0)).collect(Collectors.toList())).stream().sorted(Comparator.comparing(PersonVo::getCounttime)).collect(Collectors.toList());
resultVo.setCustomer_num(String.valueOf(customerList.size()));
Integer customerAdultNum = 0;
Integer customerChildNum = 0;
for (PersonVo personVo : customerList) {
Integer mood = personVo.getMood();
if (mood == null) {
continue;
}
if (mood.equals(108)) {
customerAdultNum += 1;
} else if (mood.equals(107)) {
customerChildNum += 1;
}
}
resultVo.setCustomer_adult(String.valueOf(customerAdultNum));
resultVo.setCustomer_child(String.valueOf(customerChildNum));
int customer_alone = 0;
int customer_group = 0;
StringBuilder str = new StringBuilder();
for (int i = 0; i < customerList.size() - 1; ++i) {
PersonVo pvo1 = customerList.get(i);
PersonVo pvo2 = customerList.get(i + 1);
Long time1 = pvo1.getCounttime().getTime();
Long time2 = pvo2.getCounttime().getTime();
int seconds = (int) ((time2 - time1) / 1000L);
if (i == 0) {
str.append(pvo1.getMood());
}
if (seconds <= 6) {
str.append(",").append(pvo2.getMood());
} else {
str.append("#").append(pvo2.getMood());
}
}
String[] group = str.toString().split("#");
for (String s : group) {
String[] split = s.split(",");
//单独的加一
if (split.length == 1) {
customer_alone += 1;
} else {
//分组加一
customer_group += 1;
}
}
resultVo.setCustomer_alone(String.valueOf(customer_alone));
resultVo.setCustomer_group(String.valueOf(customer_group));
resultVos.add(resultVo);
}
}
return resultVos;
}
private List<List<String>> titleHead() {
List<List<String>> list = new ArrayList();
List<String> head0 = new ArrayList();
head0.add("广场名称");
List<String> head1 = new ArrayList();
head1.add("总人数");
List<String> head2 = new ArrayList();
head2.add("店员人数");
List<String> head3 = new ArrayList();
head3.add("顾客人数");
List<String> head4 = new ArrayList();
head4.add("顾客组数(30秒)");
List<String> head5 = new ArrayList();
head5.add("顾客组数(60秒)");
list.add(head0);
list.add(head1);
list.add(head2);
list.add(head3);
list.add(head4);
list.add(head5);
return list;
}
}