admin
2019-07-30 573c491b4a1ba60e12a5678a01c1546c0077c1ee
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
package com.yeshi.fanli.job;
 
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;
 
import javax.annotation.Resource;
 
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import com.yeshi.fanli.entity.bus.user.UserRankings;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoOrderService;
import com.yeshi.fanli.service.inter.user.UserRankingsService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
 
/**
 * 淘宝商品更细
 * 
 * @author Administrator
 *
 */
 
@Component
public class UserRankingsJob {
 
    @Resource
    private ConfigService configService;
 
    @Resource
    private UserRankingsService userRankingsService;
 
    @Resource
    private TaoBaoOrderService taoBaoOrderService;
    
 
    // 1加,2减
    private static int ADD = 1;
 
    // 前端显示个数
    private static int SHOW_NUM = 50;
 
    // 每天00点10执行 : 更改排行榜上的奖金
    @Scheduled(cron = "0 5 0 * * ? ")
    public void updateReward() {
 
        if (!Constant.IS_TASK) {
            return;
        }
 
        try {
 
            LogHelper.test("------执行中--updateReward---------");
            
            // 清理数据
            userRankingsService.updateClearTradeState();
            
            
            // 系统总条数
            int count = (int) userRankingsService.queryCount(null, null, null);
 
            Random random = new Random();
 
            // 随机将显示的50个人信息
            List<Long> listId = new ArrayList<Long>();
            while (listId.size() < SHOW_NUM) {
                long id = random.nextInt(count);
                if (!listId.contains(id)) {
                    listId.add(id);
                }
            }
 
            // 显示新的人
            List<UserRankings> listNew = userRankingsService.listQueryByIds(listId);
 
            Calendar cal=Calendar.getInstance();
            cal.add(Calendar.DATE,-1);
            Date time=cal.getTime();
            
            // 昨日预计总收益
            double countEstimate = taoBaoOrderService.countEstimate(new SimpleDateFormat("yyyy-MM-dd").format(time));
            if (countEstimate == 0) {
                countEstimate = 600;
            }
            // 均值:总预计收益*50%
            double avgAmount = countEstimate * 0.5;
            
            // 上下 浮动金额范围
            double rangeAmount = 100;
            String range = configService.get("reward_rank_range");
            if (!StringUtil.isNullOrEmpty(range)) {
                rangeAmount = Double.parseDouble(range);
            }
            
            
            for (UserRankings userRankings : listNew) {
                
                // 状态
                userRankings.setTradeState(1);
                userRankings.setTradeTime(new Date());
                
                // 浮动金额
                double amount = Math.random() * rangeAmount;
 
                int[] arr = { 1, 2 };
                int index = random.nextInt(arr.length);
                if (ADD == arr[index]) {
                    userRankings.setTradeReward(new BigDecimal(avgAmount + amount));
                } else {
                    while (amount > avgAmount) {
                        amount = Math.random() * rangeAmount;
                    }
                    userRankings.setTradeReward(new BigDecimal(avgAmount - amount));
                }
            }
 
            // 批量更新昨日数据-移除排行榜
            userRankingsService.updateBatchSelective(listNew);
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
    
}