package com.yunzhupaas.flowable.job;

import cn.hutool.core.util.ObjectUtil;
import com.yunzhupaas.base.UserInfo;
import com.yunzhupaas.config.ConfigValueUtil;
import com.yunzhupaas.database.util.TenantDataSourceUtil;
import com.yunzhupaas.flowable.model.task.FlowModel;
import com.yunzhupaas.flowable.model.templatenode.nodejson.NodeModel;
import com.yunzhupaas.flowable.model.templatenode.nodejson.TimeConfig;
import com.yunzhupaas.flowable.model.time.FlowTimeModel;
import com.yunzhupaas.flowable.util.OverTimeUtil;
import com.yunzhupaas.flowable.util.TimeUtil;
import com.yunzhupaas.util.JsonUtil;
import com.yunzhupaas.util.RedisUtil;
import com.yunzhupaas.util.context.SpringContext;
import org.quartz.*;
import org.springframework.scheduling.quartz.QuartzJobBean;


@DisallowConcurrentExecution
public class FlowTime extends QuartzJobBean {

    private static RedisUtil redisUtil;
    private static ConfigValueUtil configValueUtil;
    private static OverTimeUtil overTimeUtil;


    static {
        redisUtil = SpringContext.getBean(RedisUtil.class);
        configValueUtil = SpringContext.getBean(ConfigValueUtil.class);
        overTimeUtil = SpringContext.getBean(OverTimeUtil.class);
    }

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        JobDetail jobDetail = context.getJobDetail();
        String jobName = jobDetail.getKey().getName();
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        FlowTimeModel model = FlowJobUtil.getModel(JsonUtil.getJsonToBean(jobDataMap, FlowTimeModel.class), redisUtil);
        boolean isModel = model == null;
        FlowTimeModel timeModel = isModel ? JsonUtil.getJsonToBean(jobDataMap, FlowTimeModel.class) : model;
        if (timeModel != null) {
            FlowModel flowModel = timeModel.getFlowModel();
            UserInfo userInfo = flowModel.getUserInfo();
            if (configValueUtil.isMultiTenancy()) {
                TenantDataSourceUtil.switchTenant(userInfo.getTenantId());
            }
            try {
                overTimeUtil.overMsg(timeModel);
                Boolean isPause = timeModel.getIsPause();
                timeModel.setIsPause(false);
                boolean overTime = timeModel.getOverTime();
                FlowJobUtil.insertModel(timeModel, redisUtil);
                if (overTime) {
                    if (isPause) {
                        return;
                    }
                    NodeModel nodeModel = timeModel.getChildNode();
                    TimeConfig time = nodeModel.getOverTimeConfig();
                    Integer num = time.getOverAutoApproveTime();
                    Boolean overAutoApprove = timeModel.getChildNode().getOverTimeConfig().getOverAutoApprove();
                    if (overAutoApprove && ObjectUtil.equals(timeModel.getNum(), num)) {
                        // 放在另一个redis的key(用于自动审批)
                        FlowJobUtil.insertOperator(timeModel, redisUtil);
                    }
                    Boolean overAutoTransfer = timeModel.getChildNode().getOverTimeConfig().getOverAutoTransfer();
                    Integer transferNum = time.getOverAutoTransferTime();
                    if (!overAutoApprove && overAutoTransfer && ObjectUtil.equals(timeModel.getTransferNum(), transferNum)) {
                        FlowJobUtil.insertTransfer(timeModel, redisUtil);
                    }
                } else {
                    // 下一次时间大于结束时间 删除提醒缓存
                    if (timeModel.getEndDate().getTime() < context.getNextFireTime().getTime()) {
                        FlowJobUtil.removeModel(timeModel, redisUtil);
                        TimeUtil.deleteJob(jobName);
                    }
                }
            } catch (Exception e) {
                FlowJobUtil.removeModel(timeModel, redisUtil);
                TimeUtil.deleteJob(jobName);
            }
        } else {
            TimeUtil.deleteJob(jobName);
            FlowJobUtil.removeModel(model, redisUtil);
        }
    }


}

