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 StorageSpaceItemTransactionBLL : BaseOrclBLL<StorageSpaceItemTransaction>
    {
        public StorageSpaceItemTransactionBLL() : base()
        {
            AliasTableName = "sit";
        }

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

        internal override void InitMaps()
        {
            ColumnPropMaps.Add("KEY", "Key");
            ColumnPropMaps.Add("ITEM_TRANSACTION_KEY", "ItemTransactionKey");
            ColumnPropMaps.Add("STORAGE_SPACE_ITEM_KEY", "StorageSpaceItemKey");
            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");
            ColumnPropMaps.Add("S_P_I_SNAPSHOT_KEY", "SPISnapshotKey");
            ColumnPropMaps.Add("DEVICE_KEY", "DeviceKey");
            ColumnPropMaps.Add("STORAGE_SPACE_KEY", "StorageSpaceKey");
            ColumnPropMaps.Add("STORAGE_SPACE_SNAPSHOT_KEY", "StorageSpaceSnapshotKey");
            ColumnPropMaps.Add("PLAN_KEY", "PlanKey");//COLUMN_MAPS
        }

        internal override object GetParam(StorageSpaceItemTransaction storagespaceitemtransaction)
        {
            return new
            {
                Key = storagespaceitemtransaction.Key,
                ItemTransactionKey = storagespaceitemtransaction.ItemTransactionKey,
                StorageSpaceItemKey = storagespaceitemtransaction.StorageSpaceItemKey,
                CreateBy = storagespaceitemtransaction.CreateBy,
                CreateTime = storagespaceitemtransaction.CreateTime,
                Flag = storagespaceitemtransaction.Flag,
                ClientTime = storagespaceitemtransaction.ClientTime,
                ServerTime = storagespaceitemtransaction.ServerTime,
                Ext01 = storagespaceitemtransaction.Ext01,
                Ext02 = storagespaceitemtransaction.Ext02,
                Ext03 = storagespaceitemtransaction.Ext03,
                Ext04 = storagespaceitemtransaction.Ext04,
                Ext05 = storagespaceitemtransaction.Ext05,
                Ext06 = storagespaceitemtransaction.Ext06,
                Ext07 = storagespaceitemtransaction.Ext07,
                Ext08 = storagespaceitemtransaction.Ext08,
                Ext09 = storagespaceitemtransaction.Ext09,
                Ext10 = storagespaceitemtransaction.Ext10,
                SPISnapshotKey = storagespaceitemtransaction.SPISnapshotKey,
                DeviceKey = storagespaceitemtransaction.DeviceKey,
                StorageSpaceKey = storagespaceitemtransaction.StorageSpaceKey,
                StorageSpaceSnapshotKey = storagespaceitemtransaction.StorageSpaceSnapshotKey,
                PlanKey = storagespaceitemtransaction.PlanKey,   //Params
            };
        }
        public IEnumerable<StorageSpaceItemTransaction> ListTransactions(DateTime startTime, DateTime endTime, DispensingDevice device, params String[] transactionTypes)
        {
            #region SqlBuilder
            SqlBuilder sqlBuilder = new SqlBuilder(this);
            #region StorageSpaceItem
            StorageSpaceItemBLL storageSpaceItemBLL = new StorageSpaceItemBLL();
            TableJoinInfo storageItemTable = new TableJoinInfo
            {
                LeftDal = this,
                LeftColumnName = "STORAGE_SPACE_ITEM_KEY",
                RightDal = storageSpaceItemBLL,
                RightColumnName = "KEY",
                JoinType = TableJoinTypes.Inner,
            };
            sqlBuilder.AppendSelectionTable(storageItemTable);
            #endregion
            #region StorageSpace
            StorageSpaceBLL storageSpaceBLL = new StorageSpaceBLL();
            TableJoinInfo storageTable = new TableJoinInfo
            {
                LeftDal = storageSpaceItemBLL,
                LeftColumnName = "STORAGE_SPACE_KEY",
                RightDal = storageSpaceBLL,
                RightColumnName = "KEY",
                JoinType = TableJoinTypes.Inner,
            };
            sqlBuilder.AppendSelectionTable(storageTable);
            #endregion
            #region parent StorageSpace
            TableJoinInfo storageParentTable = new TableJoinInfo
            {
                LeftDal = storageSpaceBLL,
                LeftColumnName = "PARENT_KEY",
                RightDal = storageSpaceBLL,
                RightColumnName = "KEY",
                RightTableAliasName = "sp",
                JoinType = TableJoinTypes.Inner,
            };
            sqlBuilder.AppendSelectionTable(storageParentTable);
            #endregion
            #region item
            ItemBLL itemBLL = new ItemBLL();
            TableJoinInfo itemTable = new TableJoinInfo
            {
                LeftDal = storageSpaceItemBLL,
                LeftColumnName = "ITEM_KEY",
                RightDal = itemBLL,
                RightColumnName = "KEY",
                JoinType = TableJoinTypes.Inner,
            };
            sqlBuilder.AppendSelectionTable(itemTable);
            #endregion
            #region ItemTransaction
            ItemTransactionBLL itemTransactionBLL = new ItemTransactionBLL();
            TableJoinInfo itemTransactionTable = new TableJoinInfo
            {
                LeftDal = this,
                LeftColumnName = "ITEM_TRANSACTION_KEY",
                RightDal = itemTransactionBLL,
                RightColumnName = "KEY",
                JoinType = TableJoinTypes.Inner,
            };
            sqlBuilder.AppendSelectionTable(itemTransactionTable);
            #endregion
            #region user
            UserInfoBLL userInfoBLL = new UserInfoBLL();
            TableJoinInfo userTable = new TableJoinInfo
            {
                LeftDal = itemTransactionBLL,
                LeftColumnName = "CREATE_BY",
                RightDal = userInfoBLL,
                RightColumnName = "KEY",
                JoinType = TableJoinTypes.Inner,
            };
            sqlBuilder.AppendSelectionTable(userTable);
            #endregion
            sqlBuilder.AppendWherePhrases(String.Format("{0}.CREATE_TIME BETWEEN :startTime AND :endTime", itemTransactionBLL.AliasTableName));
            if (device != null)
            {
                sqlBuilder.AppendWherePhrases(String.Format("{0}.DEVICE_KEY=:deviceKey", itemTransactionBLL.AliasTableName));
            }
            if (transactionTypes != null)
            {
                sqlBuilder.AppendWherePhrases(String.Format("{0}.TRANSACTION_TYPE IN :transactionTypes", itemTransactionBLL.AliasTableName));
            }
            #endregion
            return SqlMapper.Query<StorageSpaceItemTransaction, StorageSpaceItem, StorageSpace, StorageSpace, Item, ItemTransaction, UserInfo, StorageSpaceItemTransaction>(
                ConnectionFactory.Current.GetSessionConnection(),
                sqlBuilder.ToString(),
                (sit, si, s, sp, i, it, u) =>
                {
                    sit.StorageSpaceItem = si;
                    si.StorageSpace = s;
                    s.ParentInfo = sp;
                    si.Item = i;
                    sit.ItemTransaction = it;
                    it.Creator = u;
                    return sit;
                },
                new
                {
                    startTime = startTime,
                    endTime = endTime,
                    deviceKey = device == null ? default(Guid) : device.Key,
                    transactionTypes = transactionTypes
                },
                splitOn: sqlBuilder.GetSpiltors());
        }
    }

}
