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
{
	/// <summary>
	/// 角色组
	/// </summary>
	[Route("api/FM/RoleGroup")]
	public class RoleGroupController : AuthorizeApiController<T_FM_ROLE_GROUP>
	{

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

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

		/// <summary>
		/// 更新
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPost, Route("Update")]
		public JsonActionResult<bool> Update([FromBody]T_FM_ROLE_GROUP entity)
		{
			return SafeExecute<bool>(() =>
			{
				var belongRoles = entity.Nav_BelongRoles;
				entity.Nav_BelongRoles = null;
				
				if (belongRoles != null && belongRoles.Any())
					belongRoles.ForEach(t => t.ROLE_GROUP_ID = entity.ID);

				List<Guid> deleteBelongRoleIds = new List<Guid>();

				var dbEntity = this.GetEntity<T_FM_ROLE_GROUP>(entity.ID.ToString(), new string[] {  "Nav_BelongRoles" });
				if (dbEntity != null)
				{
					
					if (dbEntity.Nav_BelongRoles != null)
					{
						dbEntity.Nav_BelongRoles.ForEach(item =>
						{
							if (belongRoles==null|| belongRoles.Count==0||!belongRoles.Any(i => i.ID == item.ID))
								deleteBelongRoleIds.Add(item.ID);
						});
					}
					
				}

				UnifiedCommit(() =>
				{
					this.UpdateEntityNoCommit(entity);
					if (belongRoles != null && belongRoles.Any())
						this.BantchSaveEntityNoCommit(belongRoles);
					if (deleteBelongRoleIds.Any())
						this.DeleteEntityNoCommit<T_FM_ROLE_GROUP_BELONG_ROLE>(t => deleteBelongRoleIds.Contains(t.ID));

				});
				return true;
			});
		}

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


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

	}
}
