package com.zcxk.core.utils;

import com.alibaba.fastjson.JSONObject;
import com.zcxk.core.common.enums.RespCodeEnum;
import com.zcxk.core.common.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;

/**
 * @author Andy
 * @description: JSONObject 传参 校验
 * @date 2021/7/16
 * @version V1.0
 **/
public class JsonObjectParamCheckUtils {


    /**
     * 检验字段
     * @param msg 字段异常描述
     * @param name 字段名称
     * @param type 字段类型  0 字符串 1 整数 2 double 3 大于0 double
     * @param param 入参
     */
    public static void checkParams(String name, Integer type, JSONObject param, String msg) {

        // 校验数值 > 0
        if ( 3 == type) {
            if (0 > param.getDouble(name)) {
                throw BusinessException
                    .builder(RespCodeEnum.ERROR_500.getStatus(), msg);
            }
        }

        // double 数值行
        if (2 == type) {
            if (null == param.getDouble(name)) {
                throw BusinessException.builder(RespCodeEnum.GLOBAL_PARAMETER_NOT_EMPTY.getStatus(),
                    msg + RespCodeEnum.GLOBAL_PARAMETER_NOT_EMPTY);
            }
        }

        // 数字整形
        if (1 == type) {
            if (null == param.getInteger(name)) {
                throw BusinessException.builder(RespCodeEnum.GLOBAL_PARAMETER_NOT_EMPTY.getStatus(),
                    msg + RespCodeEnum.GLOBAL_PARAMETER_NOT_EMPTY.getMessage());
            }
        }

        // 字符串
        if (0 == type) {
            if (StringUtils.isBlank(param.getString(name))) {
                throw BusinessException.builder(RespCodeEnum.GLOBAL_PARAMETER_NOT_EMPTY.getStatus(),
                    msg + RespCodeEnum.GLOBAL_PARAMETER_NOT_EMPTY.getMessage());
            }
        }
    }


}
