using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.IServices;
using APT.Infrastructure.Core;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using APT.Utility;
using APT.Utility;namespace APT.PF.WebApiControllers.Api.PF
{
	[Route("api/PF/AppMenu")]
    public class AppMenuController : AuthorizeTreeApiController<T_PF_APPMENU>
    {
        
        IPFAppMenuService MenuService { get; set; }

        public AppMenuController(IPFAppMenuService menuService)
        {
            MenuService = menuService;
        }
		 

		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="pageFilter"></param>
		/// <returns></returns>
		[HttpPost, Route("Entities")]
		public JsonActionResult<IEnumerable<T_PF_APPMENU>> Entities([FromBody]KeywordFilter filter)
		{
			return WitEntities(null, filter);
		}
		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="pageFilter"></param>
		/// <returns></returns>
		[HttpPost, Route("OrderEntities")]
		public JsonActionResult<IEnumerable<T_PF_APPMENU>> OrderEntities([FromBody]KeywordFilter filter)
		{
			return WitOrderEntities(null, filter);

		}
		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="pageFilter"></param>
		/// <returns></returns>
		[HttpPost, Route("Paged")]
		public PagedActionResult<T_PF_APPMENU> Paged([FromBody]KeywordPageFilter pageFilter)
		{
			return WitPaged(null, pageFilter);
		}
		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="pageFilter"></param>
		/// <returns></returns>
		[HttpPost, Route("OrderPaged")]
		public PagedActionResult<T_PF_APPMENU> OrderPaged([FromBody]KeywordPageFilter pageFilter)
		{
			return WitOrderPaged(null, pageFilter);
		}
		/// <summary>
		/// 更新
		/// </summary>
		/// <param name="entity"></param>
		/// <returns></returns>
		[HttpPost, Route("Update")]
		public JsonActionResult<bool> Update([FromBody]T_PF_APPMENU entity)
		{
			return SafeExecute<bool>(() =>
			{
				T_PF_APPMENU parentMenu = null;
				if (entity.PARENT_ID != null)
				{
					parentMenu = this.GetEntity<T_PF_APPMENU>(entity.PARENT_ID.ToString());
					parentMenu.HAS_CHILDREN = true;
					entity.MENU_LEVEL = parentMenu.MENU_LEVEL + 1;
					parentMenu.IS_LEAF = false;
				}
				else
					entity.MENU_LEVEL = 1;
				UnifiedCommit(() =>
				{
					this.TreeUpdateEntityNoCommit(entity);
					if (parentMenu != null)
						this.TreeUpdateEntityNoCommit(parentMenu);
				});
				return true;
			});
		}
		/// <summary>
		/// 获取子菜单ID
		/// </summary>
		/// <param name="parendIds"></param>
		/// <param name="childIds"></param>
		private void GetChildIds(List<Guid> parendIds, List<Guid> childIds)
		{
			var tmp = this.GetEntities<T_PF_APPMENU>(t => parendIds.Contains(t.PARENT_ID ?? Guid.Empty), new BaseFilter()).Select(t => t.ID).ToList();
			if (tmp != null && tmp.Any())
			{
				childIds.AddRange(tmp);
				GetChildIds(tmp, childIds);
			}
		}
		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpGet, Route("Delete")]
		public JsonActionResult<bool> Delete(string id)
		{
			return SafeExecute<bool>(() => {
				if (string.IsNullOrEmpty(id)) return false;
				List<Guid> deleteIds = new List<Guid>();
				deleteIds.Add(new Guid(id));
				List<Guid> parendIds = new List<Guid>();
				parendIds.Add(new Guid(id));
				GetChildIds(parendIds, deleteIds);
				this.TreeDeleteEntity<T_PF_APPMENU>(t => deleteIds.Contains(t.ID));
				return true;
			});
		}

		/// <summary>
		/// 批量删除
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpGet, Route("BatchDelete")]
		public JsonActionResult<bool> BatchDelete(string ids)
		{
			return SafeExecute<bool>(() => {
				if (string.IsNullOrEmpty(ids)) return false;
				string[] tmp = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
				if (tmp == null || tmp.Length == 0) return false;
				List<Guid> deleteIds = new List<Guid>();
				List<Guid> parendIds = new List<Guid>();
				tmp.ForEach(t =>
				{
					deleteIds.Add(new Guid(t));
					parendIds.Add(new Guid(t));
				});
				GetChildIds(parendIds, deleteIds);
				this.TreeDeleteEntity<T_PF_APPMENU>(t => deleteIds.Contains(t.ID));
				return true;
			});
		}
		/// <summary>
		/// 根据ID获得实体数据
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPost, Route("Get")]
		public JsonActionResult<T_PF_APPMENU> Get([FromBody] KeywordFilter filter)
		{
			return WitEntity(null, filter);
		}

		 
    }
}
