package com.sundot.airport.exam.common;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.sundot.airport.common.exception.ServiceException;

import java.util.List;

public class CheckUtil {

    /**
     * <b>方法名：validateStringParam </b><br>
     * <b>说明：String类型验证</b><br>
     *
     * @param str String
     * @param msg AbstractBaseExceptionEnum <b>修改履历：</b>
     * @author 2020/12/1 xuxiongzi
     */
    public static void strCheck(String str, AbstractBaseExceptionEnum msg) {
        if (StrUtil.isBlank(str)) {
            throw new ServiceException(msg.getMessage(),msg.getCode());
        }
    }

    /**
     * <b>方法名：objCheck </b><br>
     * <b>说明：对象类型为空验证</b><br>
     *
     * @param obj Object
     * @param msg UserExceptionEnum <b>修改履历：</b>
     * @author 2020/12/1 xuxiongzi
     */
    public static void objIsNull(Object obj, AbstractBaseExceptionEnum msg) {
        if (ObjectUtil.isNull(obj)) {
            throw new ServiceException(msg.getMessage(),msg.getCode());
        }
    }

    /**
     * <b>方法名：objCheck </b><br>
     * <b>说明：对象类型非空验证</b><br>
     *
     * @param obj Object
     * @param msg UserExceptionEnum <b>修改履历：</b>
     * @author 2020/12/1 xuxiongzi
     */
    public static void objIsNotNull(Object obj, AbstractBaseExceptionEnum msg) {
        if (ObjectUtil.isNotNull(obj)) {
            throw new ServiceException(msg.getMessage(),msg.getCode());
        }
    }

    /**
     * <b>方法名：booleanCheck </b><br>
     * <b>说明：</b>boolean表达式异常抛出，此方法可部分代替if<br>
     *
     * @param flag boolean表达式
     * @param msg  UserExceptionEnum <b>修改履历：</b>
     * @author 2020/12/1 xuxiongzi
     */
    public static void boolCheck(boolean flag, AbstractBaseExceptionEnum msg) {
        if (flag) {
            throw new ServiceException(msg.getMessage(),msg.getCode());
        }
    }

    /**
     * <b>方法名：phoneCheck </b><br>
     * <b>说明：验证手机号</b><br>
     *
     * @param phone 手机号 <b>修改履历：</b>
     * @author 2020/11/25 xuxiongzi
     */
    public static void phoneCheck(String phone) {
        CheckUtil.strCheck(phone, CheckExceptionEnum.LOGIN_PHONE_NULL);
        if (!Validator.isMobile(phone)) {
            throw new ServiceException(CheckExceptionEnum.LOGIN_PHONE_ERROR.getMessage(),CheckExceptionEnum.LOGIN_PHONE_ERROR.getCode());
        }
    }

    /**
     * <b>方法名：数组类型非空校验</b><br>
     * <b>说明：</b> <br>
     *
     * @param list List
     * @param msg  AbstractBaseExceptionEnum
     * @author 2020/12/7 wangdonghao
     */

    public static void listNotEmpty(List list, AbstractBaseExceptionEnum msg) {
        if (CollectionUtils.isNotEmpty(list)) {
            throw new ServiceException(msg.getMessage(),msg.getCode());
        }
    }

    /**
     * <b>方法名:</b> ListEmpty <br>
     * <b>说明:</b> 数组非空校验 <br>
     *
     * @param list List
     * @param msg  AbstractBaseExceptionEnum <b>修改履历:</b>
     * @author 2020/12/16 0016 Rambo
     */
    public static void listEmpty(List list, AbstractBaseExceptionEnum msg) {
        if (CollectionUtils.isEmpty(list)) {
            throw new ServiceException(msg.getMessage(),msg.getCode());
        }
    }

    private CheckUtil() {
    }
}
