namespace Yw.Application { /// /// ProductMenu /// [Route("Auth/Product/Menu")] [ApiDescriptionSettings("Auth", Name = "产品菜单", Order = 9900)] public class ProductMenu_Controller : IDynamicApiController { private readonly Service.ProductMenu _service = new(); #region Query /// /// 通过 ProductID 获取 /// [Route("GetByProductID@V1.0")] [HttpGet] public List GetByProductID([FromQuery][Required] ProductIDInput input) { var list = _service.GetByProductID(input.ProductID); var vmList = list?.Select(x => new ProductMenuDto(x)).ToList(); return vmList; } /// /// 通过 ID 获取 /// [Route("GetByID@V1.0")] [HttpGet] public ProductMenuDto GetByID([FromQuery][Required] IDInput input) { var model = _service.GetByID(input.ID); return model == null ? null : new ProductMenuDto(model); } /// /// 通过 Ids 获取 /// [Route("GetByIds@V1.0")] [HttpGet] public List GetByIds([FromQuery][Required] IdsInput model) { var ids = LongListHelper.ToList(model.Ids); var list = _service.GetByIds(ids); var vmList = list?.Select(x => new ProductMenuDto(x)).ToList(); return vmList; } #endregion #region Insert /// /// 插入一条 /// [Route("Insert@V1.0")] [HttpPost] public long Insert([Required] AddProductMenuInput input) { var product = new Service.Product().GetByID(input.ProductID); if (product == null) { throw YOops.Oh(eResultCode.Alert, InternalErrorCodes.D001, $"ProductID:{input.ProductID} 数据不存在"); } var parentIds = new List(); if (input.ParentID > 0) { var parent = _service.GetByID(input.ParentID); if (parent == null) { throw YOops.Oh(eResultCode.Alert, InternalErrorCodes.D001, $"ParentID:{input.ParentID} 数据不存在"); } if (parent.ProductID != product.ID) { throw YOops.Oh(eResultCode.Alert, InternalErrorCodes.V001, $"ParentID:{input.ParentID} 隶属于不同产品"); } if (input.Weight < parent.Weight) { throw YOops.Oh(eResultCode.Alert, InternalErrorCodes.V001, $"Weight:{input.Weight} 不能低于父级权重"); } parentIds = TreeParentIdsHelper.GetChildParentIds(parent.ID, parent.ParentIds); } var model = input.Adapt(); model.ParentIds = parentIds; model.SortCode = _service.GetMaxSortCode(input.ProductID, input.ParentID) + 1; var id = _service.Insert(model); return id; } #endregion #region Update /// /// 更新一条 ★ /// [Route("Update@V1.0")] [HttpPut] public bool Update([Required] UpdateProductMenuInput input) { var model = _service.GetByID(input.ID); if (model == null) { throw YOops.Oh(eResultCode.Alert, InternalErrorCodes.D001, $"ID:{input.ID} 数据不存在"); } var parentId = model.ParentIds.LastOrDefault(); if (parentId > 0) { var parent = _service.GetByID(parentId); if (input.Weight < parent.Weight) { throw YOops.Oh(eResultCode.Alert, InternalErrorCodes.V001, $"Weight:{input.Weight} 不能低于父级权重"); } } var rhs = new Model.ProductMenu(model); input.Adapt(rhs); var bol = _service.Update(rhs); return bol; } /// /// 更新排序码 ★ /// [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 inputList) { var list = inputList.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 YOops.Oh(eResultCode.Alert, InternalErrorCodes.D001, $"ID:{input.ID} 数据不存在"); } var parentIds = new List(); if (input.ParentID > 0) { var parent = _service.GetByID(input.ParentID); if (parent == null) { throw YOops.Oh(eResultCode.Alert, InternalErrorCodes.D001, $"ParentID:{input.ParentID} 数据不存在"); } if (parent.ProductID != model.ProductID) { throw YOops.Oh(eResultCode.Alert, InternalErrorCodes.V001, $"ParentID:{input.ParentID} 隶属于不同产品"); } parentIds = TreeParentIdsHelper.GetChildParentIds(parent.ID, parent.ParentIds); } var sorterList = new List() { new 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 Model.TreeSorter() { ID = item.ID, ParentIds = itemParentIds, SortCode = item.SortCode }; sorterList.Add(sorter); } } } var bol = _service.UpdateTreeSorter(sorterList); return bol; } #endregion #region Delete /// /// 通过 ID 删除 /// [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, InternalErrorCodes.D999, msg); } return bol; } #endregion } }