Пример #1
0
        /// <summary>
        /// 保存角色管理
        /// </summary>
        public JsonResult SaveRoleManage(string id, string KEYED_NAME, string personList, string region)
        {
            var retModel = new JsonReturnModel();

            try
            {
                string description = "OASystem;";
                if (!string.IsNullOrEmpty(region))
                {
                    //判断地区是否在数据中存在
                    if (!RegionDA.isExistRegionByName(inn, region))
                    {
                        retModel.AddError("errorMessage", "选择的地区不存在!");
                        return(Json(retModel, JsonRequestBehavior.AllowGet));
                    }
                    //根据角色名称和地区判断是否已经存在该角色
                    description = description + region;
                }

                if (string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(region))
                {
                    KEYED_NAME = KEYED_NAME + "(" + region + ")";
                }

                //////判断Keyed_Name是否存在
                //if (IdentityDA.ValidIsExistByKeyed_Name(KEYED_NAME,id))
                //{
                //    retModel.AddError("errorMessage", "输入的角色已经存在!");
                //    return Json(retModel, JsonRequestBehavior.AllowGet);
                //}

                Item identity = IdentityDA.GetIdentityByCondition(inn, id, KEYED_NAME);
                int  count    = int.Parse(identity.getProperty("ncount"));
                if (count > 0)
                {
                    retModel.AddError("errorMessage", "输入的角色和地区已经存在!");
                    return(Json(retModel, JsonRequestBehavior.AllowGet));
                }

                Item result;
                if (string.IsNullOrEmpty(id))
                {
                    //拼写Insert语句
                    string        amlStr = "<AML><Item type='IDENTITY' action='add'><name>" + KEYED_NAME + "</name><description>" + description + "</description>";
                    List <string> list   = personList.Split(';').Where(x => x != "" && x != null).Select(x => x.Trim()).Distinct().ToList();
                    if (list.Count() > 0)
                    {
                        amlStr += "<Relationships>";
                        for (int i = 0; i < list.Count; i++)
                        {
                            amlStr += "<Item type='MEMBER' action='add'>";
                            amlStr += "<related_id> ";
                            string textValue = list[i];
                            //验证用户在数据库中是否存在
                            if (UserDA.ValidUserIsExist(inn, textValue))
                            {
                                amlStr += "<Item type='IDENTITY' action='get'><name>" + list[i] + "</name></Item>";
                            }
                            else
                            {
                                retModel.AddError("errorMessage", "输入的人员在系统中不存在!");
                                return(Json(retModel, JsonRequestBehavior.AllowGet));
                            }
                            amlStr += "</related_id></Item>";
                        }
                        amlStr += "</Relationships>";
                    }
                    amlStr += "</Item></AML>";
                    result  = IdentityDA.InsertRoleManage(inn, amlStr);
                }
                else
                {
                    string amlStr = "<AML><Item type='IDENTITY' action='edit' id='" + id + "'>";
                    amlStr += "<name>" + KEYED_NAME + "</name>";
                    amlStr += "<description>" + description + "</description>";
                    Item item = IdentityDA.GetRoleManageById(inn, id);
                    if (!string.IsNullOrEmpty(item.getErrorString()))
                    {
                        retModel.AddError("errorMessage", item.getErrorString());
                        return(Json(retModel, JsonRequestBehavior.AllowGet));
                    }
                    List <string> list     = personList.Split(';').Where(x => x != "" && x != null).Select(x => x.Trim()).Distinct().ToList();
                    Item          Relation = item.getRelationships();
                    List <string> oldList  = new List <string>();
                    if (Relation.getItemCount() > 0 || list.Count() > 0)
                    {
                        amlStr += "<Relationships>";
                    }
                    if (Relation.getItemCount() > 0)
                    {
                        //删除不存在的
                        for (int i = 0; i < Relation.getItemCount(); i++)
                        {
                            Item   relationObJ = Relation.getItemByIndex(i);
                            string name        = relationObJ.getRelatedItem().getProperty("name");
                            oldList.Add(name);
                            string strValue = list.Where(x => x == name).FirstOrDefault();
                            if (string.IsNullOrEmpty(strValue))
                            {
                                string memberId = relationObJ.getProperty("id");
                                amlStr += "<Item type='MEMBER' action='delete' id='" + memberId + "'></Item>";
                            }
                        }
                    }
                    if (list.Count() > 0)
                    {
                        //添加新的
                        for (int index = 0; index < list.Count(); index++)
                        {
                            string value = list[index];
                            var    obj   = oldList.Where(x => x == value).FirstOrDefault();
                            //验证用户在数据库中是否存在
                            if (UserDA.ValidUserIsExist(inn, value))
                            {
                                if (string.IsNullOrEmpty(obj))
                                {
                                    amlStr += "<Item type='MEMBER' action='add'>";
                                    amlStr += "<related_id> ";
                                    amlStr += "<Item type='IDENTITY' action='get'><name>" + value + "</name></Item>";
                                    amlStr += "</related_id></Item>";
                                }
                            }
                            else
                            {
                                retModel.AddError("errorMessage", "输入的人员在系统中不存在!");
                                return(Json(retModel, JsonRequestBehavior.AllowGet));
                            }
                        }
                    }
                    if (Relation.getItemCount() > 0 || list.Count() > 0)
                    {
                        amlStr += "</Relationships>";
                    }
                    amlStr += "</Item></AML>";
                    result  = inn.applyAML(amlStr);
                }
                if (!string.IsNullOrEmpty(result.getErrorString()))
                {
                    retModel.AddError("errorMessage", result.getErrorString());
                }
            }
            catch (Exception ex)
            {
                retModel.AddError("errorMessage", ex.Message);
            }
            return(Json(retModel, JsonRequestBehavior.AllowGet));
        }