using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Enums.PF;
using APT.BaseData.Domain.IServices;
using APT.BaseData.Domain.IServices.AE;
using APT.BaseData.Domain.IServices.FM;
using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.OH;
using APT.MS.Domain.Entities.TI;
using APT.MS.Domain.Enums;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace APT.SC.WebApi.Controllers.Api.OH
{
    [Route("api/OH/OHHealthExamBatch")]
    public class OHHealthExamBatchController : AuthorizeApiController<T_OH_HEALTH_EXAM_BATCH>
    {
        IFMFlowPermitService MFlowPermitService { get; set; }
        IPFApproveCallBackService ApproveCallBackService { get; set; }
        IAEAccidentEventReportService AccidentEventReportService { get; set; }
        public OHHealthExamBatchController(IFMFlowPermitService mFlowPermitService, IPFApproveCallBackService approveCallBackService, IAEAccidentEventReportService accidentEventReportService)
        {
            MFlowPermitService = mFlowPermitService;
            ApproveCallBackService = approveCallBackService;
            AccidentEventReportService = accidentEventReportService;
        }

        /// <summary>
        /// 体检周期表 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_OH_HEALTH_EXAM_BATCH entity)
        {
            return SafeExecute<bool>(() =>
            {
                var ListDepartment = entity.Nav_ListDepartment;
                entity.Nav_ListDepartment = null;

                #region     值判断

                if (string.IsNullOrEmpty(entity.BATCH))
                {
                    throw new Exception("请填写体检批次");
                }
                if (entity.START_TIME == DateTime.MinValue)
                {
                    throw new Exception("请选择体检开始时间");
                }
                if (entity.END_TIME == DateTime.MinValue)
                {
                    throw new Exception("请选择体检截止时间");
                }
                if (entity.START_TIME > entity.END_TIME)
                {
                    throw new Exception("体检开始时间不能大于截止时间");
                }
                if (string.IsNullOrEmpty(entity.LOCATION))
                {
                    throw new Exception("请填写体检地点");
                }
                if (!entity.USER_ID.HasValue)
                {
                    throw new Exception("请填写带队人员");
                }

                if (ListDepartment == null || !ListDepartment.Any())
                {
                    throw new Exception("请选择体检人所在部门！");
                }
                else
                {
                    foreach (var item in ListDepartment)
                    {
                        item.Nav_Department = null;
                    }
                }

                #endregion

                //entity.START_TIME = entity.START_TIME.Date;
                //entity.END_TIME = entity.END_TIME.Date.AddDays(1).AddSeconds(-1);

                this.UnifiedCommit(() =>
                {
                    if (entity != null)
                        UpdateEntityNoCommit(entity);   //事故事件上报
                    if (ListDepartment != null && ListDepartment.Any())
                        BantchSaveEntityNoCommit(ListDepartment);
                });

                return true;
            });
        }
    }
}