Exemplo n.º 1
0
        public bool AddOrEditTrain(TrainModel model, UserTokenModel curUser)
        {
            if (model.Id > 0)
            {
                goto editLogic;
            }

            var ue = model.Adapt <Train>();

            ue.AddUserId  = curUser.UserId;
            ue.EditUserId = curUser.UserId;
            ue.AddTime    = DateTime.Now;
            ue.EditTime   = DateTime.Now;
            db.Set <Train>().Add(ue);
            db.SaveChanges();

            model.TeacherIds.ForEach(t =>
            {
                var te        = new TrainTeacher();
                te.AddUserId  = curUser.UserId;
                te.EditUserId = curUser.UserId;
                te.AddTime    = DateTime.Now;
                te.EditTime   = DateTime.Now;
                te.IsValid    = true;
                te.TrainId    = ue.Id;
                te.UserId     = t;
                db.Set <TrainTeacher>().Add(te);
            });
            db.SaveChanges();

            var classUsers = db.Set <ClassUser>().Where(t => t.IsValid && t.ClassId == model.ClassId).ToList();

            classUsers.ForEach(t =>
            {
                var te        = new TrainLearner();
                te.AddUserId  = curUser.UserId;
                te.EditUserId = curUser.UserId;
                te.AddTime    = DateTime.Now;
                te.EditTime   = DateTime.Now;
                te.IsValid    = true;
                te.TrainId    = ue.Id;
                te.UserId     = t.UserId;
                te.Remark     = "";
                db.Set <TrainLearner>().Add(te);
            });
            db.SaveChanges();

            return(true);

editLogic:

            var exist = db.Set <Train>().Find(model.Id);

            exist.StartTime       = model.StartTime;
            exist.EndTime         = model.EndTime;
            exist.ClassId         = model.ClassId;
            exist.CourseCode      = model.CourseCode;
            exist.Remark          = model.Remark;
            exist.EditTime        = DateTime.Now;
            exist.EditUserId      = curUser.UserId;
            exist.IsValid         = model.IsValid;
            db.Entry(exist).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();

            var existTrainTeachers = db.Set <TrainTeacher>().Where(t => t.IsValid && t.TrainId == exist.Id).ToList();

            existTrainTeachers.ForEach(t =>
            {
                t.IsValid         = false;
                db.Entry(t).State = System.Data.Entity.EntityState.Modified;
            });
            db.SaveChanges();

            model.TeacherIds.ForEach(t =>
            {
                var te        = new TrainTeacher();
                te.AddUserId  = curUser.UserId;
                te.EditUserId = curUser.UserId;
                te.AddTime    = DateTime.Now;
                te.EditTime   = DateTime.Now;
                te.IsValid    = true;
                te.TrainId    = exist.Id;
                te.UserId     = t;
                db.Set <TrainTeacher>().Add(te);
            });
            db.SaveChanges();

            var existTrainLearners = db.Set <TrainLearner>().Where(t => t.IsValid && t.TrainId == exist.Id).ToList();

            existTrainLearners.ForEach(t =>
            {
                t.IsValid         = false;
                db.Entry(t).State = System.Data.Entity.EntityState.Modified;
            });
            db.SaveChanges();

            var classUserse = db.Set <ClassUser>().Where(t => t.IsValid && t.ClassId == model.ClassId).ToList();

            classUserse.ForEach(t =>
            {
                var te        = new TrainLearner();
                te.AddUserId  = curUser.UserId;
                te.EditUserId = curUser.UserId;
                te.AddTime    = DateTime.Now;
                te.EditTime   = DateTime.Now;
                te.IsValid    = true;
                te.TrainId    = exist.Id;
                te.UserId     = t.UserId;
                te.Remark     = "";
                db.Set <TrainLearner>().Add(te);
            });
            db.SaveChanges();

            return(true);
        }