using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Edu.Cache.User;
using Edu.Common.API;
using Edu.Common.Enum;
using Edu.Common.Plugin;
using Edu.Model.ViewModel.Course;
using Edu.Module.Course;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;

namespace Edu.WebApi.Controllers.Course
{
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class ClassRoomController : BaseController
    {
        /// <summary>
        /// 教室处理类对象
        /// </summary>
        private readonly ClassRoomModule classRoomModule = new ClassRoomModule();


        /// <summary>
        /// 班级处理类对象
        /// </summary>
        private readonly ClassModule classModule = new ClassModule();

        #region 教室管理
        /// <summary>
        /// 获取教室页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetClassRoomPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_Class_Room_ViewModel()
            {
                Group_Id = base.UserInfo.Group_Id,
                School_Id = base.ParmJObj.GetInt("School_Id"),
                RoomName = base.ParmJObj.GetStringValue("RoomName"),
                Status = DateStateEnum.Normal, //(DateStateEnum)base.ParmJObj.GetInt("Status"),
            };
            var list = classRoomModule.GetClassRoomPageListModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                if (item.CreateBy > 0)
                {
                    item.CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "";
                }
                if (item.UpdateBy > 0)
                {
                    item.UpdateByName = UserReidsCache.GetUserLoginInfo(item.UpdateBy)?.AccountName ?? "";
                }
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = list;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取教室列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetClassRoomList()
        {
            var query = new RB_Class_Room_ViewModel()
            {
                Group_Id = base.UserInfo.Group_Id,
                School_Id = base.ParmJObj.GetInt("School_Id"),
                RoomName = base.ParmJObj.GetStringValue("RoomName")
            };
            var list = classRoomModule.GetClassRoomListRepository(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 添加修改教室
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetClassRoom()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Class_Room_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateTime = DateTime.Now;
            extModel.CreateBy = UserInfo.Id;
            extModel.UpdateBy = UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            extModel.Group_Id = this.UserInfo.Group_Id;
            if (!string.IsNullOrWhiteSpace(extModel.StartTime) && !string.IsNullOrWhiteSpace(extModel.EndTime))
            {
                DateTime startTime = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy-MM-dd " + extModel.StartTime + ":00"));
                DateTime endTime = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy-MM-dd " + extModel.EndTime + ":00"));
                extModel.TotalHour = Convert.ToDecimal((endTime - startTime).TotalMinutes);
            }
            if (extModel.RoomPicList != null && extModel.RoomPicList.Any())
            {
                extModel.RoomPic = Common.Plugin.JsonHelper.Serialize(extModel.RoomPicList);
            }
            else
            {
                extModel.RoomPic = "";
            }
            bool flag = classRoomModule.SetClassRoomModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取教室实体
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetClassRoom()
        {
            var RoomId = base.ParmJObj.GetInt("RoomId", 0);
            var extModel = classRoomModule.GetClassRoomModule(RoomId);
            if (!string.IsNullOrWhiteSpace(extModel.RoomPic))
            {
                extModel.RoomPicList = Common.Plugin.JsonHelper.DeserializeObject<List<string>>(extModel.RoomPic);
            }
            else
            {
                extModel.RoomPicList = new List<string>();
            }
            extModel.EndTime ??= "21:30";
            extModel.StartTime ??= "09:30";
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 删除教室
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveClassRoom()
        {
            var RoomId = base.ParmJObj.GetInt("RoomId", 0);
            var Status = base.ParmJObj.GetInt("Status");
            var flag = classRoomModule.RemoveClassRoomModule(RoomId, Status);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion


        #region 教室使用记录


        /// <summary>
        /// 获取教室列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetUseClassRoomList()
        {
            var query = new RB_Class_Room_ViewModel()
            {
                Group_Id = base.UserInfo.Group_Id,
                School_Id = -1,
            };
            var list = classRoomModule.GetClassRoomList(query);//获取教室信息

            list.ForEach(x => x.RoomPicList = !string.IsNullOrWhiteSpace(x.RoomPic) ? Common.Plugin.JsonHelper.DeserializeObject<List<string>>(x.RoomPic) : new List<string>());

            var classTimeList = classModule.GetClassTimeList(new RB_Class_Time_ViewModel
            {
                Group_Id = base.UserInfo.Group_Id,
                School_Id = -1,
            });


            classTimeList.ForEach(x => x.NewPlanDateTime = System.DateTime.Now.ToString("yyyy-MM-dd ") + x.StartTime);
            classTimeList.ForEach(x => x.NewEndPlanDateTime = System.DateTime.Now.ToString("yyyy-MM-dd ") + x.EndTime);
            classTimeList.ForEach(x => x.SuiPaiList = !string.IsNullOrWhiteSpace(x.SuiPai) ? Common.Plugin.JsonHelper.DeserializeObject<List<string>>(x.SuiPai) : new List<string>());
            List<object> resultList = new List<object>();

            foreach (var item in list.GroupBy(x => x.School_Id))
            {
                List<object> roomList = new List<object>();

                var classRoomList = list.Where(x => x.School_Id == item.Key).ToList();
                foreach (var roomItem in classRoomList)
                {
                    decimal TotalMinutes = 0;
                    int UseState = 0;
                    var timeList = classTimeList.Where(x => x.ClassRoomId == roomItem.RoomId).ToList();
                    if (roomItem.TotalHour > 0)
                    {
                        foreach (var itemTime in timeList)
                        {
                            TotalMinutes += Convert.ToDecimal(Convert.ToDateTime(itemTime.EndTime).Subtract(Convert.ToDateTime(itemTime.StartTime)).TotalMinutes);
                            if (UseState == 0)
                            {
                                if (Convert.ToDateTime(itemTime.NewPlanDateTime) <= System.DateTime.Now && Convert.ToDateTime(itemTime.NewEndPlanDateTime) >= System.DateTime.Now)
                                {
                                    UseState = 1;
                                }
                            }
                        }
                    }

                    roomList.Add(new
                    {
                        roomItem.RoomId,
                        roomItem.RoomName,
                        UserRate = ((roomItem.TotalHour > 0) ? ((TotalMinutes / roomItem.TotalHour) * 100) : 0).ToString("f2"),//使用率
                        roomItem.RoomPic,
                        roomItem.RoomPicList,
                        UseState,
                        TimeList = (timeList != null && timeList.Any()) ? timeList.OrderBy(x => Convert.ToDateTime(x.NewPlanDateTime)).ToList() : new List<RB_Class_Time_ViewModel>(),
                    });
                }
                resultList.Add(new
                {

                    item.Key,
                    SchoolName = classRoomList.FirstOrDefault()?.SchoolName ?? "",
                    RoomList = roomList
                });
            }

            return ApiResult.Success(data: resultList);
        }




        /// <summary>
        /// 教室使用记录
        /// </summary>
        /// <returns></returns>
        public ApiResult GetNewClassRoomTimeList()
        {
            var startDate = base.ParmJObj.GetStringValue("StartTime");
            var endDate = base.ParmJObj.GetStringValue("EndTime");
            DateTime now = DateTime.Now;

            //获取当前月的第一天
            DateTime d1 = new DateTime(now.Year, now.Month, 1);
            //当月最后一天
            DateTime d2 = d1.AddMonths(1).AddDays(-1);
            if (string.IsNullOrEmpty(startDate))
            {
                startDate = Common.ConvertHelper.FormatDate(d1);
            }
            if (string.IsNullOrEmpty(endDate))
            {
                endDate = Common.ConvertHelper.FormatDate(d2);
            }
            else
            {

            }
            RB_Class_Plan_ViewModel query = new RB_Class_Plan_ViewModel()
            {
                StartTime = startDate,
                EndTime = endDate,
                TeacherId = base.ParmJObj.GetInt("TeacherId"),
                ClassRoomId = base.ParmJObj.GetInt("ClassRoomId"),
                Group_Id = base.UserInfo.Group_Id,
                School_Id = base.ParmJObj.GetInt("SchoolId", -1),
                ClassId = base.ParmJObj.GetInt("ClassId"),
            };
            //课程类型(0-全部,1-正常,2-试听课)
            int ClassType = base.ParmJObj.GetInt("ClassType");
            var obj = classModule.GetClassUseStatisticalModule(query, ClassType: ClassType);
            return ApiResult.Success(data: obj);
        }


        /// <summary>
        /// 教室使用记录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetClassRoomTimeList()
        {
            var RoomId = base.ParmJObj.GetInt("RoomId", 0);
            var SchoolId = base.ParmJObj.GetInt("SchoolId", 0);
            var StartTime = base.ParmJObj.GetStringValue("StartTime");
            var EndTime = base.ParmJObj.GetStringValue("EndTime");

            var roomList = classModule.GetClassPlanList_V2(new RB_Class_Plan_ViewModel { Group_Id = base.UserInfo.Group_Id, ClassRoomId = RoomId, StartTime = StartTime, EndTime = EndTime, School_Id = SchoolId });
            //查询所有课程的上课时间
            List<RB_Class_Time_ViewModel> timeList = new List<RB_Class_Time_ViewModel>();
            List<RB_Class_RoomUseLog_ViewModel> roomUserLogList = new List<RB_Class_RoomUseLog_ViewModel>();
            List<Model.ViewModel.Question.RB_Question_RoomUseLog_ViewModel> resultList = new List<Model.ViewModel.Question.RB_Question_RoomUseLog_ViewModel>();
            if (roomList != null && roomList.Any())
            {
                string roomIds = string.Join(",", roomList.Select(x => x.ClassRoomId));
                roomUserLogList = classRoomModule.GetClassRoomUseLogListRepository(new RB_Class_RoomUseLog_ViewModel { StartTime = StartTime, EndTime = EndTime, QRoomIds = roomIds });
                string pids = string.Join(",", roomList.Select(x => x.ClassPlanId));
                timeList = classModule.GetClassTimeListModule(new RB_Class_Time_ViewModel() { Group_Id = base.UserInfo.Group_Id, QClassPlanIds = pids });
                timeList.ForEach(x => x.NewPlanDateTime = System.DateTime.Now.ToString("yyyy-MM-dd ") + x.StartTime);
                timeList.ForEach(x => x.NewEndPlanDateTime = System.DateTime.Now.ToString("yyyy-MM-dd ") + x.EndTime);
                foreach (var item in roomUserLogList)
                {
                    int IntervalNum = 0;
                    if (item.UpdateTime.HasValue)
                    {
                        IntervalNum = (int)Math.Floor((item.UpdateTime.Value.Subtract(item.CreateTime).TotalMinutes) / 15);
                    }

                    resultList.Add(new Model.ViewModel.Question.RB_Question_RoomUseLog_ViewModel
                    {
                        Id = item.RoomErrorId,
                        Title = item.ErrorTitle,
                        Content = item.ErrorContent,
                        StartTime = item.CreateTime.ToString("HH:mm"),
                        EndTime = item.UpdateTime.HasValue ? item.UpdateTime.Value.ToString("HH:mm") : "未知",
                        InfoTime = item.CreateTime,
                        Type = item.ErrorType == 0 ? 1 : 2,
                        ClassStatus = -1,
                        IntervalNum = IntervalNum,
                    });
                }
                foreach (var item in timeList)
                {
                    int IntervalNum = 0;
                    if (!string.IsNullOrWhiteSpace(item.NewPlanDateTime) && !string.IsNullOrWhiteSpace(item.NewEndPlanDateTime))
                    {
                        IntervalNum = (int)Math.Floor((Convert.ToDateTime(item.NewEndPlanDateTime).Subtract(Convert.ToDateTime(item.NewPlanDateTime)).TotalMinutes) / 15);
                    }

                    var planModel = roomList.Where(x => x.ClassPlanId == item.ClassPlanId).FirstOrDefault();
                    if (planModel != null && planModel.ClassPlanId > 0)
                    {
                        var roomModel = roomList.Where(x => x.ClassPlanId == item.ClassPlanId).FirstOrDefault();
                        resultList.Add(new Model.ViewModel.Question.RB_Question_RoomUseLog_ViewModel
                        {
                            Id = item.ClassTimeId,
                            Title = roomModel?.ClassName,
                            Content = roomModel?.TeacherName,
                            StartTime = item.StartTime,
                            EndTime = item.EndTime,
                            InfoTime = Convert.ToDateTime(planModel.ClassDate.ToString("yyyy-MM-dd ") + item.StartTime),
                            Type = 0,
                            ClassStatus = item.ClassStatus,
                            IntervalNum = IntervalNum,
                            CourseName = roomModel?.CourseName,
                            CompleteProgress = roomModel?.CompleteProgress ?? 0,
                            StudentName = (roomModel != null && roomModel.GuestList != null && roomModel.GuestList.Any()) ? string.Join(",", roomModel.GuestList.Select(x => x.GuestName)) : ""

                        });
                    }
                }
            }

            List<object> list = new List<object>();
            DateTime startTime = Convert.ToDateTime(StartTime);
            DateTime endTime = Convert.ToDateTime(EndTime);
            TimeSpan sp = endTime.Subtract(startTime);

            for (int i = 0; i <= sp.Days; i++)
            {
                var nowDate = startTime.AddDays(i);
                list.Add(new
                {
                    Datestr = nowDate.ToString("yyyy-MM-dd"),
                    DateYear = nowDate.ToString("yyyy"),
                    DateMonth = nowDate.ToString("MM"),
                    DateDay = nowDate.ToString("dd"),
                    InfoList = resultList.Where(x => x.InfoTime.ToString("yyyy-MM-dd") == nowDate.ToString("yyyy-MM-dd")).OrderBy(x => x.InfoTime)
                });
            }
            return ApiResult.Success("", list);
        }





        /// <summary>
        /// 获取教室列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSchoolAndRoomList()
        {
            var query = new RB_Class_Room_ViewModel()
            {
                Group_Id = base.UserInfo.Group_Id,
                School_Id = -1,
            };
            var list = classRoomModule.GetClassRoomList(query);//获取教室信息

            list.ForEach(x => x.RoomPicList = !string.IsNullOrWhiteSpace(x.RoomPic) ? Common.Plugin.JsonHelper.DeserializeObject<List<string>>(x.RoomPic) : new List<string>());
            List<object> resultList = new List<object>();
            foreach (var item in list.GroupBy(x => x.School_Id))
            {
                List<object> roomList = new List<object>();
                var classRoomList = list.Where(x => x.School_Id == item.Key).ToList();
                foreach (var roomItem in classRoomList)
                {

                    roomList.Add(new
                    {
                        roomItem.RoomId,
                        roomItem.RoomName,
                        roomItem.RoomPic,
                        roomItem.RoomPicList
                    });
                }
                resultList.Add(new
                {

                    item.Key,
                    SchoolName = classRoomList.FirstOrDefault()?.SchoolName ?? "",
                    RoomList = roomList
                });
            }
            return ApiResult.Success(data: resultList);
        }

        /// <summary>
        /// 获取教室使用记录详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetRoomUseLogDetails()
        {

            var ID = base.ParmJObj.GetInt("ID", 0);
            var model = classRoomModule.GetRoomUseLogEntity(ID);
            if (model == null || model.RoomErrorId == 0)
            {
                return ApiResult.Failed("数据不存在");
            }
            else
            {
                List<string> ParticipantsList = new List<string>();
                List<string> LogPicList = new List<string>();
                if (!string.IsNullOrWhiteSpace(model.Participants))
                {
                    ParticipantsList = Common.Plugin.JsonHelper.DeserializeObject<List<string>>(model.Participants);
                }

                if (!string.IsNullOrWhiteSpace(model.LogPic))
                {
                    LogPicList = Common.Plugin.JsonHelper.DeserializeObject<List<string>>(model.LogPic);
                }
                var result = new
                {
                    model.RoomErrorId,
                    ManageName = model.ManageName ?? "",
                    model.ErrorContent,
                    model.ErrorTitle,
                    LogPicList,
                    ParticipantsList,
                    StartTime = model.CreateTime.ToString("yyyy年MM月dd日 HH:mm"),
                    EndTime = model.UpdateTime.HasValue ? model.UpdateTime.Value.ToString("yyyy年MM月dd日 HH:mm") : "未知",
                };
                return ApiResult.Success("", result);
            }
        }

        /// <summary>
        /// 获取上课详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetTimeDetaild()
        {
            var ID = base.ParmJObj.GetInt("ID", 0);
            var timeModel = classModule.GetClassTimeList_V2(new RB_Class_Time_ViewModel { ClassTimeId = ID, Group_Id = base.UserInfo.Group_Id }).FirstOrDefault();
            if (timeModel == null || timeModel.ClassTimeId == 0)
            {
                return ApiResult.Failed("班级上课时间信息不存在");
            }
            var classModel = classModule.GetClassAndCourseListRepository(new RB_Class_ViewModel()
            {
                Group_Id = base.UserInfo.Group_Id,
                School_Id = -1,
                ClassId = timeModel.ClassId
            }).FirstOrDefault();
            if (classModel == null || classModel.ClassId == 0)
            {
                return ApiResult.Failed("班级信息不存在");
            }

            string ActualTime = string.Empty;
            if (timeModel.InClassTime.HasValue && timeModel.ClassOverTime.HasValue)
            {
                ActualTime = timeModel.InClassTime.Value.ToString("HH:mm") + "-" + timeModel.ClassOverTime.Value.ToString("HH:mm");
            }
            else if (timeModel.InClassTime.HasValue)
            {
                ActualTime = timeModel.InClassTime.Value.ToString("HH:mm") + "-";
            }
            else if (timeModel.ClassOverTime.HasValue)
            {
                ActualTime = "-" + timeModel.ClassOverTime.Value.ToString("HH:mm");
            }


            var orderGuestList = classModule.GetOrderGuestListModule(new RB_Order_Guest_ViewModel { Group_Id = base.UserInfo.Group_Id, School_Id = 0, ClassId = classModel.ClassId });
            var checkLogList = classModule.GetClassCheckList(new RB_Class_Check_ViewModel { Group_Id = base.UserInfo.Group_Id, School_Id = -1, ClassId = classModel.ClassId, StartDate = timeModel.NewPlanDateTime, EndDate = timeModel.NewPlanDateTime });
            foreach (var item in orderGuestList.Where(x => x.GuestState == 1 || x.GuestState == 6 || ((x.GuestState == 5||x.GuestState==7) && x.ChangeEffectTime.HasValue && Convert.ToDateTime(Common.ConvertHelper.FormatDate(x.ChangeEffectTime)) >= Convert.ToDateTime(Common.ConvertHelper.FormatDate(timeModel.NewPlanDateTime)))))
            {
                item.IsCheck = (checkLogList != null && checkLogList.Any()) ? checkLogList.Where(y => y.OrderGuestId == item.Id).FirstOrDefault()?.CheckStatus ?? 2 : 2;
            }
            var result = new
            {
                classModel.ClassName,
                classModel.TeacherName,
                classModel.CourseName,
                timeModel.VideoUrl,//课堂录像
                PlanTime = timeModel.StartTime + "-" + timeModel.EndTime,//计划时间
                ActualTime,//实际时间
                AttendanceNum = (checkLogList != null && checkLogList.Any()) ? checkLogList.Where(x => x.CheckStatus == 0).Count() : 0,///出勤人数
                timeModel.ClassStatus,//上课状态
                GuestList = orderGuestList.OrderBy(x => x.IsCheck).Select(x => new
                {
                    x.GuestName,
                    x.Mobile,
                    x.CourseName,
                    x.TotalHours,
                    x.CompleteHours,
                    SurplusHours = (x.TotalHours - x.CompleteHours),//剩余课时
                    x.IsCheck,//0-正常，1-缺勤，2-未签到
                    x.Id
                })
            };

            return ApiResult.Success("", result);
        }

        #endregion



        #region  甲鹤上课管理系统- 教室注册

        /// <summary>
        /// 获取教室列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HttpPost]
        [Microsoft.AspNetCore.Authorization.AllowAnonymous]
        public ApiResult GetDropClassRoomList()
        {
            var query = new RB_Class_Room_ViewModel()
            {
                Group_Id = 100000,
                Status = DateStateEnum.Normal,
            };
            var list = classRoomModule.GetClassRoomListRepository(query);
            var result = list.Select(x => new
            {
                x.School_Id,
                x.RoomId,
                x.RoomName,
                RoomPicList = string.IsNullOrWhiteSpace(x.RoomPic) ? new List<string>() : JsonHelper.DeserializeObject<List<string>>(x.RoomPic),
                x.SeatNum,
                x.Group_Id

            });
            return ApiResult.Success(data: result);
        }


        /// <summary>
        /// 添加修改教室
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Microsoft.AspNetCore.Authorization.AllowAnonymous]
        public ApiResult SetNoLoginClassRoom()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Class_Room_ViewModel>(RequestParm.Msg.ToString());
            if (extModel.RoomId == 0)
            {
                extModel.CreateTime = DateTime.Now;
                extModel.CreateBy = 1;
            }
            extModel.UpdateBy = 1;
            extModel.UpdateTime = DateTime.Now;
            extModel.Group_Id = extModel.Group_Id == 0 ? 100000 : extModel.Group_Id;
            if (extModel.RoomPicList != null && extModel.RoomPicList.Any())
            {
                extModel.RoomPic = Common.Plugin.JsonHelper.Serialize(extModel.RoomPicList);
            }
            else
            {
                extModel.RoomPic = "";
            }

            if (!string.IsNullOrWhiteSpace(extModel.StartTime) && !string.IsNullOrWhiteSpace(extModel.EndTime))
            {
                DateTime startTime = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy-MM-dd " + extModel.StartTime + ":00"));
                DateTime endTime = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy-MM-dd " + extModel.EndTime + ":00"));
                extModel.TotalHour = Convert.ToDecimal((endTime - startTime).TotalMinutes);
            }
            bool flag = classRoomModule.SetClassRoomModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }




        /// <summary>
        /// 添加修改教室异常
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Microsoft.AspNetCore.Authorization.AllowAnonymous]
        public ApiResult SetClassRoomUseLog()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Class_RoomUseLog_ViewModel>(RequestParm.Msg.ToString());
            if (extModel.RoomErrorId == 0)
            {
                extModel.CreateTime = DateTime.Now;
                if (UserInfo == null || UserInfo.Id == 0)
                {
                    extModel.CreateBy = 1;
                }
                else
                {
                    extModel.CreateBy = UserInfo.Id;
                }

                if (extModel.ParticipantsList != null && extModel.ParticipantsList.Any())
                {
                    extModel.Participants = Common.Plugin.JsonHelper.Serialize(extModel.ParticipantsList);
                }
                else
                {
                    extModel.Participants = "";
                }
            }
            else
            {
                var model = classRoomModule.GetRoomUseLogEntity(extModel.RoomErrorId);
                List<string> ParticipantsList = new List<string>();
                List<string> LogPicList = new List<string>();
                if (!string.IsNullOrWhiteSpace(model.Participants))
                {
                    ParticipantsList = Common.Plugin.JsonHelper.DeserializeObject<List<string>>(model.Participants);
                }
                else
                {
                    ParticipantsList = new List<string>();
                }

                if (ParticipantsList != null && ParticipantsList.Any())
                {
                    extModel.ParticipantsList.AddRange(ParticipantsList);
                }
                if (extModel.ParticipantsList != null && extModel.ParticipantsList.Any())
                {
                    extModel.Participants = Common.Plugin.JsonHelper.Serialize(extModel.ParticipantsList);
                }
                else
                {
                    extModel.Participants = "";
                }
            }
            if (extModel.LogPicList != null && extModel.LogPicList.Any())
            {
                extModel.LogPic = Common.Plugin.JsonHelper.Serialize(extModel.LogPicList);
            }
            else
            {
                extModel.LogPic = "";
            }
            extModel.Group_Id = extModel.Group_Id == 0 ? 100000 : extModel.Group_Id;
            int Id = classRoomModule.SetRoomUseLog(extModel);
            return Id > 0 ? ApiResult.Success("", new { Id }) : ApiResult.Failed("操作失败");
        }
        #endregion
    }
}
