Esempio n. 1
0
        public async Task <BaseResponse> DeleteRegionAsync(string account, string Id, string GroupId)
        {
            var data = await _rr.FindAsync(Id, GroupId);

            if (data == null)
            {
                return(new BaseResponse {
                    Success = false, Message = "输入的区域标示不存在"
                });
            }
            var d = await _rr.FindWithChildAsync(a => a.Id == Id && a.GroupId == GroupId);

            if (d.Child.Count > 0)
            {
                return(new BaseResponse {
                    Success = false, Message = "此区域下有子区域,不能删除"
                });
            }

            try
            {
                //检测区域是否使用,项目、设备和子区域
                var p = await _pr.Find(a => a.GroupId == GroupId && a.RegionId == Id).FirstOrDefaultAsync();

                if (p != null)
                {
                    return(new BaseResponse {
                        Success = false, Message = "此区域已被项目或者场站使用,不能删除"
                    });
                }
                var device = await _dr.Find(a => a.GroupId == GroupId && a.RegionId == Id).FirstOrDefaultAsync();

                if (device != null)
                {
                    return(new BaseResponse {
                        Success = false, Message = "此区域已被设备使用,不能删除"
                    });
                }
                RegionModel parent = null;
                if (data.Parent != null)//如果存在父节点,把当前的id加入到父节点已删除中
                {
                    parent = await _rr.FindAsync(data.ParentId, GroupId);

                    if (parent.DeleteId == null || parent.DeleteId.Trim() == "")
                    {
                        parent.DeleteId = Id;
                    }
                    else
                    {
                        parent.DeleteId += "," + Id;
                    }
                }
                await _rr.RemoveAsync(data, parent);

                _log.LogInformation($"{account}删除标示为{new { Id = Id, GroupId = GroupId }}的区域标示成功");
                return new BaseResponse { Success = true, Message = "删除区域成功" };
            }
            catch (Exception ex)
            {
                _log.LogError($"{account}删除标示为{new { Id = Id, GroupId = GroupId }}的区域失败,失败原因:{ex.Message}->{ex.StackTrace}->{ex.InnerException}");
                return(new BaseResponse {
                    Success = false, Message = "删除数据失败,请联系管理员"
                });
            }
        }