admin
2020-05-20 98b1a0affd69bbe63223c21fdd2c404e8bedfccb
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
package com.yeshi.fanli.service.impl.user;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.UUID;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.yeshi.utils.tencentcloud.COSManager;
 
import com.yeshi.fanli.dao.mybatis.UserRankingsMapper;
import com.yeshi.fanli.dto.money.InviteGetMoney;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.bus.user.UserRankings;
import com.yeshi.fanli.exception.user.UserRankingsException;
import com.yeshi.fanli.service.inter.count.HongBaoV2CountService;
import com.yeshi.fanli.service.inter.user.UserRankingsService;
import com.yeshi.fanli.util.FilePathEnum;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.vo.order.HongBaoV2VO;
 
@Service
public class UserRankingsServiceImpl implements UserRankingsService {
    @Resource
    private UserRankingsMapper userRankingsMapper;
 
    @Resource
    private HongBaoV2CountService hongBaoV2CountService;
 
    @Override
    public int insertSelective(UserRankings record) throws UserRankingsException {
        // TODO Auto-generated method stub
        return userRankingsMapper.insertSelective(record);
    }
 
    @Override
    public int updateByPrimaryKey(UserRankings record) throws UserRankingsException {
        return userRankingsMapper.updateByPrimaryKey(record);
    }
 
    @Override
    public int updateByPrimaryKeySelective(UserRankings record) throws UserRankingsException {
        return userRankingsMapper.updateByPrimaryKeySelective(record);
    }
 
    @Override
    public int updateBatchSelective(List<UserRankings> list) throws UserRankingsException {
        return userRankingsMapper.updateBatchSelective(list);
    }
 
    @Override
    @Transactional(rollbackFor=Exception.class)
    public int deleteBatchById(Long id) throws UserRankingsException {
        return userRankingsMapper.deleteByPrimaryKey(id);
    }
    
    
    @Override
    public void updateClearTradeState() throws UserRankingsException {
        userRankingsMapper.updateClearTradeState();
    }
 
 
    @Override
    @Transactional(rollbackFor=Exception.class)
    public int deleteBatchByPrimaryKey(List<Long> arrayID) throws UserRankingsException {
        for (Long id : arrayID) {
 
            UserRankings userRankings = userRankingsMapper.selectByPrimaryKey(id);
            String portrait = userRankings.getPortrait();
            if (!StringUtil.isNullOrEmpty(portrait)) {
                COSManager.getInstance().deleteFile(portrait);
            }
            userRankingsMapper.deleteByPrimaryKey(id);
        }
 
        return arrayID.size();
    }
 
    @Override
    public UserRankings selectByPrimaryKey(Long id) throws UserRankingsException {
        // TODO Auto-generated method stub
        return userRankingsMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public List<UserRankings> selectByNickName(String nickName) throws UserRankingsException {
        // TODO Auto-generated method stub
        return userRankingsMapper.selectByNickName(nickName);
    }
 
    @Override
    public List<UserRankings> selectByShowId(String id) throws UserRankingsException {
        // TODO Auto-generated method stub
        return userRankingsMapper.selectByShowId(id);
    }
 
    @Override
    public void insertAndUploadFile(UserRankings record, MultipartFile file) throws Exception {
        // TODO Auto-generated method stub
        record.setCreatetime(new Date());
        userRankingsMapper.insertSelective(record);
 
        // 上传图片
        if (file != null) {
            uploadPicture(file, record);
        }
 
    }
 
    @Override
    public void uploadPicture(MultipartFile file, UserRankings userRankings) throws Exception {
 
        /* 修改图片时,先删除已存在图片 */
        String portrait = userRankings.getPortrait();
        if (!StringUtil.isNullOrEmpty(portrait)) {
            COSManager.getInstance().deleteFile(portrait);
        }
 
        String contentType = file.getContentType();
        String type = contentType.substring(contentType.indexOf("/") + 1);
 
        // 上传文件相对位置
        String fileUrl = FilePathEnum.userRankings.getPath() + UUID.randomUUID().toString().replace("-", "") + "." + type;
 
        InputStream inputStream = file.getInputStream();
        String uploadFilePath = COSManager.getInstance().uploadFile(inputStream, fileUrl).getUrl();
 
        /* 更新数据库信息 */
        if (!StringUtil.isNullOrEmpty(uploadFilePath)) {
            userRankings.setPortrait(uploadFilePath);
            userRankingsMapper.updateByPrimaryKeySelective(userRankings);
        }
    }
 
    @Override
    public void removePicture(UserRankings userRankings) throws Exception {
 
        /* 修改图片时,先删除已存在图片 */
        String portrait = userRankings.getPortrait();
        if (!StringUtil.isNullOrEmpty(portrait)) {
            boolean deleteFile = COSManager.getInstance().deleteFile(portrait);
            if (deleteFile) {
                userRankings.setPortrait(null);
                userRankingsMapper.updateByPrimaryKey(userRankings);
            }
        }
 
    }
 
    @Override
    public List<UserRankings> query(long start, int count, String key, Integer tradeState, Integer shareState, Integer sort)
            throws UserRankingsException {
        return userRankingsMapper.query(start, count, key, tradeState, shareState, sort);
    }
 
    @Override
    public long queryCount(String key, Integer tradeState, Integer shareState) throws UserRankingsException {
        return userRankingsMapper.queryCount(key, tradeState, shareState);
    }
 
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void setRewardtBatch(List<Long> idList, Integer type, Double reward, Double startReward, Double endReward) throws Exception {
 
        for (Long id : idList) {
 
            UserRankings userRankings = userRankingsMapper.selectByPrimaryKey(id);
 
            if (userRankings == null) {
                continue;
            }
 
            Double result = 0.00;
 
            if (reward != null && reward >= 0) {
                result = reward;
            } else {
                if (startReward != null && endReward != null) {
                    // 随机权重
                    result = startReward + (Math.random() * ((endReward - startReward) + 1));
                } else {
                    break;
                }
            }
 
            if (type == 1 || type.equals(1)) {
                userRankings.setTradeReward(new BigDecimal(result));
            } else {
                userRankings.setShareReward(new BigDecimal(result));
            }
 
            userRankingsMapper.updateByPrimaryKeySelective(userRankings);
 
        }
 
    }
 
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void setStateBatch(List<Long> idList, Integer type, Integer state) throws Exception {
 
        for (Long id : idList) {
 
            UserRankings userRankings = userRankingsMapper.selectByPrimaryKey(id);
 
            if (userRankings == null) {
                continue;
            }
 
            if (type == 1 || type.equals(1)) {
                userRankings.setTradeState(state);
 
                if (state != null && state.equals(1)) {
                    userRankings.setTradeTime(new Date());
                }
            } else {
                userRankings.setShareState(state);
 
                if (state != null && state.equals(1)) {
                    userRankings.setShareTime(new Date());
                }
            }
 
            userRankingsMapper.updateByPrimaryKey(userRankings);
 
        }
 
    }
 
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void uploadFile(File file) throws Exception {
 
        List<UserRankings> list = new ArrayList<UserRankings>();
 
        File[] files = file.listFiles();
 
        for (File filePic : files) {// 遍历这个数组
            String name = filePic.getName();
            System.out.println("name" + name);
            String prefix = name.substring(name.lastIndexOf(".") + 1);
            // 上传文件相对位置
            String fileUrl = FilePathEnum.userRankings.getPath() + UUID.randomUUID().toString().replace("-", "") + "." + prefix;
            System.out.println(fileUrl);
 
            FileInputStream is = new FileInputStream(filePic);
 
            String uploadFilePath = COSManager.getInstance().uploadFile(is, fileUrl).getUrl();
 
            /* 更新数据库信息 */
            if (!StringUtil.isNullOrEmpty(uploadFilePath)) {
                UserRankings userRankings = new UserRankings();
                userRankings.setPortrait(uploadFilePath);
                userRankings.setCreatetime(new Date());
                list.add(userRankings);
            }
 
            is.close();
        }
 
        if (list.size() > 0) {
            userRankingsMapper.insertBatch(list);
        }
    }
 
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void updateShowID(File file) throws Exception {
        List<String> listID = new ArrayList<String>();
 
        BufferedReader br = new BufferedReader(new FileReader(file));// 构造一个BufferedReader类来读取文件
        String s = "";
        while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行
            listID.add(s);
        }
        br.close();
 
        int i = 0;
        List<UserRankings> list = userRankingsMapper.queryAll();
        for (UserRankings userRankings : list) {
 
            String showId = listID.get(i);
            i++;
 
            userRankings.setShowId(showId);
        }
 
        userRankingsMapper.updateBatchSelective(list);
 
    }
 
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void batchUpNickName(File file) throws Exception {
        List<String> listID = new ArrayList<String>();
 
        BufferedReader br = new BufferedReader(new FileReader(file));// 构造一个BufferedReader类来读取文件
        String s = "";
        while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行
            listID.add(s);
        }
        br.close();
 
        int i = 0;
        List<UserRankings> list = userRankingsMapper.queryAll();
        for (UserRankings userRankings : list) {
 
            String nickName = listID.get(i);
            i++;
 
            userRankings.setNickName(nickName);
        }
 
        userRankingsMapper.updateBatchSelective(list);
 
    }
 
    @Override
    public List<UserRankings> listQueryByIds(List<Long> ids) throws UserRankingsException {
        return userRankingsMapper.listQueryByIds(ids);
    }
 
    @Override
    public List<InviteGetMoney> getRank(long start, int count) {
        
        List<InviteGetMoney> resultList = new ArrayList<InviteGetMoney>();
        
        List<InviteGetMoney> rankingsList = new ArrayList<InviteGetMoney>();
        
        try {
 
             List<UserRankings> userRankingsList = query(0, count, null, 1, null, 2);
 
            for (UserRankings userRankings : userRankingsList) {
 
                InviteGetMoney invite = new InviteGetMoney();
                String showId = userRankings.getShowId();
                if (StringUtil.isNullOrEmpty(showId)) {
                    showId = "2***5";
                }
                invite.setUid(showId);
                invite.setName(userRankings.getNickName());
                // invite.setIcon(userRankings.getPortrait());
                invite.setPortrait(userRankings.getPortrait());
                invite.setMoney(userRankings.getTradeReward());
                invite.setCreateTime(userRankings.getCreatetime());
                rankingsList.add(invite);
            }
 
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -1);
 
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
            String defaultDate = sdf.format(calendar.getTime()); // 格式化前一天
 
            // 真实用户
            List<HongBaoV2VO> listHongBao = hongBaoV2CountService.listShareAndInviteMoney(0, count, defaultDate);
            if (listHongBao != null && listHongBao.size() > 0) {
                HongBaoV2VO hongBao = listHongBao.get(0);
                BigDecimal maxMoney = hongBao.getTotalMoney();
 
                UserRankings userRankings = userRankingsList.get(userRankingsList.size() - 1);
                BigDecimal mminMoney = userRankings.getTradeReward();
 
                // 真实数据大于最低金额
                if (maxMoney.compareTo(mminMoney) > 0) {
                    for (HongBaoV2VO hb : listHongBao) {
                        BigDecimal rewardMoney = hb.getTotalMoney();
 
                        // 奖金高到低排序,不满足结束循环
                        if (rewardMoney.compareTo(mminMoney) <= 0) {
                            break;
                        }
 
                        UserInfo userInfo = hb.getUserInfo();
                        if (userInfo != null) {
 
                            InviteGetMoney invite = new InviteGetMoney();
 
                            invite.setName(userInfo.getNickName());
                            // invite.setIcon(userRankings.getPortrait());
                            invite.setPortrait(userInfo.getPortrait());
                            invite.setMoney(rewardMoney);
                            invite.setCreateTime(new Date());
 
                            // 处理id
                            Long id = userInfo.getId();
                            String uid = id + "";
                            String index = uid.substring(0, 1);
                            String end = uid.substring(uid.length() - 1, uid.length());
                            invite.setUid(index + "***" + end);
                            rankingsList.add(invite);
 
                        }
 
                    }
                }
            }
 
            List<InviteGetMoney> list = listSort(rankingsList);
            
            if (list.size() > count) {
                resultList = list.subList(0, count-1);
            } else {
                resultList = list;
            }
            
            for (int i = 0 ;i<resultList.size() ; i++) {
                long n = i+1;
                String icon = "";
                if (n == 1) {
                    icon ="http://buxinimg.oss-cn-qingdao.aliyuncs.com/5e75b65b6eca4c74850a7306d7584663.png";
                }
                if (n == 2) {
                    icon ="http://buxinimg.oss-cn-qingdao.aliyuncs.com/442c950dd14a41a8849c0597f643ddd2.png";
                }
                if (n == 3) {
                    icon ="http://buxinimg.oss-cn-qingdao.aliyuncs.com/5c9559905f034541888e674b30dbbeb3.png";
                }
                if (n == 4) {
                    icon ="http://buxinimg.oss-cn-qingdao.aliyuncs.com/531b640cf73042ac929750e5ff610a20.png";
                }
                if (n == 5) {
                    icon ="http://buxinimg.oss-cn-qingdao.aliyuncs.com/2a229650a22c4ce09e3c60e8cddafdd6.png";
                }
                resultList.get(i).setIcon(icon);
                
                
                resultList.get(i).setRankIcon("http://ec-1255749512.file.myqcloud.com/resource/rank/rank_icon_3.png");
                resultList.get(i).setId(n);
            }
            
            
        } catch (UserRankingsException e) {
            e.printStackTrace();
        }
        
        return resultList;
        
        
    }
 
    public List<InviteGetMoney> listSort(List<InviteGetMoney> list) {
 
        Collections.sort(list, new Comparator<InviteGetMoney>() {
            public int compare(InviteGetMoney p1, InviteGetMoney p2) {
 
                // 按照Person的年龄进行升序排列
                if (p1.getMoney().compareTo(p2.getMoney()) < 0) {
                    return 1;
                }
 
                if (p1.getMoney().compareTo(p2.getMoney()) == 0) {
                    return 0;
                }
                return -1;
            }
        });
 
        return list;
    }
}