package com.sundot.airport.common.cache;

import com.sundot.airport.common.core.domain.AjaxResult;
import com.sundot.airport.common.core.page.TableDataInfo;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Map;

/**
 * 统计缓存缓存条件判断工具类
 */
@Component
public class StatisticsCacheConditionUtil {

    /**
     * 判断R对象是否成功
     */
    public static boolean isSuccess(Object result) {
        if (!(result instanceof AjaxResult)) {
            return false;
        }
        AjaxResult r = (AjaxResult) result;
        return r.get(AjaxResult.CODE_TAG) != null &&
                r.get(AjaxResult.CODE_TAG).equals(200) &&
                r.get(AjaxResult.DATA_TAG) != null;
    }

    /**
     * 判断TableDataInfo是否成功
     */
    public static boolean isTableSuccess(Object result) {
        if (!(result instanceof TableDataInfo)) {
            return false;
        }
        TableDataInfo tableData = (TableDataInfo) result;
        return tableData.getTotal() >= 0 && tableData.getRows() != null;
    }

    /**
     * 判断数据是否为空
     */
    public static boolean isEmptyData(Object result) {
        if (result instanceof AjaxResult) {
            AjaxResult r = (AjaxResult) result;
            Object data = r.get(AjaxResult.DATA_TAG);
            return data == null ||
                    (data instanceof Collection && ((Collection<?>) data).isEmpty()) ||
                    (data instanceof Map && ((Map<?, ?>) data).isEmpty());
        }
        return false;
    }

}
