
using RMYY_CallCenter_Api.DB;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;

namespace RMYY_CallCenter_Api.Dal
{
    /// <summary>
    /// 数据访问类:T_Wo_MedicalRecord
    /// </summary>
    public partial class T_Wo_MedicalRecord
    {
        public T_Wo_MedicalRecord()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("F_ID", "T_Wo_MedicalRecord");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int F_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Wo_MedicalRecord");
            strSql.Append(" where F_ID=@F_ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_ID", SqlDbType.Int,4)
            };
            parameters[0].Value = F_ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(RMYY_CallCenter_Api.Model.T_Wo_MedicalRecord model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Wo_MedicalRecord(");
            strSql.Append("F_GlobalPID,F_CardID,F_PhoneNumber,F_FamilyName,F_DateTimeOfBirth,F_Sex,F_PatientClass,F_FacilityName,F_JZH,F_PointOfCareName,F_AttendingDoctor,F_AdmittingDoctor,F_AttendingDoctorCode,F_AdmittingDoctorCode,F_AdmitDateTime,F_DiagnosisCode,F_DiagnosisName,F_CreatTime)");
            strSql.Append(" values (");
            strSql.Append("@F_GlobalPID,@F_CardID,@F_PhoneNumber,@F_FamilyName,@F_DateTimeOfBirth,@F_Sex,@F_PatientClass,@F_FacilityName,@F_JZH,@F_PointOfCareName,@F_AttendingDoctor,@F_AdmittingDoctor,@F_AttendingDoctorCode,@F_AdmittingDoctorCode,@F_AdmitDateTime,@F_DiagnosisCode,@F_DiagnosisName,@F_CreatTime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_GlobalPID", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_CardID", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_PhoneNumber", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_FamilyName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_DateTimeOfBirth", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Sex", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_PatientClass", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_FacilityName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_JZH", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_PointOfCareName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_AttendingDoctor", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_AdmittingDoctor", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_AttendingDoctorCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_AdmittingDoctorCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_AdmitDateTime", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_DiagnosisCode", SqlDbType.NVarChar,-1),
                    new SqlParameter("@F_DiagnosisName", SqlDbType.NVarChar,-1),
                    new SqlParameter("@F_CreatTime", SqlDbType.DateTime)};
            parameters[0].Value = model.F_GlobalPID;
            parameters[1].Value = model.F_CardID;
            parameters[2].Value = model.F_PhoneNumber;
            parameters[3].Value = model.F_FamilyName;
            parameters[4].Value = model.F_DateTimeOfBirth;
            parameters[5].Value = model.F_Sex;
            parameters[6].Value = model.F_PatientClass;
            parameters[7].Value = model.F_FacilityName;
            parameters[8].Value = model.F_JZH;
            parameters[9].Value = model.F_PointOfCareName;
            parameters[10].Value = model.F_AttendingDoctor;
            parameters[11].Value = model.F_AdmittingDoctor;
            parameters[12].Value = model.F_AttendingDoctorCode;
            parameters[13].Value = model.F_AdmittingDoctorCode;
            parameters[14].Value = model.F_AdmitDateTime;
            parameters[15].Value = model.F_DiagnosisCode;
            parameters[16].Value = model.F_DiagnosisName;
            parameters[17].Value = model.F_CreatTime;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(RMYY_CallCenter_Api.Model.T_Wo_MedicalRecord model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Wo_MedicalRecord set ");
            strSql.Append("F_GlobalPID=@F_GlobalPID,");
            strSql.Append("F_CardID=@F_CardID,");
            strSql.Append("F_PhoneNumber=@F_PhoneNumber,");
            strSql.Append("F_FamilyName=@F_FamilyName,");
            strSql.Append("F_DateTimeOfBirth=@F_DateTimeOfBirth,");
            strSql.Append("F_Sex=@F_Sex,");
            strSql.Append("F_PatientClass=@F_PatientClass,");
            strSql.Append("F_FacilityName=@F_FacilityName,");
            strSql.Append("F_JZH=@F_JZH,");
            strSql.Append("F_PointOfCareName=@F_PointOfCareName,");
            strSql.Append("F_AttendingDoctor=@F_AttendingDoctor,");
            strSql.Append("F_AdmittingDoctor=@F_AdmittingDoctor,");
            strSql.Append("F_AttendingDoctorCode=@F_AttendingDoctorCode,");
            strSql.Append("F_AdmittingDoctorCode=@F_AdmittingDoctorCode,");
            strSql.Append("F_AdmitDateTime=@F_AdmitDateTime,");
            strSql.Append("F_DiagnosisCode=@F_DiagnosisCode,");
            strSql.Append("F_DiagnosisName=@F_DiagnosisName,");
            strSql.Append("F_CreatTime=@F_CreatTime");
            strSql.Append(" where F_ID=@F_ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_GlobalPID", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_CardID", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_PhoneNumber", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_FamilyName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_DateTimeOfBirth", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Sex", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_PatientClass", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_FacilityName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_JZH", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_PointOfCareName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_AttendingDoctor", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_AdmittingDoctor", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_AttendingDoctorCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_AdmittingDoctorCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_AdmitDateTime", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_DiagnosisCode", SqlDbType.NVarChar,-1),
                    new SqlParameter("@F_DiagnosisName", SqlDbType.NVarChar,-1),
                    new SqlParameter("@F_CreatTime", SqlDbType.DateTime),
                    new SqlParameter("@F_ID", SqlDbType.Int,4)};
            parameters[0].Value = model.F_GlobalPID;
            parameters[1].Value = model.F_CardID;
            parameters[2].Value = model.F_PhoneNumber;
            parameters[3].Value = model.F_FamilyName;
            parameters[4].Value = model.F_DateTimeOfBirth;
            parameters[5].Value = model.F_Sex;
            parameters[6].Value = model.F_PatientClass;
            parameters[7].Value = model.F_FacilityName;
            parameters[8].Value = model.F_JZH;
            parameters[9].Value = model.F_PointOfCareName;
            parameters[10].Value = model.F_AttendingDoctor;
            parameters[11].Value = model.F_AdmittingDoctor;
            parameters[12].Value = model.F_AttendingDoctorCode;
            parameters[13].Value = model.F_AdmittingDoctorCode;
            parameters[14].Value = model.F_AdmitDateTime;
            parameters[15].Value = model.F_DiagnosisCode;
            parameters[16].Value = model.F_DiagnosisName;
            parameters[17].Value = model.F_CreatTime;
            parameters[18].Value = model.F_ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int F_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Wo_MedicalRecord ");
            strSql.Append(" where F_ID=@F_ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_ID", SqlDbType.Int,4)
            };
            parameters[0].Value = F_ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string F_IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Wo_MedicalRecord ");
            strSql.Append(" where F_ID in (" + F_IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RMYY_CallCenter_Api.Model.T_Wo_MedicalRecord GetModel(int F_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 F_ID,F_GlobalPID,F_CardID,F_PhoneNumber,F_FamilyName,F_DateTimeOfBirth,F_Sex,F_PatientClass,F_FacilityName,F_JZH,F_PointOfCareName,F_AttendingDoctor,F_AdmittingDoctor,F_AttendingDoctorCode,F_AdmittingDoctorCode,F_AdmitDateTime,F_DiagnosisCode,F_DiagnosisName,F_CreatTime from T_Wo_MedicalRecord ");
            strSql.Append(" where F_ID=@F_ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_ID", SqlDbType.Int,4)
            };
            parameters[0].Value = F_ID;

            RMYY_CallCenter_Api.Model.T_Wo_MedicalRecord model = new RMYY_CallCenter_Api.Model.T_Wo_MedicalRecord();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RMYY_CallCenter_Api.Model.T_Wo_MedicalRecord DataRowToModel(DataRow row)
        {
            RMYY_CallCenter_Api.Model.T_Wo_MedicalRecord model = new RMYY_CallCenter_Api.Model.T_Wo_MedicalRecord();
            if (row != null)
            {
                if (row["F_ID"] != null && row["F_ID"].ToString() != "")
                {
                    model.F_ID = int.Parse(row["F_ID"].ToString());
                }
                if (row["F_GlobalPID"] != null)
                {
                    model.F_GlobalPID = row["F_GlobalPID"].ToString();
                }
                if (row["F_CardID"] != null)
                {
                    model.F_CardID = row["F_CardID"].ToString();
                }
                if (row["F_PhoneNumber"] != null)
                {
                    model.F_PhoneNumber = row["F_PhoneNumber"].ToString();
                }
                if (row["F_FamilyName"] != null)
                {
                    model.F_FamilyName = row["F_FamilyName"].ToString();
                }
                if (row["F_DateTimeOfBirth"] != null)
                {
                    model.F_DateTimeOfBirth = row["F_DateTimeOfBirth"].ToString();
                }
                if (row["F_Sex"] != null)
                {
                    model.F_Sex = row["F_Sex"].ToString();
                }
                if (row["F_PatientClass"] != null)
                {
                    model.F_PatientClass = row["F_PatientClass"].ToString();
                }
                if (row["F_FacilityName"] != null)
                {
                    model.F_FacilityName = row["F_FacilityName"].ToString();
                }
                if (row["F_JZH"] != null)
                {
                    model.F_JZH = row["F_JZH"].ToString();
                }
                if (row["F_PointOfCareName"] != null)
                {
                    model.F_PointOfCareName = row["F_PointOfCareName"].ToString();
                }
                if (row["F_AttendingDoctor"] != null)
                {
                    model.F_AttendingDoctor = row["F_AttendingDoctor"].ToString();
                }
                if (row["F_AdmittingDoctor"] != null)
                {
                    model.F_AdmittingDoctor = row["F_AdmittingDoctor"].ToString();
                }
                if (row["F_AttendingDoctorCode"] != null)
                {
                    model.F_AttendingDoctorCode = row["F_AttendingDoctorCode"].ToString();
                }
                if (row["F_AdmittingDoctorCode"] != null)
                {
                    model.F_AdmittingDoctorCode = row["F_AdmittingDoctorCode"].ToString();
                }
                if (row["F_AdmitDateTime"] != null)
                {
                    model.F_AdmitDateTime = row["F_AdmitDateTime"].ToString();
                }
                if (row["F_DiagnosisCode"] != null)
                {
                    model.F_DiagnosisCode = row["F_DiagnosisCode"].ToString();
                }
                if (row["F_DiagnosisName"] != null)
                {
                    model.F_DiagnosisName = row["F_DiagnosisName"].ToString();
                }
                if (row["F_CreatTime"] != null && row["F_CreatTime"].ToString() != "")
                {
                    model.F_CreatTime = DateTime.Parse(row["F_CreatTime"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F_ID,F_GlobalPID,F_CardID,F_PhoneNumber,F_FamilyName,F_DateTimeOfBirth,F_Sex,F_PatientClass,F_FacilityName,F_JZH,F_PointOfCareName,F_AttendingDoctor,F_AdmittingDoctor,F_AttendingDoctorCode,F_AdmittingDoctorCode,F_AdmitDateTime,F_DiagnosisCode,F_DiagnosisName,F_CreatTime ");
            strSql.Append(" FROM T_Wo_MedicalRecord ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" F_ID,F_GlobalPID,F_CardID,F_PhoneNumber,F_FamilyName,F_DateTimeOfBirth,F_Sex,F_PatientClass,F_FacilityName,F_JZH,F_PointOfCareName,F_AttendingDoctor,F_AdmittingDoctor,F_AttendingDoctorCode,F_AdmittingDoctorCode,F_AdmitDateTime,F_DiagnosisCode,F_DiagnosisName,F_CreatTime ");
            strSql.Append(" FROM T_Wo_MedicalRecord ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_Wo_MedicalRecord ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.F_ID desc");
            }
            strSql.Append(")AS Row, T.*  from T_Wo_MedicalRecord T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "T_Wo_MedicalRecord";
			parameters[1].Value = "F_ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}