package com.bocloud.ims.service.task;

import com.bocloud.cmp.model.NotifyMessage;
import com.bocloud.common.enums.BoCloudExchange;
import com.bocloud.ims.entity.resource.ObjectRelation;
import com.bocloud.ims.interfaces.resource.CloudVendorService;
import com.bocloud.ims.service.internal.CosInternalService;
import com.bocloud.ims.service.internal.SmsSystemConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;

/**
 * 抽象到期任务
 *
 * @author wangyu
 * @version 1.0
 * @since 2020年5月12日
 */
public abstract class AbstractExpiredTask {

    private ObjectRelation relation;
    private SmsSystemConfigService smsSystemConfigService;
    public CosInternalService cosInternalService;
    public CloudVendorService vendorService;
    private AmqpTemplate amqpTemplate;
    public static final Long ONE_DAY = 24 * 60 * 60 * 1000L;// 一天
    /**
     * 资源到期提前提醒时间
     */
    public int resourceExpireRemindMillis = 15 * ONE_DAY.intValue();
    /**
     * 资源到期最长保留时长
     */
    public int resourceExpireReservedMillis = 30 * ONE_DAY.intValue();
    /**
     * 到期是否自动删除
     */
    public Boolean autoDestroy = false;
    /**
     * 消息通知队列
     */
    private static final String NOTICE_QUEUE = "event.notice.queue";

    /**
     *
     */
    public AbstractExpiredTask() {
    }

    /**
     * @param relation
     * @param smsSystemConfigService
     * @param cosInternalService
     * @param vendorService
     * @param amqpTemplate
     */
    public AbstractExpiredTask(ObjectRelation relation, SmsSystemConfigService smsSystemConfigService,
                               CosInternalService cosInternalService, CloudVendorService vendorService, AmqpTemplate amqpTemplate) {
        super();
        this.relation = relation;
        this.smsSystemConfigService = smsSystemConfigService;
        this.cosInternalService = cosInternalService;
        this.vendorService = vendorService;
        this.amqpTemplate = amqpTemplate;
        this.initConfig();
    }

    /**
     * 初始化相关配置
     */
    private void initConfig() {
        String remindConfig = smsSystemConfigService.getByCode("ResourceExpireRemindDays");
        if (!StringUtils.isEmpty(remindConfig)) {
            resourceExpireRemindMillis = Integer.valueOf(remindConfig) * ONE_DAY.intValue();
        }
        String reservedConfig = smsSystemConfigService.getByCode("ResourceExpireReservedDays");
        if (!StringUtils.isEmpty(reservedConfig)) {
            resourceExpireReservedMillis = Integer.valueOf(reservedConfig) * ONE_DAY.intValue();
        }
        // 到期是否自动删除
        String autoDestroy = smsSystemConfigService.getByCode("AutoDestroy");
        if ("是".equalsIgnoreCase(autoDestroy)) {
            this.autoDestroy = true;
        } else {
            this.autoDestroy = false;
        }
    }

    /**
     * 按步执行资源到期或快到期相关处理
     */
    public void step() {
        boolean flag = this.preCheck();
        if (!flag) {
            return;
        }
        NotifyMessage message = this.process(relation);
        if (null == message) {
            return;
        }
        // 发送消息通知用户
        this.sendMessage(message);
    }

    /**
     * 预检查是否已到期
     */
    public boolean preCheck() {
        if (null == relation || null == relation.getExpiredTime()) {
            return false;
        }
        // 资源到期时间和当前时间差已达到资源到期提前提醒时间
        if (relation.getExpiredTime().getTime() - System.currentTimeMillis() <= resourceExpireRemindMillis) {
            return true;
        }
        return false;
    }

    /**
     * 处理已到期或即将到期任务
     *
     * @param relation
     * @return
     */
    public abstract NotifyMessage process(ObjectRelation relation);

    /**
     * 发送消息通知用户
     *
     * @param message
     */
    private void sendMessage(NotifyMessage message) {
        amqpTemplate.convertAndSend(BoCloudExchange.Default.DIRECT.getExchange(), NOTICE_QUEUE, message);
    }

}
