using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Entities;
using APT.Infrastructure.Core;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using APT.BaseData.Domain.IServices.FM;
using APT.MS.Domain.Enums;
using System.Collections.Generic;
using APT.BaseData.Domain.Enums.PF;
using APT.MS.Domain.Entities.OG;
using InfluxData.Net.InfluxDb.Models.Responses;
using Newtonsoft.Json.Schema;
using APT.MS.Domain.Entities.SC.SC;
using APT.MS.Domain.Entities.SC.PT;
using APT.BaseData.Domain.Enums;
using APT.BaseData.Services.Services.FM;
using APT.MS.Domain.Entities.SC;
using ICSharpCode.SharpZipLib.Core;
using APT.MS.Domain.Entities.SE;
using Google.Protobuf.WellKnownTypes;
using APT.WebApi.Models;

namespace APT.PP.WebApi.Controllers.Api.PP
{
    /// <summary>
    /// 组织保障后台任务
    /// </summary>
    [Route("api/PP/OGController")]
    [TypeFilter(typeof(CustomActionFilterAttribute))]
    public class OGController : AuthorizeApiController<T_FM_BASE_CONFIG>
    {
        IFMNotificationTaskService NotificationTaskService { get; set; }

        public OGController(IFMNotificationTaskService notificationTaskService)
        {
            NotificationTaskService = notificationTaskService;
        }

        /// <summary>
        /// 安全教育模块所有跑批
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("OGTask")]
        public JsonActionResult<bool> OGTask([FromBody] KeywordFilter filter)
        {
            return SafeExecute<bool>(() =>
            {
                //创建相关方提醒
                CreateReleatedAlert(filter);
                return true;
            });
        }
        private void CreateReleatedAlert(KeywordFilter filter)
        {
            var baseFilter1 = new BaseFilter(filter.GetOrgId());
            var baseFilter2 = new BaseFilter(filter.GetOrgId());
            var baseFilter3 = new BaseFilter(filter.GetOrgId());

            baseFilter1.Include = new string[] { "Nav_Related.Nav_User" };
            baseFilter2.Include = new string[] { "Nav_Qualification.Nav_Related.Nav_User", "Nav_Item" };
            baseFilter3.Include = new string[] { "Nav_Related.Nav_User" };

            var compareDay = DateTime.Now.AddMonths(3);
            var releated1 = GetEntities<T_OG_RELATED_AGREEMENT>(t => t.END_TIME.Value.Year == compareDay.Year && t.END_TIME.Value.Month == compareDay.Month && t.END_TIME.Value.Day == compareDay.Day, baseFilter1);
            var releated2 = GetEntities<T_OG_RELATED_QUALIFICATION_DETAIL>(t => t.VALID_PERIOD.Value.Year == compareDay.Year && t.VALID_PERIOD.Value.Month == compareDay.Month && t.VALID_PERIOD.Value.Day == compareDay.Day, baseFilter2);
            var releated3 = GetEntities<T_OG_RELATED_USER>(t => t.VALIDITY_TIME.Value.Year == compareDay.Year && t.VALIDITY_TIME.Value.Month == compareDay.Month && t.VALIDITY_TIME.Value.Day == compareDay.Day, baseFilter3);



            var allSendUserTitles = new List<string>();
            var allSendDataIds = new List<Guid>();
            var allSendUserIds = new List<Guid>();
            var allSendUserNames = new List<string>();
            if (releated1.Any())
            {
                foreach (var item in releated1)
                {
                    allSendUserTitles.Add("相关方《"+item.Nav_Related.NAME + "》合同/协议表中的" +item.NAME+"将于"+item.END_TIME.Value.ToShortDateString()+ "到期，请及时处理！");
                    allSendDataIds.Add(item.ID);
                    allSendUserIds.Add((Guid)item.Nav_Related.USER_ID);
                    allSendUserNames.Add(item.Nav_Related.Nav_User.NAME);

                }
            }
            if (releated2.Any())
            {
                foreach (var item in releated2)
                {
                    allSendUserTitles.Add("相关方《" + item.Nav_Qualification.Nav_Related.NAME+"》资质情况表中的" + item.Nav_Item.NAME+ "将于" + item.VALID_PERIOD.Value.ToShortDateString() + "到期，请及时处理！");
                    allSendDataIds.Add(item.ID);
                    allSendUserIds.Add((Guid)item.Nav_Qualification.Nav_Related.USER_ID);
                    allSendUserNames.Add(item.Nav_Qualification.Nav_Related.Nav_User.NAME);
                }
            }
            if (releated3.Any())
            {
                foreach (var item in releated3)
                {
                    allSendUserTitles.Add("相关方《" + item.Nav_Related.NAME + "》人员信息表中" + item.NAME+"的" + item.CERTIFICATE_NAME +"证书将于"+ item.VALIDITY_TIME.Value.ToShortDateString() + "到期，请及时处理！");
                    allSendDataIds.Add(item.ID);
                    allSendUserIds.Add((Guid)item.Nav_Related.USER_ID);
                    allSendUserNames.Add(item.Nav_Related.Nav_User.NAME);
                }
            }
            if (allSendDataIds.Any())
            {
                var sendNotices = NotificationTaskService.InsertUserNoticeTaskModels(allSendUserTitles, allSendDataIds, filter.GetOrgId(), allSendUserIds, allSendUserNames, DateTime.Now, DateTime.Now.AddDays(1).Date, (int)FMNoticeTypeEnum.今日提醒, "PF135");
                UnifiedCommit(() =>
                {
                    if (sendNotices.Any())
                        BantchAddEntityNoCommit(sendNotices);
                });
            }
        }
    }
}
