using APT.BaseData.Domain.Entities.FM;
using APT.Infrastructure.Core;
using Microsoft.AspNetCore.Mvc;
using APT.Infrastructure.Api;
using System.Collections.Generic;
using System.Linq.Expressions;
using System;
using System.Linq;
using APT.BaseData.Domain.ApiModel;
using Ubiety.Dns.Core;
using System.Security.AccessControl;
using APT.MS.Domain.Entities.FO;
using APT.BaseData.Domain.Entities;
using APT.MS.Domain.Enums;
using APT.MS.Domain.Entities.BI;
using static APT.MS.Domain.Enums.BI.BIEnums;
using APT.Utility;
using APT.BaseData.Services.Services.FM;
using APT.BaseData.Domain.IServices.FM;

namespace APT.SC.WebApi.Controllers.Api.BI
{
    /// <summary>
    ///  年度监测汇总
    /// </summary>
    [Route("api/BI/BITask")]
    public class BITaskController : AuthorizeApiController<T_BI_BSSAFECHECK>
    {
        IFMNotificationTaskService NotificationTaskService { get; set; }

        public BITaskController(IFMNotificationTaskService notificationTaskService)
        {
            NotificationTaskService = notificationTaskService;
        }
        /// <summary>
        /// 关闭待办申请
        /// <returns></returns>
        /// </summary>
        [HttpPost, Route("CloseTask")]
        public JsonActionResult<bool> CloseTask([FromBody] KeywordFilter filter)
        {
            return base.SafeExecute(() =>
            {
                if ( filter!=null&& filter.Parameter1 != "" && filter.Parameter2 != "")
                {

                    var currUserid = APT.Infrastructure.Api.AppContext.CurrentSession.UserId;
                    var currUser = GetEntity<T_FM_USER>(currUserid, "Nav_Department.Nav_User");
                    T_BI_CLOSE_TASKS task = new T_BI_CLOSE_TASKS();
                    task.ID = Guid.NewGuid();
                    task.DEPARTMENT_NAME = currUser.Nav_Department.NAME;
                    task.USER_NAME = currUser.NAME;
                    task.TASK_IDS = filter.Parameter1;
                    task.TASK_NAMES = filter.Parameter2;
                    task.AUDIT_USER_ID = (Guid)currUser.Nav_Department.USER_ID;
                    task.AUDIT_USER_NAME = currUser.Nav_Department.Nav_User.NAME;
                    task.ORG_ID = filter.GetOrgId();
                    var sendNotice = NotificationTaskService.InsertUserNoticeTaskModel("待办关闭申请", task.ID, filter.GetOrgId(), task.AUDIT_USER_ID, task.AUDIT_USER_NAME, DateTime.Now, DateTime.Now.AddDays(30), (int)FMNoticeTypeEnum.消息, "CLOSETASKS");
                    UnifiedCommit(() =>
                    {
                        if (sendNotice != null)
                            AddEntityNoCommit(sendNotice);
                        if (task != null)
                            AddEntityNoCommit(task);
                    });
                }
                return true;
            });
        }
        /// <summary>
        /// 确认关闭待办
        /// <returns></returns>
        /// </summary>
        [HttpPost, Route("ConfirmCloseTask")]
        public JsonActionResult<bool> ConfirmCloseTask([FromBody] KeywordFilter filter)
        {
            return base.SafeExecute(() =>
            {
                List<Guid> closeTasks = new List<Guid>();
                var closeTaskIds = filter.Parameter1.Split(',');
                foreach (var closeId in closeTaskIds)
                {
                    closeTasks.Add(Guid.Parse(closeId));
                }
                var allTasks = GetEntities<T_FM_NOTIFICATION_TASK>(t => closeTasks.Contains(t.ID), new BaseFilter(filter.GetOrgId()));
                foreach (var task in allTasks)
                {
                    task.NOTICE_STATUS = (int)FMNoticeStatusEnum.关闭;
                }
                var finishNotice = NotificationTaskService.GetTaskFinishModel(Guid.Parse(filter.Parameter2));
                UnifiedCommit(() =>
                    {
                        if (allTasks != null && allTasks.Any())
                            BantchUpdateEntityNoCommit(allTasks, "NOTICE_STATUS");
                        if (finishNotice != null)
                            UpdateEntityNoCommit(finishNotice);
                    });
                return true;
            });
        }
    }
}