using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Enums;
using APT.BaseData.Domain.IServices;
using APT.Infrastructure.Core;
using APT.MS.Domain.ApiModel;
using APT.BaseData.Domain.IServices.EX;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using APT.Infrastructure.Api;
using APT.Utility;
using APT.BaseData.Domain.ApiModel;

namespace APT.PF.WebApiControllers.Api.PF
{
    [Route("api/PF/Query")]
    [APT.Infrastructure.Api.RootOrg]
    public class QueryController : AuthorizeApiController<T_PF_QUERY>
    {
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("Entities")]
        public JsonActionResult<IEnumerable<T_PF_QUERY>> Entities([FromBody] KeywordFilter filter)
        {
            return WitEntities(null, filter);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("OrderEntities")]
        public JsonActionResult<IEnumerable<T_PF_QUERY>> OrderEntities([FromBody] KeywordFilter filter)
        {
            return SafeExecute<IEnumerable<T_PF_QUERY>>(() =>
            {
                var data = this.GetOrderEntities<T_PF_QUERY>(null, filter);
                return data;
            });

        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("Paged")]
        public PagedActionResult<T_PF_QUERY> Paged([FromBody] KeywordPageFilter pageFilter)
        {
            return WitPaged(null, pageFilter);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("OrderPaged")]
        public PagedActionResult<T_PF_QUERY> OrderPaged([FromBody] KeywordPageFilter pageFilter)
        {
            return WitOrderPaged(null, pageFilter);
        }
        [HttpPost, Route("Update")]
        public JsonActionResult<bool> Update([FromBody] T_PF_QUERY entity)
        {
            return SafeExecute<bool>(() =>
            {
                this.UpdateEntity(entity);
                var formService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFFormService>();
                formService.CreateFormConfigVersion(PFFormConfigVersionEnum.Query, 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.Query, 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_QUERY>(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.Query, ids);
                var arrays = string.IsNullOrEmpty(ids) ? null : ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                        .Select(t => new Guid(t)).ToList();
                if (arrays != null && arrays.Any())
                    this.DoDelete(arrays);
                return true;
            });
        }
        /// <summary>
        /// 根据ID获得实体数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Route("Get")]
        public JsonActionResult<T_PF_QUERY> Get([FromBody] KeywordFilter filter)
        {
            return WitEntity(null, filter);
        }


        /// <summary>
        /// 获取查询字段
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetQueryFieldsByTableName")]
        public JsonActionResult<IEnumerable<EntityFieldByQueryFieldModel>> GetQueryFieldsByTableName([FromBody] KeywordFilter filter)
        {
            return SafeExecute<IEnumerable<EntityFieldByQueryFieldModel>>(() =>
            {
                string formIdStr = filter.Keyword;//表单ID;
                string curFieldName = filter.Parameter1;//当前字段名称
                string tableName = filter.Parameter2;//表名
                string lablePrefix = filter.Parameter3;//显示名称前缀
                var entityOperateService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IEntityOperateService>();
                return entityOperateService.GetEntityFieldsByTableName(tableName, curFieldName, lablePrefix, null);

            });
        }

        /// <summary>
        /// 获取查询字段
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetQueryFieldsByQueryFormId")]
        public JsonActionResult<IEnumerable<EntityFieldByQueryFieldModel>> GetQueryFieldsByQueryFormId([FromBody] KeywordFilter filter)
        {
            return SafeExecute<IEnumerable<EntityFieldByQueryFieldModel>>(() =>
            {
                string formIdStr = filter.Keyword;//表单ID;
                string curFieldName = filter.Parameter1;//当前字段名称
                string tableName = filter.Parameter2;//表名
                string lablePrefix = filter.Parameter3;//显示名称前缀
                string code = filter.Parameter4;//编号
                string expandFieldStr = filter.Parameter5;//需展开的字段
                if (string.IsNullOrEmpty(formIdStr))
                    throw new Exception("请选择表单ID");
                List<string> expandFields = null;
                if (!string.IsNullOrEmpty(expandFieldStr))
                    expandFields = expandFieldStr.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                var entityOperateService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IEntityOperateService>();
                return entityOperateService.GetEntityFieldsByQueryFormId(new Guid(formIdStr), code, curFieldName, lablePrefix, expandFields, filter);

            });
        }

    }
}
