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();
|
}
|
|
}
|
|
}
|