using RDH.Data.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace RDH.Data.BLL
{
    public partial class PortableStorageBLL : BaseOrclBLL<PortableStorage>
    {
        public PortableStorageBLL() : base()
        {
            AliasTableName = "ps";
        }

        internal override String TABLE_NAME { get { return "PORTABLE_STORAGE"; } }//TB_NAME
        internal override string ENTITY_NAME { get { return "PortableStorage"; } }
        internal override String KEY_COLUMN { get { return "KEY"; } }//KEY_COLUMN

        internal override void InitMaps()
        {
            ColumnPropMaps.Add("KEY", "Key");
            ColumnPropMaps.Add("TYPE_CODE", "TypeCode");
            ColumnPropMaps.Add("NAME", "Name");
            ColumnPropMaps.Add("CODE", "Code");
            ColumnPropMaps.Add("CREATE_BY", "CreateBy");
            ColumnPropMaps.Add("CREATE_TIME", "CreateTime");
            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");
        }

        internal override object GetParam(PortableStorage item)
        {
            return new
            {
                TypeCode = item.TypeCode,
                Code = item.Code,
                Name = item.Name,
                CreateBy = item.CreateBy,
                CreateTime = item.CreateTime,
                Key = item.Key,
                Flag = item.Flag,
                ClientTime = item.ClientTime,
                ServerTime = item.ServerTime,
                Ext01 = item.Ext01,
                Ext02 = item.Ext02,
                Ext03 = item.Ext03,
                Ext04 = item.Ext04,
                Ext05 = item.Ext05,
                Ext06 = item.Ext06,
                Ext07 = item.Ext07,
                Ext08 = item.Ext08,
                Ext09 = item.Ext09,
                Ext10 = item.Ext10,
            };
        }
        public PortableStorage GetModelByName(String name, DispensingDevice device)
        {
            return SqlMapper.Query<PortableStorage>(ConnectionFactory.Current.GetSessionConnection(),
                String.Format("{0} WHERE NAME=:pName AND ext03=:deviceKey", BuildBaseSql()),
                new
                {
                    pName = name,
                    deviceKey = device.Key.ToString()
                }).FirstOrDefault();
        }
        public void UpdateRFIDBySerialNo(StorageSpace storage, String rfid)
        {
            SqlMapper.Execute(ConnectionFactory.Current.GetSessionConnection(),
                "UPDATE portable_storage s SET s.ext02=:rfidString WHERE s.code=:storageNo AND s.ext03=:deviceKey",
                new
                {
                    rfidString = rfid,
                    storageNo = storage.Ext02,
                    deviceKey = storage.DeviceKey.GetValueOrDefault().ToString()
                });
        }
        public PortableStorage GetModelByRFID(String rfid)
        {
            return SqlMapper.Query<PortableStorage>(ConnectionFactory.Current.GetSessionConnection(),
                String.Format("{0} WHERE ext02 IS NOT NULL AND ext02=:rfidString", BuildBaseSql()),
                new { rfidString = rfid }).FirstOrDefault();
        }
    }

}
