using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.IServices.FM;
using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.BS;
using APT.MS.Domain.Enums;
using APT.Utility;
using APT.WebApi.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;

namespace APT.BS.WebApi.Controllers.Api
{
    [Route("api/BS/BSPlanSet")]
    public partial class BSPlanSetController : AuthorizeApiController<T_BS_PLAN_SET>
    {
        /// <summary>
        /// 排序分页查询数据
        /// </summary>
        /// <param name="pageFilter">分页过滤实体</param>
        /// <returns></returns>
        [HttpPost, Route("OrderPagedDescription")]
        public PagedActionResult<T_BS_PLAN_SET> OrderPagedDescription([FromBody] KeywordPageFilter pageFilter)
        {
            //return SafeGetPagedData<bool>(() =>
            //{

            //});
            pageFilter.IgnoreDataRule = true;
            //参考 NotificationTask 
            var result = this.GetOrderPageEntities<T_BS_PLAN_SET>(null, pageFilter);
            if (result.TotalCount > 0)
            {
                List<T_BS_PLAN_SET> listCHECK_TYPE_LEVEL = result.Data.ToList();
                //foreach (var item in listCHECK_TYPE_LEVEL)
                //{
                //    item.Nav_CheckTypeLevel.CheckLevelDescription = ((MS.Domain.Enums.HMLevleEnum)item.Nav_CheckTypeLevel.CheckLevel).GetDescription();
                //}
                result.Data = listCHECK_TYPE_LEVEL;
            }
            return result;
        }

        /// <summary>
        /// 获得单条实体数据
        /// </summary>
        /// <param name="filter">过滤实体</param>
        /// <returns></returns>
        [HttpPost, Route("GetInfo")]
        public JsonActionResult<T_BS_PLAN_SET> Get([FromBody] KeywordFilter filter)
        {
            //this.ActionOrderEntities(null, filter);
            //this.GetEntities(null,filter);
            JsonActionResult<T_BS_PLAN_SET> result = WitEntity(null, filter);
            try
            {
                //result.Data.Nav_CheckTypeLevel.CheckLevelDescription = ((HMLevleEnum)(result.Data.Nav_CheckTypeLevel.CheckLevel)).GetDescription();
            }
            catch
            {
                return result;
            }
            return result;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_BS_PLAN_SET entity)
        {
            return SafeExecute<bool>(() =>
            {
                var ListSetObject = entity.Nav_ListSetObject;
                entity.Nav_ListSetObject = null;

                var ListSetDepObject = entity.Nav_ListSetDepObject;
                entity.Nav_ListSetDepObject = null;
                int rowIndex = 1;
                foreach (var item in ListSetDepObject)
                {
                    if (!item.DEPARTMENT_ID.HasValue)
                    {
                        throw new Exception("请设置部门，行：" + rowIndex + "！");
                    }
                    rowIndex++;
                }
                this.UnifiedCommit(() =>
                {
                    if (entity != null)
                        UpdateEntityNoCommit(entity);
                    if (ListSetObject != null && ListSetObject.Any())
                        BantchSaveEntityNoCommit(ListSetObject);
                    if (ListSetDepObject != null && ListSetDepObject.Any())
                        BantchSaveEntityNoCommit(ListSetDepObject);
                });
                return true;
            });
        }
    }
}
