using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Enums;
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.CM;
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.CM
{
    [Route("api/CM/CMMaterialLibrary")]
    public class CMMaterialLibraryController : AuthorizeApiController<T_CM_MATERIAL_LIBRARY>
    {
        IFMNotificationTaskService NotificationTaskService { get; set; }
        public CMMaterialLibraryController(IFMNotificationTaskService notificationTaskService)
        {
            NotificationTaskService = notificationTaskService;
        }

        /// <summary>
        /// 体检周期表 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_CM_MATERIAL_LIBRARY entity)
        {
            return SafeExecute<bool>(() =>
            {
                if (string.IsNullOrEmpty(entity.NAME))
                {
                    throw new Exception("物资名称不能为空！");
                }
                if (entity.MATERIAL_TYPE_ID == Guid.Empty)
                {
                    throw new Exception("类型不能为空！");
                }
                if (entity.COUNT < 1)
                {
                    throw new Exception("请填写数量！");
                }
                if (string.IsNullOrEmpty(entity.UNIT))
                {
                    throw new Exception("单位不能为空！");
                }
                if (string.IsNullOrEmpty(entity.LOCATION))
                {
                    throw new Exception("存放位置不能为空！");
                }

                if (entity.DEPARMTNET_ID == Guid.Empty)
                {
                    throw new Exception("管理部门不能为空！");
                }
                if (entity.USER_ID == Guid.Empty)
                {
                    throw new Exception("管理责任人不能为空！");
                }
                var check = GetEntity<T_CM_MATERIAL_LIBRARY>(e => e.ID != entity.ID && e.MATERIAL_TYPE_ID == entity.MATERIAL_TYPE_ID && e.DEPARMTNET_ID == entity.DEPARMTNET_ID && e.NAME == entity.NAME && e.INTERNAL_NO == entity.INTERNAL_NO && e.LOCATION == entity.LOCATION && e.SPECIFICATION == entity.SPECIFICATION && e.MAIN_TEC_PARAMS == entity.MAIN_TEC_PARAMS);
                if (check != null)
                {
                    throw new Exception("应急物资库已有该信息，不能重复添加！");
                }
                var Files = entity.Nav_Files;
                entity.Nav_Files = null;
                T_FM_NOTIFICATION_TASK task = null;
                if (entity.TaskID != Guid.Empty)
                {
                    task = NotificationTaskService.GetEntityTask(entity.TaskID);
                }
                this.UnifiedCommit(() =>
                {
                    if (entity != null)
                        UpdateEntityNoCommit(entity);
                    if (Files != null && Files.Any())//预计影响范围
                        BantchSaveEntityNoCommit(Files);
                    if (task != null)
                        UpdateEntityNoCommit(task);
                });
                return true;
            });
        }

    }
}