using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Enums.PF;
using APT.BaseData.Domain.Enums;
using APT.BaseData.Services.Services.FM;
using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.SC.PT;
using APT.MS.Domain.Enums;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System;
using APT.BaseData.Domain.IServices.FM;
using APT.BaseData.Domain.IServices;
using APT.BaseData.Services.DomainServices;
using APT.MS.Domain.Entities.HM;
using APT.MS.Domain.Entities.BS;
using NPOI.SS.Formula.Functions;

namespace APT.SC.WebApi.Controllers.Api.PTController
{
    /// <summary>
    ///  年度监测汇总
    /// </summary>
    [Route("api/PT/PTAnnualMonotorSummary")]
    public partial class AnnualMonotorSummaryController : AuthorizeApiController<T_PT_ANNUAL_MONOTOR_SUMMARY>
    {
        IFMNotificationTaskService NotificationTaskService { get; set; }
        /// <summary>
        /// FOPreOperSch
        /// </summary>
        /// <param name="notificationTaskService"></param>
        public AnnualMonotorSummaryController(IFMNotificationTaskService notificationTaskService)
        {
            NotificationTaskService = notificationTaskService;
        }
        /// <summary>
        /// 已阅
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("IdentityUpdate")]
        public JsonActionResult<bool> IdentityUpdate([FromBody] T_PT_ANNUAL_MONOTOR_SUMMARY entity)
        {
            return SafeExecute(() => {
                var summary = this.GetEntity<T_PT_ANNUAL_MONOTOR_SUMMARY>(entity.ID);
                var loginUserId = APT.Infrastructure.Api.AppContext.CurrentSession.UserID;
                if (entity.TaskID != Guid.Empty)
                {
                    var currTask = GetEntity<T_FM_NOTIFICATION_TASK>(t => t.ID == entity.TaskID);
                    if (currTask != null)
                    {
                        loginUserId = currTask.USER_ID;
                    }
                }
                var notices = GetEntities<T_FM_NOTIFICATION_TASK>(t => t.SOURCE_FORMCODE == "PT036_SHOWPRINT" && t.SOURCE_DATA_ID == entity.ID && t.NOTICE_STATUS == (int)FMNoticeStatusEnum.未处理 && t.USER_ID != loginUserId, new BaseFilter(entity.ORG_ID)).ToList();
                if (notices.Count() == 0)
                {
                    summary.STATUS = PFStandardStatus.Archived;
                }
                T_FM_NOTIFICATION_TASK task = null;
                if (entity.TaskID != Guid.Empty)
                {
                    task = NotificationTaskService.GetTaskFinishModel(entity.TaskID);
                }
                UnifiedCommit(() =>
                {
                    if (summary != null)
                        this.UpdateEntityNoCommit(summary);
                    if (task != null)
                        this.UpdateEntityNoCommit(task);
                });
                return true;
            });
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetEdit")]
        public JsonActionResult<T_PT_ANNUAL_MONOTOR_SUMMARY> GetEdit([FromBody] KeywordFilter filter)
        {
            return SafeExecute(() => {
                //var id = filter.FilterGroup.Rules.FirstOrDefault(t => t.Field == "ID").Value.ToString();
                //if (string.IsNullOrEmpty(id))
                //    this.ThrowError("030017");
                //var result = this.GetEntity<T_PT_ANNUAL_MONOTOR_SUMMARY>(id);
                var result = this.GetEntity<T_PT_ANNUAL_MONOTOR_SUMMARY>(null, filter, null);
                var detailIds = result.Nav_Details.Select(i => i.ID).Distinct();
                var newFilter = new BaseFilter(result.ORG_ID);
                var userIds = result.Nav_Details.Select(i => i.USER_ID).ToList();
                newFilter.SelectField = new List<string> { "NAME","Nav_Person.Nav_Post" };
                var userList = this.GetEntities<T_FM_USER>(i => userIds.Contains(i.ID) && i.ENABLE_STATUS == 0, newFilter);
                var departmenrIds = result.Nav_Details.Select(i => i.DEPARTMENT_ID).ToList();
                newFilter.SelectField = new List<string> { "NAME", "DEPARTMENT_TYPE" };
                var departList = this.GetEntities<T_FM_DEPARTMENT>(i => departmenrIds.Contains(i.ID), newFilter);
                newFilter.SelectField = new List<string> { "MONOTOR_SUMMARY_DETAIL_ID", "ROW_NO", "QUARTER_TYPE", "NAME", "SCORE", "METHOD", "CONTENT", "MEASURES", "RESULT_SCORE" };
                var detailContens = this.GetEntities<T_PT_ANNUAL_MONOTOR_SUMMARY_DETAIL_CONTENT>(i => detailIds.Contains(i.MONOTOR_SUMMARY_DETAIL_ID), newFilter);
                foreach (var c in result.Nav_Details)
                {
                    c.Nav_User = userList.FirstOrDefault(i => i.ID == c.USER_ID);
                    c.Nav_Department = departList.FirstOrDefault(i => i.ID == c.DEPARTMENT_ID);
                    c.Nav_Contents = detailContens.Where(i => i.MONOTOR_SUMMARY_DETAIL_ID == c.ID).ToList();
                }
                result.Nav_Details = result.Nav_Details.OrderBy(t => t.USER_ID).ToList();
                return result;
            });
        }
    }
}
