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.Linq;
using APT.BaseData.Domain.Entities;

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

        public OGExternalContactCommunicateController(IFMNotificationTaskService notificationTaskService, IPFCodeRuleService codeRuleService)
        {
            NotificationTaskService = notificationTaskService;
        }
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_OG_EXTERNAL_CONTACT_COMMUNICATE entity)
        {
            return SafeExecute<bool>(() =>
            {

                if (entity.START_TIME == null)
                {
                    entity.START_TIME = DateTime.Now;
                }
                var Nav_FileList = entity.Nav_FileList;
                entity.Nav_FileList = null;
                var Nav_ImpFileList = entity.Nav_ImpFileList;
                entity.Nav_ImpFileList = null;
                T_FM_NOTIFICATION_TASK sendNotice = null;
                T_FM_NOTIFICATION_TASK finishNotice = null;
                if (entity.PUBLISH != null && entity.PUBLISH.Equals("SaveAndNotify"))
                {
                    if (entity.TaskID!=Guid.Empty)
                    {
                        finishNotice = NotificationTaskService.GetTaskFinishModel(entity.TaskID,"OG023_SHOWPRINT");
                    }
                    if (entity.STATUS == OGInnerContactStatus.草稿)
                    {
                        if (string.IsNullOrEmpty(entity.EXTERNAL_COMMUNICATE_PERSON))
                        {
                            throw new Exception("请填写外部单位沟通人");
                        }
                        if (string.IsNullOrEmpty(entity.COMMUNICATE_PURPOSE))
                        {
                            throw new Exception("请填写沟通目的");
                        }
                        if (string.IsNullOrEmpty(entity.COMMUNICATE_NAME))
                        {
                            throw new Exception("请填写沟通事项名称");
                        }
                        if (string.IsNullOrEmpty(entity.COMMUNICATE_DES))
                        {
                            throw new Exception("请填写沟通事项描述");
                        }
                        if (entity.DO_DEPARTMENT_ID != null && entity.DO_USER_ID == null)
                        {
                            throw new Exception("当前已选落实部门，请指定落实人员");
                        }
                        if (entity.USER_ID != null && entity.DEPARTMENT_ID == null)
                        {
                            var u = GetEntity<T_FM_USER>(entity.USER_ID.Value);
                            if (u != null)
                            {
                                entity.DEPARTMENT_ID = u.DEPARTMENT_ID;
                            }
                        }
                        var doUserName = "";
                        if (entity.DO_USER_ID != null && entity.DO_DEPARTMENT_ID == null)
                        {
                            var u = GetEntity<T_FM_USER>(t => t.ID == entity.DO_USER_ID.Value);
                            if (u != null)
                            {
                                entity.DO_DEPARTMENT_ID = u.DEPARTMENT_ID;
                                doUserName = u.NAME;
                            }
                        }
                        if (entity.DO_USER_ID != null)
                        {
                            entity.STATUS = OGInnerContactStatus.落实中;
                            sendNotice=NotificationTaskService.InsertUserNoticeTaskModel("外部联系沟通落实", entity.ID, entity.ORG_ID.Value, entity.DO_USER_ID.Value, doUserName, DateTime.Now, DateTime.Now.AddDays(1), 1, "OG024_EDIT");
                        }
                        else
                        {
                            entity.STATUS = OGInnerContactStatus.完成;
                        }
                    }
                    else if (entity.STATUS == OGInnerContactStatus.落实中)
                    {
                        if (string.IsNullOrEmpty(entity.IMPLEMENT_DESCRIPTION))
                        {
                            throw new Exception("请填写落实情况描述");
                        }
                        entity.STATUS = OGInnerContactStatus.完成;
                    }
                    else
                    {
                        throw new Exception("当前状态不可提交");
                    }
                }
                UnifiedCommit(() =>
                {
                    UpdateEntityNoCommit(entity);
                    if (Nav_FileList != null && Nav_FileList.Any())
                        BantchSaveEntityNoCommit(Nav_FileList);
                    if (Nav_ImpFileList != null && Nav_ImpFileList.Any())
                        BantchSaveEntityNoCommit(Nav_ImpFileList);
                    if (finishNotice != null)
                        UpdateEntityNoCommit(finishNotice);
                    if (sendNotice != null)
                        UpdateEntityNoCommit(sendNotice);
                });
                return true;
            });
        }
    }
}