package com.fdkankan.platform.task;

import com.fdkankan.common.constant.ConstantFilePath;
import org.apache.tools.ant.util.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;

@Component
public class Schedule {
    public static final Logger log = LoggerFactory.getLogger("programLog");

    @Value("${environment}")
    private String environment;

    @Autowired
    private TaskService taskService;


    @Scheduled(cron = "0 0 4 * * ?")
    public void job1() {
        if("hq".equals(environment)){
            return;
        }
        log.warn("每天04:00开始执行定时任务：删除二维码");
        // 删除登录二维码
        String path = ConstantFilePath.LOGIN_QR_CODE_PATH;
        File file = new File(path);
        File[] files = file.listFiles();
        if (files != null && files.length > 0){
            for (File delFile : files){
                if (delFile.isFile()) FileUtils.delete(delFile);
            }
        }

        // 删除支付宝支付二维码
        path = ConstantFilePath.ALI_QRCODE_FOLDER;
        file = new File(path);
        files = file.listFiles();
        if (files != null && files.length > 0){
            for (File delFile : files){
                if (delFile.isFile()) FileUtils.delete(delFile);
            }
        }

        // 删除微信支付二维码
        path = ConstantFilePath.WEIXIN_QRCODE_FOLDER;
        file = new File(path);
        files = file.listFiles();
        if (files != null && files.length > 0){
            for (File delFile : files){
                if (delFile.isFile()) FileUtils.delete(delFile);
            }
        }
    }

    @Scheduled(cron = "0 0 4 * * ?")
    public void job2() {
        if("hq".equals(environment)){
            return;
        }
        log.warn("每天04:00开始执行定时任务：双目相机扣费");
        // 扣费
        try {
            taskService.autoDedu();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }

    @Scheduled(cron = "0 0 4 * * ?")
    public void job3() {
        if("hq".equals(environment)){
            return;
        }
        log.warn("每天04:00开始执行定时任务：更新收货状态");
        try {
            //更新收货状态，发货后15天，默认用户已经收到货物
            taskService.autoUpdateOrderStatus();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }

    @Scheduled(cron = "0 0 4 * * ?")
    public void job5() {
        if("hq".equals(environment)){
            return;
        }
        log.warn("每天04:00开始执行定时任务：相机容量到期检查（扩容、商业）");
        try {
            taskService.maturitySpace();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }

    @Scheduled(cron = "0 0 0 * * ?")
    public void job10() {
        if("hq".equals(environment)){
            return;
        }
        log.warn("每天00:00开始执行定时任务：增值权益到期检查");
        try {
            taskService.incrementExpire();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }

    @Scheduled(cron = "0 0 12 * * ?")
    public void job11() {
        if("hq".equals(environment)){
            return;
        }
        log.warn("每天12:00开始执行定时任务：短信提醒增值权益准备到期");
        try {
            taskService.incrementExpireSendSms();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }

}
