品牌热度定时任务

简介: 品牌热度定时任务

1 设置定时任务

/**
 * @description: 品牌热度定时任务
  * @create: 2021-06-23 18:06
 **/
@Slf4j
@RestController
@RequestMapping("/v1/api/brand/task")
@EnableScheduling
public class BrandHotTask {
    @Resource
    private IBrandHotTaskService brandHotTaskService;
    /**
     *  @ Scheduled(cron = "0 0 1 * * ?")
     *  每日一点开始分析品牌热度数据
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void execute() {
        log.info("品牌热度定时任务start");
        Date date = DateUtils.getNow();
        Date time = DateUtils.getDayTimeAtFirst(DateUtils.diffDate(date, 1));
        brandHotTaskService.handlerBrandHotData(time);
        log.info("品牌热度定时任务end ");
    }
}

2 进入定时任务接口

public interface IBrandHotTaskService {
    /**
     *  处理品牌热度数据
     * @param executeDate
     */
    void handlerBrandHotData(Date executeDate);
}

3 具体实现

/**
 * @description: 品牌热度定时任务实现接口
 * @create: 2021-06-23 18:12
 **/
@SuppressWarnings("PMD")
@Slf4j
@Service
public class BrandHotTaskServiceImpl implements IBrandHotTaskService {
    @Autowired
    private JoinBrandHotMapper joinBrandHotMapper;
    @Resource
    private IUserVisitRecordRemoteService userVisitRecordRemoteService;
    /**
     *  1. lego_analysis库分析埋点数据,查询品牌详情页面的pageId数据,进行汇总统计
     *  2. 插入hot表及hot_record表
     * @param executeDate
     */
    @SneakyThrows
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void handlerBrandHotData(Date executeDate) {
        log.info("品牌热度定时任务 start executeDate:{}", executeDate);
        BaseResponse<List<UserVisitRecordHitNumber>> response = getUserVisitPageRecord(executeDate);
        if (response == null || CollectionUtil.isEmpty(response.getData())) {
            return;
        }
        List<UserVisitRecordHitNumber> data = response.getData();
        // TODO 后续需要考虑集合过大 循环执行事务时间过长
        for (UserVisitRecordHitNumber datum : data) {
            String brandCode = datum.getRefId();
            String clickCount = datum.getU();
            String realClick = datum.getDu();
            JoinBrandHot joinBrandHot = getJoinBrandHot(brandCode, clickCount, realClick);
            joinBrandHot.setStatus(1);
            JoinBrandHot brandHot = joinBrandHotMapper.getByBrandCode(brandCode);
            if (brandHot == null) {
                joinBrandHotMapper.saveBrandHotTask(joinBrandHot);
            } else {
                joinBrandHot.setId(brandHot.getId());
                joinBrandHotMapper.updateBrandHotTask(joinBrandHot);
            }
            /**
             * 插入到hot_record,直接插入,只作为记录就好
             */
            JoinBrandHotRecord joinBrandHotRecord = getJoinBrandHotRecord(brandCode, clickCount, realClick);
            joinBrandHotMapper.deleteByBrandCodeAndDate(brandCode, DateUtils.getDate());
            joinBrandHotMapper.saveBrandHotTaskToRecord(joinBrandHotRecord);
        }
    }
    private BaseResponse<List<UserVisitRecordHitNumber>> getUserVisitPageRecord(Date executeDate) {
        Date startTime = DateUtils.getDayTimeAtFirst(executeDate);
        Date endTime = DateUtils.getDayTimeAtLast(executeDate);
        String pageId = "10014";
        UserVisitPageReq visitPageReq = new UserVisitPageReq();
        visitPageReq.setPageId(pageId);
        visitPageReq.setStartTime(startTime);
        visitPageReq.setEndTime(endTime);
        BaseResponse<List<UserVisitRecordHitNumber>> response = userVisitRecordRemoteService
                .userVisitRecordByYest(visitPageReq);
        log.info("品牌数据查询pageId:{}, startTime:{},endTime:{}, 结果 response:{}", pageId, startTime,
                endTime, response);
        return response;
    }
    private JoinBrandHot getJoinBrandHot(String brandCode, String clickCount, String realClick) {
        JoinBrandHot joinBrandHot = new JoinBrandHot();
        joinBrandHot.setBrandCode(brandCode);
        joinBrandHot.setClickCount(clickCount);
        joinBrandHot.setRealClick(realClick);
        joinBrandHot.setGmtCreate(DateUtils.getNow());
        joinBrandHot.setGmtModified(DateUtils.getNow());
        return joinBrandHot;
    }
    private JoinBrandHotRecord getJoinBrandHotRecord(String brandCode, String clickCount, String realClick) {
        JoinBrandHotRecord joinBrandHotRecord = new JoinBrandHotRecord();
        joinBrandHotRecord.setBrandCode(brandCode);
        joinBrandHotRecord.setClickCount(clickCount);
        joinBrandHotRecord.setRealClick(realClick);
        joinBrandHotRecord.setGmtCreate(DateUtils.getNow());
        return joinBrandHotRecord;
    }
}
相关文章
|
1月前
|
人工智能
阿里上线AI电商工具“绘蛙”,让你的商品第一时间被种草
【2月更文挑战第15天】阿里上线AI电商工具“绘蛙”,让你的商品第一时间被种草
172 2
阿里上线AI电商工具“绘蛙”,让你的商品第一时间被种草
|
存储 SQL 缓存
Aerospike在实时竞价广告中的应用
Aerospike在实时竞价广告中的应用
162 0
Aerospike在实时竞价广告中的应用
|
人工智能 算法 视频直播
多模态视频商品检索记录再刷新!第二届淘宝直播算法大赛完美落幕
多模态视频商品检索记录再刷新!第二届淘宝直播算法大赛完美落幕
多模态视频商品检索记录再刷新!第二届淘宝直播算法大赛完美落幕
|
算法 搜索推荐 数据可视化
阿里拍卖全链路导购策略首次揭秘
拍卖,是一个大家熟悉的词汇,但对在线拍卖以及在线拍卖的个性化推荐,大家可能不那么熟悉。本文将对阿里拍卖业务背景先做介绍,进而描述业务中的浅库存约束及拍品冷启动问题,并分别提出经线上AB实验验证的解法,最后是对解法适用范围的讨论。
|
SQL 缓存 搜索推荐
安排!活动素材的亿级用户精准投放
作者:闲鱼技术-齐悟 1.背景   随着闲鱼用户快速增长,运营活动越来越趋于精细和个性化,运营会根据用户偏好为其投放合适的活动,如下图所示在闲鱼首页商品展示时,会在商品的列表中插入活动Banner,通过这些活动banner引导用户进入到相应活动会场,实现会场导流。
17350 0