

using System;
using System.Collections.Generic;
using System.Linq;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Enums;
using APT.BaseData.Domain.Enums.PF;
using APT.BaseData.Domain.IServices;
using APT.BaseData.Domain.IServices.FM;
using APT.BaseData.Services.DomainServices;
using APT.BaseData.Services.Services.FM;
using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.SE;
using APT.MS.Domain.Enums;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;

namespace APT.SC.WebApi.Controllers.Api.SE
{
    [Route("api/SE/SEDepTrainPlan")]
    public class SEDepTrainPlanController : AuthorizeApiController<T_SE_DEP_TRAIN_PLAN>
    {
        IFMNotificationTaskService NotificationTaskService { get; set; }
        IFMFlowPermitService MFlowPermitService { get; set; }
        IPFApproveCallBackService ApproveCallBackService { get; set; }

        public SEDepTrainPlanController(IFMNotificationTaskService notificationTaskService, IFMFlowPermitService mFlowPermitService, IPFApproveCallBackService approveCallBackService)
        {
            NotificationTaskService = notificationTaskService;
            MFlowPermitService = mFlowPermitService;
            ApproveCallBackService = approveCallBackService;
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_SE_DEP_TRAIN_PLAN entity)
        {
            return SafeExecute(() =>
            {
                if (entity.STATUS != PFStandardStatus.Draft)
                {
                    throw new Exception("当前状态数据不可编辑");
                }
                var currentUserId = APT.Infrastructure.Api.AppContext.CurrentSession.UserID;
                if (entity.LAUNCH_USER_ID != Guid.Empty && entity.LAUNCH_USER_ID != currentUserId)
                {
                    throw new Exception("当前表单可能已被其它安全员提交");
                }
                if (entity.LAUNCH_USER_ID == Guid.Empty)
                {
                    entity.LAUNCH_USER_ID = (Guid)currentUserId;
                }
                if (entity.LAUNCH_DEPARTMENT_ID == Guid.Empty)
                {
                    entity.LAUNCH_DEPARTMENT_ID = (Guid)GetEntity<T_FM_USER>(entity.LAUNCH_USER_ID.ToString()).DEPARTMENT_ID;
                }
                if (entity.LAUNCH_TIME == null)
                {
                    entity.LAUNCH_TIME = DateTime.Now;
                }
                if (entity.PLAN_YEAR == 0 )
                {
                    entity.PLAN_YEAR = int.Parse(DateTime.Now.Year.ToString());
                }
                var Nav_DepTrainPlanDetail = entity.Nav_DepTrainPlanDetail;
                List<T_SE_DEP_TRAIN_PLAN_CONTENT> planContent = new List<T_SE_DEP_TRAIN_PLAN_CONTENT>();
                if (Nav_DepTrainPlanDetail != null)
                {
                    Nav_DepTrainPlanDetail.ForEach(i =>
                    {
                        i.DEP_TRAIN_PLAN_ID = entity.ID;
                        planContent.AddRange(i.Nav_DepTrainPlanContent);
                        i.Nav_DepTrainPlanContent = null;
                        i.Nav_TrainCheckTypeEnum = null;
                        i.Nav_TrainTypeEnum = null;
                    });
                }
                entity.Nav_DepTrainPlanDetail = null;
                entity.Nav_LaunchDepartment = null;
                entity.Nav_LaunchUser = null;
                if (entity.PUBLISH != null && entity.PUBLISH.Equals("SaveAndNotify"))
                {
                    entity.STATUS = PFStandardStatus.Approving;
                    MFlowPermitService.InsertApprove(DateTime.Now.ToString("yyyyMMddHHmmss"), "SE009", "", entity.ID, "SE009_SHOWPRINT", entity.TaskID, true, () =>
                    {
                        UpdateEntityNoCommit(entity);
                        if (Nav_DepTrainPlanDetail != null)
                            BantchSaveEntityNoCommit(Nav_DepTrainPlanDetail);
                        if (planContent != null)
                            BantchSaveEntityNoCommit(planContent);
                    }, null, currentUserId,null,null,"", "SE009_SHOWPRINT");
                    return true;
                }
                UnifiedCommit(() =>
                {
                    UpdateEntityNoCommit(entity);
                    if (Nav_DepTrainPlanDetail != null)
                        BantchSaveEntityNoCommit(Nav_DepTrainPlanDetail);
                    if (planContent != null)
                        BantchSaveEntityNoCommit(planContent);
                });
                return true;
            });
        }
        /// <summary>
        /// 隐患上报 审核通过 给每个通知负责人发送通知
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("departmentTrainPlanAgree")]
        public JsonActionResult<bool> departmentTrainPlanAgree(string id)
        {
            return SafeExecute<bool>(() =>
            {
                return ApproveCallBackService.CallBack("SE/SEDepTrainPlan/departmentTrainPlanAgree", id);
            });
        }
    }
}
