using APT.BaseData.Domain.Enums;
using APT.Infrastructure.Core;
using APT.BaseData.Domain.ApiModel.EX;
using APT.BaseData.Domain.Entities.FM;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Collections.Generic;

using APT.Utility;
namespace APT.FM.WebApi.Controllers.Api.FM
{
    /// <summary>
    /// 通知
    /// </summary>
    [Route("api/FM/FMDepartmentCalendarConfig")]
    public class DepartmentCalendarConfigController : AuthorizeApiController<T_FM_DEPARTMENT_CALENDAR_CONFIG>
    {
        /// <summary>
        /// 保存配置信息
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_FM_DEPARTMENT_CALENDAR_CONFIG entity)
        {
            return SafeExecute<bool>(() =>
            {
                if (entity != null)
                {
                    entity.CURR_TIME = (entity.CURR_TIME < entity.START_TIME) ? entity.START_TIME : entity.CURR_TIME;
                    var detail = entity.Nav_DepartmentCalendarConfigDetail;
                    var team = entity.Nav_DepartmentCalendarConfigTeam;
                    entity.Nav_DepartmentCalendarConfigDetail = null;
                    if(detail == null|| team == null)
                    {
                        this.ThrowError("040002");
                    }
                    if(team.Select(i=>i.NUM).Distinct().Count()!= detail.Count(t => t.IS_DELETED == false))
                    {
                        this.ThrowError("040003");
                    }
                    this.UnifiedCommit(() =>
                    {
                        this.UpdateEntityNoCommit(entity);
                        if (detail != null)
                        {
                            detail.ForEach(i=>i.DEPARTMENT_CALENDAR_CONFIG_ID = entity.ID);
                            this.BantchSaveEntityNoCommit(detail);
                        }
                        if (team != null)
                        {
                            team.ForEach(i => i.DEPARTMENT_CALENDAR_CONFIG_ID = entity.ID);
                            this.BantchSaveEntityNoCommit(team);
                        }
                          
                    });
                }

                return true;
            });
        }
        /// <summary>
        /// 根据主键删除数据
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpGet, Route("FullDelete")]
        public JsonActionResult<bool> FullDelete(string id)
        {
            return SafeExecute<bool>(() =>
            {
                this.UnifiedCommit(() =>
                {
                    DeleteEntityNoCommit<T_FM_DEPARTMENT_CALENDAR_CONFIG_DETAIL>(i => i.DEPARTMENT_CALENDAR_CONFIG_ID == new Guid(id));
                    DeleteEntityNoCommit<T_FM_DEPARTMENT_CALENDAR_CONFIG_TEAM>(i => i.DEPARTMENT_CALENDAR_CONFIG_ID == new Guid(id));
                    DeleteEntityNoCommit<T_FM_DEPARTMENT_CALENDAR_CONFIG>(id);
                });

                return true;
            });
        }


    }
}
