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

namespace APT.SC.WebApi.Controllers.Api.CM
{
    [Route("api/CM/CMDrillRecord")]
    public class CMDrillRecordController : AuthorizeApiController<T_CM_DRILL_RECORD>
    {
        IFMFlowPermitService MFlowPermitService { get; set; }
        IPFApproveCallBackService ApproveCallBackService { get; set; }
        IFMNotificationTaskService NotificationTaskService { get; set; }
        IFMUserService UserService { get; set; }
        public CMDrillRecordController(IFMFlowPermitService mFlowPermitService, IPFApproveCallBackService approveCallBackService,  IFMNotificationTaskService notificationTaskService, IFMUserService userService)
        {
            MFlowPermitService = mFlowPermitService;
            ApproveCallBackService = approveCallBackService;
            NotificationTaskService = notificationTaskService;
            UserService = userService;
        }

        /// <summary>
        /// 应急演练活动 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_CM_DRILL_RECORD entity)
        {
            return SafeExecute<bool>(() =>
            {
                var ListUser = entity.Nav_ListUserEstimate;
                entity.Nav_ListUserEstimate = null;
                var ListDepartment = entity.Nav_ListDepartment;
                entity.Nav_ListDepartment = null;
                var ListSummaryRequirement = entity.Nav_ListSummaryRequirement;
                entity.Nav_ListSummaryRequirement = null;
                var FilesSummary = entity.Nav_FilesSummary;
                entity.Nav_FilesSummary = null;
                var FilesRecord = entity.Nav_FilesRecord;
                entity.Nav_FilesRecord = null;
                if (ListUser != null && ListUser.Any())
                {
                    ListUser.ToList()[0].ISSIGN = true;//第一个人为填表人
                    ListUser.ToList()[0].ISAGREE = true;
                    foreach (var item in ListUser)
                    {
                        item.Nav_User = null;
                    }
                }
                if (ListSummaryRequirement != null && ListSummaryRequirement.Any())
                {
                    foreach (var item in ListSummaryRequirement)
                    {
                        item.Nav_TypeRequirement = null;
                    }
                }
                if (ListDepartment != null && ListDepartment.Any())
                {
                    foreach (var item in ListDepartment)
                    {
                        item.Nav_Department = null;
                    }
                }
                if (entity.STATUS == PFStandardStatus.Draft)
                {
                    this.UnifiedCommit(() =>
                    {
                        if (entity != null)
                            UpdateEntityNoCommit(entity);
                        if (ListUser != null && ListUser.Any())//评估人员
                            BantchSaveEntityNoCommit(ListUser);
                        if (ListDepartment != null && ListDepartment.Any())//内部参与演练单位
                            BantchSaveEntityNoCommit(ListDepartment);
                        if (ListSummaryRequirement != null && ListSummaryRequirement.Any())//演练总结要求
                            BantchSaveEntityNoCommit(ListSummaryRequirement);
                        if (FilesSummary != null && FilesSummary.Any())//演练总结报告
                            BantchSaveEntityNoCommit(FilesSummary);
                        if (FilesRecord != null && FilesRecord.Any())//演练活动记录
                            BantchSaveEntityNoCommit(FilesRecord);
                    });
                }
                else
                {
                    if (string.IsNullOrEmpty(entity.NAME))
                        throw new Exception("请填写评估主题！");
                    if (entity.NAME_ID == Guid.Empty)
                        throw new Exception("请选择应急演练名称！");
                    if (!entity.TIME.HasValue)
                        throw new Exception("请选择演练时间！");
                    if (string.IsNullOrEmpty(entity.PLACE))
                        throw new Exception("请填写演练地点！");
                    if (ListUser == null || !ListUser.Any())
                        throw new Exception("请选择评估人！");
                    if (ListDepartment == null || !ListDepartment.Any())
                        throw new Exception("请选择内部参与演练单位！");
                    if (ListSummaryRequirement == null || !ListSummaryRequirement.Any())
                        throw new Exception("请选择演练总结要求！");
                    if (FilesSummary == null || !FilesSummary.Any())
                        throw new Exception("请选择演练总结报告！");

                    entity.APPROVE_ID = Guid.NewGuid();
                    var serialCode = DateTime.Now.ToString("yyyyMMddHHmmss");
                    MFlowPermitService.InsertApprove("CM" + serialCode, "CM034", null, entity.ID, "CM034_SHOWPRINT", entity.TaskID, true, () =>
                    {
                        if (entity != null)
                            UpdateEntityNoCommit(entity);
                        if (ListUser != null && ListUser.Any())//评估人员
                            BantchSaveEntityNoCommit(ListUser);
                        if (ListDepartment != null && ListDepartment.Any())//内部参与演练单位
                            BantchSaveEntityNoCommit(ListDepartment);
                        if (ListSummaryRequirement != null && ListSummaryRequirement.Any())//演练总结要求
                            BantchSaveEntityNoCommit(ListSummaryRequirement);
                        if (FilesSummary != null && FilesSummary.Any())//演练总结报告
                            BantchSaveEntityNoCommit(FilesSummary);
                        if (FilesRecord != null && FilesRecord.Any())//演练活动记录
                            BantchSaveEntityNoCommit(FilesRecord);
                    }, entity.APPROVE_ID, null, null, null, null, "CM034_SHOWPRINT", null, "应急演练活动记录审批");
                }
                return true;
            });
        }


        /// <summary>
        /// 获得单条实体数据
        /// </summary>
        /// <param name="filter">过滤实体</param>
        /// <returns></returns>
        [HttpPost, Route("GetOrder")]
        public JsonActionResult<T_CM_DRILL_RECORD> GetOrder([FromBody] KeywordFilter filter)
        {
            return SafeExecute(() =>
            {
                var model = GetEntity<T_CM_DRILL_RECORD>(null, filter, null);
                if (model.Nav_ListUserEstimate != null && model.Nav_ListUserEstimate.Any())
                {
                    //第一个人 记录人
                    model.Nav_ListUserEstimate = model.Nav_ListUserEstimate.OrderByDescending(e => e.ISSIGN).ToList();
                }
                return model;
            });
        }

        /// <summary>
        /// 应急演练活动  审批结束
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("DrillRecordEnd")]
        public JsonActionResult<bool> DrillRecordEnd(string id)
        {
            return SafeExecute<bool>(() =>
            {
                return ApproveCallBackService.CallBack("CM/CMDrillPlan/DrillRecordEnd", id);
            });
        }
    }
}