using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Enums;
using APT.BaseData.Services.Services.FM;
using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.FO;
using APT.MS.Domain.Entities.HM;
using APT.MS.Domain.Enums;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System;

namespace APT.HM.WebApi.Controllers.Api
{
    /// <summary>
    ///  作业环节
    /// </summary>
    [Route("api/HM/HMOperationStep")]
    public partial class OperationStepController : AuthorizeApiController<T_HM_OPERATION_STEP>
    {
        /// <summary>
        /// 新增修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_HM_OPERATION_STEP entity)
        {
            return SafeExecute<bool>(() =>
            {
                var operationStep = this.GetEntities<T_HM_OPERATION_STEP>(t => t.NAME == entity.NAME,new BaseFilter(entity.ORG_ID));
                if (operationStep !=null && operationStep.Count() > 1)
                    this.ThrowError("030019");
                var safeConfirms = entity.Nav_SafeConfirms;
                entity.Nav_SafeConfirms = null;
                if (safeConfirms != null && safeConfirms.Any())
                {
                    safeConfirms.ForEach(t =>
                    {
                        t.ORG_ID = entity.ORG_ID;
                        t.OPERATION_STEP_ID = entity.ID;
                    });
                }
                var safeMesasures = entity.Nav_SafeMeasures;
                entity.Nav_SafeMeasures = null;
                if (safeMesasures != null && safeMesasures.Any())
                {
                    safeMesasures.ForEach(t =>
                    {
                        t.ORG_ID = entity.ORG_ID;
                        t.OPERATION_STEP_ID = entity.ID;
                    });
                }
                var safeDeal = entity.Nav_DealMeasures;
                entity.Nav_DealMeasures = null;
                if (safeDeal != null && safeDeal.Any())
                {
                    safeDeal.ForEach(t =>
                    {
                        t.ORG_ID = entity.ORG_ID;
                        t.OPERATION_STEP_ID = entity.ID;
                    });
                }
                this.UnifiedCommit(() =>
                {
                    if (entity != null)
                        UpdateEntityNoCommit(entity);
                    if (safeConfirms != null && safeConfirms.Any())
                        this.BantchSaveEntityNoCommit(safeConfirms);
                    if (safeMesasures != null && safeMesasures.Any())
                        this.BantchSaveEntityNoCommit(safeMesasures);
                    if (safeDeal != null && safeDeal.Any())
                        this.BantchSaveEntityNoCommit(safeDeal);
                });
                return true;
            });
        }
    }
}
