public Boolean Edit(PermissionCategory model)
        {
            try
            {

                DateTime now;

                now = DateTime.Now;

                if (null == model.PC_Level)
                {
                    model.PC_Level = String.Format("{0:D4}", model.PC_Id);
                }

                olsEni.Entry(model).State = EntityState.Modified;
                olsEni.SaveChanges();

                UpdatePermission(model);

                return true;
            }
            catch (Exception ex)
            {
                StaticHelper.RecordSystemLog(ex);
                return false;
            }
        }
        public Boolean Create(PermissionCategory model)
        {
            try
            {

                DateTime now;
                Int32 id;

                now = DateTime.Now;

                id = GetPCId();

                model.PC_Id = id;

                if (null == model.PC_Level)
                {
                    model.PC_Level = String.Format("{0:D4}", model.PC_Id);
                }

                olsEni.PermissionCategories.Add(model);
                olsEni.SaveChanges();

                UpdatePermission(model);

                return true;
            }
            catch (Exception ex)
            {
                StaticHelper.RecordSystemLog(ex);
                return false;
            }
        }
        public ActionResult Create(PermissionCategory m)
        {
            if (ModelState.IsValid)
            {

                if (um.Create(m))
                {
                    return Redirect("/Contents/html/parent_reload.htm");
                }
            }

            ViewBag.ActionPermissions = StaticHelper.GetActionPermission();

            return View(m);
        }
        private void UpdatePermission(PermissionCategory model)
        {
            Int32 id, recycleBinPCId;
            Configuration config;
            AppSettingsSection appseting;
            Permission p;
            List<ActionPermission> aps;
            List<Permission> ps;

            config = WebConfigurationManager.OpenWebConfiguration("/");
            appseting = (AppSettingsSection)config.GetSection("appSettings");
            recycleBinPCId = Convert.ToInt32(appseting.Settings["RecycleBinPermissionCategoryId"].Value);

            // 先清除已选择的权限记录
            ps = olsEni.Permissions.Where(m => m.PC_Id == model.PC_Id).ToList();
            foreach (var p1 in ps)
            {
                p1.PC_Id = recycleBinPCId;
            }
            olsEni.SaveChanges();

            aps = (List<ActionPermission>)JsonConvert.DeserializeObject<List<ActionPermission>>(model.PC_Permissions);

            id = GetPId();

            foreach (var ap in aps)
            {

                p = olsEni
                    .Permissions
                    .SingleOrDefault(m =>
                        m.P_Controller == ap.ControllerName
                        && m.P_Action == ap.ActionName
                        && m.P_Name == ap.Description);

                // 不重复插入权限记录,如果已存在权限记录将重设其权限目录
                if (null == p)
                {

                    olsEni.Permissions.Add(new Permission
                    {
                        P_Id = id,
                        PC_Id = model.PC_Id,
                        P_Name = ap.Description,
                        P_Controller = ap.ControllerName,
                        P_Action = ap.ActionName,
                        P_AddTime = now
                    });

                    id += 1;
                }
                else
                {

                    p.PC_Id = model.PC_Id;
                    olsEni.Entry(p).State = EntityState.Modified;
                }
            }
            olsEni.SaveChanges();
        }
        public PermissionCategory GetNew()
        {
            PermissionCategory model;

            model = new PermissionCategory()
            {
                PC_Id = 0,
                PC_Name = "",
                PC_Permissions = "[]",
                PC_Remark = "",
                PC_AddTime = DateTime.Now,
                PC_Status = (Byte)Status.Available
            };

            return model;
        }