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

namespace RDH.Data.BLL
{
    public partial class PortableStorageTrackBLL : BaseOrclBLL<PortableStorageTrack>
    {
        public PortableStorageTrackBLL() : base()
        {
            AliasTableName = "pst";
        }

        internal override String TABLE_NAME { get { return "PORTABLE_STORAGE_TRACK"; } }//TB_NAME
        internal override string ENTITY_NAME { get { return "PortableStorageTrack"; } }
        internal override String KEY_COLUMN { get { return "ITEM_CODE"; } }//KEY_COLUMN

        internal override void InitMaps()
        {
            ColumnPropMaps.Add("ITEM_CODE", "ItemCode");
            ColumnPropMaps.Add("TRACK_CODE", "TrackCode");
            ColumnPropMaps.Add("TRACK_VALUE", "TrackValue");
            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(PortableStorageTrack t)
        {
            return new
            {
                ItemCode = t.ItemCode,
                TrackCode = t.TrackCode,
                TrackValue = t.TrackValue,
                CreateBy = t.CreateBy,
                CreateTime = t.CreateTime,
                Flag = t.Flag,
                ClientTime = t.ClientTime,
                ServerTime = t.ServerTime,
                Ext01 = t.Ext01,
                Ext02 = t.Ext02,
                Ext03 = t.Ext03,
                Ext04 = t.Ext04,
                Ext05 = t.Ext05,
                Ext06 = t.Ext06,
                Ext07 = t.Ext07,
                Ext08 = t.Ext08,
                Ext09 = t.Ext09,
                Ext10 = t.Ext10,
            };
        }

        public IEnumerable<PortableStorageTrack> ListTrackByItem(String itemCode, DateTime beginTime, DateTime endTime)
        {
            StringBuilder sqlBuilder = new StringBuilder();
            sqlBuilder.Append(BuildBaseSql());
            sqlBuilder.Append(" WHERE ");
            if (!String.IsNullOrEmpty(itemCode))
            {
                sqlBuilder.AppendFormat("{0}.item_code=:itemCode AND ", this.TABLE_NAME);
            }
            sqlBuilder.AppendFormat("{0}.create_time BETWEEN :startTime AND :endTime ORDER BY {0}.CREATE_TIME", this.TABLE_NAME);
            return SqlMapper.Query<PortableStorageTrack>(ConnectionFactory.Current.GetSessionConnection(),
                sqlBuilder.ToString(),
                new
                {
                    itemCode = itemCode,
                    startTime = beginTime,
                    endTime = endTime
                });
        }
        public PortableStorageTrack GetLastTrackByItem(String itemCode)
        {
            StringBuilder sqlBuilder = new StringBuilder();
            sqlBuilder.Append(BuildBaseSql());
            sqlBuilder.Append(" WHERE ");
            if (!String.IsNullOrEmpty(itemCode))
            {
                sqlBuilder.AppendFormat("{0}.item_code=:itemCode ", this.TABLE_NAME);
            }
            sqlBuilder.Append($"ORDER BY {this.TABLE_NAME}.CREATE_TIME DESC");
            return SqlMapper.Query<PortableStorageTrack>(ConnectionFactory.Current.GetSessionConnection(),
                sqlBuilder.ToString(),
                new
                {
                    itemCode = itemCode,
                })
                .FirstOrDefault();
        }
    }
}
