using APT.Infrastructure.Core;
using APT.BaseData.Domain.Entities.FM;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;

using APT.Utility;namespace APT.FM.WebApi.Controllers.Api.FM
{
	[Route("api/FM/ImportConfigDetail")]
	public class ImportConfigDetailController : AuthorizeApiController<T_FM_IMPORT_CONFIG_DETAIL>
	{
		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="filter"></param>
		/// <returns></returns>
		[HttpPost, Route("Entities")]
		public JsonActionResult<IEnumerable<T_FM_IMPORT_CONFIG_DETAIL>> Entities([FromBody]KeywordFilter filter)
		{
			return WitEntities(null, filter);
		}
		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="filter"></param>
		/// <returns></returns>
		/// 

		[HttpPost, Route("OrderEntities")]
		public JsonActionResult<IEnumerable<T_FM_IMPORT_CONFIG_DETAIL>> OrderEntities([FromBody]KeywordFilter filter)
		{
			return WitOrderEntities(null, filter);
		}
		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="pageFilter"></param>
		/// <returns></returns>
		[HttpPost, Route("Paged")]
		public PagedActionResult<T_FM_IMPORT_CONFIG_DETAIL> Paged([FromBody]KeywordPageFilter pageFilter)
		{
			return WitPaged(null, pageFilter);
		}
		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="pageFilter"></param>
		/// <returns></returns>
		[HttpPost, Route("OrderPaged")]
		public PagedActionResult<T_FM_IMPORT_CONFIG_DETAIL> 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 WitDelete(id);
		}

		/// <summary>
		/// 更新
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPost, Route("Update")]
		public JsonActionResult<bool> Update([FromBody]T_FM_IMPORT_CONFIG_DETAIL entity)
		{
			return WitUpdate(entity);
		}

		/// <summary>
		/// 批量删除
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpGet, Route("BatchDelete")]
		public JsonActionResult<bool> BatchDelete(string ids)
		{
			return WitBatchDelete(ids);
		}


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

		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpGet, Route("FullDelete")]
		public JsonActionResult<bool> FullDelete(string id)
		{
			return SafeExecute<bool>(() =>
			{
				if (string.IsNullOrEmpty(id)) return false;
				this.DeleteEntity<T_FM_IMPORT_CONFIG_FIELD>(t => t.IMPORT_CONFIG_DETAIL_ID == new Guid(id));
				this.DeleteEntity<T_FM_IMPORT_CONFIG_DETAIL>(id);
				return true;
			});
		}

		/// <summary>
		/// 批量删除
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpGet, Route("FullBatchDelete")]
		public JsonActionResult<bool> FullBatchDelete(string ids)
		{
			return SafeExecute<bool>(() =>
			{
				if (string.IsNullOrEmpty(ids)) return false;
				var tmpIds = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(t => new Guid(t)).ToList();
				if (tmpIds.Any())
				{
					this.DeleteEntity<T_FM_IMPORT_CONFIG_FIELD>(t => tmpIds.Contains(t.IMPORT_CONFIG_DETAIL_ID));
					this.DeleteEntity<T_FM_IMPORT_CONFIG_DETAIL>(t => tmpIds.Contains(t.ID));
				}
				return true;

			});
		}



		/// <summary>
		/// 更新
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPost, Route("FullUpdate")]
		public JsonActionResult<bool> FullUpdate([FromBody]T_FM_IMPORT_CONFIG_DETAIL entity)
		{
			return SafeExecute<bool>(() =>
			{

				var fields= entity.Nav_Fields;
				entity.Nav_Fields = null;
				if (fields != null && fields.Any())
				{
					fields.ForEach(t =>
					{
						t.IMPORT_CONFIG_DETAIL_ID = entity.ID;
					});
				}
				this.UpdateEntity(entity);
				if (fields != null && fields.Any())
					this.BantchSaveEntity(fields);

				return true;
			});
		}


	}
}
