Ejemplo n.º 1
0
        public ActionResult Edit(int id = 0)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                var vm = new Models.SysMessage.Edit();

                if (id != 0)
                {
                    var tb = (from p in db.Table <Sys.Entity.tbSysMessage>()
                              where p.Id == id
                              select new Dto.SysMessage.Edit
                    {
                        Id = p.Id,
                        InputDate = p.InputDate,
                        IsEmail = p.IsEmail,
                        IsSms = p.IsSms,
                        MessageContent = p.MessageContent,
                        MessageTitle = p.MessageTitle,
                        SysUserId = p.tbSysUser.Id,
                        Url = p.Url,
                        IsPublic = p.IsPublic
                    }).FirstOrDefault();
                    if (tb != null)
                    {
                        if (!tb.IsPublic)
                        {
                            vm.RoleIds = string.Join(",", (from p in db.Table <Entity.tbSysMessageRole>() where p.tbSysMessage.Id == tb.Id select p.tbSysRole.Id).ToList());
                        }
                        vm.MessageEdit = tb;
                    }
                }
                vm.RoleList = SysRoleController.SelectList();
                return(View(vm));
            }
        }
Ejemplo n.º 2
0
        public ActionResult Edit(Models.SysMessage.Edit vm)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                var error = new List <string>();
                if (error.Count == decimal.Zero)
                {
                    if (vm.MessageEdit.Id == 0)
                    {
                        var tb = new Sys.Entity.tbSysMessage();
                        tb.MessageTitle = vm.MessageEdit.MessageTitle;
                        tb.tbProgram    = db.Set <Admin.Entity.tbProgram>().Find(Code.Common.ProgramId);
                        //tb.IsSms = vm.MessageEdit.IsSms;
                        //tb.IsEmail = vm.MessageEdit.IsEmail;
                        //tb.Url = vm.MessageEdit.Url;
                        tb.MessageContent = vm.MessageEdit.MessageContent;
                        tb.tbSysUser      = db.Set <Sys.Entity.tbSysUser>().Find(Code.Common.UserId);
                        tb.InputDate      = DateTime.Now;
                        tb.IsPublic       = vm.MessageEdit.IsPublic;
                        db.Set <Sys.Entity.tbSysMessage>().Add(tb);

                        if (!vm.MessageEdit.IsPublic)
                        {
                            var roleIds = vm.RoleIds.Split(',').ToList();
                            roleIds.RemoveAll(p => string.IsNullOrWhiteSpace(p));
                            if (roleIds != null || roleIds.Count > 0)
                            {
                                var tbSysRoleList    = (from p in db.Table <Entity.tbSysRole>() where roleIds.Contains(p.Id.ToString()) select p);
                                var tbSysMessageRole = vm.RoleIds.Split(',').Select(p => new Entity.tbSysMessageRole()
                                {
                                    tbSysMessage = tb,
                                    tbSysRole    = tbSysRoleList.First(r => r.Id.ToString() == p)
                                }).ToList();
                                db.Set <Entity.tbSysMessageRole>().AddRange(tbSysMessageRole);
                            }
                        }

                        if (db.SaveChanges() > 0)
                        {
                            SysUserLogController.Insert("添加消息");
                        }
                    }
                    else
                    {
                        var tb = (from p in db.Table <Sys.Entity.tbSysMessage>()
                                  where p.Id == vm.MessageEdit.Id
                                  select p).FirstOrDefault();
                        var oldIsPublic = tb.IsPublic;
                        if (tb != null)
                        {
                            tb.MessageTitle = vm.MessageEdit.MessageTitle;
                            //tb.IsSms = vm.MessageEdit.IsSms;
                            //tb.IsEmail = vm.MessageEdit.IsEmail;
                            //tb.Url = vm.MessageEdit.Url;
                            tb.MessageContent = vm.MessageEdit.MessageContent;
                            tb.tbSysUser      = db.Set <Sys.Entity.tbSysUser>().Find(Code.Common.UserId);
                            tb.InputDate      = DateTime.Now;
                            tb.IsPublic       = vm.MessageEdit.IsPublic;
                            if (vm.MessageEdit.IsPublic)
                            {
                                if (!oldIsPublic)   //非公开变成公开
                                {
                                    var tbSysMessageRole = (from p in db.Table <Entity.tbSysMessageRole>() where p.tbSysMessage.Id == tb.Id select p);
                                    foreach (var item in tbSysMessageRole)
                                    {
                                        item.IsDeleted  = true;
                                        item.UpdateTime = DateTime.Now;
                                    }
                                }
                            }
                            else
                            {
                                var roleIds = vm.RoleIds.Split(',').ToList();
                                roleIds.RemoveAll(p => string.IsNullOrWhiteSpace(p));
                                if (roleIds != null || roleIds.Count > 0)
                                {
                                    var existsRoleId = (from p in db.Table <Entity.tbSysMessageRole>() where p.tbSysMessage.Id == tb.Id select p.tbSysRole.Id).ToList();
                                    roleIds.RemoveAll(p => existsRoleId.Contains(p.ConvertToInt()));

                                    if (roleIds != null || roleIds.Count > 0)
                                    {
                                        var tbSysRoleList    = (from p in db.Table <Entity.tbSysRole>() where roleIds.Contains(p.Id.ToString()) select p);
                                        var tbSysMessageRole = vm.RoleIds.Split(',').Select(p => new Entity.tbSysMessageRole()
                                        {
                                            tbSysMessage = tb,
                                            tbSysRole    = tbSysRoleList.First(r => r.Id.ToString() == p)
                                        }).ToList();
                                        db.Set <Entity.tbSysMessageRole>().AddRange(tbSysMessageRole);
                                    }
                                }
                            }
                            if (db.SaveChanges() > 0)
                            {
                                XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("修改消息");
                            }
                        }
                        else
                        {
                            error.AddError(Resources.LocalizedText.MsgNotFound);
                        }
                    }
                }

                return(Code.MvcHelper.Post(error, Url.Action("List"), "提交成功!"));
            }
        }