using APT.Infrastructure.Core;
using APT.BaseData.Domain.Entities.FM;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using APT.Utility;
using APT.BaseData.Domain.ApiModel.FM;
using Newtonsoft.Json;
using APT.BaseData.Domain.ApiModel.Platform;
using APT.BaseData.Domain.Entities;
using System.Linq.Expressions;

namespace APT.FM.WebApi.Controllers.Api.FM
{
    /// <summary>
    /// 企业基本信息
    /// </summary>
    [Route("api/FM/FMPersonalSchedule")]
    public class PersonalScheduleController : AuthorizeApiController<T_FM_DEPARTMENT_SCHEDULING>
    {
        class PersonalScheduleModel {
            public string NAME;
            public string DEPARTMENTNAME;
            public string TEAMNAME;
            public string POSTNAME;
            public DateTime DATE;
            public string CLASSNAME;
            public string REMARK;

        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("OrderPaged")]
        public JsonActionResult<dynamic> OrderPaged([FromBody] KeywordPageFilter pageFilter)
        {
            return SafeExecute<dynamic>(() =>
            {
                var OrgId = APT.Infrastructure.Api.AppContext.CurrentSession.OrgId;
                dynamic ret = new System.Dynamic.ExpandoObject();
                //pageFilter.SelectField = new string[] { "DATE_TIME", "Nav_Class.NAME", "Nav_Class.REMARK", "Nav_Team.NAME", "Nav_DepartMent.NAME" };
                //pageFilter.Include = new string[] { "Nav_Class", "Nav_Team", "Nav_DepartmentSchedulingDetail.Nav_Person","Nav_DepartMent" };
                //BaseFilter filter = new BaseFilter(OrgId);
                //filter.Include = new string[] { "Nav_Post" };
              
                Expression<Func<T_FM_DEPARTMENT_SCHEDULING_DETAIL, bool>> express = t => true;
                if (!string.IsNullOrEmpty(pageFilter.Parameter1))
                {
                    //express = express.And(t => t.Nav_DepartmentSchedulingDetail.Any(i => i.PERSON_ID==person.ID));
                    express = express.And(i => i.Nav_Person.NAME.Contains(pageFilter.Parameter1));
                }
              
                pageFilter.Include = new string[] { "Nav_Person.Nav_DepartMent", "Nav_Person.Nav_Post", 
                    "Nav_DepartmentScheduling.Nav_Team", "Nav_Person.Nav_DepartMent" ,"Nav_DepartmentScheduling.Nav_Class"};
                var result = this.GetOrderPageEntities<T_FM_DEPARTMENT_SCHEDULING_DETAIL>(express, pageFilter);
                List<PersonalScheduleModel> personalSchedules= new List< PersonalScheduleModel >();
                foreach(var item in result.Data)
                {
                    PersonalScheduleModel personalSchedule = new PersonalScheduleModel();
                    personalSchedule.NAME = item.Nav_Person.NAME;
                    personalSchedule.DEPARTMENTNAME = item.Nav_Person.Nav_DepartMent.NAME;
                    personalSchedule.TEAMNAME = item.Nav_DepartmentScheduling.Nav_Team.NAME;
                    personalSchedule.POSTNAME = item.Nav_Person.Nav_Post.NAME;
                    personalSchedule.DATE = item.Nav_DepartmentScheduling.DATE_TIME;
                    personalSchedule.CLASSNAME = item.Nav_DepartmentScheduling.Nav_Class.NAME;
                    personalSchedule.REMARK = item.Nav_DepartmentScheduling.Nav_Class.REMARK;
                    personalSchedules.Add(personalSchedule);
                }
                ret.newData = personalSchedules;
                ret.TotalCount = result.TotalCount;
                return ret;
            });
        }
    }
}
