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

namespace Edu.Repository.User
{
    /// <summary>
    /// 管理者仓储层
    /// </summary>
    public class RB_ManagerRepository : BaseRepository<Model.Entity.User.RB_Manager>
    {
        /// <summary>
        /// 获取管理者列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Manager_ViewModel> GetManagerListRepository(RB_Manager_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.* 
FROM rb_manager AS A 
WHERE 1=1 
");
            builder.AppendFormat(" AND a.{0}={1} ", nameof(RB_Manager_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (!string.IsNullOrWhiteSpace(query.MName))
                {
                    builder.AppendFormat(" AND a.{0} LIKE @MName ", nameof(RB_Manager_ViewModel.MName));
                    parameters.Add("MName", "%" + query.MName.Trim() + "%");
                }
                if (!string.IsNullOrWhiteSpace(query.MTel))
                {
                    builder.AppendFormat(" AND a.{0} LIKE @MTel ", nameof(RB_Manager_ViewModel.MTel));
                    parameters.Add("MTel", "%" + query.MTel.Trim() + "%");
                }
                if (query.School_Id > 0)
                {
                    builder.AppendFormat(" AND a.{0}={1} ", nameof(RB_Manager_ViewModel.School_Id), query.School_Id);
                }
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND a.{0}={1} ", nameof(RB_Manager_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.MId > 0)
                {
                    builder.AppendFormat(" AND a.{0}={1} ", nameof(RB_Manager_ViewModel.MId), query.MId);
                }
            }
            return Get<RB_Manager_ViewModel>(builder.ToString(), parameters).ToList();
        }




        /// <summary>
        /// 根据学生id获取学生对应的销售
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Manager_ViewModel> GetManagerListByStuId(int Student_Id, int Group_Id)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat($@"
SELECT * from rb_manager where MId in(
SELECT b.AccountId from 
rb_account as b 
LEFT JOIN rb_order as o on o.EnterID=b.Id
LEFT JOIN rb_class as c on o.ClassId=c.ClassId
LEFT JOIN rb_student_orderguest as sog on sog.ClassId=c.ClassId
WHERE 1=1  and o.OrderState=1 and sog.Account_Id={Student_Id} and c.ClassStatus in(1,2) and c.Status =0 and sog.Status=0 ");

            if (Group_Id > 0)
            {
                builder.AppendFormat(" AND b.{0}={1} ", nameof(RB_Manager_ViewModel.Group_Id), Group_Id);
            }
            builder.AppendFormat(")");
            return Get<RB_Manager_ViewModel>(builder.ToString(), parameters).ToList();
        }
    }
}