using APT.BaseData.Domain.Entities.FM;
using APT.Infrastructure.Core;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Linq.Expressions;

namespace APT.FM.WebApi.Controllers.Api
{
    [Route("api/FM/FMTeam")]
    public partial class TeamDetailController : AuthorizeApiController<T_FM_TEAM>
    {
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_FM_TEAM entity)
        {
            return SafeExecute<bool>(() =>
            {
                var files = entity.Nav_TeamPersons;
                entity.Nav_TeamPersons = null;
                this.UnifiedCommit(() =>
                {
                    this.UpdateEntityNoCommit(entity);
                    if (files != null && files.Any())
                        this.BantchSaveEntityNoCommit(files);
                });

                return true;
            });
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("TeamOrderPaged")]
        public PagedActionResult<T_FM_TEAM> TeamOrderPaged([FromBody] KeywordPageFilter filter)
        {
            return SafeGetPagedData<T_FM_TEAM>((result) =>
            {
                Expression<Func<T_FM_TEAM, bool>> express = t => true;
                if (!string.IsNullOrEmpty(filter.Keyword) && filter.Keyword != Guid.Empty.ToString())
                    express = express.And(i => i.ID == new Guid(filter.Keyword));
                var data = this.GetOrderPageEntities<T_FM_TEAM>(express, filter);
                result.Data = data.Data;
                result.TotalCount = data.TotalCount;
            });
        }
    }
}
