yujian
2020-06-29 f94a5a1d3fe9bde0e599d41f2d97caea1b08a88d
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
package com.yeshi.fanli.job.goods;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
 
import javax.annotation.Resource;
 
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.yeshi.utils.DateUtil;
 
import com.yeshi.fanli.dto.taobao.haodanku.HDKGoodsListResultDTO;
import com.yeshi.fanli.entity.bus.user.UserRankings;
import com.yeshi.fanli.entity.goods.PullNewGoods;
import com.yeshi.fanli.entity.taobao.haodanku.HDKGoodsDetail;
import com.yeshi.fanli.service.inter.goods.PullNewGoodsService;
import com.yeshi.fanli.service.inter.user.UserRankingsService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.taobao.HaoDanKuApiUtil;
 
@Component
public class PullNewJob {
 
    @Resource
    private UserRankingsService userRankingsService;
 
    
    @Resource
    private PullNewGoodsService pullNewGoodsService;
 
    /**
     * 拉新商品
     */
    @Scheduled(cron = "0 30 1 * * ? ")
    private void addPullNewGoods() {
        if (!Constant.IS_TASK) {
            return;
        }
            
        Integer array[] = {1,2,3,4,5,6,7,8,9,10,11,12,15};
        for (Integer catId: array) {
            Integer minId = 1;
            while (minId != null) {
                HDKGoodsListResultDTO dto = HaoDanKuApiUtil.getInstance().getHighitems(minId, 100, catId);
                if (dto == null || dto.getMinId() == null) {
                    break;
                }
                
                minId = dto.getMinId();
                List<HDKGoodsDetail> goodsList = dto.getGoodsList();
                if (goodsList == null || goodsList.size() == 0) {
                    break;
                }
 
                for (HDKGoodsDetail goods : goodsList) {
                    // 佣金比例大于50%
                    if (goods.getTkrates() == null || goods.getTkrates() < 50)
                        continue;
                    // 是否存在券
                    if (goods.getCouponmoney() == null || goods.getCouponmoney() <= 0)
                        continue;
                    // 商品券后价大于9.9元
                    if (goods.getItemendprice() == null || goods.getItemendprice() <= 9.9)
                        continue;
 
                    PullNewGoods pullNewGoods = new PullNewGoods();
                    try {
                        PropertyUtils.copyProperties(pullNewGoods, goods);
                    } catch (Exception e) {
                        e.printStackTrace();
                        continue;
                    }
                    pullNewGoodsService.saveGoods(pullNewGoods);
                }
            }
        }
    }
    
    
    // 每天00点10执行 : 更改排行榜上的奖金
    @Scheduled(cron = "0 15 0 * * ? ")
    public void updateReward() {
        if (!Constant.IS_TASK) {
            return;
        }
        
        // 更新人数
        int showNum = 15;
        // 每日增幅
        double rangeAmount = 100;
        try {
            List<UserRankings> listExist = null;
            // 每月1号归0
            int currentDate = DateUtil.getCurrentDate();
            if (currentDate != 1) 
                listExist = userRankingsService.getRankList(0, showNum);
            
            // 清理数据
            userRankingsService.clearShareReward();
            
            // 假用户数量
            int count = (int) userRankingsService.queryCount(null, null, null);
            // 随机显示新的人
            Random random = new Random();
            List<Long> listId = new ArrayList<Long>();
            while (listId.size() < showNum) {
                long id = random.nextInt(count);
                if (!listId.contains(id)) {
                    listId.add(id);
                }
            }
            List<UserRankings> listNew = userRankingsService.listQueryByIds(listId);
            
            if (listExist == null || listExist.size() == 0) {
                for (UserRankings user : listNew) {
                    // 浮动金额
                    double amount = Math.random() * rangeAmount;
                    if (amount < 0.5) {
                        amount = 0.5;
                    }
                    user.setShareReward(new BigDecimal(amount));
                }
            } else {
                for (int i = 0; i < listNew.size(); i ++) {
                    UserRankings user = listNew.get(i);
                    
                    // 浮动金额
                    BigDecimal shareReward = null;
                    if (i < listExist.size()) {
                        shareReward = listExist.get(i).getShareReward();
                    }
                    
                    if (shareReward == null)
                        shareReward = BigDecimal.ZERO;
                    
                    // 浮动金额
                    double amount = Math.random() * rangeAmount;
                    user.setShareReward(shareReward.add(BigDecimal.valueOf(amount)));
                }
            }
            // 批量更新昨日数据-移除排行榜
            userRankingsService.updateBatchSelective(listNew);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
 
}