using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using RDH.Data.Models;

namespace RDH.Data.BLL
{
    public partial class PatientOperationInfoBLL : BaseOrclSnapshotBLL<PatientOperationInfo>, IPatientOperationInfoBLL
    {
        public PatientOperationInfoBLL() : base() 
        {
            AliasTableName = "poi";
        }

        internal override String TABLE_NAME { get { return "PATIENT_OPERATION_INFO"; } }//TB_NAME
        internal override string ENTITY_NAME { get { return "PatientOperationInfo"; } }
        internal override String KEY_COLUMN { get { return "KEY"; } }//KEY_COLUMN
        internal override string SNAPSHOT_BASE { get { return "PATIENT_OPERATION_INFO_BASE"; } }

        internal override void InitMaps()
        {
            ColumnPropMaps.Add("SNAPSHOT_KEY", "SnapshotKey");
            ColumnPropMaps.Add("KEY", "Key");
            ColumnPropMaps.Add("CREATE_BY", "CreateBy");
            ColumnPropMaps.Add("PATIENT_KEY", "PatientKey");
            ColumnPropMaps.Add("BED_NUMBER", "BedNumber");
            ColumnPropMaps.Add("ENCOUNTER_ID", "EncounterId");
            ColumnPropMaps.Add("OPERATION_ID", "OperationId");
            ColumnPropMaps.Add("OPERATION_NAME", "OperationName");
            ColumnPropMaps.Add("CREATE_TIME", "CreateTime");
            ColumnPropMaps.Add("OPERATION_BEGIN_TIME", "OperationBeginTime");
            ColumnPropMaps.Add("OPERATION_OVER_TIME", "OperationOverTime");
            ColumnPropMaps.Add("DESCRIPTION", "Description");
            ColumnPropMaps.Add("ANES_SCHEME", "AnesScheme");
            ColumnPropMaps.Add("PATIENT_NAME", "PatientName");
            ColumnPropMaps.Add("IS_TEMP_SURGERY", "IsTempSurgery");
            ColumnPropMaps.Add("AGE", "Age");
            ColumnPropMaps.Add("GENDER", "Gender");
            ColumnPropMaps.Add("REMARK", "Remark");
            ColumnPropMaps.Add("OPERATION_TYPE", "OperationType");
            ColumnPropMaps.Add("OPERATION_ROOM_KEY", "OperationRoomKey");
            ColumnPropMaps.Add("FLAG", "Flag");
            ColumnPropMaps.Add("CLIENT_TIME", "ClientTime");
            ColumnPropMaps.Add("SERVER_TIME", "ServerTime");
            ColumnPropMaps.Add("EXT01", "Ext01");
            ColumnPropMaps.Add("EXT02", "Ext02");
            ColumnPropMaps.Add("EXT03", "Ext03");
            ColumnPropMaps.Add("EXT04", "Ext04");
            ColumnPropMaps.Add("EXT05", "Ext05");
            ColumnPropMaps.Add("EXT06", "Ext06");
            ColumnPropMaps.Add("EXT07", "Ext07");
            ColumnPropMaps.Add("EXT08", "Ext08");
            ColumnPropMaps.Add("EXT09", "Ext09");
            ColumnPropMaps.Add("EXT10", "Ext10");
            ColumnPropMaps.Add("SURGEON_KEY", "SurgeonKey");
            ColumnPropMaps.Add("ANALGESIST_KEY", "AnalgesistKey");
            ColumnPropMaps.Add("HAS_CANCELED", "HasCanceled");
            ColumnPropMaps.Add("OPERATION_ROOM_ID", "OperationRoomId");
            ColumnPropMaps.Add("ANALGESIST_ID", "AnalgesistId");//COLUMN_MAPS
        }
        internal override object GetParam(PatientOperationInfo patientoperationinfo)
        {
            return new
            {
                SnapshotKey = patientoperationinfo.SnapshotKey,
                Key = patientoperationinfo.Key,
                CreateBy = patientoperationinfo.CreateBy,
                PatientKey = patientoperationinfo.PatientKey,
                BedNumber = patientoperationinfo.BedNumber,
                EncounterId = patientoperationinfo.EncounterId,
                OperationId = patientoperationinfo.OperationId,
                OperationName = patientoperationinfo.OperationName,
                CreateTime = patientoperationinfo.CreateTime,
                OperationBeginTime = patientoperationinfo.OperationBeginTime,
                OperationOverTime = patientoperationinfo.OperationOverTime,
                Description = patientoperationinfo.Description,
                AnesScheme = patientoperationinfo.AnesScheme,
                PatientName = patientoperationinfo.PatientName,
                IsTempSurgery = patientoperationinfo.IsTempSurgery,
                Age = patientoperationinfo.Age,
                Gender = patientoperationinfo.Gender,
                Remark = patientoperationinfo.Remark,
                OperationType = patientoperationinfo.OperationType,
                OperationRoomKey = patientoperationinfo.OperationRoomKey,
                Flag = patientoperationinfo.Flag,
                ClientTime = patientoperationinfo.ClientTime,
                ServerTime = patientoperationinfo.ServerTime,
                Ext01 = patientoperationinfo.Ext01,
                Ext02 = patientoperationinfo.Ext02,
                Ext03 = patientoperationinfo.Ext03,
                Ext04 = patientoperationinfo.Ext04,
                Ext05 = patientoperationinfo.Ext05,
                Ext06 = patientoperationinfo.Ext06,
                Ext07 = patientoperationinfo.Ext07,
                Ext08 = patientoperationinfo.Ext08,
                Ext09 = patientoperationinfo.Ext09,
                Ext10 = patientoperationinfo.Ext10,
                SurgeonKey = patientoperationinfo.SurgeonKey,
                AnalgesistKey = patientoperationinfo.AnalgesistKey,
                HasCanceled = patientoperationinfo.HasCanceled,
                OperationRoomId = patientoperationinfo.OperationRoomId,
                AnalgesistId = patientoperationinfo.AnalgesistId,   //Params
            };
        }
        public IEnumerable<PatientOperationInfo> ListPatientOperationByRoom(String roomCode) 
        {
            SqlBuilder sqlBuilder = new SqlBuilder(this);
            //patient table
            PatientInfoBLL patientInfoBLL = new PatientInfoBLL();
            TableJoinInfo patientTable = new TableJoinInfo
            {
                LeftDal = this,
                LeftColumnName = "PATIENT_KEY",
                RightDal = patientInfoBLL,
                RightColumnName = "KEY",
                JoinType = TableJoinTypes.Inner,
            };
            sqlBuilder.AppendSelectionTable(patientTable);
            //operation room table
            OperationRoomBLL operationRoomBLL = new OperationRoomBLL();
            TableJoinInfo roomTable = new TableJoinInfo
            {
                LeftDal = this,
                LeftColumnName = "OPERATION_ROOM_KEY",
                RightDal = operationRoomBLL,
                RightColumnName = "KEY",
                JoinType = TableJoinTypes.Inner,
            };
            sqlBuilder.AppendSelectionTable(roomTable);
            sqlBuilder.AppendWherePhrases($"poi.OPERATION_BEGIN_TIME BETWEEN TRUNC(SYSDATE) AND TRUNC(SYSDATE)+1"
                + $" AND {operationRoomBLL.AliasTableName}.ROOM_ID=:roomCode");
            //当使用场景不严谨的情况下可以将手术结束状态的判断去掉
            //    + $" AND (poi.OPERATION_OVER_TIME IS NULL OR poi.OPERATION_OVER_TIME<poi.OPERATION_BEGIN_TIME)");
            return SqlMapper.Query<PatientOperationInfo, PatientInfo, OperationRoom, PatientOperationInfo>(
                ConnectionFactory.Current.GetSessionConnection(),
                sqlBuilder.ToString(),
                (o, p, r) =>
                {
                    o.PatientInfo = p;
                    o.OperationRoom = r;
                    return o;
                },
                new
                {
                    roomCode = roomCode
                },
                splitOn: sqlBuilder.GetSpiltors());
        }
    }
}