Пример #1
0
        public async Task <ActionResult <BaseResponse> > RegionAdd(string GroupId, RegionAddDto req)
        {
            //管理员权限
            //var GId = User.Claims.FirstOrDefault(a => a.Type == "GroupId").Value;
            //var isAdmin = User.Claims.FirstOrDefault(a => a.Type == "IsAdmin").Value.ToLower() == "true" ? true : false;
            string Account = User.Claims.FirstOrDefault(a => a.Type == "Account").Value;
            //string Code = User.Claims.FirstOrDefault(a => a.Type == "Code").Value;
            //管理员和超级管理员有权限
            //if (true)
            //{

            //}
            //if (!isAdmin)
            //{
            //    return Unauthorized("用户没有权限添加区域");
            //}
            //else if (GroupId != GId && Code != _config["Group"])
            //{
            //    return Unauthorized("用户没有权限添加区域");
            //}
            var rm = await _rs.AddRegionAsync(Account, GroupId, req);

            return(rm);
        }
Пример #2
0
        public async Task <BaseResponse> AddRegionAsync(string account, string groupId, RegionAddDto req)
        {
            //检测parentId是否合法,不合法置为null
            if (req.ParentId != null && "" == req.ParentId.Trim())
            {
                req.ParentId = null;
            }
            //统一组织的,统一层级区域名称不能重复
            var data = await _rr.Find(a => a.GroupId == groupId && a.ParentId == req.ParentId && a.Name == req.Name).FirstOrDefaultAsync();

            if (data != null)
            {
                return(new BaseResponse {
                    Success = false, Message = "该区域下已存在相同的区域名称"
                });
            }
            RegionModel parent = null;

            try
            {
                bool   bP       = false;
                string fullPath = null;
                string regionId = "101";
                //生成区域标示
                if (req.ParentId != null)     //有父区域标示
                {
                    //获取父节点
                    parent = await _rr.FindAsync(req.ParentId, groupId);

                    if (parent == null)
                    {
                        return(new BaseResponse {
                            Success = false, Message = "输入的父节点不存在"
                        });
                    }
                    if (parent.DeleteId == null || "" == parent.DeleteId)//没有删除节点
                    {
                        //获取Id最大的节点
                        var bro = await _rr.Find(a => a.GroupId == groupId && a.ParentId == req.ParentId).OrderByDescending(a => a.Id).FirstOrDefaultAsync();

                        if (bro == null)
                        {
                            regionId = req.ParentId + "001";
                        }
                        else
                        {
                            string r  = bro.Id;
                            int    ir = Convert.ToInt32(r.Substring(r.Length - 3)) + 1;
                            regionId = r.Substring(0, r.Length - 3) + ir.ToString().PadLeft(3, '0');
                        }
                    }    //父节点没有删除节点
                    else //父节点有删除节点
                    {
                        bP = true;
                        string[] strDelete = parent.DeleteId.Split(',');
                        regionId = strDelete[0];
                        string[] arr = new string[strDelete.Length - 1];
                        //删除数组第一个元素
                        Array.Copy(strDelete, 1, arr, 0, arr.Length);
                        parent.DeleteId = string.Join(",", arr);
                    }
                    fullPath = parent.FullPath + "/" + regionId;
                }    //有父节点
                else //没有父节点
                {
                    req.ParentId = null;
                    if (req.RegionCode.Length != 3)
                    {
                        return(new BaseResponse {
                            Success = false, Message = "添加区域失败,区域的区域码必须为3位"
                        });
                    }
                    regionId = req.RegionCode;
                    fullPath = regionId;
                    //检测是否添加过
                    var top = await _rr.FindAsync(regionId, groupId);

                    if (top != null)
                    {
                        return(new BaseResponse {
                            Success = false, Message = "此区域已存在,添加失败"
                        });
                    }
                }
                var entity = _mapper.Map <RegionModel>(req);
                entity.GroupId  = groupId;
                entity.Id       = regionId;
                entity.Create   = account;
                entity.FullPath = fullPath;
                await _rr.AddAsync(entity, parent, bP);

                _log.LogInformation($"{account}添加标示为{new { entity.Id, entity.GroupId }}的区域成功");
                return new HandleResponse<object> { Success = true, Message = "添加区域成功", Key = new { Id = regionId, GroupId = groupId } };
            }
            catch (Exception ex)
            {
                _log.LogError($"{account}添加区域失败,失败原因:{ex.Message}->{ex.StackTrace}->{ex.InnerException}");
                return(new BaseResponse {
                    Success = false, Message = "添加区域失败,请联系管理员"
                });
            }
        }