package com.ywt.outpatient.service.rpc;

import com.ywt.biz.common.util.DateUtil;
import com.ywt.biz.common.util.RandomUtil;
import com.ywt.biz.common.util.StringHelper;
import com.ywt.outpatient.domain.entity.center.SequenceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class IdGenerator {
    /**
     * 患者表名
     */
    private final String PATIENT_TABLE_NAME = "patient";

    /**
     * 用户表名
     */
    private final String USER_TABLE_NAME = "user";

    /**
     * 短信
     */
    private final String SMS_TABLE_NAME = "sms";

    /**
     * 订单表名
     */
    private final String ORDER_TABLE_NAME = "order";

    /**
     * 订单号标志
     */
    private final String ORDER_NO_TAG_NAME = "orderno";

    /**
     * 询诊
     */
    private final String TREATMENT_TABLE_NAME = "treatment";

    /**
     * 银行卡
     */
    private final String BANK_CARD_NAME = "bank_card";

    /**
     * 提现
     */
    private final String WITHDRAW_APPLICATION_NAME = "withdraw_application";

    /**
     * 关联医生表
     */
    private final String UNION_DOCTOR_NAME = "union_doctor";

    /**
     * 反馈表
     */
    private final String FEEDBACK_NAME = "feedback";

    /**
     * 转诊表
     */
    private final String TRANSFER_TREATMENT_NAME = "transfer_treatment";

    /**
     * 主诊表
     */
    private final String ATTENDING_TREATMENT_NAME = "attending_treatment";

    /**
     * 社交用户表
     */
    private final String SNS_USER_TABLE_NAME = "sns_user";

    private final String WXAPP_USER_SESSION_TABLE_NAME = "wxapp_user_session";

    /**
     * 用户订单评价表
     */
    private final String ORDER_EVALUATION_TABLE_NAME = "order_evaluation";

    private final String ORDER_PAYMENT_TABLE_NAME = "order_payment";

    private final String REPORT_TABLE_NAME = "report";

    /**
     * 支付流水号
     */
    private final String PAYMENT_NO_TAG_NAME = "payment_no";

    /**
     * 诊疗卡表
     */
    private final String MEDICAL_CARD_TABLE_NAME = "medical_card";

    private final String MEDICAL_RECORDS_TABLE_NAME = "medical_records";

    /**
     * 病历模版
     */
    private final String MEDICAL_MODULE_TABLE_NAME = "medical_module";

    private final String MEDICAL_RECORDS_IMAGE_TABLE_NAME = "medical_records_image";

    private final String DOCTOR_SIGN_TABLE_NAME = "doctor_sign";

    private final String USER_SIGN_TABLE_NAME = "user_sign";

    private final String TREATMENT_REPORT_TABLE_NAME = "treatment_report";

    private final String REGISTERED_ORDER_TABLE_NAME = "registered_order";

    private final String REFUNDED_TABLE_NAME = "refunded";

    private final String CONSULT_ORDER = "consult_order";

    private final String FOLLOW_UP_ORDER = "follow_up_order";

    private final String DOCTOR_INFO_AUDIT = "doctor_info_audit";

    /**
     * 登录日志表
     */
    private final String LOGIN_LOG = "login_log";

    @Autowired
    private SequenceRepository sequenceRepository;

    /**
     * 生成 Patient Id
     *
     * @return
     */
    public int generatePatientId() {
        return sequenceRepository.gen(PATIENT_TABLE_NAME);
    }

    /**
     * 生成 User Id
     *
     * @return
     */
    public int generateUserId() {
        return sequenceRepository.gen(USER_TABLE_NAME);
    }

    /**
     * 生成短信Id
     *
     * @return
     */
    public int generateSmsId() {
        return sequenceRepository.gen(SMS_TABLE_NAME);
    }

    /**
     * 生成订单Id
     *
     * @return
     */
    public int generateOrderId() {
        return sequenceRepository.gen(ORDER_TABLE_NAME);
    }

    /**
     * 生成银行卡Id
     *
     * @return
     */
    public int generateBankCardId() {
        return sequenceRepository.gen(BANK_CARD_NAME);
    }

    /**
     * 生成提现表Id
     *
     * @return
     */
    public int generateWithdrawApplicationId() {
        return sequenceRepository.gen(WITHDRAW_APPLICATION_NAME);
    }

    /**
     * 生成提现表Id
     *
     * @return
     */
    public int generateUnionDoctorId() {
        return sequenceRepository.gen(UNION_DOCTOR_NAME);
    }

    /**
     * 生成反馈表Id
     *
     * @return
     */
    public int generateFeedbackId() {
        return sequenceRepository.gen(FEEDBACK_NAME);
    }


    /**
     * 生成订单号
     *
     * @return
     */
    public String generateOrderNo(String prefix) {
        if (StringHelper.isNullOrEmpty(prefix)) {
            prefix = "";
        }

        Date currentDate = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");

        int sn = sequenceRepository.generateSn(ORDER_NO_TAG_NAME);
        return prefix + format.format(currentDate) + String.format("%04d", sn);
    }

    public int genPaymentNo() {
        return sequenceRepository.generateSn(PAYMENT_NO_TAG_NAME);
    }

    /**
     * 生成支付流水号（微信支付，商户订单号总长度不能超过32位）
     * prefix + yyyyMMddHHmmssSSS + 0001(四位唯一标识码) + 321(三位随机数)
     *
     * @param prefix
     * @return
     */
    public String genPaymentNo(String prefix) {
        StringBuilder paymentNo = new StringBuilder();

        if (!StringHelper.isNullOrWhiteSpace(prefix)) {
            paymentNo.append(prefix);
        }

        paymentNo.append(DateUtil.convertToString(new Date(), "yyyyMMddHHmmssSSS"));
        paymentNo.append(String.format("%04d", genPaymentNo()));
        paymentNo.append(RandomUtil.getRandomCode(3, 1));

        return paymentNo.toString();
    }

    /**
     * 生成询诊id
     *
     * @return
     */
    public int generateTreatmentId() {
        return sequenceRepository.gen(TREATMENT_TABLE_NAME);
    }

    /**
     * 生成转诊id
     *
     * @return
     */
    public int generateTransferTreatmentId() {
        return sequenceRepository.gen(TRANSFER_TREATMENT_NAME);
    }

    /**
     * 生成主诊id
     *
     * @return
     */
    public int generateAttendingTreatmentId() {
        return sequenceRepository.gen(ATTENDING_TREATMENT_NAME);
    }

    /**
     * 生成社交用户表主键
     *
     * @return
     */
    public int generateSnsUserId() {
        return sequenceRepository.gen(SNS_USER_TABLE_NAME);
    }

    public int generateWxappUserSessionId() {
        return sequenceRepository.gen(WXAPP_USER_SESSION_TABLE_NAME);
    }


    /**
     * 生成用户订单评价表主键
     *
     * @return
     */
    public int generateOrderEvaluationId() {
        return sequenceRepository.gen(ORDER_EVALUATION_TABLE_NAME);
    }


    /**
     * 订单支付流水表
     *
     * @return
     */
    public int genOrderPaymentId() {
        return sequenceRepository.gen(ORDER_PAYMENT_TABLE_NAME);
    }

    /**
     * 报告主键Id
     *
     * @return
     */
    public int genReportId() {
        return sequenceRepository.gen(REPORT_TABLE_NAME);
    }

    /**
     * 诊疗卡主键Id
     *
     * @return
     */
    public int genMedicalCard() {
        return sequenceRepository.gen(MEDICAL_CARD_TABLE_NAME);
    }

    /**
     * 病历主键Id
     *
     * @return
     */
    public int genMedicalRecordsId() {
        return sequenceRepository.gen(MEDICAL_RECORDS_TABLE_NAME);
    }

    /**
     * 病历模版主键Id
     *
     * @return
     */
    public int genMedicalModuleId() {
        return sequenceRepository.gen(MEDICAL_MODULE_TABLE_NAME);
    }

    /**
     * 病历图片主键Id
     *
     * @return
     */
    public int genMedicalRecordsImageId() {
        return sequenceRepository.gen(MEDICAL_RECORDS_IMAGE_TABLE_NAME);
    }

    /**
     * 获取医生签名Id
     *
     * @return
     */
    public int genDoctorSignId() {
        return sequenceRepository.gen(DOCTOR_SIGN_TABLE_NAME);
    }

    public int genUserSignId() {
        return sequenceRepository.gen(USER_SIGN_TABLE_NAME);
    }

    /**
     * 获取远程会诊报告Id
     *
     * @return
     */
    public int genTreatmentReportId() {
        return sequenceRepository.gen(TREATMENT_REPORT_TABLE_NAME);
    }

    /**
     * 获取预约挂号订单Id
     *
     * @return
     */
    public int genRegisteredOrderId() {
        return sequenceRepository.gen(REGISTERED_ORDER_TABLE_NAME);
    }

    /**
     * 取消预约订单 / 退款 流水表Id
     *
     * @return
     */
    public int genRefundedId() {
        return sequenceRepository.gen(REFUNDED_TABLE_NAME);
    }

    /**
     * 咨询订单表主键
     *
     * @return
     */
    public int genConsultOrderId() {
        return sequenceRepository.gen(CONSULT_ORDER);
    }

    /**
     * 随访订单主键
     *
     * @return
     */
    public int genFollowUpOrderId() {
        return sequenceRepository.gen(FOLLOW_UP_ORDER);
    }

    /**
     * 登录日志表
     *
     * @return
     */
    public int genLoginLogId() {
        return sequenceRepository.gen(LOGIN_LOG);
    }

    /**
     * 医生信息审核主键
     *
     * @return
     */
    public int genDoctorInfoAuditId() {
        return sequenceRepository.gen(DOCTOR_INFO_AUDIT);
    }

    public int genAccountId() {
        return sequenceRepository.gen("account");
    }

    public int genAccountDetailId() {
        return sequenceRepository.gen("account_detail");
    }

    public int genInspectionReportId() {
        return sequenceRepository.gen("inspection_report");
    }

    public int genInspectionDetailId() {
        return sequenceRepository.gen("inspection_detail");
    }

    public int genLaboratoryReportId() {
        return sequenceRepository.gen("laboratory_report");
    }

    public int genLaboratoryDetailId() {
        return sequenceRepository.gen("laboratory_detail");
    }

    public int genAddressId() {
        return sequenceRepository.gen("address");
    }

    public int genSnsUserInfoId() {
        return sequenceRepository.gen("sns_user_info");
    }

    public int genScheduleConfigId() {
        return sequenceRepository.gen("schedule_config");
    }

    public int genDoctorScheduleId() {
        return sequenceRepository.gen("doctor_schedule");
    }

    public int genFollowUpConfigId() {
        return sequenceRepository.gen("follow_up_config");
    }

    public int genAdditionalRegistrationId() {
        return sequenceRepository.gen("additional_registration");
    }

    /**
     * 问卷回答内容Id
     *
     * @return
     */
    public int genAnswerId() {
        return sequenceRepository.gen("answer");
    }

    /**
     * 团队ID
     *
     * @return
     */
    public int genTeamId() {
        return sequenceRepository.gen("team");
    }

    /**
     * 团队成员表ID
     *
     * @return
     */
    public int genTeamDoctorId() {
        return sequenceRepository.gen("team_doctor");
    }

    /**
     * 用户关联表ID
     *
     * @return
     */
    public int genUserUnionId() {
        return sequenceRepository.gen("user_union");
    }

    /**
     * 用户关联表ID
     *
     * @return
     */
    public int genUserUnionsId() {
        return sequenceRepository.gen("user_unions");
    }

    /**
     * 门诊缴费单Id
     *
     * @return
     */
    public int genOutpatientOrderId() {
        return sequenceRepository.gen("outpatient_order");
    }

    /**
     * 提现订单id
     *
     * @return
     */
    public int genWithdrawOrderId() {
        return sequenceRepository.gen("withdraw_order");
    }

    /**
     * 资讯关联ID
     *
     * @return
     */
    public int genArticlesUnionId() {
        return sequenceRepository.gen("articles_union");
    }

    /**
     * 关注表ID
     *
     * @return
     */
    public int genAttentionId() {
        return sequenceRepository.gen("attention");
    }

    public int genBrowsingHistoryId() {
        return sequenceRepository.gen("browsing_history");
    }

    /**
     * 医生统计汇总表
     *
     * @return
     */
    public int genDoctorStatisticsTotal() {
        return sequenceRepository.gen("doctor_statistics_total");
    }

    /**
     * 用户的个人资料表
     *
     * @return
     */
    public int genUserPatientId() {
        return sequenceRepository.gen("user_patient");
    }

    public int genArticlesFavoritesId() {
        return sequenceRepository.gen("articles_favorites");
    }

    public int genArticlesId() {
        return sequenceRepository.gen("articles");
    }

    public int genArticleCommentId() {
        return sequenceRepository.gen("articles_comments");
    }

    public int genArticleSettingId() {
        return sequenceRepository.gen("article_setting");
    }

    public int genArticlePaidRecordId() {
        return sequenceRepository.gen("articles_paid_record");
    }

    public int genArticlesLikeId() {
        return sequenceRepository.gen("articles_like");
    }

    public int genArticleQRCodeId() {
        return sequenceRepository.gen("article_qrcodes");
    }

    public int genVideoConvertingLogId() {
        return sequenceRepository.gen("video_converting_log");
    }

    public int genFeedbackId() {
        return sequenceRepository.gen("adverse_reaction_feedback");
    }

    public int generateDepositId() {
        return sequenceRepository.gen("deposit");
    }

    public int genMedicalRecordMailingPatientId() {
        return sequenceRepository.gen("medical_record_mailing_patient");
    }

    public int genMedicalRecordMailingHospitalRecords() {
        return sequenceRepository.gen("medical_record_mailing_hospital_records");
    }

    public int genMedicalRecordMailingOrdersId() {
        return sequenceRepository.gen("medical_record_mailing_orders");
    }

    public int genMarkettingApply() {
        return sequenceRepository.gen("marketing_apply");
    }

    public int genMarkettingImage() {
        return sequenceRepository.gen("marketing_image");
    }

    public int genMarkettingJobTypeUser() {
        return sequenceRepository.gen("marketing_job_type_user");
    }

    public int genCollection() {
        return sequenceRepository.gen("marketing_collection");
    }

    public int genCommonImage() {
        return sequenceRepository.gen("common_image");
    }

    public int genDoctorBankCard() {
        return sequenceRepository.gen("doctor_bank_card");
    }
}
