using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Edu.Model.Entity.EduTask;
using Edu.Model.Entity.Sell;
using Edu.Model.ViewModel.EduTask;
using Edu.Repository.Sell;
using VT.FW.DB;

namespace Edu.Repository.EduTask
{
    public class RB_Student_StopClassRepository : BaseRepository<RB_Student_StopClass>
    {
        // <summary>
        /// 订单学员仓储层对象
        /// </summary>
        private readonly RB_Order_GuestRepository order_GuestRepository = new RB_Order_GuestRepository();

        /// <summary>
        /// 教务单据仓储层对象
        /// </summary>
        private readonly RB_Education_ReceiptRepository education_ReceiptRepository = new RB_Education_ReceiptRepository();
        /// <summary>
        /// 获取停课列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Student_StopClass_ViewModel> GetStudentStopClassListRepository(RB_Student_StopClass_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@" 
SELECT A.*,IFNULL(B.ClassName,'') AS ClassName
FROM RB_Student_StopClass AS A LEFT JOIN rb_class AS B ON A.ClassId=B.ClassId
WHERE 1=1
");
            if (query != null)
            {
                if (!string.IsNullOrEmpty(query.Q_Student_StopClass_Ids))
                {
                    builder.AppendFormat(@" AND A.{0} IN ({1}) ", nameof(RB_Student_StopClass_ViewModel.Id), query.Q_Student_StopClass_Ids);
                }
                if (query.IsAuditThrough > -1)
                {
                    builder.AppendFormat(@" AND A.{0} = ({1}) ", nameof(RB_Student_StopClass_ViewModel.IsAuditThrough), query.IsAuditThrough);
                }
            }
            return Get<RB_Student_StopClass_ViewModel>(builder.ToString()).ToList();
        }

        /// <summary>
        /// 审核通过后更新申请表中的状态
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="AuditStatus">2审核通过  3 不通过  4 驳回</param>
        /// <returns></returns>
        public bool UpdateStudentStopClass(object Id, int AuditStatus)
        {
            //查询当前调课信息
            bool flag = false;
            var receiptModel = education_ReceiptRepository.GetEntity(Id);
            if (receiptModel == null || receiptModel.Id == 0)
            {
                return false;
            }
            if (receiptModel.ReceiptType != Common.Enum.Finance.ReceiptTypeEnum.StopClass)
            {
                return false;
            }
            var model = GetEntity(receiptModel.RelationId);
            if (model == null || model.Id == 0)
            {
                return false;
            }
            else //更新学生的信息
            {
                if (AuditStatus == 2)//审核通过
                {
                    Dictionary<string, object> keyValues = new Dictionary<string, object>()
                    {
                        { nameof(RB_Student_StopClass_ViewModel.IsAuditThrough),1}
                    };
                    List<WhereHelper> wheres = new List<WhereHelper>() {
                        new WhereHelper(){
                             FiledName=nameof(RB_Student_StopClass_ViewModel.Id),
                              FiledValue=model.Id,
                               OperatorEnum=OperatorEnum.Equal
                        }
                     };
                    flag = Update(keyValues, wheres);
                }
                else
                { //拒绝or 不通过将状态改回之前的状态
                    Dictionary<string, object> keyValues = new Dictionary<string, object>()
                    {
                        { nameof(RB_Student_StopClass_ViewModel.IsAuditThrough),(AuditStatus-1)}
                    };
                    List<WhereHelper> wheres = new List<WhereHelper>() {
                        new WhereHelper(){
                             FiledName=nameof(RB_Student_StopClass_ViewModel.Id),
                              FiledValue=model.Id,
                               OperatorEnum=OperatorEnum.Equal
                        }
                     };
                    flag = Update(keyValues, wheres);
                    if (flag)
                    {
                        Dictionary<string, object> keyStudentValues = new Dictionary<string, object>()
                        {
                            { nameof(RB_Order_Guest.GuestState),model.OldStudentStatus}
                        };
                        List<WhereHelper> wheresStudnetn = new List<WhereHelper>() {
                            new WhereHelper()
                            {
                                 FiledName=nameof(RB_Order_Guest.Id),
                                 FiledValue=model.OrderGuestId,
                                 OperatorEnum=OperatorEnum.Equal
                            }
                         };
                        flag = order_GuestRepository.Update(keyStudentValues, wheresStudnetn);
                    }
                }
            }

            return flag;
        }

    }
}
