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 PortableStorageItemLotBLL : BaseOrclBLL<PortableStorageItemLot>
    {
        public PortableStorageItemLotBLL() : base()
        {
            AliasTableName = "psil";
        }

        internal override String TABLE_NAME { get { return "PORTABLE_STORAGE_ITEM_LOT"; } }//TB_NAME
        internal override string ENTITY_NAME { get { return "PortableStorageItemLot"; } }
        internal override String KEY_COLUMN { get { return "KEY"; } }//KEY_COLUMN4
        internal override void InitMaps()
        {
            ColumnPropMaps.Add("KEY", "Key");
            ColumnPropMaps.Add("QUANTITY", "Quantity");
            ColumnPropMaps.Add("ITEM_KEY", "ItemKey");
            ColumnPropMaps.Add("LOT_KEY", "LotKey");
            ColumnPropMaps.Add("PORTABLE_ITEM_KEY", "PortableStorageItemkey");
            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(PortableStorageItemLot item)
        {
            return new
            {
                Key = item.Key,
                Quantity = item.Quantity,
                ItemKey = item.ItemKey,
                LotKey = item.LotKey,
                PortableStorageItemkey = item.PortableStorageItemkey,
                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,
            };
        }
        /// <summary>
        /// 根据药品计数条码获取药品库存信息
        /// </summary>
        public PortableStorageItemLot GetByItemBarcode(String barcode) 
        {
            SqlBuilder sqlBuilder = new SqlBuilder(this);
            PortableStorageItemBLL portableStorageItemBLL = new PortableStorageItemBLL();
            TableJoinInfo psiTable = new TableJoinInfo
            {
                LeftDal = this,
                LeftColumnName = "PORTABLE_ITEM_KEY",
                RightDal = portableStorageItemBLL,
                RightColumnName = "KEY",
                JoinType = TableJoinTypes.Inner,
            };
            sqlBuilder.AppendSelectionTable(psiTable);
            ItemBLL itemBLL = new ItemBLL();
            TableJoinInfo itemTable = new TableJoinInfo
            {
                LeftDal = portableStorageItemBLL,
                LeftColumnName = "ITEM_KEY",
                RightDal = itemBLL,
                RightColumnName = "KEY",
                JoinType = TableJoinTypes.Inner,
            };
            sqlBuilder.AppendSelectionTable(itemTable);
            ItemLotBLL itemLotBLL = new ItemLotBLL();
            TableJoinInfo itemlotTable = new TableJoinInfo
            {
                LeftDal = this,
                LeftColumnName = "LOT_KEY",
                RightDal = itemLotBLL,
                RightColumnName = "KEY",
                JoinType = TableJoinTypes.Inner,
            };
            sqlBuilder.AppendSelectionTable(itemlotTable);
            sqlBuilder.AppendWherePhrases($"{this.AliasTableName}.EXT01=:itemBarcode");
            return SqlMapper.Query<PortableStorageItemLot, PortableStorageItem, Item ,ItemLot, PortableStorageItemLot>(ConnectionFactory.Current.GetSessionConnection(),
                sqlBuilder.ToString(),
                (psil, psi, i, il) =>
                {
                    psil.PortableStorageItem = psi;
                    psi.Item = i;
                    psil.ItemLot = il;
                    return psil;
                },
                new { itemBarcode = barcode },
                splitOn: sqlBuilder.GetSpiltors()).FirstOrDefault();
        }
    }

}
