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

using APT.Utility;namespace APT.PF.WebApiControllers.Api.PF
{
	[Route("api/PF/EditColumn")]
    [APT.Infrastructure.Api.RootOrg]
    public class EditColumnController : AuthorizeApiController<T_PF_EDIT_COLUMN>
    {
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("Entities")]
        public JsonActionResult<IEnumerable<T_PF_EDIT_COLUMN>> Entities([FromBody]KeywordFilter filter)
        {
            return WitEntities(null, filter);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("OrderEntities")]
        public JsonActionResult<IEnumerable<T_PF_EDIT_COLUMN>> OrderEntities([FromBody]KeywordFilter filter)
        {
            return SafeExecute<IEnumerable<T_PF_EDIT_COLUMN>>(() =>
          {
              var data = this.GetOrderEntities<T_PF_EDIT_COLUMN>(null, filter); ;
			  data.Where(i => !string.IsNullOrEmpty(i.ENUM))
					.ForEach(i => i.ENUM = DataHelper.EnumToString(i.ENUM));
			  return data;
          });
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("Paged")]
        public PagedActionResult<T_PF_EDIT_COLUMN> Paged([FromBody]KeywordPageFilter pageFilter)
        {
            return WitPaged(null, pageFilter);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("OrderPaged")]
        public PagedActionResult<T_PF_EDIT_COLUMN> OrderPaged([FromBody]KeywordPageFilter pageFilter)
        {
            return WitOrderPaged(null, pageFilter);
        }
        [HttpPost, Route("Update")]
        public JsonActionResult<bool> Update([FromBody]T_PF_EDIT_COLUMN entity)
        {
			return SafeExecute<bool>(() =>
			{
				this.UpdateEntity(entity);
				var formService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFFormService>();
				formService.CreateFormConfigVersion(PFFormConfigVersionEnum.EditColumn, entity.ID.ToString());
				return true;
			});
		}
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("Delete")]
        public JsonActionResult<bool> Delete(string id)
        {
			return SafeExecute<bool>(() =>
			{
				var formService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFFormService>();
				formService.CreateFormConfigVersion(PFFormConfigVersionEnum.EditColumn, id);
				List<Guid> ids = new List<Guid>();
				ids.Add(new Guid(id));
				this.DoDelete(ids);
				return true;
			});
		}

		private void DoDelete(List<Guid> ids)
		{
			this.DeleteEntity<T_PF_EDIT_COLUMN_FILL_MAP_D>(t => ids.Contains(t.Nav_EditColumnFillMap.EDIT_COLUMN_ID));
			this.DeleteEntity<T_PF_EDIT_COLUMN_FILL_MAP>(t => ids.Contains(t.EDIT_COLUMN_ID));
			this.DeleteEntity<T_PF_EDIT_COLUMN_FILTER>(t => ids.Contains(t.EDIT_COLUMN_ID));
			this.DeleteEntity<T_FM_USER_C_C_EDIT_H_COL>(t => ids.Contains(t.EDIT_COLUMN_ID));
			this.DeleteEntity<T_FM_USER_C_C_EDIT_COL>(t => ids.Contains(t.EDIT_COLUMN_ID));
			this.DeleteEntity<T_PF_EDIT_COLUMN>(t => ids.Contains(t.ID));
		}

		/// <summary>
		/// 批量删除
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpGet, Route("BatchDelete")]
        public JsonActionResult<bool> BatchDelete(string ids)
        {
			return SafeExecute<bool>(() =>
			{
				var formService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFFormService>();
				formService.CreateFormConfigVersion(PFFormConfigVersionEnum.EditColumn, ids);
				var arrays = string.IsNullOrEmpty(ids) ? null : ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
						.Select(t => new Guid(t)).ToList();
				if (arrays != null && arrays.Count > 0)
					this.DoDelete(arrays);
				return true;
			});
		}
        /// <summary>
        /// 根据ID获得实体数据
        /// </summary>
        /// <param name="Get"></param>
        /// <returns></returns>
        [HttpPost, Route("Get")]
        public JsonActionResult<T_PF_EDIT_COLUMN> Get([FromBody] KeywordFilter filter)
        {
            return WitEntity(null, filter);
        }

    }
}
