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/ParamScheme")]
	public class ParamSchemeController : AuthorizeApiController<T_PF_PARAM_SCHEME>
	{
		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="pageFilter"></param>
		/// <returns></returns>
		[HttpPost, Route("Entities")]
		public JsonActionResult<IEnumerable<T_PF_PARAM_SCHEME>> Entities([FromBody]KeywordFilter filter)
		{
			return WitEntities(null, filter);
		}
		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="pageFilter"></param>
		/// <returns></returns>
		[HttpPost, Route("OrderEntities")]
		public JsonActionResult<IEnumerable<T_PF_PARAM_SCHEME>> OrderEntities([FromBody]KeywordFilter filter)
		{
			return WitOrderEntities(null, filter);
		}
		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="pageFilter"></param>
		/// <returns></returns>
		[HttpPost, Route("Paged")]
		public PagedActionResult<T_PF_PARAM_SCHEME> Paged([FromBody]KeywordPageFilter pageFilter)
		{
			return WitPaged(null, pageFilter);
		}
		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="pageFilter"></param>
		/// <returns></returns>
		[HttpPost, Route("OrderPaged")]
		public PagedActionResult<T_PF_PARAM_SCHEME> OrderPaged([FromBody]KeywordPageFilter pageFilter)
		{
			return WitOrderPaged(null, pageFilter);
		}
		[HttpPost, Route("Update")]
		public JsonActionResult<bool> Update([FromBody]T_PF_PARAM_SCHEME entity)
		{
			return SafeExecute<bool>(() =>
			{
				this.UpdateEntity(entity);
				var formService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFFormService>();
				formService.CreateFormConfigVersion(PFFormConfigVersionEnum.ParamScheme, entity.ID.ToString());
				return true;
			});
		}
		/// <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.ParamScheme, id);
				List<Guid> ids = new List<Guid>();
				ids.Add(new Guid(id));
				this.DoDelete(ids);
				return true;
			});
		}
		private void DoDelete(List<Guid> ids)
		{
			var tempPageTables = this.GetEntities<T_PF_PAGE_TABLE>(t => ids.Contains(t.PARAM_SCHEME_ID ?? Guid.Empty), new BaseFilter()).ToList();
			var tempPageEdits = this.GetEntities<T_PF_PAGE_EDIT>(t => ids.Contains(t.PARAM_SCHEME_ID ?? Guid.Empty), new BaseFilter()).ToList();

			if (tempPageTables.Any())
			{
				tempPageTables.ForEach(t =>
				{ 
					t.PARAM_SCHEME_ID = null;
				});
			}
			if (tempPageEdits.Any())
			{
				tempPageEdits.ForEach(t =>
				{
					t.PARAM_SCHEME_ID = null;
				});
			} 
			if (tempPageTables.Any())
				this.BantchUpdateEntity(tempPageTables);
			if (tempPageEdits.Any())
				this.BantchUpdateEntity(tempPageEdits);
			this.DeleteEntity<T_PF_PARAM_SCHEME_DETAIL>(t => ids.Contains(t.PARAM_SCHEME_ID));
			this.DeleteEntity<T_PF_PARAM_SCHEME>(t => ids.Contains(t.ID));
		}
		/// <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.ParamScheme, 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>
		/// 根据ID获得实体数据
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPost, Route("Get")]
		public JsonActionResult<T_PF_PARAM_SCHEME> Get([FromBody] KeywordFilter filter)
		{
			return WitEntity(null, filter);
		}

		[HttpPost, Route("FullUpdate")]
		public JsonActionResult<bool> FullUpdate([FromBody] T_PF_PARAM_SCHEME entity)
		{
			return SafeExecute<bool>(() =>
			{
				var details= entity.Nav_Details;
				entity.Nav_Details = null;

				UnifiedCommit(() =>
				{
					this.UpdateEntityNoCommit(entity);
					if (details != null)
						this.BantchSaveEntityNoCommit(details);
				});
				var formService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFFormService>();
				formService.CreateFormConfigVersion(PFFormConfigVersionEnum.ParamScheme, entity.ID.ToString());
				return true;
			});
		}

	}
}
