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 SectionBLL : BaseOrclBLL<Section>, ISectionBLL
    {
        public SectionBLL() : base()
        {
            AliasTableName = "sec";
        }

        internal override String TABLE_NAME { get { return "SECTION"; } }//TB_NAME
        internal override string ENTITY_NAME { get { return "Section"; } }
        internal override String KEY_COLUMN { get { return "KEY"; } }//KEY_COLUMN

        internal override void InitMaps()
        {
            ColumnPropMaps.Add("KEY", "Key");
            ColumnPropMaps.Add("CREATE_BY", "CreateBy");
            ColumnPropMaps.Add("CREATE_TIME", "CreateTime");
            ColumnPropMaps.Add("IS_ACTIVE", "IsActive");
            ColumnPropMaps.Add("SECTION_ID", "SectionId");
            ColumnPropMaps.Add("SECTION_NAME", "SectionName");
            ColumnPropMaps.Add("SECTION_LEADER", "SectionLeader");
            ColumnPropMaps.Add("SECTION_STATES", "SectionStates");
            ColumnPropMaps.Add("SECTION_DESCRIPTION", "SectionDescription");
            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");//COLUMN_MAPS
        }

        internal override object GetParam(Section section)
        {
            return new
            {
                Key = section.Key,
                CreateBy = section.CreateBy,
                CreateTime = section.CreateTime,
                IsActive = section.IsActive,
                IsDelete = section.IsDelete,
                SectionId = section.SectionId,
                SectionName = section.SectionName,
                SectionLeader = section.SectionLeader,
                SectionStates = section.SectionStates,
                SectionDescription = section.SectionDescription,
                Flag = section.Flag,
                ClientTime = section.ClientTime,
                ServerTime = section.ServerTime,
                Ext01 = section.Ext01,
                Ext02 = section.Ext02,
                Ext03 = section.Ext03,
                Ext04 = section.Ext04,
                Ext05 = section.Ext05,
                Ext06 = section.Ext06,
                Ext07 = section.Ext07,
                Ext08 = section.Ext08,
                Ext09 = section.Ext09,
                Ext10 = section.Ext10,   //Params
            };
        }
        public IEnumerable<Section> ListSection()
        {
            SqlBuilder sqlBuilder = new SqlBuilder(this);
            UserInfoBLL userInfoBLL = new UserInfoBLL();
            TableJoinInfo userTable = new TableJoinInfo
            {
                LeftDal = this,
                LeftColumnName = "CREATE_BY",
                RightDal = userInfoBLL,
                RightColumnName = userInfoBLL.KEY_COLUMN,
                JoinType = TableJoinTypes.Left,
            };
            sqlBuilder.AppendSelectionTable(userTable);
            return SqlMapper.Query<Section, UserInfo, Section>(ConnectionFactory.Current.GetSessionConnection(),
                sqlBuilder.ToString(),
                (s, ui) =>
                {
                    s.UserInfo = ui;
                    return s;
                },
                splitOn: sqlBuilder.GetSpiltors());
        }

        public Section GetBySectionId(string sectionId)
        {
            var sql = BuildBaseSql();
            return SqlMapper.Query<Section>(ConnectionFactory.Current.GetSessionConnection(), 
                sql + "  WHERE  SECTION_ID = :SectionId", 
                new { SectionId = sectionId }).FirstOrDefault();

        }
        public Section SearchSection(String sectionID, String sectionName)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(BuildBaseSql());
            sql.Append(" WHERE 1=1");
            if (!String.IsNullOrEmpty(sectionID))
            {
                sql.Append(" AND SECTION_ID=:sectionId");
            }
            if (!String.IsNullOrEmpty(sectionName))
            {
                sql.Append(" AND SECTION_NAME=:sectionName");
            }
            return SqlMapper.Query<Section>(ConnectionFactory.Current.GetSessionConnection(),
                sql.ToString(),
                new { sectionId = sectionID, sectionName = sectionName }).FirstOrDefault();
        }
    }
}