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

namespace RDH.Data.BLL
{

    public partial class ItemLotBLL : IItemLotBLL
    {

        public IEnumerable<ItemLot> ListItemLot()
        {
            var sql = BuildBaseSql();
            var ListItemLot = SqlMapper.Query<ItemLot>(ConnectionFactory.Current.GetSessionConnection(), sql, null);
            return ListItemLot;
        }

        public IEnumerable<ItemLot> SearchItemLot(Guid itemKey, String lotCode, DateTime? expDate)
        {
            String sqlSelect = String.Format("SELECT * FROM "
                + "({0} WHERE item_key=:ItemKey ORDER BY exp_date DESC) t WHERE ROWNUM<10 ",
                BuildBaseSql());
            if (!String.IsNullOrEmpty(lotCode))
            {
                sqlSelect += " AND \"LotNo\" = :LotNo";
            }
            if (expDate.HasValue)
            {
                sqlSelect += " AND \"ExpDate\"=:ExpDate";
            }
            ItemLot paramLot = new ItemLot();
            paramLot.ItemKey = itemKey;
            paramLot.LotNo = lotCode;
            paramLot.ExpDate = expDate.GetValueOrDefault();
            var rlt = SqlMapper.Query<ItemLot>(ConnectionFactory.Current.GetSessionConnection(),
                sqlSelect, paramLot);
            return rlt;
        }

        public ItemLot GetByItemIdAndLotNo(string itemId, string lotNo)
        {
            var sql = BuildBaseSql();
            return SqlMapper.Query<ItemLot>(ConnectionFactory.Current.GetSessionConnection(), sql + "  WHERE  ITEM_ID = :ItemId And LOT_NO = :LotNo", new { ItemId = itemId, LotNo = lotNo }).FirstOrDefault();

        }
    }
}