package com.ywt.outpatient.core.utils;


import com.ywt.biz.common.constant.YwtCommonRespCode;
import com.ywt.biz.common.exception.YwtCommonException;
import com.ywt.biz.common.util.Checker;
import com.ywt.biz.common.util.PhoneNumChecker;
import com.ywt.biz.common.util.StringHelper;

/**
 * @author Walker
 * Created on 2019/7/5
 */
public final class CheckUtil {
    public static final int INPUT_TEXT_LENGTH_100 = 100;
    public static final int INPUT_TEXT_LENGTH_10 = 10;
    public static final int INPUT_TEXT_LENGTH_20 = 20;

    public static void ensureNotEmpty(String val, String msg) throws YwtCommonException {
        if (StringHelper.isNullOrEmpty(val)) throw new YwtCommonException(YwtCommonRespCode.P_ERR, msg);
    }

    public static void ensureStringLength(String val, int length, String msg) throws YwtCommonException {
        if (Checker.getStringValue(val).trim().length() > length)
            throw new YwtCommonException(YwtCommonRespCode.P_ERR, msg);
    }

    public static void ensureLargerThanZero(Integer val, String msg) throws YwtCommonException {
        if (Checker.getIntegerValue(val) <= 0) throw new YwtCommonException(YwtCommonRespCode.P_ERR, msg);
    }

    public static void ensurePositive(Integer val, String msg) throws YwtCommonException {
        if (Checker.getIntegerValue(val) < 0) throw new YwtCommonException(YwtCommonRespCode.P_ERR, msg);
    }

    public static void ensureLargerThanZero(Double val, String msg) throws YwtCommonException {
        if (Checker.getDoubleValue(val) <= 0) throw new YwtCommonException(YwtCommonRespCode.P_ERR, msg);
    }

    public static void ensureLargerThanZero(Long val, String msg) throws YwtCommonException {
        if (Checker.getLongValue(val) <= 0) throw new YwtCommonException(YwtCommonRespCode.P_ERR, msg);
    }

    public static void ensureValidPhoneNumber(String val, String msg) throws YwtCommonException {
        if (!PhoneNumChecker.isPhoneNum(Checker.getStringValue(val)))
            throw new YwtCommonException(YwtCommonRespCode.P_ERR, msg);
    }

    public static void ensureNotNull(Object obj, String msg) throws YwtCommonException {
        if (obj == null) throw new YwtCommonException(YwtCommonRespCode.P_ERR, msg);
    }

    public static void limitString100(String val, String msg) throws YwtCommonException {
        ensureStringLength(val, INPUT_TEXT_LENGTH_100, msg);
    }

    public static void limitString10(String val, String msg) throws YwtCommonException {
        ensureStringLength(val, INPUT_TEXT_LENGTH_10, msg);
    }

    public static void limitStringForName(String val, String msg) throws YwtCommonException {
        ensureStringLength(val, INPUT_TEXT_LENGTH_20, msg);
    }
}
