namespace APT.PF.WebApi.Controllers.Api
{
    using APT.Infrastructure.Core;
    using Microsoft.AspNetCore.Mvc;
    using System.Collections.Generic;
    using APT.Utility;
    using APT.Infrastructure.Api;
    using APT.BaseData.Domain.ApiModel.PF;
    using APT.BaseData.Domain.Entities;
    using APT.BaseData.Domain.Enums.PF;
    using System.Linq;
    using System;
    using APT.BaseData.Domain.IServices.FM;
    using APT.MS.Domain.Enums;
    using System.Dynamic;
    using System.Reflection;
    using APT.BaseData.Domain.Entities.PF;
    using APT.BaseData.Domain.Entities.FM;
    using System.Linq.Expressions;
    using APT.MS.Domain.Entities.BS;
    using APT.BaseData.Domain.Enums;
    using APT.BaseData.Domain.IServices;
    using APT.MS.Domain.Entities.HM;
    using Newtonsoft.Json;
    using NPOI.Util;
    using System.Data;
    using NPOI.SS.Formula.Functions;
    using APT.MS.Domain.Entities.SC;
    using APT.Infrastructure.EF;
    using APT.BaseData.Services.DomainServices;
    using APT.BaseData.Services.Services.FM;
    using APT.BaseData.Services.Sys;
    using APT.MS.Domain.Entities.FO;

    /// <summary>
    /// 
    /// </summary>
    [Route("api/PF/PFFileDB")]
    public partial class PFFileDBController : AuthorizeApiController<T_PF_FILE_DB>
    {
        /// <summary>
        /// 更新或新增数据
        /// </summary>
        /// <param name="entity">对象实体</param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_PF_FILE_DB entity)
        {
            //视频文件上传不上传到本地再上传到播放播服务中
            //1、视频文件大，占用容量
            //2、IIS有限制IIS上传附件大小 本地服务器暂时不做大附件处理
            return SafeExecute<bool>(() =>
            {
                if (string.IsNullOrEmpty(entity.NAME))
                {
                    throw new Exception("视频库名称不能为空！");
                }
                var files = entity.Nav_Files;
                entity.Nav_Files = null;
                if (files != null && files.Any())
                {
                    foreach (var item in files)
                    {
                        item.Nav_FileDB = null;
                    }
                }

                var check = GetEntity<T_PF_FILE_DB>(e => e.TYPE_ID == entity.TYPE_ID && e.NAME == entity.NAME);
                if (check != null)
                {
                    throw new Exception("视频库中已存在该视频类型与视频名称，不能重复添加！");
                }
                UnifiedCommit(() =>
                {
                    this.UpdateEntityNoCommit(entity);
                    if (files != null && files.Any())
                        BantchSaveEntityNoCommit(files);
                });
                return true;
            });
        }
    }
}