package org.springblade.flow.base.util.jobutil;

import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springblade.flow.database.data.DataSourceContextHolder;
import org.springblade.flow.scheduletask.model.ContentNewModel;
import org.springblade.flow.scheduletask.service.TimeService;
import org.springblade.flow.util.StringUtil;
import org.springblade.flow.util.context.SpringContext;


/**
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2021/3/12 15:31
 */
@Slf4j
public class TimeJob implements Job {

    @Override
    public void execute(JobExecutionContext context) {
        System.out.println("进入调度");
        JobDataMap dataMap = context.getMergedJobDataMap();
        ContentNewModel model = dataMap.get("content") != null ? (ContentNewModel) dataMap.get("content") : null;
        String type = dataMap.getString("type");
        String id = dataMap.getString("id");
        String tenantId = dataMap.getString("tenantId");
        String token = dataMap.getString("token");
        String tenantDbConnectionString = dataMap.getString("tenantDbConnectionString");
        if (model != null) {
            if (StringUtil.isNotEmpty(tenantId)) {
                DataSourceContextHolder.setDatasource(tenantId, tenantDbConnectionString);
            }
            TimeService timeService = SpringContext.getBean(TimeService.class);
            if ("1".equals(type)) {
                timeService.connector(model, id, token);
            } else {
                timeService.storage(model, id);
            }
        }
    }

}
