using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection.Metadata;
using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Entities.OP;
using APT.BaseData.Domain.Enums;
using APT.BaseData.Domain.Enums.PF;
using APT.BaseData.Domain.IServices.FM;
using APT.Infrastructure.Api;
using APT.Infrastructure.Core;
using APT.Migrations;
using APT.MS.Domain.Entities.OG;
using APT.MS.Domain.Entities.SC.SC;
using APT.MS.Domain.Entities.SE;
using APT.MS.Domain.Enums;
using APT.Utility;
using Castle.Core.Internal;
using ICSharpCode.SharpZipLib.Core;
using Microsoft.AspNetCore.Mvc;
using NPOI.HSSF.Record;
using NPOI.HSSF.Record.Chart;
using NPOI.SS.Formula.Functions;
using NPOI.Util;

namespace APT.SC.WebApi.Controllers.Api.SE
{
    [Route("api/SE/SERenewalTrain")]
    public class SERenewalTrainController : AuthorizeApiController<T_SE_RENEWAL_TRAIN>
    {
        IFMNotificationTaskService NotificationTaskService { get; set; }
        IFMUserService UserService { get; set; }
        public SERenewalTrainController(IFMNotificationTaskService notificationTaskService, IFMUserService userService)
        {
            NotificationTaskService = notificationTaskService;
            UserService = userService;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_SE_RENEWAL_TRAIN entity)
        {
            return SafeExecute<bool>(() =>
            {
                if (entity.EXPIRY_START_DATE == DateTime.MinValue)
                {
                    throw new Exception("请选择证书的有效期限！");
                }
                if (entity.EXPIRY_DATE.Year <= 1990)
                {
                    throw new Exception(" 请选择证书合适的有效期限！");
                }
                //人员 证书名称 复审日期
                var modelCheck = GetEntity<T_SE_RENEWAL_TRAIN>(e => e.USER_ID == entity.USER_ID && e.CERTIFICATE_NAME == entity.CERTIFICATE_NAME && e.REVIEW_DATE == entity.REVIEW_DATE && e.ID != entity.ID);
                if (modelCheck != null)
                {
                    throw new Exception("该记录已存在（判断依据：人员、证书名称、复审日期不能重复），证书编号：" + modelCheck.CERTIFICATE_CODE);
                }
                var files = entity.Nav_Files;
                entity.Nav_Files = null;
                UnifiedCommit(() =>
                {
                    UpdateEntityNoCommit(entity);
                    if (files != null && files.Any())
                        BantchSaveEntityNoCommit(files);
                });
                return true;
            });
        }
    }
}
