package com.jianshui.invoice.utils;

import com.jianshui.common.core.domain.AjaxResult;
import com.jianshui.common.core.domain.entity.Companyservice;
import com.jianshui.common.exception.jianshui.JianshuiParamErrorException;
import com.jianshui.common.utils.ValidateUtils;
import com.jianshui.common.utils.bean.BeanUtils;
import com.jianshui.invoice.domain.BillDetail;

/**
 * @Description 发票订单工具类
 * @Author 巩权林
 * @Date 2022-03-21 16:58
 **/
public class BillDetailUtils {


    /**
     * 验证billInfo
     *
     * @param validateDetailfoClazz 要校验的目标bill类型
     * @param billDetail            原始billinfo
     * @param <T>
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public static <T> void validate(Class<T> validateDetailfoClazz, BillDetail billDetail, Companyservice companyservice, String serviceKey) throws IllegalAccessException, InstantiationException {
        T validateBillDetail = validateDetailfoClazz.newInstance();
        BeanUtils.copyProperties(billDetail, validateBillDetail);
        ValidateUtils.validate(validateBillDetail, companyservice, serviceKey);
    }


    /**
     * 预处理
     *
     * @param billDetail
     * @param companyservice
     * @return
     */
    public static BillDetail preprocessing(BillDetail billDetail, Companyservice companyservice) {
        BillDetail detail = billDetail;
        AjaxResult formatResult = BillDetailFormatUtil.detailFormat(billDetail);
        if (formatResult.isError()) {
            int code = formatResult.getCode();
            if (code == AjaxResult.error().getCode()) {
                throw new JianshuiParamErrorException(formatResult.getMsg(), companyservice, "invoice");
            }
        } else {
            detail = formatResult.getJsonData(BillDetail.class);
        }
        return detail;
    }
}
