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

namespace Edu.Repository.Course
{
    /// <summary>
    /// 订单客人仓储层
    /// </summary>
    public class RB_Order_GuestRepository : BaseRepository<RB_Order_Guest>
    {
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <param name="orderIds"></param>
        /// <returns></returns>
        public List<RB_Order_Guest_ViewModel> GetList(RB_Order_Guest_ViewModel demodel)
        {
            string where = $@" 1=1 and Status=0";
            if (demodel.Group_Id > 0)
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id > 0)
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.School_Id)} ={demodel.School_Id}";
            }
            if (demodel.Id > 0)
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.Id)} ={demodel.Id}";
            }
            if (demodel.OrderId > 0)
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.OrderId)} ={demodel.OrderId}";
            }
            if (demodel.ClassId > 0)
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.ClassId)} ={demodel.ClassId}";
            }
            if (!string.IsNullOrEmpty(demodel.OrderIds))
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.OrderId)} in({demodel.OrderIds})";
            }
            if (!string.IsNullOrEmpty(demodel.ClassIds))
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.ClassId)} in({demodel.ClassIds})";
            }
            if (!string.IsNullOrEmpty(demodel.GuestName))
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.GuestName)} like '%{demodel.GuestName}%'";
            }
            if (demodel.GuestState > 0)
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.GuestState)} ={demodel.GuestState}";
            }

            string sql = $@" select * from RB_Order_Guest where {where}";
            return Get<RB_Order_Guest_ViewModel>(sql).ToList();
        }

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="demodel"></param>
        /// <param name="orderIds"></param>
        /// <returns></returns>
        public List<RB_Order_Guest_ViewModel> GetPageList(int pageIndex, int pageSize, out long rowsCount, RB_Order_Guest_ViewModel demodel)
        {
            string where = $@" 1=1 and Status=0";
            if (demodel.Group_Id > 0)
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id > 0)
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.School_Id)} ={demodel.School_Id}";
            }
            if (demodel.OrderId > 0)
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.OrderId)} ={demodel.OrderId}";
            }
            if (demodel.ClassId > 0)
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.ClassId)} ={demodel.ClassId}";
            }
            if (demodel.SourceId > 0)
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.SourceId)} ={demodel.SourceId}";
            }
            if (!string.IsNullOrEmpty(demodel.GuestName))
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.GuestName)} like '%{demodel.GuestName}%'";
            }
            if (demodel.GuestState > 0)
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.GuestState)} ={demodel.GuestState}";
            }

            string sql = $@" select * from RB_Order_Guest where {where}";
            return GetPage<RB_Order_Guest_ViewModel>(pageIndex, pageSize, out rowsCount, sql).ToList();
        }


        /// <summary>
        /// 获取客人数量
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public int GetGuestNum(RB_Order_Guest_ViewModel demodel)
        {
            string where = $@" 1=1 and Status=0";
            if (demodel.Group_Id > 0)
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id > 0)
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.School_Id)} ={demodel.School_Id}";
            }
            if (demodel.OrderId > 0)
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.OrderId)} ={demodel.OrderId}";
            }
            if (demodel.ClassId > 0)
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.ClassId)} ={demodel.ClassId}";
            }
            if (!string.IsNullOrEmpty(demodel.OrderIds))
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.OrderId)} in({demodel.OrderIds})";
            }
            if (!string.IsNullOrEmpty(demodel.GuestName))
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.GuestName)} like '%{demodel.GuestName}%'";
            }
            if (demodel.GuestState > 0)
            {
                where += $@" and {nameof(RB_Order_Guest_ViewModel.GuestState)} ={demodel.GuestState}";
            }

            string sql = $@" select count(0) from RB_Order_Guest where {where}";
            var obj = ExecuteScalar(sql);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return int.Parse(obj.ToString());
            }
        }
    }
}
