package com.reptileclient.jlifeapp.web;

import com.reptileclient.jlifeapp.schedule.*;
import com.reptileclient.jlifeapp.util.ResponseUtil;
import com.reptileclient.jlifeapp.util.page.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class JobController {


    @Autowired
    private DouyinLaikeTask douyinLaikeTask;//抖来客数据 8点一次


    @Autowired
    private HMClueSyncTask hmClueSyncTask;//盒马线索

    @Autowired
    private HmETLTask hmETLTask;//获取支付和服务单

    @Autowired
    private NoticeTokenTask noticeTokenTask;//刷新token

    @Autowired
    private EmailNoticeTask emailNoticeTask;//发送邮件提醒

    static String apiKey="rpcTask";

    @GetMapping("/job")
    public Object wxLocalLiveJob(@RequestParam String sign, @RequestParam Long timestamp) {
        if (StringUtils.isEmpty(sign) || timestamp == null)
            return ResponseUtil.fail("非法请求");

        if (!MD5Util.convertMD5(apiKey + timestamp.toString()).equals(sign))
             return ResponseUtil.fail("非法请求");

        try {
            //每天一次
            douyinLaikeTask.startJob();

            //盒马线索
            hmClueSyncTask.startJob();

            //支付和服务单
            hmETLTask.startJob();

            //刷新token 一天一次
            noticeTokenTask.startJob();

            //发送邮件提醒
            emailNoticeTask.startJob();

            return ResponseUtil.ok();
        } catch (Exception ex) {
            log.error("HM Job", ex);
            return ResponseUtil.fail(ex.getMessage());
        }
    }


}
