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

namespace APT.HM.WebApi.Controllers.Api
{
    [Route("api/HM/HMEvaluateHazard")]
    public partial class EvaluateHazardController : AuthorizeApiController<T_HM_EVALUATE_HAZARD>
    {
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_HM_EVALUATE_HAZARD entity)
        {
            {
                return SafeExecute<bool>(() =>
                {
                    var Individual = entity.Nav_EvaluateHazardIndividualMeasures;
                    var Engineering = entity.Nav_EvaluateHazardEngineeringMeasures;
                    var Management = entity.Nav_EvaluateHazardManagementMeasures;
                    var Education = entity.Nav_EvaluateHazardEducationMeasures;
                    var Emergency = entity.Nav_EvaluateHazardEmergencyMeasures;

                    entity.Nav_EvaluateHazardIndividualMeasures = null;
                    entity.Nav_EvaluateHazardEngineeringMeasures = null;
                    entity.Nav_EvaluateHazardManagementMeasures = null;
                    entity.Nav_EvaluateHazardEducationMeasures = null;
                    entity.Nav_EvaluateHazardEmergencyMeasures = null;

                    this.UnifiedCommit(() =>
                    {
                        if (entity != null)//保存主表
                            UpdateEntityNoCommit(entity);   
                        if (Individual != null && Individual.Any()) //保存子表
                            this.BantchSaveEntityNoCommit(Individual);
                        if (Engineering != null && Engineering.Any()) //保存子表
                            this.BantchSaveEntityNoCommit(Engineering);
                        if (Management != null && Management.Any()) //保存子表
                            this.BantchSaveEntityNoCommit(Management);
                        if (Education != null && Education.Any()) //保存子表
                            this.BantchSaveEntityNoCommit(Education);
                        if (Emergency != null && Emergency.Any()) //保存子表
                            this.BantchSaveEntityNoCommit(Emergency);
                    });
                    return true;
                });
            }
        }
    }
}
