PersonGroupServiceImpl.java
9.58 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package com.viontech.keliu.service.impl;
import cn.hutool.core.date.DateUtil;
import com.viontech.keliu.dao.DMallPersonGroupDayCountDataDao;
import com.viontech.keliu.dao.DMallPersonGroupHourCountDataDao;
import com.viontech.keliu.dao.DataCountDao;
import com.viontech.keliu.entity.DMallPersonGroupDayCountData;
import com.viontech.keliu.entity.DMallPersonGroupHourCountData;
import com.viontech.keliu.entity.Person;
import com.viontech.keliu.entity.PersonGroupResult;
import com.viontech.keliu.service.PersonGroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;
/**
* Created with IntelliJ IDEA.
*
* @author: zhuhai
* Date: 2022-11-02
* Time: 17:57
*/
@Service
@Slf4j
public class PersonGroupServiceImpl implements PersonGroupService {
@Resource
private DataCountDao dataCountDao;
@Resource
private DMallPersonGroupDayCountDataDao dMallPersonGroupDayCountDataDao;
@Resource
private DMallPersonGroupHourCountDataDao dMallPersonGroupHourCountDataDao;
@Resource
private TaskExecutor taskExecutor;
@Override
public void personToGroup(List<Long> mallIdList, Date countDate) {
if (CollectionUtils.isEmpty(mallIdList)) {
return;
}
CountDownLatch countDownLatch = new CountDownLatch(mallIdList.size());
for (Long mallId : mallIdList) {
log.info("mallId:{},开始执行人员分组统计", mallId);
taskExecutor.execute(() -> {
try {
List<Person> personList = dataCountDao.getPersonByMallId(mallId, countDate);
//按天统计
personGroupDayStat(personList, mallId, countDate);
//按小时统计
Map<Date, List<Person>> hourPersonList = personList.stream().collect(Collectors.groupingBy((p) -> DateUtil.beginOfHour(p.getCountTime())));
for (Map.Entry<Date, List<Person>> entry : hourPersonList.entrySet()) {
personGroupHourStat(entry.getValue(), mallId, countDate, entry.getKey());
}
} catch (Exception e) {
log.error("mallId:{},人员分组异常", mallId, e);
} finally {
countDownLatch.countDown();
}
});
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
log.error("线程等待异常", e);
}
}
/**
* 顾客组数小时级统计表
* @param personList
* @param mallId
* @param countDate
* @param countTime
*/
private void personGroupHourStat(List<Person> personList, Long mallId, Date countDate, Date countTime) {
PersonGroupResult personGroupResult = personGroupCompute(personList);
DMallPersonGroupHourCountData dMallPersonGroupHourCountData = new DMallPersonGroupHourCountData();
dMallPersonGroupHourCountData.setMallId(mallId);
dMallPersonGroupHourCountData.setCountDate(countDate);
dMallPersonGroupHourCountData.setCountTime(countTime);
dMallPersonGroupHourCountData.setHour(DateUtil.hour(countTime, true));
dMallPersonGroupHourCountData.setTotalPersonNum(personList.size());
dMallPersonGroupHourCountData.setStaffNum(personGroupResult.getStaffNum());
dMallPersonGroupHourCountData.setCustomerNum(personGroupResult.getCustomerNum());
dMallPersonGroupHourCountData.setCustomerAdultNum(personGroupResult.getCustomerAdultNum());
dMallPersonGroupHourCountData.setCustomerChildNum(personGroupResult.getCustomerChildNum());
dMallPersonGroupHourCountData.setCustomerAlone(personGroupResult.getCustomerAlone());
dMallPersonGroupHourCountData.setCustomerGroup(personGroupResult.getCustomerGroup());
//判断数据库中是否已经存在记录,如果不存在则插入记录,否则更新记录
List<DMallPersonGroupHourCountData> countDataList = dMallPersonGroupHourCountDataDao.selectByMallIdAndCountDate(mallId, countDate, countTime);
if (CollectionUtils.isEmpty(countDataList)) {
//插入记录
dMallPersonGroupHourCountDataDao.create(dMallPersonGroupHourCountData);
} else {
//更新记录
DMallPersonGroupHourCountData countData = countDataList.get(0);
dMallPersonGroupHourCountData.setId(countData.getId());
dMallPersonGroupHourCountDataDao.update(dMallPersonGroupHourCountData);
}
}
/**
* 顾客组数天级统计表
* @param personList
* @param mallId
* @param countDate
*/
private void personGroupDayStat(List<Person> personList, Long mallId, Date countDate) {
DMallPersonGroupDayCountData dMallPersonGroupDayCountData = new DMallPersonGroupDayCountData();
if (CollectionUtils.isEmpty(personList)) {
dMallPersonGroupDayCountData.setMallId(mallId);
dMallPersonGroupDayCountData.setCountDate(countDate);
dMallPersonGroupDayCountData.setTotalPersonNum(0);
dMallPersonGroupDayCountData.setStaffNum(0);
dMallPersonGroupDayCountData.setCustomerNum(0);
dMallPersonGroupDayCountData.setCustomerAdultNum(0);
dMallPersonGroupDayCountData.setCustomerChildNum(0);
dMallPersonGroupDayCountData.setCustomerAlone(0);
dMallPersonGroupDayCountData.setCustomerGroup(0);
} else {
PersonGroupResult personGroupResult = personGroupCompute(personList);
dMallPersonGroupDayCountData.setMallId(mallId);
dMallPersonGroupDayCountData.setCountDate(countDate);
dMallPersonGroupDayCountData.setTotalPersonNum(personList.size());
dMallPersonGroupDayCountData.setStaffNum(personGroupResult.getStaffNum());
dMallPersonGroupDayCountData.setCustomerNum(personGroupResult.getCustomerNum());
dMallPersonGroupDayCountData.setCustomerAdultNum(personGroupResult.getCustomerAdultNum());
dMallPersonGroupDayCountData.setCustomerChildNum(personGroupResult.getCustomerChildNum());
dMallPersonGroupDayCountData.setCustomerAlone(personGroupResult.getCustomerAlone());
dMallPersonGroupDayCountData.setCustomerGroup(personGroupResult.getCustomerGroup());
}
//判断数据库中是否已经存在记录,如果不存在则插入记录,否则更新记录
List<DMallPersonGroupDayCountData> countDataList = dMallPersonGroupDayCountDataDao.selectByMallIdAndCountDate(mallId, countDate);
if (CollectionUtils.isEmpty(countDataList)) {
//插入记录
dMallPersonGroupDayCountDataDao.create(dMallPersonGroupDayCountData);
} else {
//更新记录
DMallPersonGroupDayCountData countData = countDataList.get(0);
dMallPersonGroupDayCountData.setId(countData.getId());
dMallPersonGroupDayCountDataDao.update(dMallPersonGroupDayCountData);
}
}
/**
* 分组统计逻辑
* @param personList
* @return
*/
private PersonGroupResult personGroupCompute(List<Person> personList) {
int staffNum = (personList.stream().filter((t) -> t.getPersonType().equals(1)).collect(Collectors.toList())).size();
List<Person> customerList = (personList.stream().filter((t) -> t.getPersonType().equals(0)).collect(Collectors.toList())).stream().sorted(Comparator.comparing(Person::getCountTime)).collect(Collectors.toList());
int customerNum = customerList.size();
int customerAdultNum = 0;
int customerChildNum = 0;
for (Person person : customerList) {
Integer mood = person.getMood();
if (mood == null) {
continue;
}
if (mood.equals(108)) {
customerAdultNum += 1;
} else if (mood.equals(107)) {
customerChildNum += 1;
}
}
int customerAlone = 0;
int customerGroup = 0;
StringBuilder str = new StringBuilder();
for (int i = 0; i < customerList.size() - 1; ++i) {
Person pvo1 = customerList.get(i);
Person 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) {
customerAlone += 1;
} else {
//分组加一
customerGroup += 1;
}
}
PersonGroupResult result = new PersonGroupResult();
result.setStaffNum(staffNum);
result.setCustomerNum(customerNum);
result.setCustomerAdultNum(customerAdultNum);
result.setCustomerChildNum(customerChildNum);
result.setCustomerAlone(customerAlone);
result.setCustomerGroup(customerGroup);
return result;
}
}