幸运24-y-定时任务-标记染色
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package com.accompany.business.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
@TableName("lucky_24_y_user")
|
||||
public class Lucky24YUser {
|
||||
|
||||
@TableId(type = IdType.INPUT)
|
||||
/**
|
||||
* 用户ID,主键
|
||||
*/
|
||||
private Long uid;
|
||||
|
||||
/**
|
||||
* 统计日期
|
||||
*/
|
||||
private String statDate;
|
||||
|
||||
/**
|
||||
* 自动染色标识
|
||||
*/
|
||||
private Integer autoDye;
|
||||
|
||||
/**
|
||||
* 管理员ID
|
||||
*/
|
||||
private Integer adminId;
|
||||
|
||||
/**
|
||||
* 状态
|
||||
*/
|
||||
private Byte status;
|
||||
|
||||
/**
|
||||
* 当天次数
|
||||
*/
|
||||
private Integer todayNum;
|
||||
|
||||
/**
|
||||
* 当天投产比
|
||||
*/
|
||||
private BigDecimal todayProductionRatio;
|
||||
|
||||
/**
|
||||
* 昨天次数
|
||||
*/
|
||||
private Integer yesterdayNum;
|
||||
|
||||
/**
|
||||
* 昨天投产比
|
||||
*/
|
||||
private BigDecimal yesterdayProductionRatio;
|
||||
|
||||
/**
|
||||
* 两天前次数
|
||||
*/
|
||||
private Integer twoDaysAgoNum;
|
||||
|
||||
/**
|
||||
* 两天前投产比
|
||||
*/
|
||||
private BigDecimal twoDaysAgoProductionRatio;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
private Date updateTime;
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
package com.accompany.business.mapper;
|
||||
|
||||
import com.accompany.business.entity.Lucky24YUser;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
public interface Lucky24YUserMapper extends BaseMapper<Lucky24YUser> {
|
||||
|
||||
}
|
@@ -0,0 +1,130 @@
|
||||
package com.accompany.business.service.lucky;
|
||||
|
||||
import com.accompany.business.entity.Lucky24YUser;
|
||||
import com.accompany.business.mybatismapper.lucky.Lucky24StatMapper;
|
||||
import com.accompany.common.constant.Constant;
|
||||
import com.accompany.common.utils.DateTimeUtil;
|
||||
import com.accompany.sharding.mapper.Lucky24RecordMapper;
|
||||
import com.accompany.sharding.vo.Lucky24PersonalStat;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class Lucky24YService {
|
||||
|
||||
@Autowired
|
||||
private Lucky24RecordMapper recordMapper;
|
||||
@Autowired
|
||||
private Lucky24StatMapper statMapper;
|
||||
@Autowired
|
||||
private Lucky24YUserService lucky24YUserService;
|
||||
|
||||
@Resource(name = "biz-executor")
|
||||
private ThreadPoolExecutor bizExecutor;
|
||||
|
||||
public void dye(Integer partitionId, Date systemStartTime, Date systemEndTime, ZonedDateTime zdt) {
|
||||
String zoneDate = zdt.format(DateTimeUtil.dateFormatter);
|
||||
|
||||
Map<Long, List<Lucky24PersonalStat>> personalStatMap = new HashMap<>();
|
||||
|
||||
List<Lucky24PersonalStat> todayPersonalStatList = recordMapper.listPersonal(zoneDate, partitionId, null, systemStartTime, systemEndTime, null, null);
|
||||
|
||||
for (Lucky24PersonalStat item : todayPersonalStatList){
|
||||
List<Lucky24PersonalStat> statList = personalStatMap.get(item.getUid());
|
||||
if (CollectionUtils.isEmpty(statList)){
|
||||
statList = new ArrayList<>();
|
||||
personalStatMap.put(item.getUid(), statList);
|
||||
}
|
||||
statList.add(item);
|
||||
}
|
||||
|
||||
String zoneYesterday = zdt.minusDays(1L).format(DateTimeUtil.dateFormatter);
|
||||
String zoneTwoDayAgo = zdt.minusDays(2L).format(DateTimeUtil.dateFormatter);
|
||||
|
||||
List<Lucky24PersonalStat> twoDayAgePersonalStatList = statMapper.listPersonalStat(partitionId, null, null, null, zoneTwoDayAgo, zoneYesterday);
|
||||
|
||||
for (Lucky24PersonalStat item : twoDayAgePersonalStatList){
|
||||
List<Lucky24PersonalStat> statList = personalStatMap.get(item.getUid());
|
||||
if (CollectionUtils.isEmpty(statList)){
|
||||
statList = new ArrayList<>();
|
||||
personalStatMap.put(item.getUid(), statList);
|
||||
}
|
||||
statList.add(item);
|
||||
}
|
||||
|
||||
Map<Long, Lucky24YUser> yUserMap = lucky24YUserService.mapValidYUser();
|
||||
|
||||
for (Map.Entry<Long, List<Lucky24PersonalStat>> entry : personalStatMap.entrySet()){
|
||||
Long uid = entry.getKey();
|
||||
|
||||
Lucky24YUser yUser = yUserMap.get(uid);
|
||||
if (yUser != null){
|
||||
if (Constant.Yes1No0.YES == yUser.getAutoDye()){
|
||||
bizExecutor.execute(() -> {
|
||||
tryClearYUser(yUser);
|
||||
});
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
List<Lucky24PersonalStat> personalStatList = entry.getValue();
|
||||
if (CollectionUtils.isEmpty(personalStatList) || personalStatList.size() <= 2){
|
||||
continue;
|
||||
}
|
||||
|
||||
long totalNum = personalStatList.stream().mapToLong(Lucky24PersonalStat::getNum).sum();
|
||||
boolean threeDayProductionRatioOverThreshold = personalStatList.stream().anyMatch(item -> item.getProductionRatio().compareTo(BigDecimal.ONE) > 0);
|
||||
if (totalNum < 2000L || !threeDayProductionRatioOverThreshold){
|
||||
continue;
|
||||
}
|
||||
|
||||
bizExecutor.execute(() -> {
|
||||
tryDyeYUser(uid, zoneDate, personalStatList);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void tryDyeYUser(Long uid, String statDate, List<Lucky24PersonalStat> personalStatList) {
|
||||
Date now = new Date();
|
||||
|
||||
Lucky24YUser yUser = new Lucky24YUser();
|
||||
yUser.setUid(uid);
|
||||
yUser.setStatDate(statDate);
|
||||
yUser.setAutoDye(Constant.Yes1No0.YES);
|
||||
yUser.setStatus(Constant.StatusV2.valid);
|
||||
|
||||
List<Lucky24PersonalStat> sortedPersonalStatList = personalStatList.stream().sorted(Comparator.comparing(Lucky24PersonalStat::getDate).reversed()).toList();
|
||||
|
||||
Lucky24PersonalStat todayStat = sortedPersonalStatList.get(0);
|
||||
yUser.setTodayNum(todayStat.getNum().intValue());
|
||||
yUser.setTodayProductionRatio(todayStat.getProductionRatio());
|
||||
|
||||
Lucky24PersonalStat yesterdayStat = sortedPersonalStatList.get(1);
|
||||
yUser.setYesterdayNum(yesterdayStat.getNum().intValue());
|
||||
yUser.setYesterdayProductionRatio(yesterdayStat.getProductionRatio());
|
||||
|
||||
Lucky24PersonalStat twoDaysAgoStat = sortedPersonalStatList.get(2);
|
||||
yUser.setTwoDaysAgoNum(twoDaysAgoStat.getNum().intValue());
|
||||
yUser.setTwoDaysAgoProductionRatio(twoDaysAgoStat.getProductionRatio());
|
||||
|
||||
yUser.setCreateTime(now);
|
||||
yUser.setUpdateTime(now);
|
||||
|
||||
lucky24YUserService.saveOrUpdate(yUser);
|
||||
}
|
||||
|
||||
private void tryClearYUser(Lucky24YUser yUser) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
package com.accompany.business.service.lucky;
|
||||
|
||||
import com.accompany.business.entity.Lucky24YUser;
|
||||
import com.accompany.business.mapper.Lucky24YUserMapper;
|
||||
import com.accompany.common.constant.Constant;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class Lucky24YUserService extends ServiceImpl<Lucky24YUserMapper, Lucky24YUser> {
|
||||
|
||||
public Map<Long, Lucky24YUser> mapValidYUser(){
|
||||
return lambdaQuery()
|
||||
.eq(Lucky24YUser::getStatus, Constant.StatusV2.valid)
|
||||
.list().stream()
|
||||
.collect(Collectors.toMap(Lucky24YUser::getUid, v -> v));
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.accompany.business.mapper.Lucky24YUserMapper">
|
||||
|
||||
</mapper>
|
@@ -3,6 +3,7 @@ package com.accompany.scheduler.task.luckyBag;
|
||||
import com.accompany.business.message.Lucky24Message;
|
||||
import com.accompany.business.service.gift.Lucky24MessageService;
|
||||
import com.accompany.business.service.lucky.Lucky24RecordService;
|
||||
import com.accompany.business.service.lucky.Lucky24YService;
|
||||
import com.accompany.common.utils.DateTimeUtil;
|
||||
import com.accompany.core.model.PartitionInfo;
|
||||
import com.accompany.core.service.partition.PartitionInfoService;
|
||||
@@ -29,6 +30,8 @@ public class Lucky24Task {
|
||||
private Lucky24RecordService service;
|
||||
@Autowired
|
||||
private Lucky24MessageService lucky24MessageService;
|
||||
@Autowired
|
||||
private Lucky24YService lucky24YService;
|
||||
|
||||
/**
|
||||
* 重新消费队列的消息
|
||||
@@ -97,4 +100,41 @@ public class Lucky24Task {
|
||||
}
|
||||
}
|
||||
|
||||
@Scheduled(cron = "0 0 * * * ? ")
|
||||
public void lucky24DyeY() {
|
||||
Date now = new Date();
|
||||
List<PartitionInfo> partitionInfoList = partitionInfoService.listAll();
|
||||
for (PartitionInfo partitionInfo : partitionInfoList) {
|
||||
ZonedDateTime zdt = DateTimeUtil.convertWithZoneId(now, partitionInfo.getZoneId());
|
||||
int times = zdt.getHour() / 3;
|
||||
int mod = zdt.getHour() % 3;
|
||||
|
||||
log.info("[lucky24DyeY] zdt {} zdtHour {} times {} mod {}",
|
||||
zdt, zdt.getHour(), times, mod);
|
||||
|
||||
if (times == 0 || mod > 0){
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 获取当天的第一秒
|
||||
ZonedDateTime startOfDay = zdt.withHour(0)
|
||||
.withMinute(0)
|
||||
.withSecond(0)
|
||||
.withNano(0);
|
||||
Date systemStartTime = DateTimeUtil.converLocalDateTimeToDate(startOfDay.withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime());
|
||||
Date startTime = DateTimeUtil.converLocalDateTimeToDate(startOfDay.toLocalDateTime());
|
||||
|
||||
// 获取当天的最后一秒
|
||||
ZonedDateTime endOfDay = zdt;
|
||||
Date systemEndTime = DateTimeUtil.converLocalDateTimeToDate(endOfDay.withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime());
|
||||
|
||||
lucky24YService.dye(partitionInfo.getId(), systemStartTime, systemEndTime, zdt);
|
||||
|
||||
} catch (Exception e){
|
||||
log.error("[lucky24DyeY] exception {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user