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 StorageSpacePortableLinkBLL : BaseOrclBLL<StorageSpacePortableLink>
    {

        public StorageSpacePortableLinkBLL() : base()
        {
            AliasTableName = "spl";
        }

        #region Properties
        internal override String TABLE_NAME { get { return "STORAGE_SPACE_PORTABLE_LINK"; } }//TB_NAME
        internal override string ENTITY_NAME { get { return "StorageSpacePortableLink"; } }
        internal override String KEY_COLUMN { get { return "KEY"; } }//KEY_COLUMN 
        #endregion

        #region Override Methods
        internal override void InitMaps()
        {
            ColumnPropMaps.Add("KEY", "Key");
            ColumnPropMaps.Add("STORAGE_SPACE_KEY", "StorageSpaceKey");
            ColumnPropMaps.Add("PORTABLE_STORAGE_KEY", "PortableStorageKey");
            ColumnPropMaps.Add("MAX_QUANTITY", "MaxQuantity");
            ColumnPropMaps.Add("ALART_QUANTITY", "AlartQuantity");
            ColumnPropMaps.Add("CURRENT_QUANTITY", "CurrentQuantity");
            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");//COLUMN_MAPS
        }

        internal override object GetParam(StorageSpacePortableLink storagespaceitem)
        {
            return new
            {
                Key = storagespaceitem.Key,
                StorageSpaceKey = storagespaceitem.StorageSpaceKey,
                PortableStorageKey = storagespaceitem.PortableStorageKey,
                MaxQuantity = storagespaceitem.MaxQuantity,
                AlartQuantity = storagespaceitem.AlartQuantity,
                CurrentQuantity = storagespaceitem.CurrentQuantity,
                CreateBy = storagespaceitem.CreateBy,
                CreateTime = storagespaceitem.CreateTime,
                IsAllowMultiLot = storagespaceitem.IsAllowMultiLot,
                DeviceKey = storagespaceitem.DeviceKey,
                Flag = storagespaceitem.Flag,
                ClientTime = storagespaceitem.ClientTime,
                ServerTime = storagespaceitem.ServerTime,
                Ext01 = storagespaceitem.Ext01,
                Ext02 = storagespaceitem.Ext02,
                Ext03 = storagespaceitem.Ext03,
                Ext04 = storagespaceitem.Ext04,
                Ext05 = storagespaceitem.Ext05,
                Ext06 = storagespaceitem.Ext06,
                Ext07 = storagespaceitem.Ext07,
                Ext08 = storagespaceitem.Ext08,
                Ext09 = storagespaceitem.Ext09,
                Ext10 = storagespaceitem.Ext10,
            };
        }
        #endregion
        public IEnumerable<StorageSpacePortableLink> ListStorageSpacePortableLink(DispensingDevice device)
        {
            StorageSpaceBLL bllStorage = new StorageSpaceBLL();
            StringBuilder sqlBuilder = new StringBuilder();
            sqlBuilder.Append("SELECT ");
            foreach (KeyValuePair<String, String> pair in this.ColumnPropMaps)
            {
                sqlBuilder.AppendFormat("{0}.{1} {2},", this.TABLE_NAME, pair.Key, pair.Value);
            }
            sqlBuilder.Append("'' spiltor1,");
            foreach (KeyValuePair<String, String> pair in bllStorage.ColumnPropMaps)
            {
                sqlBuilder.AppendFormat("{0}.{1} {2},", bllStorage.TABLE_NAME, pair.Key, pair.Value);
            }
            sqlBuilder.Remove(sqlBuilder.Length - 1, 1);
            sqlBuilder.AppendFormat(" FROM {0}"
                + " INNER JOIN {1} ON {1}.KEY={0}.STORAGE_SPACE_KEY"
                + " WHERE {1}.DEVICE_KEY=:deviceKey",
                this.TABLE_NAME, bllStorage.TABLE_NAME);
            IEnumerable<StorageSpacePortableLink> list = SqlMapper.Query<
                 StorageSpacePortableLink,
                 StorageSpace,
                 StorageSpacePortableLink>(ConnectionFactory.Current.GetSessionConnection(),
                 sqlBuilder.ToString(),
                 (link, s) =>
                 {
                     link.StorageSpace = s;
                     return link;
                 },
                 new { deviceKey = device.Key },
                 splitOn: "spiltor1");

            return list;

        }
        public StorageSpacePortableLink? GetByStorageCode(DispensingDevice? device, String code) 
        {
            SqlBuilder sqlBuilder = new SqlBuilder(this);
            PortableStorageBLL storageSpaceBLL = new PortableStorageBLL();
            TableJoinInfo storageTable = new TableJoinInfo
            {
                LeftDal = this,
                LeftColumnName = "PORTABLE_STORAGE_KEY",
                RightDal = storageSpaceBLL,
                RightColumnName = "KEY",
                JoinType = TableJoinTypes.Inner,
            };
            sqlBuilder.AppendSelectionTable(storageTable);
            if (device != null) 
            {
                sqlBuilder.AppendWherePhrases($"{storageSpaceBLL.AliasTableName}.DEVICE_KEY=:deviceKey");
            }
            sqlBuilder.AppendWherePhrases($"NVL({storageSpaceBLL.AliasTableName}.CODE,'0')=:storaceCode");
            return SqlMapper.Query<StorageSpacePortableLink, PortableStorage, StorageSpacePortableLink>(
                ConnectionFactory.Current.GetSessionConnection(),
                sqlBuilder.ToString(),
                (l, s) =>
                {
                    l.PortableStorage = s;
                    return l;
                },
                new
                {
                    deviceKey = device?.Key,
                    storaceCode = code,
                },
                splitOn: sqlBuilder.GetSpiltors())
                .FirstOrDefault();
        }
    }
}
