package com.ywt.outpatient.taihe.rpc.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.ParameterMode;
import javax.persistence.StoredProcedureQuery;

/**
 * 数据库表主键ID生成器
 *
 * @author johnson lin
 * @date 2019-04-29 9:45 PM
 */
@Service
public class IdGenerator {
    @Autowired
    EntityManager entityManager;

    public Integer gen(String tabName) {
        StoredProcedureQuery storedProcedureQuery = entityManager.createStoredProcedureQuery("get_sequence")
                .registerStoredProcedureParameter("flag_name", String.class, ParameterMode.IN)
                .registerStoredProcedureParameter("seq_id", Integer.class, ParameterMode.OUT)
                .setParameter("flag_name", tabName);
        storedProcedureQuery.execute();
        return (Integer) storedProcedureQuery.getOutputParameterValue("seq_id");
    }

    public Integer generateSn(String tag) {
        StoredProcedureQuery storedProcedureQuery = entityManager.createStoredProcedureQuery("generate_sn")
                .registerStoredProcedureParameter("flag_name", String.class, ParameterMode.IN)
                .registerStoredProcedureParameter("seq_id", Integer.class, ParameterMode.OUT)
                .setParameter("flag_name", tag);
        storedProcedureQuery.execute();
        return (Integer) storedProcedureQuery.getOutputParameterValue("seq_id");
    }

    /**
     * 生成预约挂号订单表主键Id
     *
     * @return 预约挂号订单表主键Id
     */
    public int genRegisteredOrderId() {
        return gen("registered_order");
    }

    public int genMedicalCardId() {
        return gen("medical_card");
    }

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

}
