using APT.BaseData.Domain.Msg;
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/UserCustomConfigHTab")]
	[APT.Infrastructure.Api.RootOrg]
	public class UserCustomConfigHTabController : AuthorizeApiController<T_FM_USER_CUSTOM_CONFIG_H_TAB>
	{

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

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

		/// <summary>
		/// 保存用户自定义配置
		/// filter.Keyword:用户ID, filter.Parameter1:表ID,filter.Parameter2:隐藏列ID  多个用逗号隔开
		/// </summary>
		/// <param name="filter"></param>
		/// <returns></returns>
		[HttpPost, Route("SaveHiddenTableConfig")]
		public JsonActionResult<bool> SaveHiddenTableConfig([FromBody] KeywordFilter filter)
		{
			return SafeExecute<bool>(() =>
			{
				string userId = filter.Keyword;//用户ID
				string pageTableId = filter.Parameter1;//表ID
				string hiddenCols = filter.Parameter2;//隐藏列ID  多个用逗号隔开
				if (string.IsNullOrEmpty(userId))
					throw new Exception(ErrMsg.FM_NO_USER_ID);
				if (string.IsNullOrEmpty(pageTableId))
					throw new Exception(ErrMsg.FM_NO_PAGE_TABLE_ID);
				//不存在新增用户配置表
				var customConfig = this.GetEntity<T_FM_USER_CUSTOM_CONFIG>(t => t.USER_ID == new Guid(userId));
				if (customConfig == null)
				{
					customConfig = new T_FM_USER_CUSTOM_CONFIG();
					customConfig.ORG_ID = filter.OrgId;
					customConfig.USER_ID = new Guid(userId);
					this.AddEntity(customConfig);
				}
				//不存在新增隐藏表
				var customConfigHTab = this.GetEntity<T_FM_USER_CUSTOM_CONFIG_H_TAB>(t => t.USER_CUSTOM_CONFIG_ID== customConfig.ID&& t.PAGE_TABLE_ID == new Guid(pageTableId));
				if (customConfigHTab == null)
				{
					customConfigHTab = new T_FM_USER_CUSTOM_CONFIG_H_TAB();
					customConfigHTab.ORG_ID = filter.OrgId;
					customConfigHTab.PAGE_TABLE_ID = new Guid(pageTableId);
					customConfigHTab.USER_CUSTOM_CONFIG_ID = customConfig.ID;
					this.AddEntity(customConfigHTab);
				}


				List<Guid> deleteIds = new List<Guid>();
				List<T_FM_USER_CUSTOM_CONFIG_H_COL> addHCols = new List<T_FM_USER_CUSTOM_CONFIG_H_COL>();
				//数据库已有数据
				var dbHCols = this.GetEntities<T_FM_USER_CUSTOM_CONFIG_H_COL>(t => t.USER_CUSTOM_CONFIG_H_TAB_ID == customConfigHTab.ID, new BaseFilter()).ToList();
				var hCols = string.IsNullOrEmpty(hiddenCols) ? null : hiddenCols.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
				//删除
				if (dbHCols != null && dbHCols.Any())
				{
					foreach (var item in dbHCols)
					{
						if (hCols == null || !hCols.Contains(item.COLUMN_ID.ToString()))
							deleteIds.Add(item.ID);
					}
				}
				//新增
				if(hCols!=null&& hCols.Length > 0)
				{
					foreach(var item in hCols)
					{
						if (dbHCols == null || !dbHCols.Any(t => t.COLUMN_ID.ToString() == item))
						{
							T_FM_USER_CUSTOM_CONFIG_H_COL hCol = new T_FM_USER_CUSTOM_CONFIG_H_COL();
							hCol.ORG_ID = filter.OrgId;
							hCol.USER_CUSTOM_CONFIG_H_TAB_ID = customConfigHTab.ID;
							hCol.COLUMN_ID = new Guid(item);
							addHCols.Add(hCol);
						}
					}
				}
				if (deleteIds.Any())
					this.DeleteEntity<T_FM_USER_CUSTOM_CONFIG_H_COL>(t => deleteIds.Contains(t.ID));
				if (addHCols.Any())
					this.BantchAddEntity(addHCols);
				return true;
			});
		}


	}
}
