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

namespace APT.SC.WebApi.Controllers.Api.PRController
{
    /// <summary>
    ///  生产运行记录表
    /// </summary>
    [Route("api/PR/PRProductionOperationRecord")]
    public partial class ProductionOperationRecordController : AuthorizeApiController<T_PR_PRODUCTION_OPERATION_RECORD>
    {
        IPFCodeRuleService CodeRuleService { get; set; }
        IFMNotificationTaskService NotificationTaskService { get; set; }
        IFMFlowPermitService MFlowPermitService { get; set; }
        IPFApproveCallBackService ApproveCallBackService { get; set; }
        /// <summary>
        /// 生产运行记录表
        /// </summary>
        /// <param name="notificationTaskService"></param>
        public ProductionOperationRecordController(IPFCodeRuleService codeRuleService, IFMNotificationTaskService notificationTaskService, IFMFlowPermitService mFlowPermitService, IPFApproveCallBackService approveCallBackService)
        {
            CodeRuleService = codeRuleService;
            NotificationTaskService = notificationTaskService;
            MFlowPermitService = mFlowPermitService;
            ApproveCallBackService = approveCallBackService;
        }
        /// <summary>
        /// 新增/编辑
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_PR_PRODUCTION_OPERATION_RECORD entity)
        {
            return SafeExecute(() =>
            {
                entity.STATUS = PFStandardStatus.Draft; 
                //entity.TIME = DateTime.Now;
                var users = entity.Nav_Users;
                var files = entity.Nav_Files;
                if (files != null && files.Any())
                {
                    files.ForEach(t =>
                    {
                        t.ORG_ID = entity.ORG_ID; t.PRODUCTION_OPERATION_RECORD_ID = entity.ID;
                    });
                }
                if (users != null && users.Any())
                {
                    users.ForEach(t =>
                    {
                        t.ORG_ID = entity.ORG_ID; t.PRODUCTION_OPERATION_RECORD_ID = entity.ID;
                        t.Nav_User = null;
                    });
                }
                T_FM_NOTIFICATION_TASK task = null;
                if (entity.PUBLISH != null && entity.PUBLISH.Equals("SaveAndNotify"))
                {
                    entity.STATUS = PFStandardStatus.Archived;
                    if (entity.TaskID != Guid.Empty)
                    {
                        task = NotificationTaskService.GetTaskFinishModel(entity.TaskID);
                    }
                }
                entity.Nav_Users = null;
                entity.Nav_Files = null;
                UnifiedCommit(() =>
                {
                    if (entity != null)
                        this.UpdateEntityNoCommit(entity);
                    if (files != null && files.Any())
                        this.BantchSaveEntityNoCommit(files);
                    if (users != null && users.Any())
                        this.BantchSaveEntityNoCommit(users);
                    if (task != null)
                        this.UpdateEntityNoCommit(task);
                });
                return true;
            });
        }
    }
}
