using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using Dapper;

namespace RDH.PharmacyPlatform.Sync.Core
{
    internal class MessageDataInDAL : BaseDAL<MessageData>
    {
        public MessageDataInDAL()
        {
            this.TableName = "T_Message_Data_In";
        }
        internal override Dictionary<string, string> InitMaps()
        {
            Dictionary<String, String> maps = new Dictionary<string, string>();
            maps.Add("Create_Time", "CreateTime");
            maps.Add("Source_Id", "SourceId");
            maps.Add("Message_Content", "MessageContent");
            maps.Add("Message_Content_Type", "MessageContentType");
            maps.Add("Filter_Type", "FilterType");
            maps.Add("Target_Id", "TargetId");
            return maps;
        }
        public override bool DeleteModel(MessageData obj)
        {
            return SqlMapper.Execute(ConnectionSessionFactory.GetConnection(),
                String.Format("DELETE FROM {0} WHERE Create_Time=:CreateTime AND Source_Id=:SourceId", this.TableName),
                obj) > 0;
        }
        internal MessageData DeQueue() 
        {
            IDbConnection conn = ConnectionSessionFactory.GetConnection();
            MessageData message = SqlMapper.Query<MessageData>(conn,
                String.Format("{0} ORDER BY Create_Time DESC", BuildSelectSql())).FirstOrDefault();
            if (message != null) 
            {
                DeleteModel(message);
            }
            return message;
        }
        internal MessageData Peek()
        {
            IDbConnection conn = ConnectionSessionFactory.GetConnection();
            MessageData message = SqlMapper.Query<MessageData>(conn,
                String.Format("{0} ORDER BY Create_Time LIMIT 1", BuildSelectSql())).FirstOrDefault();
            return message;
        }
    }
}
