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/UserGroup")]
	public class UserGroupController : AuthorizeApiController
	{
		/// 
		/// 查询
		/// 
		/// 
		/// 
		[HttpPost, Route("Entities")]
		public JsonActionResult> Entities([FromBody]KeywordFilter filter)
		{
			return WitEntities(null, filter);
		}
		/// 
		/// 查询
		/// 
		/// 
		/// 
		/// 
		[HttpPost, Route("OrderEntities")]
		public JsonActionResult> OrderEntities([FromBody]KeywordFilter filter)
		{
			return WitOrderEntities(null, filter);
		}
		/// 
		/// 查询
		/// 
		/// 
		/// 
		[HttpPost, Route("Paged")]
		public PagedActionResult Paged([FromBody]KeywordPageFilter pageFilter)
		{
			return WitPaged(null, pageFilter);
		}
		/// 
		/// 查询
		/// 
		/// 
		/// 
		[HttpPost, Route("OrderPaged")]
		public PagedActionResult OrderPaged([FromBody]KeywordPageFilter pageFilter)
		{
			return WitOrderPaged(null, pageFilter);
		}
		/// 
		/// 删除
		/// 
		/// 
		/// 
		[HttpGet, Route("Delete")]
		public JsonActionResult Delete(string id)
		{
			return WitRealDelete(id);
		}
		/// 
		/// 更新
		/// 
		/// 
		/// 
		[HttpPost, Route("Update")]
		public JsonActionResult Update([FromBody]T_FM_USER_GROUP entity)
		{
			return SafeExecute(() =>
			{
				var belongRoleGroups = entity.Nav_BelongRoleGroups;
				var belongRoles = entity.Nav_BelongRoles;
				var belongUserGroups = entity.Nav_BelongUserGroups;
				entity.Nav_BelongRoleGroups = null;
				entity.Nav_BelongRoles = null;
				entity.Nav_BelongUserGroups = null;
				if (belongUserGroups != null && belongUserGroups.Any())
					belongUserGroups.ForEach(t => t.USER_GROUP_ID = entity.ID);
				if (belongRoleGroups != null && belongRoleGroups.Any())
					belongRoleGroups.ForEach(t => t.USER_GROUP_ID = entity.ID);
				if (belongRoles != null && belongRoles.Any())
					belongRoles.ForEach(t => t.USER_GROUP_ID = entity.ID);
				List deleteBelongRoleIds = new List();
				List deleteBelongRoleGroupIds = new List();
				List deleteBelongUserGroupsIds = new List();
				var dbEntity = this.GetEntity(entity.ID.ToString(), new string[] { "Nav_BelongRoleGroups", "Nav_BelongRoles", "Nav_BelongUserGroups" });
				if (dbEntity != null)
				{
					if (dbEntity.Nav_BelongRoleGroups != null)
					{
						dbEntity.Nav_BelongRoleGroups.ForEach(item =>
						{
							if (belongRoleGroups==null||!belongRoleGroups.Any(i => i.ID == item.ID))
								deleteBelongRoleGroupIds.Add(item.ID);
						});
					}
					if (dbEntity.Nav_BelongRoles != null)
					{
						dbEntity.Nav_BelongRoles.ForEach(item =>
						{
							if (belongRoles == null||!belongRoles.Any(i => i.ID == item.ID))
								deleteBelongRoleIds.Add(item.ID);
						});
					}
					if (dbEntity.Nav_BelongUserGroups != null)
					{
						dbEntity.Nav_BelongUserGroups.ForEach(item =>
						{
							if (belongUserGroups==null||!belongUserGroups.Any(i => i.ID == item.ID))
								deleteBelongUserGroupsIds.Add(item.ID);
						});
					}
				}
				UnifiedCommit(() =>
				{
					this.UpdateEntityNoCommit(entity);
					if (belongRoleGroups != null && belongRoleGroups.Any())
						this.BantchSaveEntityNoCommit(belongRoleGroups);
					if (belongRoles != null && belongRoles.Any())
						this.BantchSaveEntityNoCommit(belongRoles);
					if (belongUserGroups != null && belongUserGroups.Any())
						this.BantchSaveEntityNoCommit(belongUserGroups);
					if (deleteBelongRoleIds.Any())
						this.DeleteEntityNoCommit(t => deleteBelongRoleIds.Contains(t.ID));
					if (deleteBelongRoleGroupIds.Any())
						this.DeleteEntityNoCommit(t => deleteBelongRoleGroupIds.Contains(t.ID));
					if (deleteBelongUserGroupsIds.Any())
						this.DeleteEntityNoCommit(t => deleteBelongUserGroupsIds.Contains(t.ID));
				});
				return true;
			});
		}
		/// 
		/// 批量删除
		/// 
		/// 
		/// 
		[HttpGet, Route("BatchDelete")]
		public JsonActionResult BatchDelete(string ids)
		{
			return WitRealBatchDelete(ids);
		}
		/// 
		/// 获得单条实体数据
		/// 
		/// 
		/// 
		[HttpPost, Route("Get")]
		public JsonActionResult Get([FromBody] KeywordFilter filter)
		{
			return WitEntity(null, filter);
		}
	}
}