using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Enums.PF;
using APT.BaseData.Domain.IServices.FM;
using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.FO;
using APT.MS.Domain.Entities.HM;
using APT.MS.Domain.Entities.LR;
using APT.MS.Domain.Entities.WB;
using APT.MS.Domain.Enums;
using APT.Utility;
using Castle.Core.Internal;
using InfluxData.Net.InfluxDb.Models.Responses;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis.Differencing;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;

namespace APT.SC.WebApi.Controllers.Api.WB
{
    /// <summary>
    /// 作业安全表 措施
    /// </summary>
    [Route("api/WB/WBJobSafeMeasureEnum")]
    public class WBJobSafeMeasureEnumController : AuthorizeApiController<T_WB_JOB_SAFE_MEASURE_ENUM>
    {
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_WB_JOB_SAFE_MEASURE_ENUM entity)
        {
            return SafeExecute(() =>
            {
                var check = GetEntity<T_WB_JOB_SAFE_MEASURE_ENUM>(e => e.ID != entity.ID && e.TYPE_ID == entity.TYPE_ID && !e.IS_DELETED);
                if (check != null)
                {
                    throw new Exception("系统已存在该作业安全表！");
                }
                else
                {
                    if (entity.Nav_Detail == null || !entity.Nav_Detail.Any())
                    {
                        throw new Exception("请添加安全措施信息！");
                    }
                    var Nav_Detail = entity.Nav_Detail;
                    entity.Nav_Detail = null;
                    UnifiedCommit(() =>
                     {
                         UpdateEntityNoCommit(entity);
                         BantchSaveEntityNoCommit(Nav_Detail);
                     });
                    return true;
                }

            });
        }
    }
}
