using APT.BaseData.Domain.Enums.PF;
using APT.BaseData.Domain.IServices;
using APT.BaseData.Domain.IServices.FM;
using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.SC;
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;

namespace APT.SC.WebApi.Controllers.Api.SC
{
    [Route("api/TI/TIInsuranceClaimRecord")]
    public class TIInsuranceClaimRecordController : AuthorizeApiController<T_TI_INSURANCE_CLAIM_RECORD>
    {
        IFMFlowPermitService MFlowPermitService { get; set; }
        IPFApproveCallBackService ApproveCallBackService { get; set; }
        public TIInsuranceClaimRecordController(IFMFlowPermitService mFlowPermitService, IPFApproveCallBackService approveCallBackService)
        {
            MFlowPermitService = mFlowPermitService;
            ApproveCallBackService = approveCallBackService;
        }

        /// <summary>
        /// 工伤保险理赔信息 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_TI_INSURANCE_CLAIM_RECORD entity)
        {
            return SafeExecute<bool>(() =>
            {
                if (entity.STATUS != PFStandardStatus.Draft && entity.STATUS != PFStandardStatus.Archived)
                {
                    throw new Exception("获取操作方式有误！");
                }
                var Files = entity.Nav_Files;
                entity.Nav_Files = null;

                if (entity.DEPARTMENT_ID == null)
                {
                    throw new Exception("请选择发起部门！");
                }
                if (entity.USER_ID == null)
                {
                    throw new Exception("请选择用户！");
                }
                if (entity.ACCIDENT_ID == null)
                {
                    throw new Exception("请选择事故事件！");
                }
                if (entity.STATUS == PFStandardStatus.Archived)
                {
                    if (entity.INJURE_TIME == null)
                    {
                        throw new Exception("请选择受伤日期！");
                    }
                    else if (entity.INJURE_TIME.Value.Date > DateTime.Now.Date)
                    {
                        throw new Exception("受伤日期不能大于今天！");
                    }
                    if (entity.INJURE_DETAIL == null)
                    {
                        throw new Exception("请填写受伤部位/疾病名称！");
                    }
                    if (entity.IN_HOSPITAL == null)
                    {
                        throw new Exception("请选择是否住院！");
                    }
                    if (entity.HOSPITAL == null)
                    {
                        throw new Exception("请填写就诊医院！");
                    }
                }
                if (entity.CREATER_ID.HasValue && entity.CREATER_ID != APT.Infrastructure.Api.AppContext.CurrentSession.UserID)
                {
                    throw new Exception("您不是添加人，不能修改此工伤保险理赔信息！");
                }

                this.UnifiedCommit(() =>
                {
                    if (entity != null)
                        UpdateEntityNoCommit(entity);   //保存主表
                    if (Files != null && Files.Any())//附件
                        BantchSaveEntityNoCommit(Files);
                });

                return true;
            });
        }
    }
}