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

namespace APT.PF.WebApi.Controllers.Api.Table
{
    [Route("api/PF/PagePicture")]
    [APT.Infrastructure.Api.RootOrg]
    public class PagePictureController : AuthorizeApiController<T_PF_FORM>
    {

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetPicturePageConfigInfo")]
        public JsonActionResult<T_PF_IMG_FILE> GetPicturePageConfigInfo([FromBody] KeywordFilter filter)
        {
            return SafeExecute(() =>
            {
                var data = new T_PF_IMG_FILE();
                string formCode = filter.Keyword;
                string formId = filter.Parameter1;
                filter.OrgType = FilterOrgTypeEnum.仅本组织;
                Expression<Func<T_PF_FORM, bool>> expression = t => true;
                T_PF_FORM form = null;
                if (!string.IsNullOrEmpty(formCode))
                    form = this.GetEntityByRedis<T_PF_FORM>(formCode, filter.GetOrgId());
                else if (!string.IsNullOrEmpty(formId))
                    form = this.GetEntityByRedis<T_PF_FORM>(formId, filter.GetOrgId());
                else
                    return null;
                if (form == null)
                    return null;
                var imgCode = form.URI;
                var picture = GetEntity<T_BD_PICTURE>(i => i.CODE == imgCode, "Nav_PictureFiles", "Nav_PictureFiles.Nav_ImgFile");
                if (picture == null)
                    return null;
                var files = picture.Nav_PictureFiles;
                data = files.FirstOrDefault().Nav_ImgFile;

                return data;
            });
        }
    }
}
