using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Entities.FM;
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.TL;
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.SC
{
    [Route("api/TL/TLFileSum")]
    public class TLFileSumController : AuthorizeApiController<T_TL_FILE_SUM>
    {
        IFMFlowPermitService MFlowPermitService { get; set; }
        IFMNotificationTaskService NotificationTaskService { get; set; }
        public TLFileSumController(IFMFlowPermitService mFlowPermitService, IFMNotificationTaskService notificationTaskService)
        {
            MFlowPermitService = mFlowPermitService;
            NotificationTaskService = notificationTaskService;
        }

        /// <summary>
        /// 附件上传
        /// 设计文件专家评审意见 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_TL_FILE_SUM entity)
        {
            return SafeExecute<bool>(() =>
            {
                var files = entity.Nav_Files;//附件
                entity.Nav_Files = null;
                if (files == null || !files.Any())
                {
                    throw new Exception("请上传附件！");
                }
                T_FM_NOTIFICATION_TASK task = null;
                if (entity.STATUS == PFStandardStatus.Archived)
                {
                    if (entity.TaskID == Guid.Empty)
                    {
                        throw new Exception("获取待办信息失败！");
                    }
                    task = NotificationTaskService.GetEntityTask(entity.TaskID, "TL004_SHOWPRINT");
                }

                this.UnifiedCommit(() =>
                {
                    if (entity != null)
                        UpdateEntityNoCommit(entity);
                    if (files != null && files.Any())//附件
                        BantchSaveEntityNoCommit(files);
                    if (task != null)//待办清除
                        UpdateEntityNoCommit(task);
                });
                return true;
            });
        }

    }
}