多余内容删除
This commit is contained in:
@@ -1,178 +1,22 @@
|
||||
package com.chaozhanggui.system.cashierservice.task;
|
||||
|
||||
import com.chaozhanggui.system.cashierservice.dao.*;
|
||||
import com.chaozhanggui.system.cashierservice.entity.*;
|
||||
import com.chaozhanggui.system.cashierservice.service.TbShopSongOrderService;
|
||||
import com.chaozhanggui.system.cashierservice.util.DateUtils;
|
||||
import com.chaozhanggui.system.cashierservice.util.NicknameGenerator;
|
||||
import com.chaozhanggui.system.cashierservice.util.RandomUtil;
|
||||
import lombok.SneakyThrows;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.text.ParseException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
@Component
|
||||
public class TaskScheduler {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TaskScheduler.class);
|
||||
@Autowired
|
||||
private TbWiningUserMapper tbWiningUserMapper;
|
||||
@Autowired
|
||||
private TbOrderInfoMapper orderInfoMapper;
|
||||
@Autowired
|
||||
private TbWiningParamsMapper winingParamsMapper;
|
||||
@Autowired
|
||||
private TbUserInfoMapper userInfoMapper;
|
||||
@Autowired
|
||||
private TbReleaseFlowMapper releaseFlowMapper;
|
||||
@Autowired
|
||||
private TbUserCouponsMapper userCouponsMapper;
|
||||
|
||||
private final TbShopSongOrderService shopSongOrderService;
|
||||
|
||||
public TaskScheduler(@Qualifier("tbShopSongOrderServiceImpl") TbShopSongOrderService shopSongOrderService) {
|
||||
this.shopSongOrderService = shopSongOrderService;
|
||||
}
|
||||
|
||||
//更新订单状态
|
||||
// @Scheduled(fixedRate = 1000000)
|
||||
public void orderStatus() throws InterruptedException {
|
||||
for (int i = 0;i<10;i++){
|
||||
TbReleaseFlow releaseFlow = new TbReleaseFlow();
|
||||
BigDecimal orderAmount = RandomUtil.getRandomBigDecimal(BigDecimal.ONE, new BigDecimal("100"));
|
||||
releaseFlow.setNum(orderAmount);
|
||||
releaseFlow.setCreateTime(new Date());
|
||||
releaseFlow.setFromSource("OWER");
|
||||
releaseFlow.setUserId("15");
|
||||
releaseFlow.setOperationType("ADD");
|
||||
releaseFlow.setType("EXCHANGEADD");
|
||||
releaseFlow.setRemark("兑换增加");
|
||||
releaseFlowMapper.insert(releaseFlow);
|
||||
}
|
||||
for (int i = 0;i<10;i++){
|
||||
TbReleaseFlow releaseFlow = new TbReleaseFlow();
|
||||
BigDecimal orderAmount = RandomUtil.getRandomBigDecimal(BigDecimal.ONE, new BigDecimal("100"));
|
||||
releaseFlow.setNum(orderAmount);
|
||||
releaseFlow.setCreateTime(new Date());
|
||||
releaseFlow.setFromSource("OWER");
|
||||
releaseFlow.setUserId("15");
|
||||
releaseFlow.setOperationType("SUB");
|
||||
releaseFlow.setType("BUYSUB");
|
||||
releaseFlow.setRemark("购买商品扣除");
|
||||
releaseFlowMapper.insert(releaseFlow);
|
||||
}
|
||||
for (int i = 0;i<10;i++){
|
||||
TbReleaseFlow releaseFlow = new TbReleaseFlow();
|
||||
BigDecimal orderAmount = RandomUtil.getRandomBigDecimal(BigDecimal.ONE, new BigDecimal("100"));
|
||||
releaseFlow.setNum(orderAmount);
|
||||
releaseFlow.setCreateTime(new Date());
|
||||
releaseFlow.setFromSource("OWER");
|
||||
releaseFlow.setOperationType("ADD");
|
||||
releaseFlow.setUserId("15");
|
||||
releaseFlow.setType("THREEADD");
|
||||
releaseFlow.setRemark("退货增加");
|
||||
releaseFlowMapper.insert(releaseFlow);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Scheduled(cron = "0 1 0 * * ?")
|
||||
public void winningUser() {
|
||||
String day = DateUtils.getDay();
|
||||
List<TbWiningParams> list = winingParamsMapper.selectAll();
|
||||
ThreadPoolExecutor es = new ThreadPoolExecutor(5, 10, 60L, TimeUnit.SECONDS,
|
||||
new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {
|
||||
@Override
|
||||
public Thread newThread(Runnable r) {
|
||||
Thread t = new Thread(r);
|
||||
t.setDaemon(true);
|
||||
return t;
|
||||
}
|
||||
});
|
||||
for (TbWiningParams winingParams : list) {
|
||||
es.submit(new winingUser(winingParams, day));
|
||||
}
|
||||
es.shutdown();
|
||||
|
||||
}
|
||||
|
||||
class winingUser implements Runnable {
|
||||
private TbWiningParams winingParams;
|
||||
private String day;
|
||||
|
||||
public winingUser(TbWiningParams winingParams, String day) {
|
||||
this.winingParams = winingParams;
|
||||
this.day = day;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
List<TbOrderInfo> list = orderInfoMapper.selectByTradeDay(day, winingParams.getMinPrice(), winingParams.getMaxPrice());
|
||||
int num = winingParams.getWiningUserNum();
|
||||
List<TbOrderInfo> newList = new ArrayList<>();
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
int noUserNum = winingParams.getWiningNum() - num;
|
||||
if (list.size() < num) {
|
||||
noUserNum = winingParams.getWiningNum();
|
||||
} else {
|
||||
for (int i = 0; i < num; i++) {
|
||||
TbOrderInfo orderInfo = RandomUtil.selectWinner(list, map);
|
||||
newList.add(orderInfo);
|
||||
map.put(orderInfo.getId(), 1);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < noUserNum; i++) {
|
||||
long endDate = DateUtils.convertDate1(day + " 00:00:00").getTime();
|
||||
long startDate = DateUtils.convertDate1(DateUtils.getSdfDayTimes(DateUtils.getNewDate(new Date(), 3, -1))+" 00:00:00").getTime();
|
||||
String orderNo = generateOrderNumber(startDate, endDate);
|
||||
String userName = NicknameGenerator.generateRandomWeChatNickname();
|
||||
BigDecimal orderAmount = RandomUtil.getRandomBigDecimal(winingParams.getMinPrice(), winingParams.getMaxPrice());
|
||||
TbWiningUser winingUser = new TbWiningUser(userName, orderNo, orderAmount, "false", day);
|
||||
tbWiningUserMapper.insert(winingUser);
|
||||
}
|
||||
for (TbOrderInfo orderInfo:newList){
|
||||
TbUserInfo userInfo = userInfoMapper.selectByPrimaryKey(Integer.valueOf(orderInfo.getUserId()));
|
||||
TbWiningUser winingUser = new TbWiningUser(userInfo.getNickName(), orderInfo.getOrderNo(), orderInfo.getPayAmount(), "true", day);
|
||||
tbWiningUserMapper.insert(winingUser);
|
||||
TbUserCoupons userCoupons = new TbUserCoupons();
|
||||
userCoupons.setUserId(orderInfo.getUserId());
|
||||
userCoupons.setCouponsAmount(orderInfo.getOrderAmount().subtract(orderInfo.getUserCouponAmount()));
|
||||
userCoupons.setStatus("0");
|
||||
userCoupons.setOrderId(orderInfo.getId());
|
||||
userCoupons.setCouponsPrice(userCoupons.getCouponsAmount().multiply(new BigDecimal("0.5")));
|
||||
userCoupons.setCreateTime(new Date());
|
||||
userCoupons.setEndTime(DateUtils.getNewDate(new Date(),3,30));
|
||||
//执行插入方法
|
||||
userCouponsMapper.insert(userCoupons);
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String generateOrderNumber(long startTimestamp, long endTimestamp) {
|
||||
long date = getRandomTimestamp(startTimestamp, endTimestamp);
|
||||
Random random = new Random();
|
||||
int randomNum = random.nextInt(900) + 100;
|
||||
return "WX" + date + randomNum;
|
||||
}
|
||||
|
||||
public static long getRandomTimestamp(long startTimestamp, long endTimestamp) {
|
||||
long randomMilliseconds = ThreadLocalRandom.current().nextLong(startTimestamp, endTimestamp);
|
||||
return randomMilliseconds;
|
||||
}
|
||||
|
||||
@Scheduled(fixedRate = 60000 * 60)
|
||||
public void clearSongOrder() {
|
||||
log.info("定时任务执行,清楚过期歌曲订单");
|
||||
|
||||
Reference in New Issue
Block a user