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 StorageSpaceItemLotBLL : BaseOrclSnapshotBLL<StorageSpaceItemLot>
    {
        public StorageSpaceItemLotBLL() : base()
        {
            AliasTableName = "sil";
        }

        internal override String TABLE_NAME { get { return "STORAGE_SPACE_ITEM_LOT"; } }//TB_NAME
        internal override string ENTITY_NAME { get { return "StorageSpaceItemLot"; } }
        internal override String KEY_COLUMN { get { return "SNAPSHOT_KEY"; } }//KEY_COLUMN
        internal override string SNAPSHOT_BASE { get { return "STORAGE_SPACE_ITEM_LOT_BASE"; } }

        internal override void InitMaps()
        {
            ColumnPropMaps.Add("SNAPSHOT_KEY", "SnapshotKey");
            ColumnPropMaps.Add("KEY", "Key");
            ColumnPropMaps.Add("QUANTITY", "Quantity");
            ColumnPropMaps.Add("CREATE_BY", "CreateBy");
            ColumnPropMaps.Add("CREATE_TIME", "CreateTime");
            ColumnPropMaps.Add("ITEM_KEY", "ItemKey");
            ColumnPropMaps.Add("LOT_KEY", "LotKey");
            ColumnPropMaps.Add("STORAGE_SAPCE_ITEM_KEY", "StorageSapceItemKey");
            ColumnPropMaps.Add("DEVICE_KEY", "DeviceKey");
            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("POCKET_HOLES", "PocketHoles");//COLUMN_MAPS
        }

        internal override object GetParam(StorageSpaceItemLot storagespaceitemlot)
        {
            return new
            {
                SnapshotKey = storagespaceitemlot.SnapshotKey,
                Key = storagespaceitemlot.Key,
                Quantity = storagespaceitemlot.Quantity,
                CreateBy = storagespaceitemlot.CreateBy,
                CreateTime = storagespaceitemlot.CreateTime,
                ItemKey = storagespaceitemlot.ItemKey,
                LotKey = storagespaceitemlot.LotKey,
                StorageSapceItemKey = storagespaceitemlot.StorageSapceItemKey,
                DeviceKey = storagespaceitemlot.DeviceKey,
                Flag = storagespaceitemlot.Flag,
                ClientTime = storagespaceitemlot.ClientTime,
                ServerTime = storagespaceitemlot.ServerTime,
                Ext01 = storagespaceitemlot.Ext01,
                Ext02 = storagespaceitemlot.Ext02,
                Ext03 = storagespaceitemlot.Ext03,
                Ext04 = storagespaceitemlot.Ext04,
                Ext05 = storagespaceitemlot.Ext05,
                Ext06 = storagespaceitemlot.Ext06,
                Ext07 = storagespaceitemlot.Ext07,
                Ext08 = storagespaceitemlot.Ext08,
                Ext09 = storagespaceitemlot.Ext09,
                Ext10 = storagespaceitemlot.Ext10,
                PocketHoles = storagespaceitemlot.PocketHoles,   //Params
            };
        }
        /// <summary>
        /// 获取存储单元的有效的库存批次
        /// </summary>
        /// <param name="storageSpaceItem"></param>
        /// <returns></returns>
        public IEnumerable<StorageSpaceItemLot> ListStorageSpaceItemLot(StorageSpaceItem storageSpaceItem)
        {
            SqlBuilder sqlBuilder = new SqlBuilder(this);
            ItemLotBLL itemLotBLL = new ItemLotBLL();
            TableJoinInfo itemlotTable = new TableJoinInfo()
            {
                LeftDal = this,
                LeftColumnName = "LOT_KEY",
                RightDal = itemLotBLL,
                RightColumnName = itemLotBLL.KEY_COLUMN,
                JoinType = TableJoinTypes.Inner,
            };
            sqlBuilder.AppendSelectionTable(itemlotTable);
            sqlBuilder.AppendWherePhrases($"{this.AliasTableName}.STORAGE_SAPCE_ITEM_KEY=:siKey");
            return SqlMapper.Query<StorageSpaceItemLot, ItemLot, StorageSpaceItemLot>(
                ConnectionFactory.Current.GetSessionConnection(),
                sqlBuilder.ToString(),
                (sil, il) =>
                {
                    sil.ItemLot = il;
                    return sil;
                },
                new
                {
                    siKey = storageSpaceItem.Key
                },
                splitOn: sqlBuilder.GetSpiltors());
        }
        /// <summary>
        /// 获取存储单元的有效的库存批次
        /// </summary>
        /// <param name="storageSpaceItem"></param>
        /// <returns></returns>
        public IEnumerable<StorageSpaceItemLot> ListStorageSpaceItemLot(StorageSpace storageSpace)
        {
            SqlBuilder sqlBuilder = new SqlBuilder(this);
            //item lot
            ItemLotBLL itemLotBLL = new ItemLotBLL();
            TableJoinInfo itemlotTable = new TableJoinInfo()
            {
                LeftDal = this,
                LeftColumnName = "LOT_KEY",
                RightDal = itemLotBLL,
                RightColumnName = itemLotBLL.KEY_COLUMN,
                JoinType = TableJoinTypes.Inner,
            };
            sqlBuilder.AppendSelectionTable(itemlotTable);
            //storage_space_item
            StorageSpaceItemBLL storageSpaceItemBLL = new StorageSpaceItemBLL();
            TableJoinInfo ssiTable = new TableJoinInfo()
            {
                LeftDal = this,
                LeftColumnName = "STORAGE_SAPCE_ITEM_KEY",
                RightDal = storageSpaceItemBLL,
                RightColumnName = "KEY",
                JoinType = TableJoinTypes.Inner
            };
            sqlBuilder.AppendSelectionTable(ssiTable);
            //item
            ItemBLL itemBLL = new ItemBLL();
            TableJoinInfo itemTable = new TableJoinInfo
            {
                LeftDal = storageSpaceItemBLL,
                LeftColumnName = "ITEM_KEY",
                RightDal = itemBLL,
                RightColumnName = "KEY",
                JoinType = TableJoinTypes.Inner
            };
            sqlBuilder.AppendSelectionTable(itemTable);
            sqlBuilder.AppendWherePhrases($"{storageSpaceItemBLL.AliasTableName}.STORAGE_SPACE_KEY=:storageKey");
            return SqlMapper.Query<StorageSpaceItemLot, ItemLot, StorageSpaceItem, Item, StorageSpaceItemLot>(
                ConnectionFactory.Current.GetSessionConnection(),
                sqlBuilder.ToString(),
                (sil, il, si, i) =>
                {
                    sil.ItemLot = il;
                    sil.StorageSpaceItem = si;
                    si.Item = i;
                    return sil;
                },
                new
                {
                    storageKey = storageSpace.Key
                },
                splitOn: sqlBuilder.GetSpiltors());
        }
    }
}
