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

using APT.Utility;namespace APT.PF.WebApiControllers.Api.PF
{

	/// <summary>
	/// 系统日志
	/// </summary>
	[Route("api/PF/SysLog")]
	public class SysLogController : AuthorizeApiController<T_PF_SYS_LOG>
	{

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

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




		/// <summary>
		/// 添加登出日志
		/// </summary>
		/// <param name="filter">
		/// Keyword：用户 ;
		/// Parameter1:平台类型; 
		/// </param>
		/// <returns></returns>
		[HttpPost, Route("AddLoginOut")]
		public JsonActionResult<bool> AddLoginOut([FromBody] KeywordFilter filter)
		{
			return SafeExecute<bool>(() =>
			{
				try
				{
					string userId = filter.Keyword;//用户
					int platformType = LibUtils.ToInt( filter.Parameter1);//平台类型
					if (!string.IsNullOrEmpty(userId))
					{
						var sysLogService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFSysLogService>(); 
						sysLogService.AddLoginOut(new Guid(userId), platformType);
					}
				}
				catch
				{

				}
				return true;
			});
		}

		/// <summary>
		/// 添加新增日志
		/// </summary>
		/// <param name="filter">
		/// Keyword：用户 ;
		/// Parameter1:表单编号;
		/// Parameter2:表单ID; 
		/// Parameter3:ID列表; 
		/// Parameter4:编号列表
		/// Parameter5:表名
		/// </param>
		/// <returns></returns>
		[HttpPost, Route("AddAddLog")]
		public JsonActionResult<bool> AddAddLog([FromBody] KeywordFilter filter)
		{
			return SafeExecute<bool>(() =>
			{
				try
				{
					string userId = filter.Keyword;//用户
					string formCode = filter.Parameter1;//表单编号
					string formId = filter.Parameter2;//表单ID
					string ids = filter.Parameter3;//ID列表
					string codes = filter.Parameter4;//编号列表
					string extData = filter.Parameter5;//额外数据
					if (!string.IsNullOrEmpty(userId))
					{
						if (string.IsNullOrEmpty(formCode) && !string.IsNullOrEmpty(formId))
						{
							var form = this.GetEntity<T_PF_FORM>(formId);
							if (form != null)
								formCode = form.CODE;
						}
						var sysLogService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFSysLogService>();
						sysLogService.AddAddLog(new Guid(userId), formCode, ids, codes, extData);
					}
				}
				catch
				{

				}
				return true;
			});
		}


		/// <summary>
		/// 添加编辑日志
		/// </summary>
		/// <param name="filter">
		/// Keyword：用户 ;
		/// Parameter1:表单编号;
		/// Parameter2:表单ID; 
		/// Parameter3:ID列表; 
		/// Parameter4:编号列表
		/// Parameter5:额外数据
		/// </param>
		/// <returns></returns>
		[HttpPost, Route("AddEditLog")]
		public JsonActionResult<bool> AddEditLog([FromBody] KeywordFilter filter)
		{
			return SafeExecute<bool>(() =>
			{
				try
				{
					string userId = filter.Keyword;//用户
					string formCode = filter.Parameter1;//表单编号
					string formId = filter.Parameter2;//表单ID
					string ids = filter.Parameter3;//ID列表
					string codes = filter.Parameter4;//编号列表
					string extData = filter.Parameter5;//额外数据
					if (!string.IsNullOrEmpty(userId))
					{
						if (string.IsNullOrEmpty(formCode) && !string.IsNullOrEmpty(formId))
						{
							var form = this.GetEntity<T_PF_FORM>(formId);
							if (form != null)
								formCode = form.CODE;
						}
						var sysLogService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFSysLogService>();
						sysLogService.AddEditLog(new Guid(userId), formCode,  ids, codes, extData);
					}
				}
				catch
				{

				}
				return true;
			});
		}


		/// <summary>
		/// 添加删除日志
		/// </summary>
		/// <param name="filter">
		/// Keyword：用户 ;
		/// Parameter1:表单编号;
		/// Parameter2:表单ID; 
		/// Parameter3:ID列表; 
		/// Parameter4:表名
		/// </param>
		/// <returns></returns>
		[HttpPost, Route("AddDeleteLog")]
		public JsonActionResult<bool> AddDeleteLog([FromBody] KeywordFilter filter)
		{
			return SafeExecute<bool>(() =>
			{
				try
				{
					string userId = filter.Keyword;//用户
					string formCode = filter.Parameter1;//表单编号
					string formId = filter.Parameter2;//表单ID
					string ids = filter.Parameter3;//ID列表
					string tableName = filter.Parameter4;//表名
                    string extData = filter.Parameter5;//额外信息
                    if (!string.IsNullOrEmpty(userId))
					{
						if (string.IsNullOrEmpty(formCode) && !string.IsNullOrEmpty(formId))
						{
							var form = this.GetEntity<T_PF_FORM>(formId);
							if (form != null)
								formCode = form.CODE;
						}
						var sysLogService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFSysLogService>();
						sysLogService.AddDeleteLog(new Guid(userId), formCode, ids, tableName, extData);
					}
				}
				catch
				{

				}
				return true;
			});
		}

		/// <summary>
		/// 添加删除日志
		/// </summary>
		/// <param name="filter">
		/// Keyword：用户 ;
		/// Parameter1:表单编号;
		/// Parameter2:表单ID; 
		/// Parameter3:标题; 
		/// Parameter4:数据
		/// Parameter5:额外数据
		/// </param>
		/// <returns></returns>
		[HttpPost, Route("AddOtherLog")]
		public JsonActionResult<bool> AddOtherLog([FromBody] KeywordFilter filter)
		{
			return SafeExecute<bool>(() =>
			{
				try
				{
					string userId = filter.Keyword;//用户
					string formCode = filter.Parameter1;//表单编号
					string formId = filter.Parameter2;//表单ID
					string title = filter.Parameter3;//标题
					string data = filter.Parameter4;//数据
					string extData = filter.Parameter5;//额外数据
					if (!string.IsNullOrEmpty(userId))
					{
						if (string.IsNullOrEmpty(formCode) && !string.IsNullOrEmpty(formId))
						{
							var form = this.GetEntity<T_PF_FORM>(formId);
							if (form != null)
								formCode = form.CODE;
						}
						var sysLogService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFSysLogService>();
						sysLogService.AddOtherLog(new Guid(userId), formCode, title, data, extData);
					}
				}
				catch
				{

				}
				return true;
			});
		}
	}
}
