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

namespace APT.SC.WebApi.Controllers.Api.PRController
{
    /// <summary>
    ///  尾砂泵运行记录
    /// </summary>
    [Route("api/PR/PRSandpumpOperationRecord")]
    public partial class SandpumpOperationRecordController : AuthorizeApiController<T_PR_SANDPUMP_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 SandpumpOperationRecordController(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_SANDPUMP_OPERATION_RECORD entity)
        {
            return SafeExecute(() =>
            {
                entity.STATUS = PFStandardStatus.Draft;
                //entity.TIME = DateTime.Now;
                var details = entity.Nav_Details;
                if (details != null && details.Any())
                {
                    details.ForEach(t =>
                    {
                        t.ORG_ID = entity.ORG_ID; t.OPERATION_RECORD_ID = entity.ID;
                    });
                }
                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);
                        task.SOURCE_FORMCODE = "PR066_SHOWPRINT";
                    }
                }
                entity.Nav_Details = null;
                UnifiedCommit(() =>
                {
                    if (entity != null)
                        this.UpdateEntityNoCommit(entity);
                    if (details != null && details.Any())
                        this.BantchSaveEntityNoCommit(details);
                    if (task != null)
                        this.UpdateEntityNoCommit(task);
                });
                return true;
            });
        }
    }
}
