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 UserInfoBLL : BaseOrclSnapshotBLL<UserInfo>, IUserInfoBLL
    {
        public UserInfoBLL() : base()
        {
            AliasTableName = "u";
        }

        internal override String TABLE_NAME { get { return "USER_INFO"; } }//TB_NAME
        internal override string ENTITY_NAME { get { return "UserInfo"; } }
        internal override String KEY_COLUMN { get { return "KEY"; } }//KEY_COLUMN
        internal override string SNAPSHOT_BASE { get { return "USER_INFO_BASE"; } }

        internal override void InitMaps()
        {
            ColumnPropMaps.Add("SNAPSHOT_KEY", "SnapshotKey");
            ColumnPropMaps.Add("KEY", "Key");
            ColumnPropMaps.Add("CREATE_BY", "CreateBy");
            ColumnPropMaps.Add("CREATE_TIME", "CreateTime");
            ColumnPropMaps.Add("USER_ID", "UserId");
            ColumnPropMaps.Add("PASSWORD", "Password");
            ColumnPropMaps.Add("IF_CARD_ID", "IfCardId");
            ColumnPropMaps.Add("PINGER_PRINT_DATA", "PingerPrintData");
            ColumnPropMaps.Add("LOING_TRY_TIMES", "LoingTryTimes");
            ColumnPropMaps.Add("IS_ACTIVE", "IsActive");
            ColumnPropMaps.Add("LOCATION_KEY", "LocationKey");
            ColumnPropMaps.Add("SECTION_KEY", "SectionKey");
            ColumnPropMaps.Add("NAME", "Name");
            ColumnPropMaps.Add("SEX", "Sex");
            ColumnPropMaps.Add("AGE", "Age");
            ColumnPropMaps.Add("MOBILE_PHONE", "MobilePhone");
            ColumnPropMaps.Add("TELPHONE", "Telphone");
            ColumnPropMaps.Add("ADDRESS", "Address");
            ColumnPropMaps.Add("IS_SUPPER_USER", "IsSupperUser");
            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("SECTION_ID", "SectionId");
            ColumnPropMaps.Add("USER_CODE", "UserCode");//COLUMN_MAPS
        }

        internal override object GetParam(UserInfo userinfo)
        {
            return new
            {
                SnapshotKey = userinfo.SnapshotKey,
                Key = userinfo.Key,
                CreateBy = userinfo.CreateBy,
                CreateTime = userinfo.CreateTime,
                UserId = userinfo.UserId,
                Password = userinfo.Password,
                IfCardId = userinfo.IfCardId,
                PingerPrintData = userinfo.PingerPrintData,
                LoingTryTimes = userinfo.LoingTryTimes,
                IsActive = userinfo.IsActive,
                LocationKey = userinfo.LocationKey,
                SectionKey = userinfo.SectionKey,
                Name = userinfo.Name,
                Sex = userinfo.Sex,
                Age = userinfo.Age,
                MobilePhone = userinfo.MobilePhone,
                Telphone = userinfo.Telphone,
                Address = userinfo.Address,
                IsSupperUser = userinfo.IsSupperUser,
                Flag = userinfo.Flag,
                ClientTime = userinfo.ClientTime,
                ServerTime = userinfo.ServerTime,
                Ext01 = userinfo.Ext01,
                Ext02 = userinfo.Ext02,
                Ext03 = userinfo.Ext03,
                Ext04 = userinfo.Ext04,
                Ext05 = userinfo.Ext05,
                Ext06 = userinfo.Ext06,
                Ext07 = userinfo.Ext07,
                Ext08 = userinfo.Ext08,
                Ext09 = userinfo.Ext09,
                Ext10 = userinfo.Ext10,
                SectionId = userinfo.SectionId,
                UserCode = userinfo.UserCode,   //Params
            };
        }

        public IEnumerable<UserInfo> ListUserInfo()
        {
            SqlBuilder sqlBuilder = new SqlBuilder(this);
            SectionBLL sectionBLL = new SectionBLL();
            TableJoinInfo sectionTable = new TableJoinInfo
            {
                LeftDal = this,
                LeftColumnName = "SECTION_ID",
                RightDal = sectionBLL,
                RightColumnName = sectionBLL.KEY_COLUMN,
                JoinType = TableJoinTypes.Left,
            };
            sqlBuilder.AppendSelectionTable(sectionTable);
            IEnumerable<UserInfo> ListUser = SqlMapper.Query<UserInfo, Section, UserInfo>(ConnectionFactory.Current.GetSessionConnection(),
                sqlBuilder.ToString(),
                (ui, sect) =>
                {
                    ui.Section = sect;
                    return ui;
                }, splitOn: sqlBuilder.GetSpiltors());
            return ListUser;
        }

        public UserInfo GetByUserId(string userId)
        {
            var sql = BuildBaseSql();
            return SqlMapper.Query<UserInfo>(ConnectionFactory.Current.GetSessionConnection(),
                sql + "  WHERE USER_ID = :UserId",
                new { UserId = userId }).FirstOrDefault();
        }

        public UserInfo GetByRfId(string rfid)
        {
            var sql = BuildBaseSql();
            return SqlMapper.Query<UserInfo>(ConnectionFactory.Current.GetSessionConnection(),
                sql + "  WHERE IF_CARD_ID = :IfIdCard",
                new { IfIdCard = rfid }).FirstOrDefault();

        }
    }
}