namespace IStation.Application { /// /// LogicTree /// [Route("SZJT/Logic/Tree")] [ApiDescriptionSettings("SZJT", Name = "清单", Order = 70000)] public class LogicTree_Controller : IDynamicApiController { private readonly Service.LogicTree _service = new Service.LogicTree(); #region Query /// /// 获取 /// [Route("GetAll@V1.0")] [HttpGet] public List GetAll() { var list = _service.GetExList(); var vmList = list?.Select(x => new LogicTreeDto(x)).ToList(); return vmList; } /// /// 通过 ID 获取 /// [Route("GetByID@V1.0")] [HttpGet] public LogicTreeDto GetByID([FromQuery][Required] IDInput input) { var model = _service.GetExByID(input.ID); return model == null ? null : new LogicTreeDto(model); } /// /// 通过 Ids 获取 /// [Route("GetByIds@V1.0")] [HttpGet] public List GetByIds([FromQuery][Required] IdsInput input) { var ids = LongListHelper.ToList(input.Ids); var list = _service.GetExByIds(ids); var vmList = list?.Select(x => new LogicTreeDto(x)).ToList(); return vmList; } /// /// 通过 PolicyID 获取 /// [Route("GetByPolicyID@V1.0")] [HttpGet] public List GetByPolicyID([FromQuery] PolicyIDInput input) { var list = _service.GetExByPolicyID(input.PolicyID); var vmList = list?.Select(x => new LogicTreeDto(x)).ToList(); return vmList; } /// /// 通过 ID 获取子项及自身 /// [Route("GetChildAndSelfByID@V1.0")] [HttpGet] public List GetChildAndSelfByID([FromQuery][Required] IDInput input) { var list = _service.GetExChildAndSelfByID(input.ID); var vmList = list?.Select(x => new LogicTreeDto(x)).ToList(); return vmList; } /// /// 通过 ID 获取子项 /// [Route("GetChildrenByID@V1.0")] [HttpGet] public List GetChildrenByID([FromQuery][Required] IDInput input) { var list = _service.GetExChildrenByID(input.ID); var vm_list = list?.Select(x => x.Adapt()).ToList(); return vm_list; } #endregion #region Insert /// /// 插入一条 /// [Route("Insert@V1.0")] [HttpPost] public long Insert([Required] AddLogicTreeInput input) { var policy = new Service.LogicPolicy().GetByID(input.PolicyID); if (policy == null) { throw YOops.Oh(eResultCode.Alert, ErrorCodes.D001, $"PolicyID:{input.PolicyID} 数据不存在"); } if (_service.IsExist(input.PolicyID, input.LogicType, input.LogicID)) { throw YOops.Oh(eResultCode.Alert, ErrorCodes.V001, $"LogicType:{input.LogicType},LogicID:{input.LogicID} 已存在"); } var parentIds = new List(); if (input.ParentID > 0) { var parent = _service.GetByID(input.ParentID); if (parent == null) { throw YOops.Oh(eResultCode.Alert, ErrorCodes.D001, $"ParentID:{input.ParentID} 数据不存在"); } if (parent.PolicyID != input.PolicyID) { throw YOops.Oh(eResultCode.Alert, ErrorCodes.V001, $"ParentID:{input.ParentID} 与 PolicyID:{input.PolicyID} 不匹配"); } parentIds = TreeParentIdsHelper.GetChildParentIds(parent.ID, parent.ParentIds); } var model = input.Adapt(); model.ParentIds = parentIds; model.SortCode = _service.GetMaxSortCodeByParentID(input.ParentID) + 1; var id = _service.Insert(model); return id; } #endregion #region Update /// /// 更新排序码 /// [Route("UpdateSortCode@V1.0")] [HttpPut] public bool UpdateSortCode([Required] UpdateSortCodeInput input) { var bol = _service.UpdateSortCode(input.ID, input.SortCode); return bol; } /// /// 更新排序 /// [Route("UpdateSorter@V1.0")] [HttpPut] public bool UpdateSorter([Required] List input) { var list = input.Select(x => x.Adapt()).ToList(); var bol = _service.UpdateSorter(list); return bol; } /// /// 更新树排序码 /// [Route("UpdateTreeSortCode@V1.0")] [HttpPut] public bool UpdateTreeSortCode([Required] UpdateTreeSortCodeInput input) { var model = _service.GetByID(input.ID); if (model == null) { throw Oops.Oh(ErrorCodes.D001, $"ID:{input.ID}"); } var parentIds = new List(); if (input.ParentID > 0) { var parent = _service.GetByID(input.ParentID); if (parent == null) { throw Oops.Oh(ErrorCodes.D001, $"ParentID:{input.ParentID}"); } parentIds = TreeParentIdsHelper.GetChildParentIds(parent.ID, parent.ParentIds); } var sorterList = new List() { new Yw.Model.TreeSorter() { ID=input.ID, ParentIds=parentIds, SortCode=input.SortCode } }; if (TreeParentIdsHelper.ToString(parentIds) != TreeParentIdsHelper.ToString(model.ParentIds)) { var children = _service.GetChildrenByID(input.ID); if (children != null && children.Count > 0) { foreach (var item in children) { var itemParent = sorterList.Find(x => x.ID == item.ParentIds.Last()); var itemParentIds = TreeParentIdsHelper.GetChildParentIds(itemParent.ID, itemParent.ParentIds); var sorter = new Yw.Model.TreeSorter() { ID = item.ID, ParentIds = itemParentIds, SortCode = item.SortCode }; sorterList.Add(sorter); } } } var bol = _service.UpdateTreeSorter(sorterList); return bol; } #endregion #region Exsit /// /// 判断是否存在 /// [Route("IsExist@V1.0")] [HttpGet] public bool IsExist([FromQuery][Required] LogicOfPolicyIDInput input) { var bol = _service.IsExist(input.PolicyID, input.LogicType, input.LogicID); return bol; } #endregion #region Delete /// /// 删除 /// [Route("DeleteByID@V1.0")] [HttpDelete] public bool DeleteByID([FromQuery][Required] IDInput input) { var bol = _service.DeleteByID(input.ID, out string Msg); if (!bol) { throw YOops.Oh(eResultCode.Alert, ErrorCodes.D001, Msg); } return bol; } #endregion #region Save /// /// 保存 /// [Route("Save@V1.0")] [HttpPost] public bool Save([Required] SaveLogicTreeInput input) { var policy = new Service.LogicPolicy().GetByID(input.PolicyID); if (policy == null) { throw YOops.Oh(eResultCode.Alert, ErrorCodes.D001, $"PolicyID:{input.PolicyID} 数据不存在"); } if (input.TreeList != null && input.TreeList.Count > 0) { var tempList = new List(); void Append(LogicTreeSaveTreeInput tree) { tempList.Add(tree); tree.Children?.ForEach(x => Append(x)); } input.TreeList.ForEach(x => Append(x)); if (tempList.Exists(x => x.LogicType == IStation.DataType.LogicSite && x.Children != null && x.Children.Count > 0)) { throw YOops.Oh(eResultCode.Alert, ErrorCodes.V001, $"节点逻辑关系错误,请检查后重试!"); } var groupTreeList = tempList.GroupBy(x => new { x.LogicType, x.LogicID }).ToList(); if (groupTreeList.Exists(x => x.Count() > 1)) { throw YOops.Oh(eResultCode.Alert, ErrorCodes.V001, $"节点不能重复,请检查后重试!"); } } var treeList = input.TreeList?.Adapt>(); var bol = _service.Save(input.PolicyID, treeList); return bol; } #endregion } }