yujian
2019-01-03 f53edec193227af90aad11f557723e72b4860400
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
package com.yeshi.fanli.service.impl.user;
 
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.yeshi.utils.DateUtil;
 
import com.yeshi.fanli.dao.mybatis.user.UserActiveLogMapper;
import com.yeshi.fanli.entity.bus.user.UserActiveLog;
import com.yeshi.fanli.service.inter.user.UserActiveLogService;
 
@Service
public class UserActiveLogServiceImpl implements UserActiveLogService {
 
    @Resource
    private UserActiveLogMapper userActiveLogMapper;
 
    @Override
    public void addUserActiveLog(UserActiveLog userActiveLog) {
        if (userActiveLog == null)
            return;
        if (userActiveLog.getUid() == null || userActiveLog.getUid() == 0)
            return;
        UserActiveLog latestLog = getUserLatestActiveInfo(userActiveLog.getUid());
        // 间隔5分钟以上再记录
        if (latestLog == null || System.currentTimeMillis() - latestLog.getCreateTime().getTime() > 1000 * 60 * 5L) {
            userActiveLog.setCreateTime(new Date());
            userActiveLog.setUpdateTime(new Date());
            userActiveLogMapper.insertSelective(userActiveLog);
        }
    }
 
    @Override
    public UserActiveLog getUserLatestActiveInfo(Long uid) {
 
        return userActiveLogMapper.selectLatestByUid(uid);
    }
 
    
    @Override
    public List<Object> countSameDayByChannel(String channel, Integer type, String years,
            String startTime, String endTime) throws Exception{
        List<Map<String, Object>> list = userActiveLogMapper.countSameDayByChannel(channel, type, years, 
                startTime, endTime);
        
        if (list == null || list.size() == 0) {
            return null;
        } 
        
        switch (type){
            case 1: // 按天处理
                return dayFactory(startTime, endTime, list);
            case 2: // 按月处理
                return monthFactory(list);
            case 3: 
                return yearFactory(list);
            default: 
                return null;
        }
    }
    
    @Override
    public List<Object> countSameDayAndBuyGoodsByChannel(String channel, Integer type, String years,
            String startTime, String endTime) throws Exception {
        List<Map<String, Object>> list = userActiveLogMapper.countSameDayAndBuyGoodsByChannel(channel, type, years, 
                startTime, endTime);
        
        if (list == null || list.size() == 0) {
            return null;
        } 
        
        switch (type){
            case 1: // 按天处理
                return dayFactory(startTime, endTime, list);
            case 2: // 按月处理
                return monthFactory(list);
            case 3: 
                return yearFactory(list);
            default: 
                return null;
        }
    }
    
    
    @Override
    public List<Object> getBuyProportion(String channel, Integer type, String years,
            String startTime, String endTime) throws Exception{
        
        List<Object> countList = countSameDayByChannel(channel, type, years, startTime, endTime);
        
        if (countList == null || countList.size() == 0) {
            return null;
        } 
        
        List<Object> countListBuy= countSameDayAndBuyGoodsByChannel(channel, type, years, startTime, endTime);
        
        List<Object> listObject = new ArrayList<Object>();
    
        for (Object object: countList) {
            
            float rate = 0;
            
            Map<String, Object> mapObject = new HashMap<String, Object>();
            
            Map<String, Object> map = (Map<String, Object>) object;
            Object createDate = map.get("createDate");
            Object sameDayNum = map.get("sameDayNum");
            
            long totle = Long.parseLong(sameDayNum.toString());
            if (totle > 0 && countListBuy != null && countListBuy.size() > 0) {
                for (int j = 0; j < countListBuy.size(); j++) {
                    Map<String, Object> mapBuy = (Map<String, Object>) countListBuy.get(j);
                    Object createDate2 = mapBuy.get("createDate");
                    if (createDate.toString().equals(createDate2.toString())) {
                        Object buyNum = mapBuy.get("sameDayNum");
                        long totleBuy = Long.parseLong(buyNum.toString());
                        rate = (float)totleBuy/totle;
                        break;
                    }
                }
            }
            
            DecimalFormat df = new DecimalFormat("0.00");//格式化小数
            mapObject.put("sameDayNum", df.format(rate*100));
            mapObject.put("createDate", createDate);
            listObject.add(mapObject);
        }
        
        return listObject;
    }
 
    
    
    public List<Object> dayFactory(String startTime, String endTime, List<Map<String, Object>> list) throws Exception {
        
        List<Object> listObject = new ArrayList<Object>();
    
        if (startTime.equals(endTime)) {
            
            Map<String, Object> map = list.get(0);
            Object sameDayNum = map.get("sameDayNum");
            
            if (sameDayNum == null) {
                map.put("sameDayNum", 0);
            }
            listObject.add(map);
            
            return listObject;
        }
        
        
        String plusDay = "";
        for (int i = 0; i < 1000 ; i++) {
            if (i == 0) {
                plusDay = startTime;
            } else {
                plusDay = DateUtil.plusDay(i, startTime);
            }
            
            Map<String, Object> mapObject = new HashMap<String, Object>();
            Object sameDayNum = null; 
            
            for (int j = 0; j < list.size(); j++) {
                Map<String, Object> map = list.get(j);
                Object createDate = map.get("createDate");
                String month = createDate.toString();
                if (plusDay.equalsIgnoreCase(month)) {
                    sameDayNum = map.get("sameDayNum");
                    break;
                }
            }
            
            if (sameDayNum == null) {
                sameDayNum = 0;
            }
            mapObject.put("sameDayNum", sameDayNum);
            
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd");
            Date parseDate = sdf.parse(plusDay.toString());
            
            mapObject.put("createDate", sdf2.format(parseDate));
            
            listObject.add(mapObject);
            
            if (plusDay.equals(endTime)) {
                break; // 时间结束
            }
        }
        
        return listObject;
    }
    
    
    public List<Object> monthFactory(List<Map<String, Object>> list) {
        
        List<Object> listObject = new ArrayList<Object>();
        // 12 个月处理
        for (int i = 1; i <= 12; i++) {
            Map<String, Object> mapObject = new HashMap<String, Object>();
            Object sameDayNum = null;
            for (int j = 0; j < list.size(); j++) {
                Map<String, Object> map = list.get(j);
                Object createDate = map.get("createDate");
                String month = createDate.toString();
                if ((i+"").equalsIgnoreCase(month) || i == Integer.parseInt(month)) {
                    sameDayNum = map.get("sameDayNum"); 
                    break;
                }
            }
            
            if (sameDayNum == null) {
                sameDayNum = 0;
            }
            mapObject.put("sameDayNum", sameDayNum);
            
            mapObject.put("createDate", i + "月");
            
            listObject.add(mapObject);
        }
        return listObject;
    }
    
    public List<Object> yearFactory(List<Map<String, Object>> list) {
        
        List<Object> listObject = new ArrayList<Object>();
        
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = list.get(i);
            Object sameDayNum = map.get("sameDayNum"); 
            Object createDate = map.get("createDate"); 
            
            if (sameDayNum == null) {
                sameDayNum = 0;
            }
            map.put("sameDayNum", sameDayNum);
            map.put("createDate", createDate + "年");
            
            listObject.add(map);
        }
        
        return listObject;
    }
    
}