using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using RDH.Data.Models;

namespace RDH.Data.BLL
{
    public partial class ItemBarcodeLibraryBLL : BaseOrclBLL<ItemBarcodeLibrary> 
    { 
        public ItemBarcodeLibraryBLL() : base() 
        {
            AliasTableName = "ibl";
        }

        internal override String TABLE_NAME { get { return "ITEM_BARCODE_LIBRARY"; } }
        internal override string ENTITY_NAME { get { return "Item"; } }
        internal override String KEY_COLUMN { get { return "BARCODE"; } }

        internal override void InitMaps()
        {
            ColumnPropMaps.Add("BARCODE", "Barcode");
            ColumnPropMaps.Add("BARCODE_STATE", "BarcodeState");
            ColumnPropMaps.Add("ITEM_KEY", "ItemKey");
            ColumnPropMaps.Add("ITEM_LOT_KEY", "ItemLotKey");
            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(ItemBarcodeLibrary item)
        {
            return new
            {
                Barcode = item.Barcode,
                BarcodeState = item.BarcodeState,
                ItemKey = item.ItemKey,
                ItemLotKey = item.ItemLotKey,
                CreateBy = item.CreateBy,
                CreateTime = item.CreateTime,
                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 IEnumerable<ItemBarcodeLibrary> GetBarcodeLibrary(params String[] barcode) 
        {
            SqlBuilder sqlBuilder = new SqlBuilder(this);
            ItemBLL itemBLL = new ItemBLL();
            TableJoinInfo itemTable = new TableJoinInfo
            {
                LeftDal = this,
                LeftColumnName = "ITEM_KEY",
                RightDal = itemBLL,
                RightColumnName = "KEY",
                JoinType = TableJoinTypes.Inner,
            };
            sqlBuilder.AppendSelectionTable(itemTable);
            ItemLotBLL itemLotBLL = new ItemLotBLL();
            TableJoinInfo itemlotTable = new TableJoinInfo
            {
                LeftDal = this,
                LeftColumnName = "ITEM_LOT_KEY",
                RightDal = itemLotBLL,
                RightColumnName = "KEY",
                JoinType = TableJoinTypes.Inner,
            };
            sqlBuilder.AppendSelectionTable(itemlotTable);
            sqlBuilder.AppendWherePhrases($"{this.AliasTableName}.BARCODE IN :arrBarcodes");
            return SqlMapper.Query<ItemBarcodeLibrary, Item, ItemLot, ItemBarcodeLibrary>(
                ConnectionFactory.Current.GetSessionConnection(),
                sqlBuilder.ToString(),
                (l, i, il) =>
                {
                    l.ItemInfo = i;
                    l.ItemLotInfo = il;
                    return l;
                },
                new
                {
                    arrBarcodes = barcode
                },
                splitOn: sqlBuilder.GetSpiltors());
        }
    }
}