using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Edu.Common.Enum;
using Edu.Model.Entity.Finance;
using Edu.Model.ViewModel.Finance;

namespace Edu.Repository.Finance
{
    /// <summary>
    /// 营收报表仓储层
    /// </summary>
    public partial class RB_Edu_RevenueReportRepository : BaseRepository<RB_Edu_RevenueReport>
    {
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<RB_Edu_RevenueReport_Extend> GetList(RB_Edu_RevenueReport_Extend model)
        {
            string where = " 1=1 ";
            if (model.Rb_Group_Id > 0)
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.Rb_Group_Id) + "=" + model.Rb_Group_Id);
            }
            if (model.SchoolId > 0)
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.SchoolId) + "=" + model.SchoolId);
            }
            if (model.DateType > 0)
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.DateType) + "=" + model.DateType);
            }
            if (model.IsSelectStuAbroad == 1)
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.DateType) + " in(2,3)");
            }
            if (model.ClassId >0)
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.ClassId) + "=" + model.ClassId);
            }
            if (!string.IsNullOrEmpty(model.ClassName))
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.ClassName) + " like '%{0}%'", model.ClassName);
            }
            if (model.ClassState > 0)
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.ClassState) + "=" + model.ClassState);
            }
            if (model.CourseId > 0)
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.CourseId) + "=" + model.CourseId);
            }
            if (!string.IsNullOrEmpty(model.CourseName))
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.CourseName) + " like '%{0}%'", model.CourseName);
            }
            if (model.TeacherId > 0)
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.TeacherId) + "=" + model.TeacherId);
            }
            if (!string.IsNullOrEmpty(model.TeacherName))
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.TeacherName) + " like '%{0}%'", model.TeacherName);
            }
            if (!string.IsNullOrEmpty(model.StartTime))
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.ClassBegins) + " >='{0}'", model.StartTime);
            }
            if (!string.IsNullOrEmpty(model.EndTime))
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.ClassBegins) + " <='{0} 23:59:59'", model.EndTime);
            }


            string sql = string.Format(@"SELECT * FROM {0} AS A WHERE {1} ", nameof(RB_Edu_RevenueReport), where);
            return Get<RB_Edu_RevenueReport_Extend>(sql).ToList();
        }

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<RB_Edu_RevenueReport_Extend> GetPageList(int pageIndex,int pageSize,out long count,RB_Edu_RevenueReport_Extend model)
        {
            string where = " 1=1 ";
            if (model.Rb_Group_Id > 0)
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.Rb_Group_Id) + "=" + model.Rb_Group_Id);
            }
            if (model.SchoolId > 0)
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.SchoolId) + "=" + model.SchoolId);
            }
            if (model.DateType > 0)
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.DateType) + "=" + model.DateType);
            }
            if (model.ClassId > 0)
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.ClassId) + "=" + model.ClassId);
            }
            if (!string.IsNullOrEmpty(model.ClassName))
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.ClassName) + " like '%{0}%'", model.ClassName);
            }
            if (model.ClassState > 0)
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.ClassState) + "=" + model.ClassState);
            }
            if (model.CourseId > 0)
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.CourseId) + "=" + model.CourseId);
            }
            if (!string.IsNullOrEmpty(model.CourseName))
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.CourseName) + " like '%{0}%'", model.CourseName);
            }
            if (model.TeacherId > 0)
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.TeacherId) + "=" + model.TeacherId);
            }
            if (!string.IsNullOrEmpty(model.TeacherName))
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.TeacherName) + " like '%{0}%'", model.TeacherName);
            }
            if (!string.IsNullOrEmpty(model.StartTime))
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.ClassBegins) + " >='{0}'", model.StartTime);
            }
            if (!string.IsNullOrEmpty(model.EndTime))
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.ClassBegins) + " <='{0} 23:59:59'", model.EndTime);
            }

            string sql = string.Format(@"SELECT * FROM {0} AS A WHERE {1} ", nameof(RB_Edu_RevenueReport), where);
            return GetPage<RB_Edu_RevenueReport_Extend>(pageIndex, pageSize, out count, sql).ToList();
        }

        /// <summary>
        /// 获取统计
        /// </summary>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public RB_Edu_RevenueReport_Extend GetRevenueReportStatistics(RB_Edu_RevenueReport_Extend model)
        {
            string where = " 1=1 ";
            if (model.Rb_Group_Id > 0)
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.Rb_Group_Id) + "=" + model.Rb_Group_Id);
            }
            if (model.SchoolId > 0)
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.SchoolId) + "=" + model.SchoolId);
            }
            if (model.DateType > 0)
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.DateType) + "=" + model.DateType);
            }
            if (model.ClassId > 0)
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.ClassId) + "=" + model.ClassId);
            }
            if (!string.IsNullOrEmpty(model.ClassName))
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.ClassName) + " like '%{0}%'", model.ClassName);
            }
            if (model.ClassState > 0)
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.ClassState) + "=" + model.ClassState);
            }
            if (model.CourseId > 0)
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.CourseId) + "=" + model.CourseId);
            }
            if (!string.IsNullOrEmpty(model.CourseName))
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.CourseName) + " like '%{0}%'", model.CourseName);
            }
            if (model.TeacherId > 0)
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.TeacherId) + "=" + model.TeacherId);
            }
            if (!string.IsNullOrEmpty(model.TeacherName))
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.TeacherName) + " like '%{0}%'", model.TeacherName);
            }
            if (!string.IsNullOrEmpty(model.StartTime))
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.ClassBegins) + " >='{0}'", model.StartTime);
            }
            if (!string.IsNullOrEmpty(model.EndTime))
            {
                where += string.Format(" AND A." + nameof(RB_Edu_RevenueReport_Extend.ClassBegins) + " <='{0} 23:59:59'", model.EndTime);
            }

            string sql = string.Format(@"SELECT -1 AS Id
,sum(YingShou) as YingShou
,sum(ShiShou) as ShiShou
,sum(TuiKuan) as TuiKuan
,sum(PingTaiShuiJin) as PingTaiShuiJin
,sum(YouHui) as YouHui
,sum(DaiShou) as DaiShou
,sum(YiShou) as YiShou
,sum(ZaiTu) as ZaiTu
,sum(GuestNum) as GuestNum
,sum(QTShouRu) as QTShouRu
,sum(TiCheng) as TiCheng
,sum(ClassFee) as ClassFee
,sum(JiXiaoMoney) as JiXiaoMoney
,sum(QTZhiChu) as QTZhiChu
,sum(YingFu) as YingFu
,sum(MaoLi) as MaoLi
,sum(MaoLiRate) as MaoLiRate
,sum(ShiJiLiRun) as ShiJiLiRun
,sum(AverageIncome) as AverageIncome
,sum(AverageCost) as AverageCost
,sum(AverageProfit) as AverageProfit FROM {0} AS A WHERE {1} ", nameof(RB_Edu_RevenueReport), where);
            return Get<RB_Edu_RevenueReport_Extend>(sql).FirstOrDefault();
        }
    }
}
