using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Enums;
using APT.BaseData.Domain.Enums.PF;
using APT.BaseData.Domain.IServices.FM;
using APT.BaseData.Domain.IServices;
using APT.Infrastructure.Api;
using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.OG;
using APT.MS.Domain.Enums;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Intrinsics.Arm;


namespace APT.SC.WebApi.Controllers.Api.OG
{

    [Route("api/OG/OGCallback")]
    public class OGCallbackController : CommonApiController
    {
        IFMNotificationTaskService NotificationTaskService { get; set; }

        public OGCallbackController(IFMNotificationTaskService notificationTaskService, IPFCodeRuleService codeRuleService)
        {
            NotificationTaskService = notificationTaskService;
        }
        /// <summary>
        /// 组织保障后台任务
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("TODOTASK")]
        public JsonActionResult<bool> TODO_DAY([FromBody] KeywordFilter filter)
        {
            return SafeExecute<bool>(() =>
            {
                    CheckSafeAssessRecord(filter);
                return true;
            });
        }
        
        public void CheckSafeAssessRecord([FromBody] KeywordFilter filter)
        {
            var dt = DateTime.Now;
            var days = (int)DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
            var currentDay = (int)DateTime.Now.Day;

            // 每月末触发待办事项给班组长和车间负责人
            if (days == currentDay)
            {
                var deps = GetEntities<T_FM_DEPARTMENT>(t => t.DEPARTMENT_TYPE == (int)FMDepartmentType.Workshop || t.DEPARTMENT_TYPE == (int)FMDepartmentType.Class,
                            new BaseFilter(null, FilterOrgTypeEnum.忽略组织), new string[] { "Nav_User" });
                var recordList = new List<T_OG_SAFE_ASSESS_RECORD>();
                deps.ForEach(dep =>
                {
                    T_OG_SAFE_ASSESS_RECORD record = new T_OG_SAFE_ASSESS_RECORD
                    {
                        ID = Guid.NewGuid(),
                        ANNUAL = dt.Year,
                        DEPARTMENT_ID = dep.ID,
                        USER_ID = dep.USER_ID,
                        STATUS = PFStandardStatus.Draft,
                        ORG_ID = dep.Nav_User.ORG_ID,
                    };
                    recordList.Add(record);

                    NotificationTaskService.InsertUserNoticeTaskModel("考核记录表", record.ID, dep.Nav_User.ORG_ID, dep.Nav_User.ID, "", DateTime.Now, DateTime.Now.AddDays(1), 1, "OGSafeAssessRecordInput");
                });
                BantchSaveEntity(recordList);
            }

            DateTime startQuarter = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day);  //本季度初
            DateTime endQuarter = startQuarter.AddMonths(3).AddDays(-1);  //本季度末
                                                                          //每季度末触发待办事项给部门负责人，安环部负责人
            if (endQuarter.DayOfYear == dt.DayOfYear)
            {
                var deps = GetEntities<T_FM_DEPARTMENT>(t => t.DEPARTMENT_TYPE == (int)FMDepartmentType.Department || t.DEPARTMENT_TYPE == (int)FMDepartmentType.Company,
                                new BaseFilter(null, FilterOrgTypeEnum.忽略组织), new string[] { "Nav_User" });
                var recordList = new List<T_OG_SAFE_ASSESS_RECORD>();
                deps.ForEach(dep =>
                {
                    if (dep.DEPARTMENT_TYPE == (int)FMDepartmentType.Department)
                    {
                        T_OG_SAFE_ASSESS_RECORD record = new T_OG_SAFE_ASSESS_RECORD
                        {
                            ID = Guid.NewGuid(),
                            ANNUAL = dt.Year,
                            DEPARTMENT_ID = dep.ID,
                            USER_ID = dep.USER_ID,
                            STATUS = (int)PFStandardStatus.Draft,
                            ORG_ID = dep.Nav_User.ORG_ID,
                        };
                        recordList.Add(record);
                        NotificationTaskService.InsertUserNoticeTaskModel("考核记录表", record.ID, dep.Nav_User.ORG_ID, dep.Nav_User.ID,"", DateTime.Now, DateTime.Now.AddDays(1),1, "OGSafeAssessRecordInput");
                    }
                    else
                    {
                    //安环部
                    var department = GetEntity<T_FM_DEPARTMENT>(t => t.CODE == "XLKAH", new string[] { "Nav_User" });
                        T_OG_SAFE_ASSESS_RECORD record = new T_OG_SAFE_ASSESS_RECORD
                        {
                            ID = Guid.NewGuid(),
                            ANNUAL = dt.Year,
                            DEPARTMENT_ID = dep.ID,
                            USER_ID = department.Nav_User.ID,
                            STATUS = (int)PFStandardStatus.Draft,
                            ORG_ID = department.Nav_User.ORG_ID,
                        };

                        UpdateEntity(record);
                        NotificationTaskService.InsertUserNoticeTaskModel("考核记录表", record.ID, dep.Nav_User.ORG_ID, dep.Nav_User.ID, "", DateTime.Now, DateTime.Now.AddDays(1), 1, "OGSafeAssessRecordInput");
                    }
                });
                BantchSaveEntity(recordList);
            }
        }
    }
}
