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

namespace Edu.Repository.System
{
    /// <summary>
    /// 客户需求仓储层
    /// </summary>
    public class RB_NeedsRepository : BaseRepository<RB_Needs>
    {
        /// <summary>
        /// 获取客户需求分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Needs_Extend> GetNeedsPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Needs_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            var parameters = new DynamicParameters();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_Needs AS A
WHERE 1=1
");
            builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Needs_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Needs_Extend.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.Name))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @Name ", nameof(RB_Needs_Extend.Name));
                    parameters.Add("Name", "%" + query.Name.Trim() + "%");
                }
            }
            return GetPage<RB_Needs_Extend>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }


        /// <summary>
        /// 获取客户需求列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Needs_Extend> GetNeedsListRepository(RB_Needs_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            var parameters = new DynamicParameters();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_Needs AS A
WHERE 1=1
");
            builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Needs_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Needs_Extend.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.Name))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @Name ", nameof(RB_Needs_Extend.Name));
                    parameters.Add("Name", "%" + query.Name.Trim() + "%");
                }
                if (!string.IsNullOrEmpty(query.QNeedsId))
                {
                    builder.AppendFormat(@" AND A.{0} IN({1}) ", nameof(RB_Needs_Extend.Id), query.QNeedsId);
                }
            }
            return Get<RB_Needs_Extend>(builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 获取客户需求实体类
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public RB_Needs_Extend GetNeedsExtEntityRepository(int Id)
        {
            var extModel = base.GetEntity<RB_Needs_Extend>(Id);
            return extModel;
        }
    }
}
