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_Customer_CategoryRepository : BaseRepository<RB_Customer_Category>
    {
        /// <summary>
        /// 获取同行分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Customer_Category_Extend> GetCustomerCategoryPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Customer_Category_Extend query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_Customer_Category AS A
WHERE 1=1 
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_Category_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_Category_Extend.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.CategoryName))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @CategoryName ", nameof(RB_Customer_Category_Extend.CategoryName));
                    parameters.Add("CategoryName", "%" + query.CategoryName.Trim() + "%");
                }
                if (query.CatetoryType > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_Category_Extend.CatetoryType), (int)query.CatetoryType);
                }
                if (query.CreateBy > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_Category_Extend.CreateBy), query.CreateBy);
                }
            }
            builder.AppendFormat(" ORDER BY A.{0} DESC  ", nameof(RB_Customer_Category_Extend.CategoryId));
            return GetPage<RB_Customer_Category_Extend>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 获取同行列表
        /// </summary>
        /// <returns></returns>
        public List<RB_Customer_Category_Extend> GetCustomerCategoryListRepository(RB_Customer_Category_Extend query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_Customer_Category AS A
WHERE 1=1 
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_Category_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_Category_Extend.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.CategoryName))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @CategoryName ", nameof(RB_Customer_Category_Extend.CategoryName));
                    parameters.Add("CategoryName", "%" + query.CategoryName.Trim() + "%");
                }
                if (query.CatetoryType > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Customer_Category_Extend.CatetoryType), (int)query.CatetoryType);
                }
            }
            builder.AppendFormat(" ORDER BY A.{0} DESC  ", nameof(RB_Customer_Category_Extend.CategoryId));
            return Get<RB_Customer_Category_Extend>(builder.ToString(), parameters).ToList();
        }
    }
}
