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/PRTungpreOperationRecord")]
    public partial class TungpreOperationRecordController : AuthorizeApiController<T_PR_TUNGPRE_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 TungpreOperationRecordController(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_TUNGPRE_OPERATION_RECORD entity)
        {
            return SafeExecute(() =>
            {
                entity.STATUS = PFStandardStatus.Draft;
                //entity.TIME = DateTime.Now;
                var seris = entity.Nav_Seris;
                if (seris != null && seris.Any())
                {
                    seris.ForEach(t =>
                    {
                        t.ORG_ID = entity.ORG_ID; t.OPERATION_RECORD_ID = entity.ID;
                    });
                }
                var drugs = entity.Nav_Drugs;
                if (drugs != null && drugs.Any())
                {
                    drugs.ForEach(t =>
                    {
                        t.ORG_ID = entity.ORG_ID; t.OPERATION_RECORD_ID = entity.ID;
                    });
                }
                var meas = entity.Nav_Meas;
                if (meas != null && meas.Any())
                {
                    meas.ForEach(t =>
                    {
                        t.ORG_ID = entity.ORG_ID; t.OPERATION_RECORD_ID = entity.ID;
                    });
                }
                var targs = entity.Nav_Targs;
                if (targs != null && targs.Any())
                {
                    targs.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 = "PR062_SHOWPRINT";
                    }
                }
                entity.Nav_Seris = null;
                entity.Nav_Drugs = null;
                entity.Nav_Meas = null;
                entity.Nav_Targs = null;
                UnifiedCommit(() =>
                {
                    if (entity != null)
                        this.UpdateEntityNoCommit(entity);
                    if (seris != null && seris.Any())
                        this.BantchSaveEntityNoCommit(seris);
                    if (drugs != null && drugs.Any())
                        this.BantchSaveEntityNoCommit(drugs);
                    if (meas != null && meas.Any())
                        this.BantchSaveEntityNoCommit(meas);
                    if (targs != null && targs.Any())
                        this.BantchSaveEntityNoCommit(targs);
                    if (task != null)
                        this.UpdateEntityNoCommit(task);
                });
                return true;
            });
        }
    }
}
