using Edu.Common.Enum;
using Edu.Model.Entity.Customer;
using Edu.Model.ViewModel.Customer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VT.FW.DB.Dapper;

namespace Edu.Repository.Customer
{
    /// <summary>
    /// 企业信息仓储层
    /// </summary>
    public class RB_EnterpriseRepository:BaseRepository<RB_Enterprise>
    {
        /// <summary>
        /// 获取企业分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Enterprise_Extend> GetEnterprisePageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Enterprise_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            var parameters = new DynamicParameters();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_Enterprise AS A
WHERE 1=1
");
            builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Enterprise_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Enterprise_Extend.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.EnterpriseName))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @EnterpriseName ", nameof(RB_Enterprise_Extend.EnterpriseName));
                    parameters.Add("EnterpriseName", "%" + query.EnterpriseName.Trim() + "%");
                }
            }
            return GetPage<RB_Enterprise_Extend>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }


        /// <summary>
        /// 获取企业列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Enterprise_Extend> GetEnterpriseListRepository( RB_Enterprise_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            var parameters = new DynamicParameters();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_Enterprise AS A
WHERE 1=1
");
            builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Enterprise_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Enterprise_Extend.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.EnterpriseName))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @EnterpriseName ", nameof(RB_Enterprise_Extend.EnterpriseName));
                    parameters.Add("EnterpriseName", "%" + query.EnterpriseName.Trim() + "%");
                }
            }
            return Get<RB_Enterprise_Extend>( builder.ToString(), parameters).ToList();
        }
    }
}
