using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Entities.BD;
using APT.BaseData.Domain.Enums;
using APT.BaseData.Domain.IServices.BD;
using APT.Infrastructure.Core;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using APT.Infrastructure.Api;
namespace APT.BD.WebApi.Controllers.Api
{
    /// <summary>
    ///  国标行业信息
    /// </summary>
    [Route("api/BD/BDWordTemplate")]
    public partial class BDWordTemplateController : CommonApiController
    {
        /// <summary>
        /// 获取word模板属性
        /// </summary>
        /// <param name="id">模板id</param>
        /// <param name="parent">模板id</param>
        /// <returns></returns>
        [HttpGet, Route("Props")]
        public JsonActionResult<IEnumerable<dynamic>> Props(string id, string type)
        {
            return SafeExecute<IEnumerable<dynamic>>(() =>
            {

                var retProps = new List<dynamic>();
                Type entityType = null;
                if (!string.IsNullOrEmpty(type))
                {
                    entityType = APT.Infrastructure.Core.Refctor.ReflectHelper.FindTypeInCurrentDomain(type);
                }
                else
                {
                    var template = this.GetEntity<T_BD_WORD_TEMPLATE>(id);
                    if (template == null)
                        return retProps;
                    entityType = APT.Infrastructure.Core.Refctor.ReflectHelper.FindTypeInCurrentDomain(template.RET_ENTITY_TYPE);
                }
                if (entityType == null)
                    return retProps;
                var fields = entityType.GetProperties();
                List<dynamic> attrs = new List<dynamic>();
                foreach (var fi in fields)
                {
                    dynamic obj = new System.Dynamic.ExpandoObject();
                    obj.IS_LEAF = true;//不是导航属性
                    if (fi.Name.Contains("ID"))
                        continue;
                    if (fi.Name.StartsWith("Nav_"))
                    {
                        //1-对多的不导航
                        if (typeof(System.Collections.IEnumerable).IsAssignableFrom(fi.PropertyType))
                        {
                            continue;
                        }
                        else
                        {
                            obj.IS_LEAF = false;
                        }
                    }
                    obj.CODE = fi.Name;
                    obj.ID = fi.Name;
                    obj.TYPE = fi.PropertyType.Name;

                    var descAtrr = fi.GetAttribute<DescriptionAttribute>();
                    if (descAtrr != null)
                    {
                        obj.NAME = descAtrr.Description;
                    }
                    else
                    {
                        obj.NAME = "";
                    }
                    attrs.Add(obj);
                }
                return attrs;
            });

        }


        [HttpPost, Route("GetTemplate")]
        public JsonActionResult<T_BD_WORD_TEMPLATE> GetTemplate([FromBody] KeywordFilter filter)
        {
            return SafeExecute<T_BD_WORD_TEMPLATE>(() =>
            {
                var retProps = new List<dynamic>();
                var template = this.GetEntityByRedis<T_BD_WORD_TEMPLATE>(filter.Keyword, filter.GetOrgId());
                return template;

            });

        }

    }
}
