public ActionResult Edit(Models.TeanntProgram.Edit vm)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                var error = new List <string>();
                if (error.Count == decimal.Zero)
                {
                    if (vm.TeanntProgramEdit.Id == 0)
                    {
                        var tb = new Admin.Entity.tbTenantProgram();
                        tb.No = vm.TeanntProgramEdit.No;
                        tb.tbProgram.ProgramName = vm.TeanntProgramEdit.TeanntProgramName;
                        db.Set <Admin.Entity.tbTenantProgram>().Add(tb);
                        if (db.SaveChanges() > 0)
                        {
                            XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("添加了程序类型");
                        }
                    }
                    else
                    {
                        var tb = (from p in db.Table <Admin.Entity.tbTenantProgram>()
                                  where p.Id == vm.TeanntProgramEdit.Id
                                  select p).FirstOrDefault();
                        if (tb != null)
                        {
                            tb.No = vm.TeanntProgramEdit.No;
                            tb.tbProgram.ProgramName = vm.TeanntProgramEdit.TeanntProgramName;
                            if (db.SaveChanges() > 0)
                            {
                                XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("修改了程序类型");
                            }
                        }
                        else
                        {
                            error.AddError(Resources.LocalizedText.MsgNotFound);
                        }
                    }
                }

                return(Code.MvcHelper.Post(error));
            }
        }
        public ActionResult Edit(Models.Tenant.Edit vm)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                var error = new List <string>();
                if (error.Count == decimal.Zero)
                {
                    var file = Request.Files["TenantEdit.Logo"];

                    if (file.ContentLength > 0 && Code.Common.GetFileType(file.FileName) != Code.FileType.Image)
                    {
                        return(Content("<script >alert('图片格式必须是jpg、jpeg、png、bmp格式!');history.go(-1);</script >", "text/html"));
                    }

                    var programIds = new List <int>();
                    if (Request["CboxProgram"] != null)
                    {
                        programIds = Request["CboxProgram"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(d => d.ConvertToInt()).ToList();
                    }

                    if (vm.TenantEdit.IsDefault)
                    {
                        var list = from p in db.TableRoot <Admin.Entity.tbTenant>()
                                   select p;

                        foreach (Admin.Entity.tbTenant tenant in list)
                        {
                            tenant.IsDefault = false;
                        }

                        db.SaveChanges();
                    }

                    var tb = new Admin.Entity.tbTenant();
                    if (vm.TenantEdit.Id == 0)
                    {
                        #region
                        tb.No = vm.TenantEdit.No == null?db.TableRoot <Admin.Entity.tbTenant>().Select(d => d.No).DefaultIfEmpty(0).Max() + 1 : (int)vm.TenantEdit.No;

                        tb.TenantName = vm.TenantEdit.TenantName;
                        tb.Title      = vm.TenantEdit.Title;
                        tb.IsDefault  = vm.TenantEdit.IsDefault;
                        tb.Host       = vm.TenantEdit.Host;
                        tb.Cdkey      = vm.TenantEdit.Cdkey;
                        tb.Power      = vm.TenantEdit.Power;
                        tb.IsVip      = vm.TenantEdit.IsVip;
                        tb.Power      = vm.TenantEdit.Power;
                        tb.Address    = vm.TenantEdit.Address;
                        tb.Deadine    = vm.TenantEdit.Deadine;
                        tb.Remark     = vm.TenantEdit.Remark;
                        db.Set <Admin.Entity.tbTenant>().Add(tb);

                        if (file.ContentLength > 0)
                        {
                            var fileName = "Logo.png";
                            var filePath = Server.MapPath("~/Content/Images/") + fileName; // DateTime.Now.ToString("yyyyMMddHHmmssfff") + r.Next(10000, 99999).ToString() + "." + file.FileName.Split('.').Last();
                            if (System.IO.File.Exists(filePath))
                            {
                                System.IO.File.Delete(filePath);
                            }
                            file.SaveAs(filePath);
                            tb.Logo = fileName;
                        }

                        if (db.SaveChanges() > 0)
                        {
                            Migrations.Configuration.Seed(db, db.Set <Admin.Entity.tbTenant>().Find(tb.Id), vm.TenantEdit.AdminLoginCode, vm.TenantEdit.AdminPassword);

                            XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("添加了学校");
                        }

                        #region 系统权限
                        programIds.ForEach(programId =>
                        {
                            var program = (from p in db.TableRoot <Admin.Entity.tbProgram>()
                                           where p.Id == programId
                                           select p).FirstOrDefault();
                            var tbTenantProgram       = new Admin.Entity.tbTenantProgram();
                            tbTenantProgram.No        = 0;
                            tbTenantProgram.tbTenant  = tb;
                            tbTenantProgram.tbProgram = program;
                            db.Set <Admin.Entity.tbTenantProgram>().Add(tbTenantProgram);
                        });
                        db.SaveChanges();
                        #endregion
                        #endregion
                    }
                    else
                    {
                        #region
                        tb = (from p in db.TableRoot <Admin.Entity.tbTenant>()
                              where p.Id == vm.TenantEdit.Id
                              select p).FirstOrDefault();
                        if (tb != null)
                        {
                            tb.TenantName = vm.TenantEdit.TenantName;
                            tb.No         = vm.TenantEdit.No == null?db.TableRoot <Admin.Entity.tbTenant>().Select(d => d.No).DefaultIfEmpty(0).Max() + 1 : (int)vm.TenantEdit.No;

                            tb.Title     = vm.TenantEdit.Title;
                            tb.IsDefault = vm.TenantEdit.IsDefault;
                            tb.Host      = vm.TenantEdit.Host;
                            tb.Cdkey     = vm.TenantEdit.Cdkey;
                            tb.Power     = vm.TenantEdit.Power;
                            tb.IsVip     = vm.TenantEdit.IsVip;
                            tb.Power     = vm.TenantEdit.Power;
                            tb.Address   = vm.TenantEdit.Address;
                            tb.Deadine   = vm.TenantEdit.Deadine;
                            tb.Remark    = vm.TenantEdit.Remark;

                            if (file.ContentLength > 0)
                            {
                                var fileName = "Logo.png";
                                var filePath = Server.MapPath("~/Content/Images/") + fileName; // DateTime.Now.ToString("yyyyMMddHHmmssfff") + r.Next(10000, 99999).ToString() + "." + file.FileName.Split('.').Last();
                                if (System.IO.File.Exists(filePath))
                                {
                                    System.IO.File.Delete(filePath);
                                }
                                file.SaveAs(filePath);
                                tb.Logo = fileName;
                            }

                            if (db.SaveChanges() > 0)
                            {
                                XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("修改了学校");
                            }

                            #region 系统权限
                            var tenantPrograms = (from p in db.Set <Admin.Entity.tbTenantProgram>().Where(d => d.IsDeleted == false)
                                                  where p.tbTenant.Id == tb.Id
                                                  select p).ToList();
                            tenantPrograms.ForEach(tenantProgram =>
                            {
                                tenantProgram.IsDeleted = true;
                            });
                            programIds.ForEach(programId =>
                            {
                                var program = (from p in db.TableRoot <Admin.Entity.tbProgram>()
                                               where p.Id == programId
                                               select p).FirstOrDefault();
                                var tbTenantProgram       = new Admin.Entity.tbTenantProgram();
                                tbTenantProgram.No        = 0;
                                tbTenantProgram.tbTenant  = tb;
                                tbTenantProgram.tbProgram = program;
                                db.Set <Admin.Entity.tbTenantProgram>().Add(tbTenantProgram);
                            });
                            db.SaveChanges();
                            #endregion
                        }
                        else
                        {
                            error.AddError(Resources.LocalizedText.MsgNotFound);
                        }
                        #endregion
                    }

                    var cache = new Areas.Sys.Controllers.SysIndexController();
                    cache.RestartCache();
                }

                return(Content("<script>window.parent.location.reload();</script>")); //Code.MvcHelper.Post(error);
            }
        }