package com.ywt.outpatient.taihe.rpc.core.utils;


import com.ywt.outpatient.taihe.rpc.domain.enums.OfflineConsultationSourceEnum;
import com.ywt.outpatient.taihe.rpc.domain.enums.TerminalEnum;
import com.ywt.outpatient.taihe.rpc.service.Constants;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author Walker
 * Created on 2020/11/24
 */
@Component
public class BizUtil {

    private static String payCode4NfyybyfyTest;

    @Value("${test.paycode4nfyybyfy:}")
    public void setPayCode4NfyybyfyTest(String pc) {
        BizUtil.payCode4NfyybyfyTest = pc;
    }
    /**
     * 根据 hospitalId 返回 hospital name, 不查询数据库，直接读取 {@link Constants}
     * @param hospitalId 医院 id
     * @return 医院名称
     */
    public static String getHospitalNameByIdSimply(int hospitalId) {
        switch (hospitalId) {
            case Constants.TAIHE_HOSPITAL_ID:
                return Constants.TAIHE_HOSPITAL_NAME;
            case Constants.NFYYBYFY_HOSPITAL_ID:
                return Constants.NFYYBYFY_HOSPITAL_NAME;
            default:
                return "";
        }
    }

    /**
     * 根据医院 id 获取对应医院的支付代码，目前只支持太和和白云分院
     * v1.6.5_nfby 开始，白云小程序支持太和医院的挂号，订单的支付代码根据创建订单的客户端来源判断
     * @param hospitalId 医院 id
     * @return 支付代码
     */
    public static String getHospPayCodeByHospId(int hospitalId, int source) {
        switch (hospitalId) {
            case Constants.TAIHE_HOSPITAL_ID:
                if (isAlipayMp(source)) return Constants.TH_PAY_CODE_ALIPAY;
                String nfyybyfyTaihePayCode = StringHelper.isNullOrEmpty(payCode4NfyybyfyTest) ? Constants.NFYYBYFY_TAIHE_REG_PAY_CODE : payCode4NfyybyfyTest;
                return source == OfflineConsultationSourceEnum.NFYYBYFY_WXAPP.getValue() ? nfyybyfyTaihePayCode : Constants.TAIHE_PAY_CODE;
            case Constants.NFYYBYFY_HOSPITAL_ID:
                if (isAlipayMp(source)) return Constants.NFYYBYFY_PAY_CODE_ALIPAY;
                if (!StringHelper.isNullOrEmpty(payCode4NfyybyfyTest)) return payCode4NfyybyfyTest;
                return Constants.NFYYBYFY_PAY_CODE;
            default:
                return "";
        }
    }

    /**
     * 根据终端判断是否支付宝小程序
     * @param terminal 终端号
     * @return 是否支付宝小程序
     */
    public static boolean isAlipayMp(int terminal) {
        return TerminalEnum.valueOf(terminal) == TerminalEnum.NFYYBYFY_ALIPAY_MP || TerminalEnum.valueOf(terminal) == TerminalEnum.TH_ALIPAY_MP;
    }

}
