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 OperationTakeItemBLL : BaseOrclSnapshotBLL<OperationTakeItem>
    {
        public OperationTakeItemBLL() : base()
        {
            AliasTableName = "oti";
        }

        internal override String TABLE_NAME { get { return "OPERATION_TAKE_ITEM"; } }//TB_NAME
        internal override string ENTITY_NAME { get { return "OperationTakeItem"; } }
        internal override String KEY_COLUMN { get { return "SNAPSHOT_KEY"; } }//KEY_COLUMN
        internal override string SNAPSHOT_BASE { get { return "OPERATION_TAKE_ITEM_BASE"; } }

        internal override void InitMaps()
        {
            ColumnPropMaps.Add("SNAPSHOT_KEY", "SnapshotKey");
            ColumnPropMaps.Add("KEY", "Key");
            ColumnPropMaps.Add("TAKE_BAG_KEY", "TakeBagKey");
            ColumnPropMaps.Add("ITEM_KEY", "ItemKey");
            ColumnPropMaps.Add("CREATE_BY", "CreateBy");
            ColumnPropMaps.Add("CREATE_TIME", "CreateTime");
            ColumnPropMaps.Add("TAKE_QUANTITY", "TakeQuantity");
            ColumnPropMaps.Add("RETURN_QUANTITY", "ReturnQuantity");
            ColumnPropMaps.Add("CHECK_QUANTITY", "CheckQuantity");
            ColumnPropMaps.Add("TRANSFER_QUANTITY", "TransferQuantity");
            ColumnPropMaps.Add("ABOLISH_QUANTITY", "AbolishQuantity");
            ColumnPropMaps.Add("RETURNSTORE_QUANTITY", "ReturnstoreQuantity");
            ColumnPropMaps.Add("IS_OUTSID", "IsOutsid");
            ColumnPropMaps.Add("REPLACE_BY", "ReplaceBy");
            ColumnPropMaps.Add("STATUS", "Status");
            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("DISPOSE_QUANTITY", "DisposeQuantity");
            ColumnPropMaps.Add("WITNESS_USER", "WitnessUser");//COLUMN_MAPS
        }

        internal override object GetParam(OperationTakeItem operationtakeitem)
        {
            return new
            {

                SnapshotKey = operationtakeitem.SnapshotKey,
                Key = operationtakeitem.Key,
                TakeBagKey = operationtakeitem.TakeBagKey,
                ItemKey = operationtakeitem.ItemKey,
                CreateBy = operationtakeitem.CreateBy,
                CreateTime = operationtakeitem.CreateTime,
                TakeQuantity = operationtakeitem.TakeQuantity,
                ReturnQuantity = operationtakeitem.ReturnQuantity,
                CheckQuantity = operationtakeitem.CheckQuantity,
                TransferQuantity = operationtakeitem.TransferQuantity,
                AbolishQuantity = operationtakeitem.AbolishQuantity,
                ReturnstoreQuantity = operationtakeitem.ReturnstoreQuantity,
                IsOutsid = operationtakeitem.IsOutsid,
                ReplaceBy = operationtakeitem.ReplaceBy,
                Status = operationtakeitem.Status,
                DeviceKey = operationtakeitem.DeviceKey,
                Flag = operationtakeitem.Flag,
                ClientTime = operationtakeitem.ClientTime,
                ServerTime = operationtakeitem.ServerTime,
                Ext01 = operationtakeitem.Ext01,
                Ext02 = operationtakeitem.Ext02,
                Ext03 = operationtakeitem.Ext03,
                Ext04 = operationtakeitem.Ext04,
                Ext05 = operationtakeitem.Ext05,
                Ext06 = operationtakeitem.Ext06,
                Ext07 = operationtakeitem.Ext07,
                Ext08 = operationtakeitem.Ext08,
                Ext09 = operationtakeitem.Ext09,
                Ext10 = operationtakeitem.Ext10,
                DisposeQuantity = operationtakeitem.DisposeQuantity,
                WitnessUser = operationtakeitem.WitnessUser,   //Params
            };
        }
        public IEnumerable<OperationTakeItem> ListTakeItemsByTakeBag(OperationTakeItemBag bag) 
        {
            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);
            sqlBuilder.AppendWherePhrases($"{this.AliasTableName}.TAKE_BAG_KEY=:bagKey");
            IEnumerable<OperationTakeItem> rlt = SqlMapper.Query<OperationTakeItem, Item, OperationTakeItem>(
                ConnectionFactory.Current.GetSessionConnection(),
                sqlBuilder.ToString(),
                (oti, i) =>
                {
                    oti.Item = i;
                    return oti;
                },
                new
                {
                    bagKey = bag.Key
                },
                splitOn: sqlBuilder.GetSpiltors());
            if (rlt != null) 
            {
                return rlt.GroupBy(x => x.ItemKey)
                    .Select(x => new OperationTakeItem
                    {
                        Item = x.First().Item,
                        TakeBagKey = x.First().TakeBagKey,
                        TakeQuantity = x.Sum(i => i.TakeQuantity.GetValueOrDefault())
                    });
            }
            return null;
        }
        public IEnumerable<OperationTakeItem> ListTakeItemLotsByTakeBag(OperationTakeItemBag bag)
        {
            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 = "EXT01",
                RightDal = itemLotBLL,
                RightColumnName = "KEY",
                JoinType = TableJoinTypes.Inner
            };
            sqlBuilder.AppendSelectionTable(itemLotTable);
            sqlBuilder.AppendWherePhrases($"{this.AliasTableName}.TAKE_BAG_KEY=:bagKey");
            IEnumerable<OperationTakeItem> rlt = SqlMapper.Query<OperationTakeItem, Item, ItemLot, OperationTakeItem>(
                ConnectionFactory.Current.GetSessionConnection(),
                sqlBuilder.ToString(),
                (oti, i, il) =>
                {
                    oti.Item = i;
                    oti.ItemLot = il;
                    return oti;
                },
                new
                {
                    bagKey = bag.Key
                },
                splitOn: sqlBuilder.GetSpiltors());
            return rlt;
        }
    }
}
