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_AssistRepository : BaseRepository<RB_Student_Assist>
    {
        /// <summary>
        /// 获取学员协助人员列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Student_Assist_Extend> GetStudentAssistListRepository(RB_Student_Assist_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,IFNULL(B.EmName,'') AS AssistName
FROM RB_Student_Assist AS A LEFT JOIN rb_employee AS B ON A.AssistId=B.EmployeeId
WHERE 1=1 
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Assist_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.StuId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Assist_Extend.StuId), query.StuId);
                }
                if (query.AssistId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Assist_Extend.AssistId), query.AssistId);
                }
                if (!string.IsNullOrEmpty(query.QStuIds))
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Student_Assist_Extend.StuId), query.QStuIds);
                }
            }
            return Get<RB_Student_Assist_Extend>(builder.ToString()).ToList();
        }


        /// <summary>
        /// 删除学员协同人员
        /// </summary>
        /// <param name="StuId"></param>
        /// <returns></returns>
        public bool DeleteStudentAssistRepository(int StuId)
        {
            string delSql = string.Format(" DELETE FROM RB_Student_Assist WHERE StuId={0} ", StuId);
            return base.Execute(delSql)>0;
        }

        /// <summary>
        /// 获取协助人员转交
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="group_Id"></param>
        /// <returns></returns>
        public List<RB_Student_Assist_Extend> GetUserStudentAssistCareOf(int userId, int group_Id)
        {
            string sql = $@"SELECT a.* FROM rb_student_assist a 
inner join rb_student s on s.StuId = a.StuId 
WHERE a.`Status`=0 and s.Group_Id ={group_Id} and s.`Status`=0 and a.AssistId ={userId} ";
            return Get<RB_Student_Assist_Extend>(sql).ToList();
        }





        /// <summary>
        /// 根据订单获取 转介绍学生的老师
        /// </summary>
        /// <param name="orderIds"></param>        
        /// <returns></returns>
        public List<RB_Student_Assist_Extend> GetAssistTeacherForOrder(string orderIds,bool IsZJS = true ) {
            string sql = $@"SELECT og.OrderId,a.AssistId FROM rb_student_orderguest og
INNER JOIN rb_student_assist a on og.Student_Id =a.StuId
INNER JOIN rb_student s on og.Student_Id =s.StuId
INNER JOIN rb_account ac on a.AssistId = ac.Id
WHERE og.`Status` =0 and a.`Status` =0 and og.OrderId in({orderIds}) and a.AssistType =4 {(IsZJS ? "and s.CreateType =4" : "")}  and ac.AccountType =2";
            return Get<RB_Student_Assist_Extend>(sql).ToList();
        }

        /// <summary>
        /// 获取试听老师
        /// </summary>
        /// <param name="orderIds"></param>
        /// <returns></returns>
        public List<RB_Student_Assist_Extend> GetTrialTeacher(string orderIds,int groupId)
        {
            string sql = $@"SELECT g.OrderId,c.TeacherId,ac.Id as AssistId FROM rb_visitor_reserve r
left JOIN rb_reserve_class c on r.ReserveClassId = c.ReserveClassId
left join rb_student_orderguest g on r.Visitor_Id = g.Student_Id
left join rb_account ac on ac.AccountId = c.TeacherId and ac.AccountType =2
WHERE r.Group_Id ={groupId} and r.`Status` =0 and c.`Status` =0 and g.`Status` =0 and g.OrderId in({orderIds})";
            return Get<RB_Student_Assist_Extend>(sql).ToList();
        }
    }
}