ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

品牌热度定时任务

2022-02-08 20:31:04  阅读:155  来源: 互联网

标签:joinBrandHotRecord DateUtils 热度 String 品牌 brandCode executeDate joinBrandHot 定时


1 设置定时任务

/**
 * @description: 品牌热度定时任务
 * @author: hewen
 * @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;
    }
}

标签:joinBrandHotRecord,DateUtils,热度,String,品牌,brandCode,executeDate,joinBrandHot,定时
来源: https://blog.csdn.net/weixin_43206161/article/details/122830638

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有