public int AddClassToDB <T>(T entity) where T : class { //int random = new Random().Next(1000, 10000); //string time = _encryption.Time().ToString("s"); //string newTime = Regex.Replace(time, @"[^0-9]+", ""); //classRoom.ClassRoomId = string.Format("0{0}{1}", newTime, random); object obj = entity; int result = -1; if (typeof(T) == typeof(ClassRoom)) { ClassRoom classRoom = (ClassRoom)obj; classRoom.ClassRoomId = _encryption.SignMD5(string.Format("{0}{1}", classRoom.Grade, classRoom.ClassRoomName)).ToUpper(); classRoom.Remark = classRoom.Remark != null ? classRoom.Remark : "班级信息"; obj = classRoom; entity = (T)obj; } else if (typeof(T) == typeof(Students_Info)) { Students_Info studentsInfo = (Students_Info)obj; studentsInfo.Id = _encryption.SignMD5(string.Format("{0}{1}", studentsInfo.Name, studentsInfo.Phone)).ToUpper(); studentsInfo.Remark = studentsInfo.Remark != null ? studentsInfo.Remark : "学生信息"; studentsInfo.DateTime = _encryption.Time(); obj = studentsInfo; entity = (T)obj; } else if (typeof(T) == typeof(Teacher_Info)) { Teacher_Info teacherInfo = (Teacher_Info)obj; teacherInfo.Id = _encryption.SignMD5(string.Format("{0}{1}", teacherInfo.Name, teacherInfo.Phone)).ToUpper(); teacherInfo.Remark = teacherInfo.Remark != null ? teacherInfo.Remark : "教师信息"; teacherInfo.Password = _encryption.SignMD5(teacherInfo.Password).ToUpper(); teacherInfo.DateTime = _encryption.Time(); obj = teacherInfo; entity = (T)obj; } _loggerHelper.WriteInfoLog(entity); try { _MsSqlContext.Set <T>().Add(entity); if (typeof(T) == typeof(Students_Info)) { Students_Info stu = (Students_Info)obj; _MsSqlContext.Set <LastclassScore>().Add(new LastclassScore() { StudentsInfoId = stu.Id, ClassInfoId = _encryption.SignMD5(string.Format("{0}{1}", stu.Grade, stu.ClassName)).ToUpper(), StudentName = stu.Name }); } result = _MsSqlContext.SaveChanges(); } catch (Exception e) { _loggerHelper.WriteInfoLog(e.StackTrace); } _loggerHelper.WriteInfoLog(result); return(result); }
public IActionResult CreateStudent(Students_Info studentsInfo) { _loggerHelper.WriteInfoLog(studentsInfo.Name); object result = _create.AddClassToDB(studentsInfo); return(Content(result.ToString())); }