package com.inkasso.adapter.util;


import com.inkasso.auth.exception.ValidatorException;
import com.inkasso.factoring.common.entity.BaseEntity;
import com.inkasso.factoring.common.entity.ValidatorEntity;

import java.util.Map;

/**
 * 验证数据
 */
public class ValidatorUtil {

    /**
     * 验证数据是否合法
     *
     * @param type       验证类型
     * @param baseEntity 被验证实体
     */
    public static void validator(ValidatorEntity.ValidatorType type, BaseEntity baseEntity) {
        if (baseEntity == null) {
            throw new ValidatorException(310, "空数据");
        }
        baseEntity.validator(type);
        if (baseEntity.hasErrors()) {
            Map<String, String> map = baseEntity.getErrors();
            for (String key : map.keySet()) {
                throw new ValidatorException(CodeUtil.PARAMETER_ERROR, map.get(key));
            }
        }
    }

    /**
     * 验证数据是否合法
     *
     * @param type   验证类型
     * @param entity 被验证实体
     */
    public static void validator(ValidatorEntity.ValidatorType type, ValidatorEntity entity) {
        if (entity == null) {
            throw new ValidatorException(310, "空数据");
        }
        entity.validator(type);
        if (entity.hasErrors()) {
            Map<String, String> map = entity.getErrors();
            for (String key : map.keySet()) {
                throw new ValidatorException(CodeUtil.PARAMETER_ERROR, map.get(key));
            }
        }
    }
}
