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

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

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