using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.OG;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;

namespace APT.SC.WebApi.Controllers.Api.OG
{
    [Route("api/OG/OGRelated")]
    public class OGRelatedController : AuthorizeApiController<T_OG_RELATED>
    {
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_OG_RELATED entity)
        {
            return SafeExecute<bool>(() =>
            {
                var update_Nav_Users_Insurances = new List<T_OG_RELATED_USER_INSURANCE>();
                var update_Nav_Users_Healths = new List<T_OG_RELATED_USER_HEALTH>();
                var update_Nav_Users_Signs = new List<T_OG_RELATED_USER_SIGN>();
                var Nav_Users = entity.Nav_Users;
                var del_Nav_Users_Insurances = new List<T_OG_RELATED_USER_INSURANCE>();
                var del_Nav_Users_Healths = new List<T_OG_RELATED_USER_HEALTH>();
                var del_Nav_Users_Signs = new List<T_OG_RELATED_USER_SIGN>();
                var del_Nav_Users = new List<T_OG_RELATED_USER>();
                var update_Nav_Users = new List<T_OG_RELATED_USER>();
                if (entity.Nav_Users != null && entity.Nav_Users.Any())
                {
                    entity.Nav_Users = null;
                    Nav_Users.ForEach(t =>
                    {
                        if (t.Nav_Insurances != null)
                        {
                            var Nav_Insurances = t.Nav_Insurances;
                            t.Nav_Insurances = null;
                            Nav_Insurances.ForEach(i =>
                            {
                                if (i.IS_DELETED)
                                {
                                    del_Nav_Users_Insurances.Add(i);
                                }
                                else
                                {
                                    update_Nav_Users_Insurances.Add(i);
                                }
                            });
                        }
                        if (t.Nav_Healths != null)
                        {
                            var Nav_Healths = t.Nav_Healths;
                            t.Nav_Healths = null;
                            Nav_Healths.ForEach(i =>
                            {
                                if (i.IS_DELETED)
                                {
                                    del_Nav_Users_Healths.Add(i);
                                }
                                else
                                {
                                    update_Nav_Users_Healths.Add(i);
                                }
                            });
                        }
                        if (t.Nav_Signs != null)
                        {
                            var Nav_Signs = t.Nav_Signs;
                            t.Nav_Signs = null;
                            Nav_Signs.ForEach(i =>
                            {
                                if (i.IS_DELETED)
                                {
                                    del_Nav_Users_Signs.Add(i);
                                }
                                else
                                {
                                    update_Nav_Users_Signs.Add(i);
                                }
                            });
                        }
                        if (t.IS_DELETED)
                        {
                            del_Nav_Users.Add(t);
                        }
                        else
                        {
                            update_Nav_Users.Add(t);
                        }
                    });
                }
                var update_Nav_Agreements_Files = new List<T_OG_RELATED_AGREEMENT_FILE>();
                var del_Nav_Agreements_Files = new List<T_OG_RELATED_AGREEMENT_FILE>();
                var Nav_Agreements = entity.Nav_Agreements;
                var del_Nav_Agreements = new List<T_OG_RELATED_AGREEMENT>();
                var update_Nav_Agreements = new List<T_OG_RELATED_AGREEMENT>();
                if (entity.Nav_Agreements != null)
                {
                    entity.Nav_Agreements = null;
                    Nav_Agreements.ForEach(t =>
                    {
                        if (t.END_TIME == null || t.END_TIME.Value < t.TIME)
                        {
                            throw new Exception("合同结束时间不能为空，且不能早于合同开始时间");
                        }
                        if (t.Nav_Files != null)
                        {
                            var Nav_Files = t.Nav_Files;
                            t.Nav_Files = null;
                            Nav_Files.ForEach(i =>
                            {
                                if (i.IS_DELETED)
                                {
                                    del_Nav_Agreements_Files.Add(i);
                                }
                                else
                                {
                                    update_Nav_Agreements_Files.Add(i);
                                }
                            });


                        }
                        if (t.IS_DELETED)
                        {
                            del_Nav_Agreements.Add(t);
                        }
                        else
                        {
                            update_Nav_Agreements.Add(t);
                        }
                    });
                }
                var update_Nav_Qualifications_Details = new List<T_OG_RELATED_QUALIFICATION_DETAIL>();
                var del_Nav_Qualifications_Details = new List<T_OG_RELATED_QUALIFICATION_DETAIL>();
                var update_Nav_Qualifications_Details_Files = new List<T_OG_RELATED_QUALIFICATION_DETAIL_FILE>();
                var del_Nav_Qualifications_Details_Files = new List<T_OG_RELATED_QUALIFICATION_DETAIL_FILE>();
                var Nav_Qualifications = entity.Nav_Qualifications;
                var update_Nav_Qualifications = new List<T_OG_RELATED_QUALIFICATION>();
                var del_Nav_Qualifications = new List<T_OG_RELATED_QUALIFICATION>();
                if (entity.Nav_Qualifications != null)
                {
                    entity.Nav_Qualifications = null;
                    Nav_Qualifications.ForEach(t =>
                    {
                        var Nav_Details = t.Nav_Details;
                        if (Nav_Details != null)
                        {
                            t.Nav_Details = null;
                            Nav_Details.ForEach(i =>
                            {
                                var files = i.Nav_Files;
                                if (files != null)
                                {
                                    i.Nav_Files = null;
                                    files.ForEach(j =>
                                    {
                                        if (j.IS_DELETED)
                                        {
                                            del_Nav_Qualifications_Details_Files.Add(j);
                                        }
                                        else
                                        {
                                            update_Nav_Qualifications_Details_Files.Add(j);
                                        }
                                    });
                                }
                                if (i.IS_DELETED)
                                {
                                    del_Nav_Qualifications_Details.Add(i);
                                }
                                else
                                {
                                    update_Nav_Qualifications_Details.Add(i);
                                }
                            });

                        }
                        if (t.IS_DELETED)
                        {
                            del_Nav_Qualifications.Add(t);
                        }
                        else
                        {
                            update_Nav_Qualifications.Add(t);
                        }

                    });
                }
                UnifiedCommit(() =>
                    {
                        if (del_Nav_Qualifications_Details_Files.Count > 0)
                        {
                            BantchSaveEntityNoCommit(del_Nav_Qualifications_Details_Files);
                        }
                        if (del_Nav_Qualifications_Details_Files.Count > 0)
                        {
                            BantchSaveEntityNoCommit(del_Nav_Qualifications_Details_Files);
                        }
                        if (del_Nav_Qualifications_Details.Count > 0)
                        {
                            BantchSaveEntityNoCommit(del_Nav_Qualifications_Details);
                        }
                        if (del_Nav_Qualifications.Count > 0)
                        {
                            BantchSaveEntityNoCommit(del_Nav_Qualifications);
                        }
                        if (del_Nav_Agreements_Files.Count > 0)
                        {
                            BantchSaveEntityNoCommit(del_Nav_Agreements_Files);
                        }
                        if (del_Nav_Agreements.Count > 0)
                        {
                            BantchSaveEntityNoCommit(del_Nav_Agreements);
                        }
                        if (del_Nav_Users_Insurances.Count > 0)
                        {
                            BantchSaveEntityNoCommit(del_Nav_Users_Insurances);
                        }
                        if (del_Nav_Users_Insurances.Count > 0)
                        {
                            BantchSaveEntityNoCommit(del_Nav_Users_Insurances);
                        }
                        if (del_Nav_Users_Healths.Count > 0)
                        {
                            BantchSaveEntityNoCommit(del_Nav_Users_Healths);
                        }
                        if (del_Nav_Users_Signs.Count > 0)
                        {
                            BantchSaveEntityNoCommit(del_Nav_Users_Signs);
                        }
                        if (del_Nav_Users.Count > 0)
                        {
                            BantchSaveEntityNoCommit(del_Nav_Users);
                        }
                        UpdateEntityNoCommit(entity);
                        if (update_Nav_Users != null)
                        {
                            BantchSaveEntityNoCommit(update_Nav_Users);
                            if (update_Nav_Users_Insurances.Count > 0)
                            {
                                BantchSaveEntityNoCommit(update_Nav_Users_Insurances);
                            }
                            if (update_Nav_Users_Healths.Count > 0)
                            {
                                BantchSaveEntityNoCommit(update_Nav_Users_Healths);
                            }
                            if (update_Nav_Users_Signs.Count > 0)
                            {
                                BantchSaveEntityNoCommit(update_Nav_Users_Signs);
                            }
                        }
                        if (update_Nav_Agreements != null)
                        {
                            BantchSaveEntityNoCommit(update_Nav_Agreements);
                            if (update_Nav_Agreements_Files.Count > 0)
                            {
                                BantchSaveEntityNoCommit(update_Nav_Agreements_Files);
                            }
                        }
                        if (update_Nav_Qualifications != null)
                        {
                            BantchSaveEntityNoCommit(update_Nav_Qualifications);
                            if (update_Nav_Qualifications_Details.Count > 0)
                            {
                                BantchSaveEntityNoCommit(update_Nav_Qualifications_Details);
                                if (update_Nav_Qualifications_Details_Files.Count > 0) ;
                                {
                                    BantchSaveEntityNoCommit(update_Nav_Qualifications_Details_Files);
                                }
                            }
                        }
                    });
                return true;
            });
        }
    }
}
