using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.IServices;
using APT.Infrastructure.Core;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using APT.Utility;
using APT.Infrastructure.Api;
using APT.Migrations;
using APT.BaseData.Domain.Entities.OP;
using APT.BaseData.Domain.Enums.OP;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.IServices.OP;
using APT.BaseData.Domain.Enums.PF;

namespace APT.PF.WebApiControllers.Api.PF
{
    /// <summary>
    /// 通知
    /// </summary>
    [Route("api/PF/PFNotice")]
    public class PFNoticeController : AuthorizeApiController<T_PF_NOTICE>
    {
        IOPTenantDBConnService ConnService { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="connService"></param>
        public PFNoticeController(IOPTenantDBConnService connService)
        {
            ConnService = connService;
        }
        /// <summary>
        /// 更新或新增数据
        /// </summary>
        /// <param name="entity">对象实体</param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_PF_NOTICE entity)
        {
            return SafeExecute(() =>
            {
                //如果是总部 同步修改 子公司
                var Files = entity.Nav_Files;
                entity.Nav_Files = null;
                entity.CREATER_ID = APT.Infrastructure.Api.AppContext.CurrentSession.UserID;

                if (entity.CREATER_ID.HasValue)
                {
                    var userLogin = GetEntity<T_FM_USER>(entity.CREATER_ID.Value, "Nav_Department");
                    entity.CREATE_NAME = userLogin.NAME;//冗余赋值 给查看
                    entity.DEPARTMENT_ID = userLogin.DEPARTMENT_ID;
                    entity.CREATE_DEPARTMENT = userLogin.Nav_Department?.NAME;//冗余赋值 给子公司查看
                }

                this.UnifiedCommit(() =>
                {
                    UpdateEntityNoCommit(entity);
                    if (Files != null && Files.Any())
                        BantchSaveEntityNoCommit(Files);
                });

                try
                {
                    if (entity.ISTOCHILE && entity.STATUS == PFStandardStatus.Archived)
                    {
                        IEnumerable<T_PF_IMG_FILE> IlistImgs = null;
                        List<T_PF_FILE> listFile = null;
                        if (Files != null && Files.Count() > 0)
                        {
                            var listIMGIDS = Files.Select(e => e.IMG_FILE_ID);
                            IlistImgs = GetEntities<T_PF_IMG_FILE>(e => listIMGIDS.Contains(e.ID), null, "Nav_File");
                            listFile = new List<T_PF_FILE>();
                            foreach (var item in IlistImgs)
                            {
                                listFile.Add(item.Nav_File);
                                item.Nav_File = null;
                            }
                        }

                        Dictionary<Guid, string> dicORGCONN = ConnService.GetConnDictionary(entity.ORG_ID);
                        entity.DEPARTMENT_ID = null; //组织清空  子公司不会有对应的ID   或者改为公司ID？
                        foreach (var itemConn in dicORGCONN)
                        {
                            using (var context = new MigrationContext(itemConn.Value))
                            {
                                entity.ISHEAD = true;
                                entity.ORG_ID = itemConn.Key;
                                if (Files != null && Files.Any())
                                {
                                    foreach (var item in Files)
                                    {
                                        item.ORG_ID = itemConn.Key;
                                    }
                                    foreach (var item in IlistImgs)
                                    {
                                        item.ORG_ID = itemConn.Key;
                                    }
                                    foreach (var item in listFile)
                                    {
                                        item.ORG_ID = itemConn.Key;
                                    }
                                }
                                try
                                {
                                    context.AddEntity(entity);
                                    if (Files != null && Files.Any())
                                    {
                                        context.AddEntities(Files);
                                        context.AddEntities(IlistImgs);
                                        context.AddEntities(listFile);
                                    }
                                    context.SaveChanges();
                                }
                                catch (Exception ex)
                                {
                                    context.Update(entity);
                                    if (Files != null && Files.Any())
                                    {
                                        context.UpdateEntities(Files);
                                        context.UpdateEntities(IlistImgs);
                                        context.UpdateEntities(listFile);
                                    }
                                }
                            }
                        }
                    }
                }
                catch { }

                return true;
            });
        }
    }
}
