其他分享
首页 > 其他分享> > 品牌热度定时任务

品牌热度定时任务

作者:互联网

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