package com.ads.common.scheduled;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.ads.business.entity.Conversation;
import com.ads.business.service.ConversationService;
import com.ads.common.dingTalk.domain.ServiceResult;
import com.ads.common.dingTalk.service.DingTalkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: hsayi_docsecurity_api
 * @description: Spring注解定时任务
 * @author: 史偕成
 * @create: 2020-08-05 09:49
 **/
@Component
@Configuration
@EnableScheduling
@Slf4j
public class ScheduleTask {

    @Resource
    private DingTalkService dingTalkService;

    @Resource
    private ConversationService conversationService;

    @Scheduled(cron = "0 0 0/1 * * ?")
    public void updateAccessToken() {
        log.info("执行--updateAccessToken任务--开始" + DateUtil.now());
        ServiceResult<String> result = dingTalkService.getAccessToken();
        dingTalkService.getJsTicket(result.getResult());
        log.info("执行--updateAccessToken任务--结束" + DateUtil.now());
    }

    /**
     * 同步会话
     */
    @Scheduled(cron = "0 0,30 * * * ?")
    public void syncInspection() {
        log.info("执行--syncInspection 任务--开始" + DateUtil.now());
        List<Conversation> list = conversationService.list(new LambdaQueryWrapper<Conversation>()
                .eq(Conversation::getStatus, 1));
        if (CollectionUtil.isNotEmpty(list)) {
            list.forEach(conversation -> {
                conversationService.sync(conversation.getChatId());
            });
        }
        log.info("执行--syncInspection 任务--结束" + DateUtil.now());

    }
}
