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

namespace APT.SC.WebApi.Controllers.Api.OG
{
    [Route("api/OG/OGEmployeeOpinionReport")]
    public class OGEmployeeOpinionReportController : AuthorizeApiController<T_OG_EMPLOYEE_OPINION_REPORT>
    {
        IFMNotificationTaskService NotificationTaskService { get; set; }

        public OGEmployeeOpinionReportController(IFMNotificationTaskService notificationTaskService, IPFCodeRuleService codeRuleService)
        {
            NotificationTaskService = notificationTaskService;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_OG_EMPLOYEE_OPINION_REPORT entity)
        {
            return SafeExecute<bool>(() =>
            {
                if (entity.START_TIME == null)
                {
                    entity.START_TIME = DateTime.Now;
                }
                if (entity.USER_ID == null)
                {
                    if (entity.STATUS == OGEmployeeOpinionReportStatus.草稿)
                    {
                        entity.USER_ID = APT.Infrastructure.Api.AppContext.CurrentSession.UserID;
                    }
                }
                if (entity.USER_ID == null)
                {
                    throw new Exception("用户信息错误");
                }
                var userInfo = GetEntity<T_FM_USER>(t => t.ID == entity.USER_ID, "Nav_ApproveRole");
                if (entity.LAUNCH_DEPARTMENT_ID == null)
                {
                    if (entity.STATUS == OGEmployeeOpinionReportStatus.草稿)
                    {
                        entity.LAUNCH_DEPARTMENT_ID = userInfo.DEPARTMENT_ID;
                    }
                }
                if (entity.LAUNCH_DEPARTMENT_ID == null)
                {
                    throw new Exception("部门信息错误");
                }
                if (entity.ASSIGNEE_ID == null)
                {
                    if (entity.STATUS == OGEmployeeOpinionReportStatus.草稿)
                    {
                        if (userInfo.APPROVE_ROLE_ID != null && userInfo.Nav_ApproveRole.NAME.Contains("负责人"))
                        {
                            entity.ASSIGNEE_ID = GetEntity<T_FM_DEPARTMENT>(t => t.ID == entity.LAUNCH_DEPARTMENT_ID, "Nav_Parent").Nav_Parent.USER_ID;
                        }
                        else
                        {
                            entity.ASSIGNEE_ID = GetEntity<T_FM_DEPARTMENT>(t => t.ID == entity.LAUNCH_DEPARTMENT_ID).USER_ID;
                        }
                    }
                }
                if (entity.ASSIGNEE_ID == null)
                {
                    throw new Exception("部门负责人信息错误");
                }
                T_FM_NOTIFICATION_TASK sendNotice = null;
                T_FM_NOTIFICATION_TASK finishNotice = null;

                if (entity.PUBLISH != null && entity.PUBLISH.Equals("SaveAndNotify"))
                {
                    DateTime dtEnd = NotificationTaskService.GetTaskEndTime(FMTASKTYPE.Default, entity.ORG_ID.Value, DateTime.Now, null, null);
                    if (entity.STATUS == (int)OGEmployeeOpinionReportStatus.草稿)
                    {
                        if (string.IsNullOrEmpty(entity.NAME))
                        {
                            throw new Exception("请填写建议事项名称");
                        }
                        if (string.IsNullOrEmpty(entity.DESCRIPTION))
                        {
                            throw new Exception("请填写建议事项描述");
                        }
                        entity.STATUS = OGEmployeeOpinionReportStatus.确认中;
                        sendNotice = NotificationTaskService.InsertUserNoticeTaskModel("员工意见受理", entity.ID, entity.ORG_ID.Value, entity.ASSIGNEE_ID.Value, "", DateTime.Now, dtEnd, 1, "OG012");
                    }
                    else if (entity.STATUS == OGEmployeeOpinionReportStatus.确认中)
                    {
                        if (string.IsNullOrEmpty(entity.ASSIGNEE_DESCRIPTION))
                        {
                            throw new Exception("请填写受理状况描述");
                        }
                        if (entity.DO_USER_ID == null)
                        {
                            throw new Exception("请选择落实人");
                        }
                        entity.STATUS = OGEmployeeOpinionReportStatus.审核中;
                        sendNotice = NotificationTaskService.InsertUserNoticeTaskModel("员工意见落实", entity.ID, entity.ORG_ID.Value, entity.DO_USER_ID.Value, "", DateTime.Now, dtEnd, 1, "OG012");
                    }
                    else if (entity.STATUS == OGEmployeeOpinionReportStatus.审核中)
                    {
                        if (string.IsNullOrEmpty(entity.IMPLEMENT_DESCRIPTION))
                        {
                            throw new Exception("请填写落实情况描述");
                        }
                        entity.STATUS = OGEmployeeOpinionReportStatus.评价中;
                        entity.ASSIGNEE_STATUS = OGAssigneeStatus.已受理;
                        sendNotice = NotificationTaskService.InsertUserNoticeTaskModel("员工意见评价", entity.ID, entity.ORG_ID.Value, entity.USER_ID.Value, "", DateTime.Now, dtEnd, 1, "OG012");
                    }
                    else if (entity.STATUS == OGEmployeeOpinionReportStatus.评价中)
                    {
                        entity.STATUS = OGEmployeeOpinionReportStatus.归档;
                    }
                    else if (entity.STATUS == OGEmployeeOpinionReportStatus.归档 && entity.TaskID != Guid.Empty)
                    {
                        //待办消除
                        finishNotice = NotificationTaskService.GetTaskFinishModel(entity.TaskID, "OG011_SHOWPRINT");
                        if (finishNotice != null)
                            UpdateEntityNoCommit(finishNotice);
                    }
                    else
                    {
                        throw new Exception("当前状态不可提交");
                    }
                    if (entity.TaskID != Guid.Empty)
                    {
                        finishNotice = NotificationTaskService.GetTaskFinishModel(entity.TaskID, "OG011_SHOWPRINT");
                    }
                }
                var Nav_FileList = entity.Nav_FileList;
                entity.Nav_FileList = null;
                var Nav_ImpFileList = entity.Nav_ImpFileList;
                entity.Nav_ImpFileList = null;
                UnifiedCommit(() =>
                {
                    UpdateEntity(entity);
                    if (Nav_FileList != null && Nav_FileList.Any())
                        BantchSaveEntity(Nav_FileList);
                    if (Nav_ImpFileList != null && Nav_ImpFileList.Any())
                        BantchSaveEntity(Nav_ImpFileList);
                    if (sendNotice != null)
                        UpdateEntityNoCommit(sendNotice);
                    if (finishNotice != null)
                        UpdateEntityNoCommit(finishNotice);
                });
                return true;
            });
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("AdviceTypes")]
        public JsonActionResult<IEnumerable<T_OG_EMPLOYEE_ITEM_CATEGORY_ENUM>> AdviceTypes([FromBody] KeywordFilter filter)
        {
            return SafeExecute<IEnumerable<T_OG_EMPLOYEE_ITEM_CATEGORY_ENUM>>(() =>
            {
                return GetEntities<T_OG_EMPLOYEE_ITEM_CATEGORY_ENUM>(null, filter);
            });
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("UpdateAdviceType")]
        public JsonActionResult<bool> UpdateAdviceType([FromBody] T_OG_EMPLOYEE_ITEM_CATEGORY_ENUM entity)
        {
            return SafeExecute<bool>(() =>
            {
                UpdateEntity(entity);
                return true;
            });
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetAdviceType")]
        public JsonActionResult<T_OG_EMPLOYEE_ITEM_CATEGORY_ENUM> GetAdviceType([FromBody] KeywordFilter filter)
        {
            return SafeExecute<T_OG_EMPLOYEE_ITEM_CATEGORY_ENUM>(() =>
            {
                return GetEntity<T_OG_EMPLOYEE_ITEM_CATEGORY_ENUM>(null, filter);
            });
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("DelAdviceType")]
        public JsonActionResult<bool> DelAdviceType(string id)
        {
            return SafeExecute<bool>(() =>
            {

                return DeleteEntity<T_OG_EMPLOYEE_ITEM_CATEGORY_ENUM>(id);
            });
        }
    }
}
