package com.kingdee.eas.custom.integration.perf.validator;

import com.kingdee.eas.custom.integration.perf.dto.PerfEvalSyncRequest;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.regex.Pattern;

/**
 * ??งน???????????งต??????
 */
public class PerfEvalSyncValidator {

    private static final Pattern YEAR_MONTH_PATTERN = Pattern.compile("^\\d{4}-\\d{2}$");

    public static String validateAndGetError(PerfEvalSyncRequest request) {
        try { validate(request); } catch (IllegalArgumentException e) { return e.getMessage(); }
        return null;
    }

    private static void validate(PerfEvalSyncRequest request) {
        if (request == null) throw new IllegalArgumentException("?????I?????");
        if (StringUtils.isBlank(request.getPersonNumber())) throw new IllegalArgumentException("???????(personNumber)???????");
        if (StringUtils.isBlank(request.getYearMonth())) throw new IllegalArgumentException("??งน????(yearMonth)???????");
        if (!YEAR_MONTH_PATTERN.matcher(request.getYearMonth()).matches())
            throw new IllegalArgumentException("??งน????(yearMonth)??????????????: yyyy-MM");
        if (request.getPerfCoeff() == null) throw new IllegalArgumentException("??งน???(perfCoeff)???????");
        if (request.getPerfCoeff().compareTo(BigDecimal.ZERO) < 0)
            throw new IllegalArgumentException("??งน???(perfCoeff)?????????");
    }
}
