yujian
2020-04-29 b5a5e733e5376a13c9a0f014175fa76cbdca203f
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
package com.yeshi.fanli.service.impl.count;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.yeshi.utils.DateUtil;
 
import com.yeshi.fanli.dao.user.count.DailyCountUserDao;
import com.yeshi.fanli.entity.admin.count.DailyCountUser;
import com.yeshi.fanli.entity.admin.count.DailyCountUser.DailyCountUserEnum;
import com.yeshi.fanli.entity.bus.user.Extract;
import com.yeshi.fanli.entity.bus.user.UserInfoRegister;
import com.yeshi.fanli.entity.system.ChannelEnum;
import com.yeshi.fanli.service.inter.count.DailyCountUserService;
import com.yeshi.fanli.service.inter.money.extract.ExtractAuditRecordService;
import com.yeshi.fanli.service.inter.order.CommonOrderCountService;
import com.yeshi.fanli.service.inter.redpack.RedPackDetailService;
import com.yeshi.fanli.service.inter.tlj.UserTaoLiJinDetailService;
import com.yeshi.fanli.service.inter.user.UserActiveLogService;
import com.yeshi.fanli.service.inter.user.UserInfoRegisterService;
import com.yeshi.fanli.service.inter.user.integral.IntegralDetailService;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TimeUtil;
 
@Service
public class DailyCountUserServiceImpl implements DailyCountUserService {
 
    @Resource
    private DailyCountUserDao dailyCountUserDao;
    
    @Resource
    private UserInfoRegisterService userInfoRegisterService;
    
    @Resource
    private CommonOrderCountService commonOrderCountService;
    
    @Resource
    private UserActiveLogService userActiveLogService;
    
    @Resource
    private IntegralDetailService integralDetailService;
    
    @Resource
    private RedPackDetailService redPackDetailService;
    
    @Resource
    private UserTaoLiJinDetailService userTaoLiJinDetailService;
    
    @Resource
    private ExtractAuditRecordService extractAuditRecordService;
    
 
    @Override
    public List<DailyCountUser> getDailyCountList(String type, Date startTime, Date endTime, String channel) throws Exception {
        // 查询类型
        DailyCountUserEnum typeEnum = getTypeEnum(type);
        if (typeEnum == null) {
            return null;
        }
 
        // 初始化数据
        initData(typeEnum);
 
        return dailyCountUserDao.query(typeEnum, startTime, endTime, channel);
    }
 
    /**
     * 获取枚举类型
     * @param type
     * @return
     */
    private DailyCountUserEnum getTypeEnum(String name) {
        if (StringUtil.isNullOrEmpty(name)) {
            return null;
        }
        
        DailyCountUserEnum[] array = DailyCountUserEnum.values();
        for (int i = 0; i < array.length; i ++) {
            if (array[i].name().equals(name)) {
                return array[i];
            }
        }
        return null;
    }
 
    
    /**
     * 获取枚举类型
     * @param type
     * @return
     */
    @Override
    public String getTypeEnumDesc(String name) {
        if (StringUtil.isNullOrEmpty(name)) {
            return "";
        }
        
        DailyCountUserEnum[] array = DailyCountUserEnum.values();
        for (int i = 0; i < array.length; i ++) {
            if (array[i].name().equals(name)) {
                return array[i].getDesc();
            }
        }
 
        return "";
    }
 
    
    
    /**
     * 初始数据
     * @param typeEnum
     * @throws Exception
     */
    @Override
    public void initData(DailyCountUserEnum typeEnum) throws Exception {
        Date lastDate = null;
        DailyCountUser lastRecord = dailyCountUserDao.getMaxDate(typeEnum);
        if (lastRecord != null) {
            lastDate = lastRecord.getUpdateDate();
        } else {
            lastDate = TimeUtil.parse("2017-05-06");
        }
        long min = DateUtil.dateDiffMin(lastDate, new Date());
        if (min <= 10) { // 10分钟以内不统计
            return;
        }
        
        Date today = new Date();
        int betweenDays = DateUtil.daysBetween2(lastDate, today);
        for (int i = 0; i <= betweenDays; i++) {
            // 计算日期
            String preDay = DateUtil.plusDay(i, lastDate);
            // 统计数据
            if (DailyCountUserEnum.newUserChannel == typeEnum) {
                addNewUserChannel(preDay, typeEnum);
            } else if (DailyCountUserEnum.newUserDownOrderDay == typeEnum) {
                addNewUserDownOrderDay(preDay, typeEnum);
            } else if (DailyCountUserEnum.newUserDownOrderWeek == typeEnum) {
                addNewUserDownOrderWeek(preDay);
            } else if (DailyCountUserEnum.newUserDownOrderWeek3 == typeEnum) {
                addNewUserDownOrderWeek(preDay);
            } else if (DailyCountUserEnum.activeAgain90 == typeEnum) {
                addActiveAgain90(preDay, typeEnum);
            } else if (DailyCountUserEnum.integralNum == typeEnum) {
                addIntegralNum(preDay, typeEnum);
            } else if (DailyCountUserEnum.newUserTljNum == typeEnum) {
                addNewUserTljNum(preDay, typeEnum);
            } else if (DailyCountUserEnum.newUserTljMoney == typeEnum) {
                addNewUserTljMoney(preDay, typeEnum);
            } else if (DailyCountUserEnum.redpackNum == typeEnum) {
                addRedpackNum(preDay, typeEnum);
            } else if (DailyCountUserEnum.redpackMoney == typeEnum) {
                addRedpackMoney(preDay, typeEnum);
            } else if (DailyCountUserEnum.extractApplyNumber == typeEnum) {
                addExtractApplyNumber(preDay, typeEnum);
            } else if (DailyCountUserEnum.extractApplyMoney == typeEnum) {
                addExtractApplyMoney(preDay, typeEnum);
            } else if (DailyCountUserEnum.extractAuditPass == typeEnum) {
                addExtractAuditNum(preDay, Extract.STATE_PASS, typeEnum);
            } else if (DailyCountUserEnum.extractAuditReject == typeEnum) {
                addExtractAuditNum(preDay, Extract.STATE_REJECT, typeEnum);
            } 
        }
    }
 
    
    /**
     * 统计各个渠道新增用户数量
     * @param preDay
     */
    private void addNewUserChannel(String preDay, DailyCountUserEnum typeEnum) throws Exception{
        ChannelEnum[] channels = ChannelEnum.values();
        for (int i = 0; i < channels.length; i++) {
            Long count = userInfoRegisterService.countByChannelAndDay(channels[i].getVlaue(), preDay);
            if (count == null) {
                count = 0L;
            }
            DailyCountUser obj = new DailyCountUser();
            obj.setTotal(BigDecimal.valueOf(count));
            obj.setType(typeEnum);
            obj.setChannel(channels[i].name());
            obj.setUpdateDate(new Date());
            obj.setDay(TimeUtil.parse(preDay));
            obj.setId(StringUtil.Md5(preDay + channels[i].name() + typeEnum.name()));
            dailyCountUserDao.save(obj);
        }
    }
    
    
    /**
     * 统计各个渠道每日下单转化率
     * @param preDay
     * @throws Exception
     */
    private void addNewUserDownOrderDay(String preDay, DailyCountUserEnum typeEnum) throws Exception {
        // 统计当日有效订单
        List<Long> listOrder = commonOrderCountService.getUidByValidOrderByDay(preDay);
        
        // 查询当日新注册用户
        List<UserInfoRegister> listRegister = userInfoRegisterService.listByDay(preDay);
        
        ChannelEnum[] channels = ChannelEnum.values();
        for (int i = 0; i < channels.length; i++) {
            long totalDay = 0;
            long totalValid = 0;
            String channelName = channels[i].getVlaue();
            
            if (listOrder != null && listOrder.size() > 0 && listRegister != null && listRegister.size() > 0) {
                for (UserInfoRegister register: listRegister) {
                    String channel = register.getChannel();
                    if (channelName.equalsIgnoreCase(channel)) {
                        // 当日渠道用户
                        totalDay ++ ;
                        
                        Long id = register.getId();
                        // 判断当前用户是否今日下单
                        for (Long uid: listOrder) {
                            if (uid.longValue() == id.longValue()) {
                                totalValid ++;
                                break;
                            }
                        }
                    }
                }
            }
                
            // 计算比例: 下单用户数/新增用户数
            BigDecimal total = new BigDecimal(0);
            if (totalDay > 0 && totalValid > 0) {
                total = MoneyBigDecimalUtil.div(BigDecimal.valueOf(totalValid),BigDecimal.valueOf(totalDay));
                total = MoneyBigDecimalUtil.mul(total, BigDecimal.valueOf(100));
            }    
                
            DailyCountUser obj = new DailyCountUser();
            obj.setTotal(total);
            obj.setRate(true);
            obj.setTotalDay(BigDecimal.valueOf(totalDay));
            obj.setTotalValid(BigDecimal.valueOf(totalValid));
            
            obj.setType(typeEnum);
            obj.setChannel(channels[i].name());
            obj.setUpdateDate(new Date());
            obj.setDay(TimeUtil.parse(preDay));
            obj.setId(StringUtil.Md5(preDay + channels[i].name() + typeEnum.name()));
            dailyCountUserDao.save(obj);
        }
    }
 
    /**
     * 统计当周下单数据
     * @param preDay
     * @throws Exception
     */
    private void addNewUserDownOrderWeek(String preDay) throws Exception {
        // 加上6天 -注册一周内数据
        Date startDay = new Date(TimeUtil.convertDateToTemp2(preDay + " 23:59:59"));
        Date limitDay = DateUtil.plusDayDate(6, startDay);
        
        // 渠道数据
        ChannelEnum[] channels = ChannelEnum.values();
        for (int i = 0; i < channels.length; i++) {
            long totalUser = 0;
            long totalWeek = 0;
            long totalWeek3 = 0;
            
            List<Long> listUid = new ArrayList<Long>();
            
            // 查询今日新增用户
            List<UserInfoRegister> list = userInfoRegisterService.listByChannelAndDay(channels[i].getVlaue(), preDay);
            
            
            if (list != null && list.size() > 0) {
                totalUser = list.size();
                for (UserInfoRegister user : list) {
                    listUid.add(user.getId());
                }
            }
            
            if (listUid.size() > 0) {
                List<Long> listUidDown = commonOrderCountService.getDownOrderUserByListUidAndDate(limitDay, listUid);
                if (listUidDown != null && listUidDown.size() > 0) {
                    for (Long uid: listUid) {
                        int total = 0;
                        for (Long id: listUidDown) {
                            if (uid.longValue() == id.longValue()) {
                                total ++;
                            }
                        }
                        
                        if (total > 0) {
                            totalWeek ++;
                        }
                        
                        if (total >= 3) {
                            totalWeek3 ++;
                        }
                    }
                }
            }
            
            
            // 计算比例:当周 下单用户数/新增用户数
            BigDecimal total = new BigDecimal(0);
            if (totalUser > 0 && totalWeek > 0) {
                total = MoneyBigDecimalUtil.div(BigDecimal.valueOf(totalWeek),BigDecimal.valueOf(totalUser));
                total = MoneyBigDecimalUtil.mul(total, BigDecimal.valueOf(100));
            }    
            DailyCountUser obj = new DailyCountUser();
            obj.setRate(true);
            obj.setTotalDay(BigDecimal.valueOf(totalUser));
            obj.setTotalValid(BigDecimal.valueOf(totalWeek));
            obj.setTotal(total);
            obj.setType(DailyCountUserEnum.newUserDownOrderWeek);
            obj.setChannel(channels[i].name());
            obj.setUpdateDate(new Date());
            obj.setDay(TimeUtil.parse(preDay));
            obj.setId(StringUtil.Md5(preDay + channels[i].name() + DailyCountUserEnum.newUserDownOrderWeek.name()));
            dailyCountUserDao.save(obj);
            
            
            // 计算比例: 下3单用户数/新增用户数
            BigDecimal total3 = new BigDecimal(0);
            if (totalUser > 0 && totalWeek3 > 0) {
                total3 = MoneyBigDecimalUtil.div(BigDecimal.valueOf(totalWeek3),BigDecimal.valueOf(totalUser));
                total3 = MoneyBigDecimalUtil.mul(total3, BigDecimal.valueOf(100));
            }    
            DailyCountUser obj3 = new DailyCountUser();
            obj3.setRate(true);
            obj3.setTotalDay(BigDecimal.valueOf(totalUser));
            obj3.setTotalValid(BigDecimal.valueOf(totalWeek3));
            obj3.setTotal(total3);
            obj3.setType(DailyCountUserEnum.newUserDownOrderWeek3);
            obj3.setChannel(channels[i].name());
            obj3.setUpdateDate(new Date());
            obj3.setDay(TimeUtil.parse(preDay));
            obj3.setId(StringUtil.Md5(preDay + channels[i].name() + DailyCountUserEnum.newUserDownOrderWeek3.name()));
            dailyCountUserDao.save(obj3);
        }
    }
    
    /**
     * 统计90天内再次活跃
     * @param preDay
     * @throws Exception
     */
    private void addActiveAgain90(String preDay, DailyCountUserEnum typeEnum) throws Exception{
        long count = userActiveLogService.count90DaysLaterActiveNum(preDay);
        
        DailyCountUser obj3 = new DailyCountUser();
        obj3.setTotal(BigDecimal.valueOf(count));
        obj3.setType(typeEnum);
        obj3.setUpdateDate(new Date());
        obj3.setDay(TimeUtil.parse(preDay));
        obj3.setId(StringUtil.Md5(preDay + typeEnum.name()));
        dailyCountUserDao.save(obj3);
    }
    
    
    /**
     * 金币新增数量
     * @param preDay
     * @throws Exception
     */
    private void addIntegralNum(String preDay, DailyCountUserEnum typeEnum) throws Exception{
        Long count = integralDetailService.countNewAddByDate(preDay);
        if (count == null) {
            count = 0L;
        }
        
        DailyCountUser obj3 = new DailyCountUser();
        obj3.setTotal(BigDecimal.valueOf(count));
        obj3.setType(typeEnum);
        obj3.setUpdateDate(new Date());
        obj3.setDay(TimeUtil.parse(preDay));
        obj3.setId(StringUtil.Md5(preDay + typeEnum.name()));
        dailyCountUserDao.save(obj3);
    }
    
    /**
     * 红包新增数量
     * @param preDay
     * @throws Exception
     */
    private void addRedpackNum(String preDay, DailyCountUserEnum typeEnum) throws Exception{
        Long total = redPackDetailService.countNumByDay(preDay);
        if (total == null) {
            total = 0L;
        }
        
        DailyCountUser obj3 = new DailyCountUser();
        obj3.setTotal(new BigDecimal(total));
        obj3.setType(typeEnum);
        obj3.setUpdateDate(new Date());
        obj3.setDay(TimeUtil.parse(preDay));
        obj3.setId(StringUtil.Md5(preDay + typeEnum.name()));
        dailyCountUserDao.save(obj3);
    }
    
    
    /**
     * 红包新增金额
     * @param preDay
     * @throws Exception
     */
    private void addRedpackMoney(String preDay, DailyCountUserEnum typeEnum) throws Exception{
        BigDecimal total = redPackDetailService.countMoneyByDay(preDay);
        if (total == null) {
            total = new BigDecimal(0);
        }
        
        DailyCountUser obj3 = new DailyCountUser();
        obj3.setTotal(total);
        obj3.setType(typeEnum);
        obj3.setUpdateDate(new Date());
        obj3.setDay(TimeUtil.parse(preDay));
        obj3.setId(StringUtil.Md5(preDay + typeEnum.name()));
        dailyCountUserDao.save(obj3);
    }
    
    
    /**
     * 红包新增数量
     * @param preDay
     * @throws Exception
     */
    private void addNewUserTljNum(String preDay, DailyCountUserEnum typeEnum) throws Exception{
        Long total = userTaoLiJinDetailService.countNumByDay(preDay);
        if (total == null) {
            total = 0L;
        }
        
        DailyCountUser obj3 = new DailyCountUser();
        obj3.setTotal(new BigDecimal(total));
        obj3.setType(typeEnum);
        obj3.setUpdateDate(new Date());
        obj3.setDay(TimeUtil.parse(preDay));
        obj3.setId(StringUtil.Md5(preDay + typeEnum.name()));
        dailyCountUserDao.save(obj3);
    }
    
    
    /**
     * 红包新增金额
     * @param preDay
     * @throws Exception
     */
    private void addNewUserTljMoney(String preDay, DailyCountUserEnum typeEnum) throws Exception{
        BigDecimal total = userTaoLiJinDetailService.countMoneyByDay(preDay);
        if (total == null) {
            total = new BigDecimal(0);
        }
        
        DailyCountUser obj3 = new DailyCountUser();
        obj3.setTotal(total);
        obj3.setType(typeEnum);
        obj3.setUpdateDate(new Date());
        obj3.setDay(TimeUtil.parse(preDay));
        obj3.setId(StringUtil.Md5(preDay + typeEnum.name()));
        dailyCountUserDao.save(obj3);
    }
    
    
    /**
     * 提现申请数量
     * @param preDay
     * @throws Exception
     */
    private void addExtractApplyNumber(String preDay, DailyCountUserEnum typeEnum) throws Exception{
        Long total = extractAuditRecordService.countApplyNumberByDay(preDay);
        if (total == null) {
            total = 0L;
        }
        
        DailyCountUser obj3 = new DailyCountUser();
        obj3.setTotal(new BigDecimal(total));
        obj3.setType(typeEnum);
        obj3.setUpdateDate(new Date());
        obj3.setDay(TimeUtil.parse(preDay));
        obj3.setId(StringUtil.Md5(preDay + typeEnum.name()));
        dailyCountUserDao.save(obj3);
    }
    
    
    /**
     * 提现申请金额
     * @param preDay
     * @throws Exception
     */
    private void addExtractApplyMoney(String preDay, DailyCountUserEnum typeEnum) throws Exception{
        BigDecimal total = extractAuditRecordService.countApplyMoneyByDay(preDay);
        if (total == null) {
            total = new BigDecimal(0);
        }
        
        DailyCountUser obj3 = new DailyCountUser();
        obj3.setTotal(total);
        obj3.setType(typeEnum);
        obj3.setUpdateDate(new Date());
        obj3.setDay(TimeUtil.parse(preDay));
        obj3.setId(StringUtil.Md5(preDay + typeEnum.name()));
        dailyCountUserDao.save(obj3);
    }
    
    /**
     * 提现审核数量
     * @param preDay
     * @throws Exception
     */
    private void addExtractAuditNum(String preDay, int state, DailyCountUserEnum typeEnum) throws Exception{
        Long total = extractAuditRecordService.countAuditNumberByDay(state, preDay);
        if (total == null) {
            total = 0L;
        }
        
        DailyCountUser obj3 = new DailyCountUser();
        obj3.setTotal(new BigDecimal(total));
        obj3.setType(typeEnum);
        obj3.setUpdateDate(new Date());
        obj3.setDay(TimeUtil.parse(preDay));
        obj3.setId(StringUtil.Md5(preDay + state + typeEnum.name()));
        dailyCountUserDao.save(obj3);
    }
    
    
}