using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Enums;
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;namespace APT.PF.WebApiControllers.Api.PF
{
	[Route("api/PF/PageEditPanel")]
	[APT.Infrastructure.Api.RootOrg]
	public class PageEditPanelController : AuthorizeApiController<T_PF_PAGE_EDIT_PANEL>
	{

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

		[HttpPost, Route("OrderEntities")]
		public JsonActionResult<IEnumerable<T_PF_PAGE_EDIT_PANEL>> OrderEntities([FromBody]KeywordFilter filter)
		{
			return WitOrderEntities(null, filter);
		}
		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="pageFilter"></param>
		/// <returns></returns>
		[HttpPost, Route("Paged")]
		public PagedActionResult<T_PF_PAGE_EDIT_PANEL> Paged([FromBody]KeywordPageFilter pageFilter)
		{
			return WitPaged(null, pageFilter);
		}
		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="pageFilter"></param>
		/// <returns></returns>
		[HttpPost, Route("OrderPaged")]
		public PagedActionResult<T_PF_PAGE_EDIT_PANEL> OrderPaged([FromBody]KeywordPageFilter pageFilter)
		{
			return WitOrderPaged(null, pageFilter);
		}
		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpGet, Route("Delete")]
		public JsonActionResult<bool> Delete(string id)
		{
			return SafeExecute<bool>(() =>
			{
				var formService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFFormService>();
				formService.CreateFormConfigVersion(PFFormConfigVersionEnum.PageEditPanel, id);
				List<Guid> ids = new List<Guid>();
				ids.Add(new Guid(id));
				this.DoDelete(ids);
				return true;
			});
		}

		private void DoDelete(List<Guid> ids)
		{
			var cols = this.GetEntities<T_PF_EDIT_COLUMN>(t => ids.Contains(t.PAGE_EDIT_PANEL_ID ?? Guid.Empty), new BaseFilter()).ToList();
			if (cols.Any())
			{
				cols.ForEach(t =>
				{
					t.IS_DEFAULT = false;
					t.PAGE_EDIT_PANEL_ID = null;
				});
			}
			if (cols.Any())
				this.BantchUpdateEntity(cols);
			this.DeleteEntity<T_PF_PAGE_EDIT_PANEL>(t => ids.Contains(t.ID));
		}

		/// <summary>
		/// 更新
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPost, Route("Update")]
		public JsonActionResult<bool> Update([FromBody]T_PF_PAGE_EDIT_PANEL entity)
		{
			return SafeExecute<bool>(() =>
			{
				this.UpdateEntity(entity);
				var formService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFFormService>();
				formService.CreateFormConfigVersion(PFFormConfigVersionEnum.PageEditPanel, entity.ID.ToString());
				return true;
			});
		}

		/// <summary>
		/// 批量删除
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpGet, Route("BatchDelete")]
		public JsonActionResult<bool> BatchDelete(string ids)
		{
			return SafeExecute<bool>(() =>
			{
				var formService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFFormService>();
				formService.CreateFormConfigVersion(PFFormConfigVersionEnum.PageEditPanel, ids);
				var arrays = string.IsNullOrEmpty(ids) ? null : ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
						.Select(t => new Guid(t)).ToList();
				if (arrays != null && arrays.Count > 0)
					this.DoDelete(arrays);
				return true;
			});
		}


		/// <summary>
		/// 获得单条实体数据
		/// </summary>
		/// <param name="filter"></param>
		/// <returns></returns>
		[HttpPost, Route("Get")]
		public JsonActionResult<T_PF_PAGE_EDIT_PANEL> Get([FromBody] KeywordFilter filter)
		{
			return WitEntity(null, filter);
		}
	}
}
