using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using APT.BaseData.Domain.Entities.FM;
using APT.Infrastructure.Core;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;

namespace APT.FM.WebApi.Controllers.Api
{
    /// <summary>
    ///  枚举管理
    /// </summary>
    public partial class EnumController
    {
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody]T_FM_ENUM entity)
        {
            return SafeExecute<bool>(() =>
            {
                var details = entity.Nav_EnumItems;
                entity.Nav_EnumItems = null;
                this.UnifiedCommit(() =>
                {
                    this.UpdateEntityNoCommit(entity);
                    if (details != null && details.Any())
                        this.BantchSaveEntityNoCommit(details);
                });

                return true;
            });
        }

        /// <summary>
        /// 根据Code获取指定枚举项
        /// </summary>
        /// <param name="filter">过滤实体</param>
        /// <param name="Parameter1">参数</param>
        /// <returns></returns>
        [HttpPost, Route("GetEnumDataByCode")]
        public JsonActionResult<List<T_FM_ENUM_ITEM>> GetEnumDataByCode([FromBody] KeywordFilter filter)
        {
            return SafeExecute<List<T_FM_ENUM_ITEM>>(() =>
            {
                var data = this.GetEntities<T_FM_ENUM>(e => e.CODE == filter.Parameter1, filter, "Nav_EnumItems").FirstOrDefault();
                return data?.Nav_EnumItems?.ToList()??null;
            });
        }

        /// <summary>
        /// 获取枚举
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetENUMType")]
        public PagedActionResult<T_FM_ENUM> GetENUMType([FromBody] KeywordPageFilter filter)
        {
            return SafeGetPagedData<T_FM_ENUM>((result) =>
            {
                var enumCode = "ENUM";
                var data = GetOrderPageEntities<T_FM_ENUM>(x => x.CODE.StartsWith(enumCode), filter);
                result.Data = data.Data;
                result.TotalCount = data.TotalCount;
            });
        }

        /// <summary>
        /// 根据Code获取指定枚举项
        /// </summary>
        /// <param name="filter">过滤实体</param>
        /// <param name="Parameter1">参数</param>
        /// <returns></returns>
        [HttpPost, Route("GetEnumsByCode")]
        public PagedActionResult<T_FM_ENUMS> GetEnumsByCode([FromBody] KeywordPageFilter filter)
        {
            return SafeGetPagedData<T_FM_ENUMS>((result) =>
            {
                var enumCode = "BSMineTypeEnum";
                var data = GetOrderPageEntities<T_FM_ENUMS>(x => x.CODE == enumCode && x.VALUE != 0, filter);
                result.Data = data.Data.OrderBy(t=>t.VALUE).ToList();
                result.TotalCount = data.TotalCount;
            });
        }
    }
}
