package com.yunzhupaas.flowable.job;

import cn.hutool.core.collection.CollectionUtil;
import com.yunzhupaas.flowable.model.time.FlowTimeModel;
import com.yunzhupaas.flowable.util.TimeUtil;
import com.yunzhupaas.util.JsonUtil;
import com.yunzhupaas.util.RedisUtil;
import com.yunzhupaas.util.StringUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;


/**
 * @author 云筑产品开发平台组
 * @version V3.3.0 flowable
 * @copyright 深圳市乐程软件有限公司
 * @date 2024/6/15 17:37
 */
@Slf4j
public class FlowJobUtil {
    /**
     * 自动审批
     */
    public static final String OPERATOR_REDIS_KEY = "flowable_OperatorList";

    /**
     * 自动转审
     */
    public static final String OPERATOR_TRANSFER = "flowable_transfer";

    /**
     * 超时
     */
    public static final String TIME_REDIS_KEY = "flowable_TimeModel";

    /**
     * 提醒
     */
    public static final String NOTICE_REDIS_KEY = "flowable_NoticeModel";

    public static FlowTimeModel getModel(FlowTimeModel model, RedisUtil redisUtil) {
        String id = model.getOperatorId();
        String hashValues = redisUtil.getHashValues(model.getOverTime() ? TIME_REDIS_KEY : NOTICE_REDIS_KEY, id);
        FlowTimeModel integrateModel = StringUtil.isNotEmpty(hashValues) ? JsonUtil.getJsonToBean(hashValues, FlowTimeModel.class) : null;
        return integrateModel;
    }

    public static void insertModel(FlowTimeModel model, RedisUtil redisUtil) {
        String integrateId = model.getOperatorId();
        redisUtil.insertHash(model.getOverTime() ? TIME_REDIS_KEY : NOTICE_REDIS_KEY, integrateId, JsonUtil.getObjectToString(model));
    }

    public static void removeModel(FlowTimeModel model, RedisUtil redisUtil) {
        redisUtil.removeHash(model.getOverTime() ? TIME_REDIS_KEY : NOTICE_REDIS_KEY, model.getOperatorId());
    }

    public static void insertOperator(FlowTimeModel model, RedisUtil redisUtil) {
        redisUtil.insertHash(OPERATOR_REDIS_KEY, model.getOperatorId(), JsonUtil.getObjectToString(model));
    }

    public static void remove(FlowTimeModel model, RedisUtil redisUtil) {
        redisUtil.removeHash(OPERATOR_TRANSFER, model.getOperatorId());
        redisUtil.removeHash(OPERATOR_REDIS_KEY, model.getOperatorId());
        redisUtil.removeHash(TIME_REDIS_KEY, model.getOperatorId());
        redisUtil.removeHash(NOTICE_REDIS_KEY, model.getOperatorId());
    }

    public static void removeTransfer(FlowTimeModel model, RedisUtil redisUtil) {
        redisUtil.removeHash(OPERATOR_TRANSFER, model.getOperatorId());
    }

    public static List<FlowTimeModel> getOperator(RedisUtil redisUtil) {
        List<FlowTimeModel> list = new ArrayList<>();
        List<String> hashValues = redisUtil.getHashValues(OPERATOR_REDIS_KEY);
        if (CollectionUtil.isNotEmpty(hashValues)) {
            for (String hashValue : hashValues) {
                FlowTimeModel integrateModel = StringUtil.isNotEmpty(hashValue) ? JsonUtil.getJsonToBean(hashValue, FlowTimeModel.class) : null;
                list.add(integrateModel);
            }
        }
        return list;
    }

    public static void insertTransfer(FlowTimeModel model, RedisUtil redisUtil) {
        redisUtil.insertHash(OPERATOR_TRANSFER, model.getOperatorId(), JsonUtil.getObjectToString(model));
    }

    public static List<FlowTimeModel> getTransfer(RedisUtil redisUtil) {
        List<FlowTimeModel> list = new ArrayList<>();
        List<String> hashValues = redisUtil.getHashValues(OPERATOR_TRANSFER);
        if (CollectionUtil.isNotEmpty(hashValues)) {
            for (String hashValue : hashValues) {
                FlowTimeModel integrateModel = StringUtil.isNotEmpty(hashValue) ? JsonUtil.getJsonToBean(hashValue, FlowTimeModel.class) : null;
                list.add(integrateModel);
            }
        }
        return list;
    }

    public static void deleteByOperatorId(String operatorId, RedisUtil redisUtil) {
        FlowTimeModel timeModel = new FlowTimeModel();
        timeModel.setOperatorId(operatorId);
        FlowTimeModel flowTimeModel = FlowJobUtil.getModel(timeModel, redisUtil);
        if (null != flowTimeModel) {
            TimeUtil.deleteJob(flowTimeModel.getId());
            FlowJobUtil.remove(flowTimeModel, redisUtil);
        }
        timeModel.setOverTime(true);
        FlowTimeModel overTimeModel = FlowJobUtil.getModel(timeModel, redisUtil);
        if (null != overTimeModel) {
            TimeUtil.deleteJob(overTimeModel.getId());
            FlowJobUtil.remove(overTimeModel, redisUtil);
        }
    }
}

