using Edu.Model.Entity.EduTask;
using Edu.Model.ViewModel.EduTask;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using VT.FW.DB.Dapper;
using VT.FW.DB;
using Edu.Repository.Flow;
using Edu.Model.ViewModel.Flow;
using Edu.Repository.User;
using Edu.Repository.Course;
using Edu.Repository.Grade;
using Edu.Repository.Sell;
using System.Web;
using Edu.Common.Plugin;
using Edu.Common;

namespace Edu.Repository.EduTask
{
    /// <summary>
    /// 教务单据仓储层
    /// </summary>
    public class RB_Education_ReceiptRepository : BaseRepository<RB_Education_Receipt>
    {
        /// <summary>
        /// 教务单据审核人员信息仓储层对象
        /// </summary>
        private readonly RB_Education_RelevanceRepository student_BackRelevanceRepository = new RB_Education_RelevanceRepository();

        /// <summary>
        /// 教务单据审核记录仓储层对象
        /// </summary>
        private readonly RB_Education_RecordRepository student_BackRecordRepository = new RB_Education_RecordRepository();

        /// <summary>
        /// 流程仓储层对象
        /// </summary>
        private readonly RB_FlowRepository flowRepository = new RB_FlowRepository();

        /// <summary>
        /// 账号仓储层对象
        /// </summary>
        private readonly RB_AccountRepository accountRepository = new RB_AccountRepository();

        /// <summary>
        /// 班级仓储层对象
        /// </summary>
        private readonly RB_ClassRepository classRepository = new RB_ClassRepository();

        /// <summary>
        /// 订单仓储层对象
        /// </summary>
        private readonly RB_OrderRepository orderRepository = new RB_OrderRepository();

        /// <summary>
        /// 获取教务单据分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Education_Receipt_ViewModel> GetEducationReceiptPageRepository(int pageIndex, int pageSize, out long rowsCount, EducationReceiptQuery query)
        {
            var parameters = new DynamicParameters();
            string isSpecNode = "";
            if (query.Conditon == Common.Enum.Course.EduReceiptConditionEnum.WaitAgree)
            {
                isSpecNode = string.Format(",IFNULL((SELECT SpecialNode FROM rb_Education_relevance WHERE Id IN(SELECT AuditId FROM rb_education_record WHERE ConditionId=A.Id AND AuditStatus=1 AND AuditEmId={0})),0) AS SpecialNode ", query.CreateBy);
            }
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,IFNULL(B.ClassName,'') AS ClassName,IFNULL(B.ClassNo,'') AS ClassNo,IFNULL(C.TeacherName,'') AS TeacherName
       ,IFNULL(D.SName,'') AS SchoolName,IFNULL(E.CourseName,'') AS CourseName {0} 
FROM RB_Education_Receipt AS A LEFT JOIN rb_class AS B ON A.ClassId=B.ClassId
     LEFT JOIN rb_teacher AS C ON B.Teacher_Id=C.TId
	 LEFT JOIN rb_school AS D ON A.School_Id=D.SId
     LEFT JOIN rb_course AS E ON B.CouseId=E.CourseId
WHERE 1=1  
", isSpecNode);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Education_Receipt_ViewModel.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.QIds))
                {
                    builder.AppendFormat(@" AND A.{0} IN ({1}) ", nameof(RB_Education_Receipt_ViewModel.Id), query.QIds);
                }
                if (!string.IsNullOrEmpty(query.Title))
                {
                    builder.AppendFormat(@" AND A.{0} LIKE @Title ", nameof(RB_Education_Receipt_ViewModel.Title));
                    parameters.Add("Title", "%" + query.Title.Trim() + "%");
                }
                if (!string.IsNullOrEmpty(query.StartTime))
                {
                    builder.AppendFormat(@" AND A.{0}>='{1}' ", nameof(RB_Education_Receipt_ViewModel.CreateTime), query.StartTime);
                }
                if (!string.IsNullOrEmpty(query.EndTime))
                {
                    builder.AppendFormat(@" AND A.{0}<='{1} 23:59:59' ", nameof(RB_Education_Receipt_ViewModel.CreateTime), query.EndTime);
                }
                if (query.ReceiptType > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Education_Receipt_ViewModel.ReceiptType), query.ReceiptType);
                }
                if (query.RelationId > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Education_Receipt_ViewModel.RelationId), query.RelationId);
                }
                if (query.Conditon != null && query.Conditon > 0)
                {
                    switch (query.Conditon)
                    {
                        //待我审批
                        case Common.Enum.Course.EduReceiptConditionEnum.WaitAgree:
                            builder.AppendFormat(" AND A.VerifyStatus NOT IN(6) AND EXISTS (SELECT 1 FROM rb_education_record WHERE ConditionId=A.Id AND AuditStatus=1 AND AuditEmId={0}) ", query.CreateBy);
                            break;
                        //已审批的
                        case Common.Enum.Course.EduReceiptConditionEnum.Agree:
                            builder.AppendFormat(" AND EXISTS (SELECT 1 FROM rb_education_record WHERE ConditionId=A.Id AND AuditStatus NOT IN(1,5) AND AuditEmId={0}) ", query.CreateBy);
                            break;
                        //我发起的
                        case Common.Enum.Course.EduReceiptConditionEnum.Launch:
                            builder.AppendFormat(" AND A.CreateBy={0} ", query.CreateBy);
                            break;
                        //抄送给我的
                        case Common.Enum.Course.EduReceiptConditionEnum.CarbonCopy:
                            builder.AppendFormat(" AND FIND_IN_SET({0},A.RecipientIds) ", query.CreateBy);
                            break;
                    }
                }
                if (query.VerifyStatus > -1)
                {
                    if (query.VerifyStatus == 3)
                    {
                        builder.AppendFormat(@" AND A.{0} IN(3,4) ", nameof(RB_Education_Receipt_ViewModel.VerifyStatus));
                    }
                    else
                    {
                        builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Education_Receipt_ViewModel.VerifyStatus), query.VerifyStatus);
                    }
                }
            }
            builder.AppendFormat(" ORDER BY A.{0} DESC ", nameof(RB_Education_Receipt_ViewModel.Id));
            return GetPage<RB_Education_Receipt_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }


        /// <summary>
        /// 获取教务单据列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Education_Receipt_ViewModel> GetEducationReceiptListRepository(EducationReceiptQuery query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,IFNULL(B.ClassName,'') AS ClassName,IFNULL(B.ClassNo,'') AS ClassNo,IFNULL(C.TeacherName,'') AS TeacherName
       ,IFNULL(D.SName,'') AS SchoolName,IFNULL(E.CourseName,'') AS CourseName
FROM RB_Education_Receipt AS A LEFT JOIN rb_class AS B ON A.ClassId=B.ClassId
     LEFT JOIN rb_teacher AS C ON B.Teacher_Id=C.TId
	 LEFT JOIN rb_school AS D ON A.School_Id=D.SId
     LEFT JOIN rb_course AS E ON B.CouseId=E.CourseId
WHERE 1=1  
");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Education_Receipt_ViewModel.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.QIds))
                {
                    builder.AppendFormat(@" AND A.{0} IN ({1}) ", nameof(RB_Education_Receipt_ViewModel.Id), query.QIds);
                }
                if (query.RelationId > 0)
                {
                    builder.AppendFormat(@" AND A.{0} IN ({1}) ", nameof(RB_Education_Receipt_ViewModel.RelationId), query.RelationId);
                }
                if (!string.IsNullOrEmpty(query.QRelationIds))
                {
                    builder.AppendFormat(@" AND A.{0} IN ({1}) ", nameof(RB_Education_Receipt_ViewModel.RelationId), query.QRelationIds);
                }
                if (!string.IsNullOrEmpty(query.Title))
                {
                    builder.AppendFormat(@" AND A.{0} LIKE @Title ", nameof(RB_Education_Receipt_ViewModel.Title));
                    parameters.Add("Title", "%" + query.Title.Trim() + "%");
                }
                if (!string.IsNullOrEmpty(query.StartTime))
                {
                    builder.AppendFormat(@" AND A.{0}>='{1}' ", nameof(RB_Education_Receipt_ViewModel.CreateTime), query.StartTime);
                }
                if (!string.IsNullOrEmpty(query.EndTime))
                {
                    builder.AppendFormat(@" AND A.{0}<='{1} 23:59:59' ", nameof(RB_Education_Receipt_ViewModel.CreateTime), query.EndTime);
                }
                if (query.ReceiptType > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Education_Receipt_ViewModel.ReceiptType), query.ReceiptType);
                }
                if (query.VerifyStatus > -1)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Education_Receipt_ViewModel.VerifyStatus), query.VerifyStatus);
                }
                if (!string.IsNullOrEmpty(query.QEffectiveDate))
                {
                    builder.AppendFormat(@" AND A.{0}>='{1}' ", nameof(RB_Education_Receipt_ViewModel.EffectiveDate), query.QEffectiveDate);
                }
            }
            return Get<RB_Education_Receipt_ViewModel>(builder.ToString(), parameters).ToList();
        }


        /// <summary>
        /// 获取教务单据列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Education_Receipt_ViewModel> GetEducationReceiptListExtRepository(EducationReceiptQuery query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,IFNULL(B.ClassName,'') AS ClassName,IFNULL(B.ClassNo,'') AS ClassNo,IFNULL(C.TeacherName,'') AS TeacherName
       ,IFNULL(D.SName,'') AS SchoolName,IFNULL(E.CourseName,'') AS CourseName,IFNULL(backClass.GuestId,0) AS BackClassGuestId
	   ,IFNULL(stopClass.OrderGuestId,0) AS StopClassGuestId,IFNULL(orderChange.OrderGuestId,0) AS OrderChangeGuestId,IFNULL(tempinvitation.OrderGuestIds,'') AS TempinvitationGuestIds
FROM RB_Education_Receipt AS A LEFT JOIN rb_class AS B ON A.ClassId=B.ClassId
     LEFT JOIN rb_teacher AS C ON B.Teacher_Id=C.TId
	 LEFT JOIN rb_school AS D ON A.School_Id=D.SId
     LEFT JOIN rb_course AS E ON B.CouseId=E.CourseId
	 LEFT JOIN rb_student_backclass AS backClass ON A.RelationId=backClass.BackId AND A.ReceiptType=2
	 LEFT JOIN rb_student_stopclass AS stopClass ON A.RelationId=stopClass.Id AND A.ReceiptType=3
	 LEFT JOIN rb_order_change AS orderChange ON A.RelationId=orderChange.Id AND A.ReceiptType IN(5,6)
	 LEFT JOIN rb_student_tempinvitation AS tempinvitation ON A.RelationId=tempinvitation.Id AND A.ReceiptType=4
WHERE 1=1  
");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Education_Receipt_ViewModel.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.QIds))
                {
                    builder.AppendFormat(@" AND A.{0} IN ({1}) ", nameof(RB_Education_Receipt_ViewModel.Id), query.QIds);
                }
                if (query.RelationId > 0)
                {
                    builder.AppendFormat(@" AND A.{0} IN ({1}) ", nameof(RB_Education_Receipt_ViewModel.RelationId), query.RelationId);
                }
                if (!string.IsNullOrEmpty(query.QRelationIds))
                {
                    builder.AppendFormat(@" AND A.{0} IN ({1}) ", nameof(RB_Education_Receipt_ViewModel.RelationId), query.QRelationIds);
                }
                if (!string.IsNullOrEmpty(query.Title))
                {
                    builder.AppendFormat(@" AND A.{0} LIKE @Title ", nameof(RB_Education_Receipt_ViewModel.Title));
                    parameters.Add("Title", "%" + query.Title.Trim() + "%");
                }
                if (!string.IsNullOrEmpty(query.StartTime))
                {
                    builder.AppendFormat(@" AND A.{0}>='{1}' ", nameof(RB_Education_Receipt_ViewModel.CreateTime), query.StartTime);
                }
                if (!string.IsNullOrEmpty(query.EndTime))
                {
                    builder.AppendFormat(@" AND A.{0}<='{1} 23:59:59' ", nameof(RB_Education_Receipt_ViewModel.CreateTime), query.EndTime);
                }
                if (query.ReceiptType > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Education_Receipt_ViewModel.ReceiptType), query.ReceiptType);
                }
                if (!string.IsNullOrEmpty(query.QReceiptTypes))
                {
                    builder.AppendFormat(@" AND A.{0} IN({1}) ", nameof(RB_Education_Receipt_ViewModel.ReceiptType), query.QReceiptTypes);
                }
                if (query.VerifyStatus > -1)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Education_Receipt_ViewModel.VerifyStatus), query.VerifyStatus);
                }
                if (!string.IsNullOrEmpty(query.QEffectiveDate))
                {
                    builder.AppendFormat(@" AND A.{0}>='{1}' ", nameof(RB_Education_Receipt_ViewModel.EffectiveDate), query.QEffectiveDate);
                }
                if (!string.IsNullOrEmpty(query.QGuestIds))
                {
                    builder.AppendFormat("AND (backClass.GuestId IN({0}) OR stopClass.OrderGuestId IN({0}) OR orderChange.OrderGuestId IN({0}) OR tempinvitation.OrderGuestIds IN({0}) ) ", query.QGuestIds);
                }
            }
            return Get<RB_Education_Receipt_ViewModel>(builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 新增修改教务单据
        /// </summary>
        /// <param name="model"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool SetEducationReceiptRepository(RB_Education_Receipt model, out string message)
        {
            message = "";
            bool flag = false;
            var newId = base.Insert(model);
            model.Id = newId;
            flag = newId > 0;
            //审核流程
            var flowModel = new RB_Flow_ViewModel();
            //退课流程
            if (model.ReceiptType == Common.Enum.Finance.ReceiptTypeEnum.BackClass)
            {
                flowModel = flowRepository.GetFlowRepository(Common.Config.BackClassFlowId);
                if (flowModel == null)
                {
                    message = "未配置退课流程!";
                    return false;
                }
            }
            else if (model.ReceiptType == Common.Enum.Finance.ReceiptTypeEnum.ChangeClass)
            {
                flowModel = flowRepository.GetFlowRepository(Common.Config.ChangeClassFlowId);
                if (flowModel == null)
                {
                    message = "未配置调课流程!";
                    return false;
                }
            }
            else if (model.ReceiptType == Common.Enum.Finance.ReceiptTypeEnum.StopClass)
            {
                flowModel = flowRepository.GetFlowRepository(Common.Config.StopClassFlowId);
                if (flowModel == null)
                {
                    message = "未配置停课流程!";
                    return false;
                }
            }
            else if (model.ReceiptType == Common.Enum.Finance.ReceiptTypeEnum.InvitationClass)
            {
                flowModel = flowRepository.GetFlowRepository(Common.Config.TempInvitationClassFlowId);
                if (flowModel == null)
                {
                    message = "未配置临时上课邀请流程!";
                    return false;
                }
            }
            else if (model.ReceiptType == Common.Enum.Finance.ReceiptTypeEnum.OrderTransClass)
            {
                flowModel = flowRepository.GetFlowRepository(Common.Config.OrderTransClassFlowId);
                if (flowModel == null)
                {
                    message = "未配置订单转班流程!";
                    return false;
                }
            }
            else if (model.ReceiptType == Common.Enum.Finance.ReceiptTypeEnum.OrderSplitClass)
            {
                flowModel = flowRepository.GetFlowRepository(Common.Config.OrderSplitClassFlowId);
                if (flowModel == null)
                {
                    message = "未配置订单分拆流程!";
                    return false;
                }
            }
            else if (model.ReceiptType == Common.Enum.Finance.ReceiptTypeEnum.LeaveClass)
            {
                flowModel = flowRepository.GetFlowRepository(Common.Config.StudentLeaveFlowId);
                if (flowModel == null)
                {
                    message = "未配置学生请假流程!";
                    return false;
                }
            }
            else if (model.ReceiptType == Common.Enum.Finance.ReceiptTypeEnum.ZuJuan)
            {
                flowModel = flowRepository.GetFlowRepository(Common.Config.ZuJuanFlowId);
                if (flowModel == null)
                {
                    message = "未配置组卷审批流程!";
                    return false;
                }
            }
            else if (model.ReceiptType == Common.Enum.Finance.ReceiptTypeEnum.ExamApply)
            {
                flowModel = flowRepository.GetFlowRepository(Common.Config.ExamApplyFlowId);
                if (flowModel == null)
                {
                    message = "未配置考试申请审批流程!";
                    return false;
                }
            }
            else if (model.ReceiptType == Common.Enum.Finance.ReceiptTypeEnum.Contribute)
            {
                flowModel = flowRepository.GetFlowRepository(Common.Config.ContributeFlowId);
                if (flowModel == null)
                {
                    message = "未配置投稿审批流程!";
                    return false;
                }
            }
            if (flowModel?.FlowNodeList?.Count == 0)
            {
                message = "未配置审核流程!";
                return false;
            }
            int index = 0;
            foreach (var item in flowModel.FlowNodeList)
            {
                if (item.FlowAduitList != null && item.FlowAduitList.Count > 0)
                {
                    string AuditedId = "";//审核人
                    foreach (var subItem in item.FlowAduitList)
                    {
                        //指定人员
                        if (subItem.AuditType == Common.Enum.User.WFTAuditTypeEnum.SpecifiedMember)
                        {
                            AuditedId += subItem.UserId + ",";
                        }
                        //指定角色
                        else if (subItem.AuditType == Common.Enum.User.WFTAuditTypeEnum.Role)
                        {
                            //本班教师审核
                            if (subItem.PostType == 1)
                            {
                                var teacherId = GetTeacherIdRepository(model.ClassId);
                                if (teacherId <= 0)
                                {
                                    message = "未找到此班级的配置的教师信息!";
                                    return false;
                                }
                                AuditedId += teacherId + ",";
                                subItem.UserId = teacherId;
                            }
                            //关联销售审核
                            else if (subItem.PostType == 2)
                            {
                                var EnterId = GetOrderSaleIdRepository(model.OrderId);
                                AuditedId += EnterId + ",";
                                subItem.UserId = EnterId;
                            }
                            //分区校长审核
                            else if (subItem.PostType == 3)
                            {
                                var headmasterId = GetSchoolMasterIdRepository(model.Group_Id);
                                AuditedId += headmasterId + ",";
                                subItem.UserId = headmasterId;
                            }
                            //教务长审核
                            else if (subItem.PostType == 4)
                            {
                                var jiaoWuZhangId = GetSchoolJiaoWuZhangIdRepository(model.Group_Id);
                                AuditedId += jiaoWuZhangId + ",";
                                subItem.UserId = jiaoWuZhangId;
                            }
                        }
                        //系统角色
                        else if (subItem.AuditType == Common.Enum.User.WFTAuditTypeEnum.Post)
                        {
                            var postIds = GetPostIdsRepository(model.Group_Id, subItem.PostType.ToString());
                            if (postIds != null && postIds.Count > 0)
                            {
                                AuditedId += string.Join(",", postIds) + ",";
                                subItem.PostUserIds = string.Join(",", postIds);
                            }
                        }
                    }

                    var levance = new RB_Education_Relevance()
                    {
                        ConditionId = model.Id,
                        AuditDescription = item.NodeName,
                        AuditedId = "",
                        AuditWay = item.FlowAduitList[0].AuditWay,
                        AuditType = item.FlowAduitList[0].AuditType,
                        OriginalAuditId = 0,
                        RoleOrInitiator = 0,
                        PostIds = "",
                        UserIds = "",
                        SysPostIds = "",
                        ToAuditId = AuditedId.TrimEnd(','),
                        Sort = item.SortNum,
                        SpecialNode = item.FlowAduitList[0].IsSpecNode,
                        Stauts = Common.Enum.EduTask.EduTaskRrocessStatus.AuditIng,
                    };
                    //指定人员审核
                    if (levance.AuditType == Common.Enum.User.WFTAuditTypeEnum.SpecifiedMember)
                    {
                        levance.UserIds = string.Join(",", item.FlowAduitList.Where(qitem => qitem.AuditType == Common.Enum.User.WFTAuditTypeEnum.SpecifiedMember).Select(qitem => qitem.UserId));
                    }
                    //指定角色审核
                    else if (levance.AuditType == Common.Enum.User.WFTAuditTypeEnum.Role)
                    {
                        levance.PostIds = string.Join(",", item.FlowAduitList.Where(qitem => qitem.AuditType == Common.Enum.User.WFTAuditTypeEnum.Role).Select(qitem => qitem.PostType));
                    }
                    //系统角色审核
                    else if (levance.AuditType == Common.Enum.User.WFTAuditTypeEnum.Post)
                    {
                        //系统角色编号
                        levance.SysPostIds = string.Join(",", item.FlowAduitList.Where(qitem => qitem.AuditType == Common.Enum.User.WFTAuditTypeEnum.Post).Select(qitem => qitem.PostType));
                    }

                    if (model.ReceiptType == Common.Enum.Finance.ReceiptTypeEnum.BackClass)
                    {
                        levance.WorkFlowId = Common.Config.BackClassFlowId;
                    }
                    if (model.ReceiptType == Common.Enum.Finance.ReceiptTypeEnum.ChangeClass)
                    {
                        levance.WorkFlowId = Common.Config.ChangeClassFlowId;
                    }
                    var backLevanceId = student_BackRelevanceRepository.Insert(levance);
                    levance.Id = backLevanceId;
                    flag = backLevanceId > 0;
                    if (index == 0)
                    {
                        foreach (var subItem in item.FlowAduitList)
                        {
                            var recordModel = new RB_Education_Record_ViewModel()
                            {
                                ConditionId = model.Id,
                                AuditStatus = Common.Enum.EduTask.EduTaskRrocessStatus.AuditIng,
                                AuditId = levance.Id,
                                CreateBy = model.CreateBy,
                                CreateTime = DateTime.Now,
                                UpdateBy = model.CreateBy,
                                UpdateTime = DateTime.Now,
                                Description = "",
                            };
                            //系统岗位审核;
                            if (!string.IsNullOrEmpty(subItem.PostUserIds))
                            {
                                var userList = Common.ConvertHelper.StringToList(subItem.PostUserIds);
                                foreach (var tItem in userList)
                                {
                                    recordModel.Id = 0;
                                    recordModel.AuditEmId = tItem;
                                    flag = student_BackRecordRepository.Insert(recordModel) > 0;
                                    #region 审核通知
                                    var account = new RB_AccountRepository().GetEmployeeInfo(tItem);
                                    if (account != null && !string.IsNullOrEmpty(account.WorkUserId))
                                    {
                                        var recipientPath = HttpUtility.UrlEncode($"/teacher/change-class-manager?tab=3&id={model.Id}");
                                        var content = $"待审通知 你有一张业务单据等待审核\n>**概要信息** \n>单据信息：<font color='comment'>{EnumHelper.ToName(model.ReceiptType)}单据 ({model.Id})</font>\n>创  建  人：<font color='comment'>{account.EmployeeName}</font>\n>创建时间：<font color='comment'>{DateTime.Now.ToString("MM-dd HH-mm")}</font>\n>请点击：[查看详情]({Config.ErpUrl}/autologin?loginId={tItem}&target={recipientPath})";
                                        PushWorkChatHelper.PushToWorkChat(content, account.WorkUserId, "业务单据待审通知");
                                    }
                                    #endregion
                                }
                            }
                            else
                            {
                                recordModel.AuditEmId = subItem.UserId;

                                flag = student_BackRecordRepository.Insert(recordModel) > 0;
                                #region 审核通知
                                var account = new RB_AccountRepository().GetEmployeeInfo(subItem.UserId);
                                if (account != null && !string.IsNullOrEmpty(account.WorkUserId))
                                {
                                    var recipientPath = HttpUtility.UrlEncode($"/teacher/change-class-manager?tab=3&id={model.Id}");
                                    var content = $"待审通知 你有一张业务单据等待审核\n>**概要信息** \n>单据信息：<font color='comment'>{EnumHelper.ToName(model.ReceiptType)}单据 ({model.Id})</font>\n>创  建  人：<font color='comment'>{account.EmployeeName}</font>\n>创建时间：<font color='comment'>{DateTime.Now.ToString("MM-dd HH-mm")}</font>\n>请点击：[查看详情]({Config.ErpUrl}/autologin?loginId={subItem.UserId}&target={recipientPath})";
                                    PushWorkChatHelper.PushToWorkChat(content, account.WorkUserId, "业务单据待审通知");
                                }
                                #endregion
                            }
                        }
                    }
                }
                index++;
            }
            return flag;
        }

        /// <summary>
        /// 获取班级教师编号
        /// </summary>
        /// <param name="classId"></param>
        /// <returns></returns>
        public int GetTeacherIdRepository(int classId)
        {
            var teacherId = 0;
            //班级实体类
            var classModel = classRepository.GetEntity(classId);
            if (classModel != null && classModel.Teacher_Id > 0)
            {
                //获取教师实体信息
                var teacherModel = accountRepository.GetAccountListExtRepository(new Model.ViewModel.User.RB_Account_ViewModel()
                {
                    AccountId = (classModel?.Teacher_Id ?? 0),
                    AccountType = Common.Enum.User.AccountTypeEnum.Teacher
                })?.FirstOrDefault();
                teacherId = teacherModel?.Id ?? 0;
            }
            return teacherId;
        }

        /// <summary>
        /// 获取订单销售人员编号
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public int GetOrderSaleIdRepository(int orderId)
        {
            var orderModel = orderRepository.GetEntity(orderId);
            var enterId = (orderModel?.EnterID ?? 0);
            return enterId;
        }

        /// <summary>
        /// 获取学校校长编号
        /// </summary>
        /// <param name="GroupId"></param>
        /// <returns></returns>
        public int GetSchoolMasterIdRepository(int GroupId)
        {
            var headmaster = accountRepository.GetAccountListExtRepository(new Model.ViewModel.User.RB_Account_ViewModel()
            {
                Post_Id = Common.Config.SchoolPostId,
                Group_Id = GroupId
            })?.FirstOrDefault();
            var headmasterId = (headmaster?.Id ?? 0);
            return headmasterId;
        }

        /// <summary>
        /// 获取教务长编号
        /// </summary>
        /// <param name="GroupId"></param>
        /// <returns></returns>
        public int GetSchoolJiaoWuZhangIdRepository(int GroupId)
        {
            var headmaster = accountRepository.GetAccountListExtRepository(new Model.ViewModel.User.RB_Account_ViewModel()
            {
                Post_Id = Common.Config.JiaoWuZhang,
                Group_Id = GroupId
            })?.FirstOrDefault();
            var jiaoWuZhangId = (headmaster?.Id ?? 0);
            return jiaoWuZhangId;
        }

        /// <summary>
        /// 根据岗位编号获取人员
        /// </summary>
        /// <param name="GroupId"></param>
        /// <param name="PostId"></param>
        /// <returns></returns>
        public List<int> GetPostIdsRepository(int GroupId, string PostId)
        {
            List<int> result = new List<int>();
            var list = accountRepository.GetAccountListExtRepository(new Model.ViewModel.User.RB_Account_ViewModel()
            {
                Group_Id = GroupId,
                QPostIds = PostId
            });
            if (list != null && list.Count > 0)
            {
                foreach (var item in list)
                {
                    result.Add(item.Id);
                }
            }
            return result;
        }
    }
}
