示例#1
0
        public ApiResult <ClassSchedule> CloseClass(int classScheduleId)
        {
            ApiResult <ClassSchedule> r  = new ApiResult <ClassSchedule>();
            ClassSchedule             cs = RCSContext.ClassSchedules.Where(i => i.Id == classScheduleId).FirstOrDefault();

            if (cs == null)
            {
                r.Messages.Add("Lớp học không tồn tại.");
                return(r);
            }
            if (cs.Status == (int)EClassStatus.Schedule)
            {
                r.Messages.Add("Lớp học chưa mở.");
                return(r);
            }
            if (cs.Status == (int)EClassStatus.Closed)
            {
                r.Messages.Add("Lớp học đã kết thúc.");
                return(r);
            }
            cs.Status      = (int)EClassStatus.Closed;
            cs.EndDatetime = DateTime.Now;
            RCSContext.SaveChanges();
            r.IsSuccess = true;
            r.Data      = cs;
            r.Messages.Add("Kết thúc lớp học thành công.");
            return(r);
        }
示例#2
0
        public ApiResult <List <User> > CreateAccount(List <UserCreateModel> newUsers)
        {
            var apiResult = new ApiResult <List <User> >();
            var users     = new List <User>();

            newUsers.ForEach(u =>
            {
                users.Add(new User()
                {
                    Password = parseToMD5Byte(u.Password),
                    Username = u.Username,
                    Role     = u.Role,
                    IsActive = true,
                });
            });
            RCSContext.Users.AddRange(users);
            try
            {
                RCSContext.SaveChanges();
                apiResult.IsSuccess = true;
            }
            catch (Exception e)
            {
                apiResult.IsSuccess = false;
            }
            return(apiResult);
        }
示例#3
0
        public ApiResult <ClassSchedule> UpdateClassSchedule(ClassSchedule classSchedule)
        {
            var r   = new ApiResult <ClassSchedule>();
            var cs1 = RCSContext.ClassSchedules.Where(i => i.Id == classSchedule.Id).FirstOrDefault();

            if (cs1 == null)
            {
                r.IsSuccess = false;
                r.Messages.Add("Không tìm thấy lớp học.");
            }
            else
            {
            }
            cs1.Status   = classSchedule.Status;
            cs1.Datetime = classSchedule.Datetime;
            try
            {
                RCSContext.SaveChanges();
            }
            catch (Exception e) { r.Messages.Add(e.Message); return(r); }
            var temp = (from cs in RCSContext.ClassSchedules
                        where cs.Id == classSchedule.Id
                        join c in RCSContext.Classes on cs.ClassId equals c.Id into c1
                        from c in c1.DefaultIfEmpty()
                        join s in RCSContext.Studyings on c.Id equals s.ClassId into s1
                        from s in s1.DefaultIfEmpty()
                        join sj in RCSContext.Subjects on c.SubjectId equals sj.Id into sj1
                        from sj in sj1.DefaultIfEmpty()
                        join rc in RCSContext.RollCalls on cs.Id equals rc.ClassScheduleId into rc1
                        from rc in rc1.DefaultIfEmpty()
                        join src in RCSContext.Students on rc.StudentId equals src.Id into src1
                        from src in src1.DefaultIfEmpty()
                        join ss in RCSContext.Students on s.StudentId equals ss.Id into ss1
                        from ss in ss1.DefaultIfEmpty()
                        select new { c, s, sj, cs, rc, src, ss }
                        ).ToList();
            var cs2 = temp.Select(t => t.cs).Distinct().FirstOrDefault();

            cs2.Class.ClassSchedules  = null;
            cs2.Class.Subject.Classes = null;
            cs2.Class.Studyings.ForEach(s =>
            {
                s.Class             = null;
                s.Student.Studyings = null;
            });
            cs2.RollCalls.ForEach(rc =>
            {
                rc.ClassSchedule     = null;
                rc.Student.RollCalls = null;
            });
            r.Data      = cs2;
            r.IsSuccess = true;
            return(r);
        }
示例#4
0
        public ApiResult <int> DeleteFile(int fileId)
        {
            var ar            = new ApiResult <int>();
            var trainingImage = RCSContext.TrainingImages.Where(t => t.FileId == fileId).FirstOrDefault();

            if (trainingImage == null)
            {
                ar.Data      = fileId;
                ar.IsSuccess = false;
                ar.Messages.Add("Không tìm thấy file.");
                return(ar);
            }
            ar.Data = fileId;
            RCSContext.TrainingImages.Remove(trainingImage);
            RCSContext.SaveChanges();
            DetectService.RemoveTrainsByImageId(trainingImage.FileId);
            ar.IsSuccess = true;
            ar.Messages.Add("Xóa file thành công.");
            return(ar);
        }
示例#5
0
        private TrainingImage saveImageTraining(int studentId, string base64Image, Rectangle r)
        {
            var image  = base64Image.base64ToImage();
            var bitmap = new Bitmap(image);

            bitmap = bitmap.cropAtRect(r);
            bitmap = new Bitmap(bitmap, new Size(100, 100));
            var trainingImage = createTrainImage(studentId, bitmap);

            RCSContext.TrainingImages.Add(trainingImage);
            try
            {
                RCSContext.SaveChanges();
            }
            catch (Exception e)
            {
                var a = 1;
            }
            return(trainingImage);
        }
示例#6
0
        public ApiResult <RollCall> ChangeRollCall(int classScheduleId, int studentId)
        {
            var r             = new ApiResult <RollCall>();
            var classSchedule = RCSContext.ClassSchedules.Where(cs => cs.Id == classScheduleId).FirstOrDefault();

            if (classSchedule == null)
            {
                r.Messages.Add("Không tìm thấy lớp học.");
                return(r);
            }
            if (classSchedule.Status == (int)EClassStatus.Schedule)
            {
                r.Messages.Add("Lớp chưa mở, bạn không thể điểm danh vào lúc này.");
                return(r);
            }
            var studying = RCSContext.Studyings.Where(s => s.ClassId == classSchedule.ClassId && s.StudentId == studentId).FirstOrDefault();

            if (studying == null)
            {
                r.Messages.Add("Học sinh không thuộc lớp học này.");
                return(r);
            }
            var rollCall = RCSContext.RollCalls.Where(rc => rc.StudentId == studentId && rc.ClassScheduleId == classScheduleId).FirstOrDefault();

            if (rollCall == null)
            {
                rollCall = new RollCall
                {
                    ClassScheduleId = classScheduleId,
                    StudentId       = studentId,
                    CreatedDate     = DateTime.Now,
                    Type            = (int)ERollCallType.Manually,
                    IsActive        = true
                };
                RCSContext.RollCalls.Add(rollCall);
            }
            else
            {
                rollCall.IsActive    = !rollCall.IsActive;
                rollCall.CreatedDate = DateTime.Now;
                rollCall.Type        = (int)ERollCallType.Manually;
            }
            try
            {
                RCSContext.SaveChanges();
                if (rollCall.IsActive == true)
                {
                    r.Messages.Add("Điểm danh thành công.");
                }
                else
                {
                    r.Messages.Add("Hủy điểm danh thành công");
                }
            }
            catch (Exception e)
            {
                r.Messages.Add(e.Message);
                return(r);
            }
            var rl = (from rc in RCSContext.RollCalls
                      where (rc.ClassScheduleId == classScheduleId && rc.StudentId == studentId)
                      join st in RCSContext.Students on rc.StudentId equals st.Id into st1
                      from st in st1.DefaultIfEmpty()
                      select new { rc, st }).FirstOrDefault().rc;

            rl.Student.RollCalls      = null;
            rl.Student.Studyings      = null;
            rl.ClassSchedule          = null;
            rl.Student.TrainingImages = null;
            r.IsSuccess = true;
            r.Data      = rl;
            return(r);
        }
示例#7
0
        public ApiResult <RollCallReponseModel> RollCall(RollCallModel model)
        {
            var r = new ApiResult <RollCallReponseModel>();

            r.Data             = new RollCallReponseModel();
            r.Data.Base64Image = model.Base64Image;
            var rollCallData  = new ApiResult <List <RollCall> >();
            var classSchedule = RCSContext.ClassSchedules.Where(cs => cs.Id == model.ClassScheduleId).FirstOrDefault();

            //check thông tin lớp học
            if (classSchedule == null)
            {
                r.Messages.Add("Lớp học không tồn tại.");
                return(r);
            }
            if (classSchedule.Status == (int)EClassStatus.Schedule)
            {
                r.Messages.Add("Lớp học chưa mở.");
                return(r);
            }
            if (classSchedule.Status == (int)EClassStatus.Closed)
            {
                r.Messages.Add("Lớp học đã kết thúc");
                return(r);
            }
            var imageGray      = model.Base64Image.base64ToImageGray();
            var image          = model.Base64Image.base64ToImage();
            var bitmap         = new Bitmap(image);
            var bitmapResponse = new Bitmap(bitmap);

            imageGray._EqualizeHist();
            var rectangles = DetectService.DetectFaceRollCall(imageGray);

            if (rectangles.Count() == 0)
            {
                r.Messages.Add("Không có sinh viên nào trong ảnh.");
                return(r);
            }
            var classId      = RCSContext.ClassSchedules.Where(cs => cs.Id == model.ClassScheduleId).FirstOrDefault().ClassId;
            var studyings    = RCSContext.Studyings.Where(st => st.ClassId == classId).ToList();
            var newRollCalls = new List <RollCall>();
            var oldRollCalls = new List <RollCall>();

            //Detect khuôn mặt
            rectangles.ForEach(rec =>
            {
                var face      = bitmap.cropAtRect(rec);
                var faceGray  = new Image <Gray, byte>(face);
                var studentId = DetectService.RecognizeFace(faceGray);
                if (studentId == -1)
                {
                    var faceGrayFlip = faceGray.Flip(FlipType.Horizontal);
                    studentId        = DetectService.RecognizeFace(faceGrayFlip);
                }
                if (studentId != -1)
                {
                    if (studyings.Any(st => st.StudentId == studentId))
                    {
                        bitmapResponse = drawFaceAndNoteOnBitmap(bitmapResponse, rec, studentId.ToString(), Color.Green);
                        var rc         = RCSContext.RollCalls.Where(i => i.StudentId == studentId && i.ClassScheduleId == model.ClassScheduleId).FirstOrDefault();
                        var fileData   = new FileData
                        {
                            Data = face.bitmapToByteArr(),
                        };
                        var fileAttachment = new FileAttachment
                        {
                            CreateDate = DateTime.Now,
                            Height     = face.Height,
                            Width      = face.Width,
                            Extension  = "jpg",
                            Type       = (int)EFileType.ImageJPG,
                            Name       = Guid.NewGuid().ToString(),
                            FileData   = fileData
                        };
                        if (rc == null)
                        {
                            var rollCall = new RollCall();
                            rollCall     = new RollCall
                            {
                                ClassScheduleId = model.ClassScheduleId,
                                StudentId       = studentId,
                                CreatedDate     = DateTime.Now,
                                Type            = (int)ERollCallType.Auto,
                                IsActive        = true
                            };
                            newRollCalls.Add(rollCall);
                        }
                        else
                        {
                            rc.IsActive       = true;
                            rc.CreatedDate    = DateTime.Now;
                            rc.FileAttachment = fileAttachment;
                            oldRollCalls.Add(rc);
                        }
                    }
                }
                else
                {
                    bitmapResponse = drawFaceAndNoteOnBitmap(bitmapResponse, rec, "unknown", Color.Red);
                }
            });
            if (newRollCalls.Count == 0 && oldRollCalls.Count == 0)
            {
                r.Messages.Add("Các sinh viên không thuộc lớp này.");
                r.Data.Base64Image = Convert.ToBase64String(bitmapResponse.bitmapToByteArr());
                return(r);
            }
            RCSContext.RollCalls.AddRange(newRollCalls);
            try
            {
                RCSContext.SaveChanges();
            }
            catch (Exception e) { r.Messages.Add(e.Message); return(r); }
            newRollCalls.AddRange(oldRollCalls);
            newRollCalls.ForEach(i =>
            {
                i.ClassSchedule.RollCalls = null;
                i.FileAttachment          = null;
                i.Student = null;
                i.Image   = null;
            });
            r.Messages.Add("Điểm danh thành công.");
            r.IsSuccess        = true;
            r.Data.Base64Image = Convert.ToBase64String(bitmapResponse.bitmapToByteArr());
            r.Data.RollCalls   = newRollCalls;
            return(r);
        }
示例#8
0
 public BaseSevice()
 {
     RCSContext    = new RCSContext();
     DetectService = DetectService.Instance;
     md5Hash       = MD5.Create();
 }