admin
2020-04-27 e44f0a6f4aba26f3b9c63b6581668cb3da5ff577
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
package com.yeshi.fanli.service.impl.count;
 
import java.math.BigDecimal;
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.DailyCountCouponDao;
import com.yeshi.fanli.entity.admin.count.DailyCountCoupon;
import com.yeshi.fanli.entity.admin.count.DailyCountCoupon.DailyCountCouponEnum;
import com.yeshi.fanli.entity.system.SystemCoupon.CouponTypeEnum;
import com.yeshi.fanli.service.inter.count.DailyCountCouponService;
import com.yeshi.fanli.service.inter.count.UserSystemCouponCountService;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TimeUtil;
 
@Service
public class DailyCountCouponServiceImpl implements DailyCountCouponService {
 
    @Resource
    private DailyCountCouponDao dailyCountCouponDao;
    
    @Resource
    private UserSystemCouponCountService userSystemCouponCountService;
 
    @Override
    public List<DailyCountCoupon> getDailyCountList(String type, Date startTime, Date endTime) throws Exception {
        // 查询类型
        DailyCountCouponEnum typeEnum = getTypeEnum(type);
        if (typeEnum == null) {
            return null;
        }
 
        // 初始化数据
        initData(typeEnum);
 
        return dailyCountCouponDao.query(typeEnum, startTime, endTime);
    }
 
    /**
     * 获取枚举类型
     * @param type
     * @return
     */
    private DailyCountCouponEnum getTypeEnum(String name) {
        if (StringUtil.isNullOrEmpty(name)) {
            return null;
        }
        
        DailyCountCouponEnum[] array = DailyCountCouponEnum.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 "";
        }
        
        DailyCountCouponEnum[] array = DailyCountCouponEnum.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(DailyCountCouponEnum typeEnum) throws Exception {
        Date lastDate = null;
        DailyCountCoupon lastRecord = dailyCountCouponDao.getMaxDate(typeEnum);
        if (lastRecord != null) {
            lastDate = lastRecord.getUpdateDate();
        } else {
            lastDate = TimeUtil.parse("2019-03-04");
        }
        
        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);
            // 统计数据
            BigDecimal total = getCountByType(preDay, typeEnum);
 
            // 保存信息
            DailyCountCoupon obj = new DailyCountCoupon();
            obj.setTotal(total);
            obj.setType(typeEnum);
            obj.setUpdateDate(new Date());
            obj.setDay(TimeUtil.parse(preDay));
            obj.setId(StringUtil.Md5(preDay + typeEnum.name()));
            dailyCountCouponDao.save(obj);
        }
    }
 
    /**
     * 数据统计
     * @param preDay
     * @param typeEnum
     * @return
     */
    private BigDecimal getCountByType(String preDay, DailyCountCouponEnum typeEnum) {
        BigDecimal total = null;
        if (DailyCountCouponEnum.freeCouponNum == typeEnum) {
            total = userSystemCouponCountService.countCouponNumByDay(CouponTypeEnum.freeCoupon, preDay);
        } else if (DailyCountCouponEnum.freeCouponMoney == typeEnum) {
            total = userSystemCouponCountService.countFreeMoneyByTypeAndDay(CouponTypeEnum.freeCoupon, preDay);
        } else if (DailyCountCouponEnum.freeCouponBuyNum == typeEnum) {
            total = userSystemCouponCountService.countCouponNumByDay(CouponTypeEnum.freeCouponBuy, preDay);
        } else if (DailyCountCouponEnum.freeCouponBuyMoney == typeEnum) {
            total = userSystemCouponCountService.countFreeMoneyByTypeAndDay(CouponTypeEnum.freeCouponBuy, preDay);
        } else if (DailyCountCouponEnum.welfareFreeCouponNum == typeEnum) {
            total = userSystemCouponCountService.countCouponNumByDay(CouponTypeEnum.welfareFreeCoupon, preDay);
        } else if (DailyCountCouponEnum.welfareFreeCouponMoney == typeEnum) {
            total = userSystemCouponCountService.countFreeMoneyByTypeAndDay(CouponTypeEnum.welfareFreeCoupon, preDay);
        } else if (DailyCountCouponEnum.freeCouponGiveNum == typeEnum) {
            total = userSystemCouponCountService.countCouponNumByDay(CouponTypeEnum.freeCouponGive, preDay);
        } else if (DailyCountCouponEnum.rebateCouponNum == typeEnum) {
            total = userSystemCouponCountService.countCouponNumByDay(CouponTypeEnum.rebatePercentCoupon, preDay);
        } else if (DailyCountCouponEnum.rebateCouponMoney == typeEnum) {
            total = userSystemCouponCountService.countRebateMoneyByDay(preDay);
        }
 
        if (total == null) {
            total = new BigDecimal(0);
        }
        return total;
    }
 
}