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;

namespace Edu.Repository.Customer
{
    /// <summary>
    /// 客户续费介绍仓储层
    /// </summary>
    public class RB_Student_RenewOrderRepository : BaseRepository<RB_Student_RenewOrder>
    {
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Student_RenewOrder_Extend> GetList(RB_Student_RenewOrder_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_Student_RenewOrder AS A
WHERE 1=1
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_RenewOrder_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_RenewOrder_Extend.Group_Id), query.Group_Id);
                }
            }
            builder.AppendFormat(" ORDER BY A.{0} ASC ", nameof(RB_Student_RenewOrder_Extend.Id));
            return Get<RB_Student_RenewOrder_Extend>(builder.ToString()).ToList();
        }

        /// <summary>
        /// 获取学员每月续费，介绍情况
        /// </summary>
        /// <param name="group_Id"></param>
        /// <returns></returns>
        public List<RB_Student_RenewOrder_Extend> GetStuMonthList(int group_Id)
        {
            string sql = $@"SELECT r.Type,r.`Month`,COUNT(0) as OrderNum FROM rb_student_reneworder r
INNER JOIN rb_order o on r.OrderId = o.OrderId
WHERE r.Group_Id ={group_Id} and r.`Status` =0 and o.OrderState <>3
GROUP BY r.Type,r.`Month`";
            return Get<RB_Student_RenewOrder_Extend>(sql).ToList();
        }
    }
}
