package com.kiisoo.ic.job.behavior;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kiisoo.ic.system.entity.SysTaskDO;
import com.kiisoo.ic.system.mapper.SysTaskDOMapper;
import com.kiisoo.ic.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import static com.kiisoo.ic.system.constant.SysTaskConstant.*;

/**
 * @author jinchaofan
 * @date 2019/4/28 15:20
 * @company kiisoo
 * @details 获取联系客户统计数据定时任务 job 0 0 1 * * ?
 */
@Component
@Slf4j
public class StorePromotionDataJob {

    /**
     * 客流定时任务 service
     */
    @Autowired
    private CustomerBehaviorJob customerBehaviorJob;
    @Autowired
    private SysTaskDOMapper sysTaskDOMapper;

//    @Scheduled(cron = "0 0 1 * * ?")
    public void syncData(){
        try {
            int random = new Random().nextInt(100);
            Thread.sleep(random*100);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
            QueryWrapper<SysTaskDO> qw = new QueryWrapper<>();
            qw.eq("type",TASK_TYPE_USER_BEHAVIOR).eq("create_time",sdf.format(new Date())).last("limit 1");
            SysTaskDO sysTaskDO = sysTaskDOMapper.selectOne(qw);
            if (sysTaskDO == null){
                sysTaskDO = new SysTaskDO(TASK_NAME_USER_BEHAVIOR,TASK_STATUS_RUNNING,TASK_TYPE_USER_BEHAVIOR,new Date());
                sysTaskDOMapper.insert(sysTaskDO);
                log.info("<--获取联系客户统计数据定时任务执行-->");
                customerBehaviorJob.handle(DateUtils.addDay(new Date(),-3),DateUtils.addDay(new Date(),-1),sysTaskDO);
                log.info("<--获取联系客户统计数据定时任务结束-->");
            }
        } catch (Exception e) {
            log.error("获取联系客户统计数据任务出错", e);
        }
    }
}
