/// <summary>
 /// Create a new SysOperation object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 public static SysOperation CreateSysOperation(global::System.String id)
 {
     SysOperation sysOperation = new SysOperation();
     sysOperation.Id = id;
     return sysOperation;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the SysOperation EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToSysOperation(SysOperation sysOperation)
 {
     base.AddObject("SysOperation", sysOperation);
 }
 public ActionResult SetSysMenu(SysOperation entity)
 {
     if (entity != null)
     {
         string currentPerson = GetCurrentPerson();
         //entity.UpdateTime = DateTime.Now;
         //entity.UpdatePerson = currentPerson;
         string returnValue = string.Empty;
         if (m_BLL.SetSysMenu(ref validationErrors, entity))
         {
             LogClassModels.WriteServiceLog(Suggestion.UpdateSucceed + ",角色信息的Id为" + entity.Id, "消息"
                 );//写入日志                           
             return Json(Suggestion.UpdateSucceed); //提示更新成功 
         }
         else
         {
             if (validationErrors != null && validationErrors.Count > 0)
             {
                 validationErrors.All(a =>
                 {
                     returnValue += a.ErrorMessage;
                     return true;
                 });
             }
             LogClassModels.WriteServiceLog(Suggestion.DeleteFail + ",信息的Id为" + entity.Id + "," + returnValue, "消息"
                 );//删除失败,写入日志
             return Json(Suggestion.UpdateFail + returnValue);                  
          }
     }
     else
     {
         return Json(Suggestion.UpdateFail + ",请核对输入的数据的格式"); //提示输入的数据的格式不对               
     }
 }
        public ActionResult Create(string id)
        { 
                     
            if (!string.IsNullOrWhiteSpace(id))
            {
                using (SysMenuBLL bll = new SysMenuBLL())
                {
                    SysMenu entityId = bll.GetById(id);
                    if (entityId != null)
                    { 
                        SysOperation entity = new SysOperation();  
                        entity.SysMenuId = id + '&' + entityId.Name;
                        return View(entity);
                    }
                }
            }

            return View();
        }
        public ActionResult Create(SysOperation entity)
        {           
            if (entity != null && ModelState.IsValid)
            {
                string currentPerson = GetCurrentPerson();
                entity.CreateTime = DateTime.Now;
                entity.CreatePerson = currentPerson;
              
                entity.Id = Result.GetNewId();   
                string returnValue = string.Empty;
                if (m_BLL.Create(ref validationErrors, entity))
                {
                    LogClassModels.WriteServiceLog(Suggestion.InsertSucceed  + ",操作的信息的Id为" + entity.Id,"操作"
                        );//写入日志 
                    return Json(Suggestion.InsertSucceed);
                }
                else
                { 
                    if (validationErrors != null && validationErrors.Count > 0)
                    {
                        validationErrors.All(a =>
                        {
                            returnValue += a.ErrorMessage;
                            return true;
                        });
                    }
                    LogClassModels.WriteServiceLog(Suggestion.InsertFail + ",操作的信息," + returnValue,"操作"
                        );//写入日志                      
                    return Json(Suggestion.InsertFail  + returnValue); //提示插入失败
                }
            }

            return Json(Suggestion.InsertFail + ",请核对输入的数据的格式"); //提示输入的数据的格式不对 
        }
 public ActionResult Edit(string id, SysOperation entity)
 {
     if (entity != null && ModelState.IsValid)
     {   //数据校验
     
         string currentPerson = GetCurrentPerson();                 
         //entity.UpdateTime = DateTime.Now;
         //entity.UpdatePerson = currentPerson;
                    
         string returnValue = string.Empty;   
         if (m_BLL.Edit(ref validationErrors, entity))
         {
             LogClassModels.WriteServiceLog(Suggestion.UpdateSucceed + ",操作信息的Id为" + id,"操作"
                 );//写入日志                           
             return Json(Suggestion.UpdateSucceed); //提示更新成功 
         }
         else
         { 
             if (validationErrors != null && validationErrors.Count > 0)
             {
                 validationErrors.All(a =>
                 {
                     returnValue += a.ErrorMessage;
                     return true;
                 });
             }
             LogClassModels.WriteServiceLog(Suggestion.UpdateFail + ",操作信息的Id为" + id + "," + returnValue, "操作"
                 );//写入日志                           
             return Json(Suggestion.UpdateFail  + returnValue); //提示更新失败
         }
     }
     return Json(Suggestion.UpdateFail + "请核对输入的数据的格式"); //提示输入的数据的格式不对               
   
 }
        /// <summary>
        /// 创建一个菜单
        /// </summary>
        /// <param name="validationErrors">返回的错误信息</param>
        /// <param name="db">数据库上下文</param>
        /// <param name="entity">一个菜单</param>
        /// <returns></returns>
        public bool Create(ref ValidationErrors validationErrors, SysEntities db, SysMenu entity)
        {
            int count = 1;

            foreach (string item in entity.SysOperationId.GetIdSort())
            {
                SysOperation sys = new SysOperation { Id = item };
                db.SysOperation.Attach(sys);
                entity.SysOperation.Add(sys);
                count++;
            }

            repository.Create(db, entity);
            if (count == repository.Save(db))
            {
                //创建后重置菜单编码
                List<int> flags = new List<int>();//层级
                GetMenus2(null, flags);
                db.SaveChanges();
                return true;
            }
            else
            {
                validationErrors.Add("创建出错了");
            }
            return false;
        }
        /// <summary>
        /// 编辑一个菜单
        /// </summary>
        /// <param name="validationErrors">返回的错误信息</param>
        /// <param name="db">数据上下文</param>
        /// <param name="entity">一个菜单</param>
        /// <returns>是否编辑成功</returns>
        public bool Edit(ref ValidationErrors validationErrors, SysEntities db, SysMenu entity)
        {  /*                       
                           * 不操作 原有 现有
                           * 增加   原没 现有
                           * 删除   原有 现没
                           */
            if (entity == null)
            {
                return false;
            }
            int count = 1;
            SysMenu editEntity = repository.Edit(db, entity);

            List<string> addSysOperationId = new List<string>();
            List<string> deleteSysOperationId = new List<string>();
            DataOfDiffrent.GetDiffrent(entity.SysOperationId.GetIdSort(), entity.SysOperationIdOld.GetIdSort(), ref addSysOperationId, ref deleteSysOperationId);
            if (addSysOperationId != null && addSysOperationId.Count() > 0)
            {
                foreach (var item in addSysOperationId)
                {
                    SysOperation sys = new SysOperation { Id = item };
                    db.SysOperation.Attach(sys);
                    editEntity.SysOperation.Add(sys);
                    count++;
                }
            }
            if (deleteSysOperationId != null && deleteSysOperationId.Count() > 0)
            {
                List<SysOperation> listEntity = new List<SysOperation>();
                foreach (var item in deleteSysOperationId)
                {
                    SysOperation sys = new SysOperation { Id = item };
                    listEntity.Add(sys);
                    db.SysOperation.Attach(sys);
                }
                foreach (SysOperation item in listEntity)
                {
                    editEntity.SysOperation.Remove(item);//查询数据库
                    count++;
                }
            }

            if (count == repository.Save(db))
            {
                //修改后重置菜单编码
                List<int> flags = new List<int>();//层级
                GetMenus2(null, flags);
                db.SaveChanges();
                return true;
            }
            else
            {
                validationErrors.Add("编辑菜单出错了");
            }
            return false;
        }
        /// <summary>
        /// 创建一个操作
        /// </summary>
        /// <param name="validationErrors">返回的错误信息</param>
        /// <param name="db">数据库上下文</param>
        /// <param name="entity">一个操作</param>
        /// <returns></returns>
        public bool Create(ref ValidationErrors validationErrors, SysEntities db, SysOperation entity)
        {
            int count = 1;

            foreach (string item in entity.SysMenuId.GetIdSort())
            {
                SysMenu sys = new SysMenu { Id = item };
                db.SysMenu.Attach(sys);
                entity.SysMenu.Add(sys);
                count++;
            }

            repository.Create(db, entity);
            if (count == repository.Save(db))
            {
                return true;
            }
            else
            {
                validationErrors.Add("创建出错了");
            }
            return false;
        }
        /// <summary>
        /// 设置一个菜单
        /// </summary>
        /// <param name="validationErrors">返回的错误信息</param>
        /// <param name="entity">一个菜单</param>
        /// <returns>是否设置成功</returns>
        public bool SetSysMenu(ref ValidationErrors validationErrors, SysOperation entity)
        {
            bool bResult = false;
            int count = 0;
            using (TransactionScope transactionScope = new TransactionScope())
            {
                try
                {
                    SysOperation editEntity = repository.GetById(db, entity.Id);

                    List<string> addSysMenuId = new List<string>();
                    List<string> deleteSysMenuId = new List<string>();
                    if (entity.SysMenuId != null)
                    {
                        addSysMenuId = entity.SysMenuId.Split(',').ToList();
                    }
                    if (entity.SysMenuIdOld != null)
                    {
                        deleteSysMenuId = entity.SysMenuIdOld.Split(',').ToList();
                    }
                    DataOfDiffrent.GetDiffrent(addSysMenuId, deleteSysMenuId, ref addSysMenuId, ref deleteSysMenuId);

                    if (addSysMenuId != null && addSysMenuId.Count() > 0)
                    {
                        foreach (var item in addSysMenuId)
                        {
                            SysMenu sys = new SysMenu { Id = item };
                            db.SysMenu.Attach(sys);
                            editEntity.SysMenu.Add(sys);
                            count++;
                        }
                    }
                    if (deleteSysMenuId != null && deleteSysMenuId.Count() > 0)
                    {
                        List<SysMenu> listEntity = new List<SysMenu>();
                        foreach (var item in deleteSysMenuId)
                        {
                            SysMenu sys = new SysMenu { Id = item };
                            listEntity.Add(sys);
                            db.SysMenu.Attach(sys);
                        }
                        foreach (SysMenu item in listEntity)
                        {
                            editEntity.SysMenu.Remove(item);//查询数据库
                            count++;
                        }
                    }

                    if (count > 0 && count == repository.Save(db))
                    {
                        transactionScope.Complete();
                        bResult = true;
                    }
                    else if (count == 0)
                    {
                        validationErrors.Add("数据没有改变");
                    }
                }
                catch (Exception ex)
                {
                    Transaction.Current.Rollback();
                    ExceptionsHander.WriteExceptions(ex);
                    validationErrors.Add("编辑出错了。原因" + ex.Message);
                }
            }

            return bResult;
        }
 /// <summary>
 /// 编辑一个操作
 /// </summary>
 /// <param name="validationErrors">返回的错误信息</param>
 /// <param name="entity">一个操作</param>
 /// <returns>是否编辑成功</returns>
 public bool Edit(ref ValidationErrors validationErrors, SysOperation entity)
 {
     try
     {
         using (TransactionScope transactionScope = new TransactionScope())
         {
             if (Edit(ref validationErrors, db, entity))
             {
                 transactionScope.Complete();
                 return true;
             }
             else
             {
                 Transaction.Current.Rollback();
             }
         }
     }
     catch (Exception ex)
     {
         validationErrors.Add(ex.Message);
         ExceptionsHander.WriteExceptions(ex);
     }
     return false;
 }