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

namespace APT.BD.WebApi.Controllers.Api
{
    /// <summary>
    ///  国标行业信息
    /// </summary>
    [Route("api/BD/Picture")]
    public partial class PictuerController : AuthorizeApiController<T_BD_PICTURE>
    {

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_BD_PICTURE entity)
        {
            return SafeExecute<bool>(() =>
            {
                var files = entity.Nav_PictureFiles;
                entity.Nav_PictureFiles = null;
                var after = this.GetEntity<T_BD_PICTURE>(x => x.ID == entity.ID, "Nav_PictureFiles");
                if (after == null)
                {
                    this.AddEntity(entity);
                    if (files != null && files.Any())
                        this.BantchAddEntity(files);
                }
                else
                {
                    UpdateEntity(entity);
                    if (after.Nav_PictureFiles != null)
                        BantchDeleteEntity<T_BD_PICTURE_FILE>(after.Nav_PictureFiles.Select(x => x.ID).ToList());
                    if (files != null && files.Any())
                        BantchAddEntity(files);
                }
                return true;
            });
        }
        /// <summary>
        /// 文件更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FileFullUpdate")]
        public JsonActionResult<bool> FileFullUpdate([FromBody] T_BD_PICTURE entity)
        {
            return SafeExecute<bool>(() =>
            {
                entity.DATA_TYPE = (int)FMFileType.文件;
                var files = entity.Nav_PictureFiles;
                entity.Nav_PictureFiles = null;
                var after = this.GetEntity<T_BD_PICTURE>(x => x.ID == entity.ID, "Nav_PictureFiles");
                if (after == null)
                {
                    this.AddEntity(entity);
                    if (files != null && files.Any())
                        this.BantchAddEntity(files);
                }
                else
                {
                    UpdateEntity(entity);
                    if (after.Nav_PictureFiles != null)
                        BantchDeleteEntity<T_BD_PICTURE_FILE>(after.Nav_PictureFiles.Select(x => x.ID).ToList());
                    BantchAddEntity(files);
                }
                return true;
            });
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("UpdateAndSendFolw")]
        public JsonActionResult<bool> UpdateAndSendFolw([FromBody] T_BD_PICTURE entity)
        {
            return SafeExecute<bool>(() =>
            {
                var files = entity.Nav_PictureFiles;
                entity.Nav_PictureFiles = null;
                var after = this.GetEntity<T_BD_PICTURE>(x => x.ID == entity.ID, "Nav_PictureFiles");
                var flowService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFFlowService>();
                //图片审批
                flowService.SendFlowAndCommit<T_BD_PICTURE>(entity, (int)PFCodeRuleType.审批流编码, "1592907699376", () =>
                {
                    if (after == null)
                    {
                        this.AddEntityNoCommit(entity);
                        if (files != null && files.Any())
                            this.BantchAddEntityNoCommit(files);
                    }
                    else
                    {
                        UpdateEntityNoCommit(entity);
                        if (after.Nav_PictureFiles != null)
                            BantchDeleteEntityNoCommit<T_BD_PICTURE_FILE>(after.Nav_PictureFiles.Select(x => x.ID).ToList());
                        if (files != null && files.Any())
                            BantchAddEntityNoCommit(files);
                    }
                });
                return true;
            });
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="ids">id字符串（id用逗号分隔）</param>
        /// <returns></returns>
        [HttpGet, Route("FullBatchDelete")]
        public JsonActionResult<bool> FullBatchDelete(string ids)
        {
            return SafeExecute<bool>(() =>
            {
                var arrIds = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(i => new Guid(i));
                var filter = new BaseFilter
                {
                    IgnoreOrgRule = true,
                    SelectField = new string[] {"ID", "Nav_PictureFiles.ID",
                    "Nav_PictureFiles.Nav_ImgFile.ID",
                "Nav_PictureFiles.Nav_ImgFile.Nav_Details.ID",
                    "Nav_PictureFiles.Nav_ImgFile.Nav_Details.Nav_File.ID",
                "Nav_PictureFiles.Nav_ImgFile.Nav_File.ID"}
                };
                var pictures = this.GetEntities<T_BD_PICTURE>(i => arrIds.Contains(i.ID), filter,
                    "Nav_PictureFiles.Nav_ImgFile.Nav_Details.Nav_File");

                var pictureIds = pictures.Select(i => i.ID).ToList();
                var pictureFileId = pictures.SelectMany(i => i.Nav_PictureFiles.Select(i => i.ID)).ToList();
                var imgFileId = pictures.SelectMany(i => i.Nav_PictureFiles.Select(i => i.Nav_ImgFile.ID)).ToList();
                //var pfFileId= pictures.SelectMany(i => i.Nav_PictureFiles.Select(i => i.Nav_ImgFile.Nav_File.ID)).ToList();
                var detailId = pictures.SelectMany(i => i.Nav_PictureFiles.SelectMany(i => i.Nav_ImgFile.Nav_Details.Select(x => x.ID))).ToList();
                var fileId = pictures.SelectMany(i => i.Nav_PictureFiles.SelectMany(i => i.Nav_ImgFile.Nav_Details.Select(x => x.Nav_File.ID))).ToList();
                this.BantchDeleteEntity<T_PF_IMG_FILE_DETAIL>(detailId);
                this.BantchDeleteEntity<T_BD_PICTURE_FILE>(pictureFileId);
                this.BantchDeleteEntity<T_PF_IMG_FILE>(imgFileId);
                this.BantchDeleteEntity<T_PF_FILE>(fileId);
                this.BantchDeleteEntity<T_BD_PICTURE>(pictureIds);
                return true;
            });

        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="ids">id字符串（id用逗号分隔）</param>
        /// <returns></returns>
        [HttpGet, Route("FullDelete")]
        public JsonActionResult<bool> FullDelete(string id)
        {
            return this.FullBatchDelete(id);
        }




    }
}
