using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Enums.PF;
using APT.BaseData.Domain.IServices;
using APT.BaseData.Domain.IServices.AE;
using APT.BaseData.Domain.IServices.FM;
using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.OH;
using APT.MS.Domain.Entities.TI;
using APT.MS.Domain.Enums;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace APT.SC.WebApi.Controllers.Api.OH
{
    [Route("api/OH/OHRunConfig")]
    public class OHRunConfigController : AuthorizeApiController<T_OH_RUNCONFIG>
    {
        IFMFlowPermitService MFlowPermitService { get; set; }
        IPFApproveCallBackService ApproveCallBackService { get; set; }
        IAEAccidentEventReportService AccidentEventReportService { get; set; }
        IFMNotificationTaskService NotificationTaskService { get; set; }
        IFMUserService UserService { get; set; }
        public OHRunConfigController(IFMFlowPermitService mFlowPermitService, IPFApproveCallBackService approveCallBackService, IAEAccidentEventReportService accidentEventReportService, IFMNotificationTaskService notificationTaskService, IFMUserService userService)
        {
            MFlowPermitService = mFlowPermitService;
            ApproveCallBackService = approveCallBackService;
            AccidentEventReportService = accidentEventReportService;
            NotificationTaskService = notificationTaskService;
            UserService = userService;
        }

        /// <summary>
        /// 表单触发时间设置
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_OH_RUNCONFIG entity)
        {
            return SafeExecute<bool>(() =>
            {
                if (string.IsNullOrEmpty(entity.STRDATE))
                {
                    throw new Exception("请填写待办日期！");
                }
                else if (!entity.STRDATE.Contains("-") && !entity.STRDATE.Contains("/"))
                {
                    throw new Exception("待办日期格式【MM-dd】！");
                }
                else
                {
                    try
                    {
                        entity.DATE = Convert.ToDateTime(DateTime.Now.Year + "-" + entity.STRDATE);
                    }
                    catch
                    {
                        throw new Exception("待办日期格式错误！");
                    }
                }

                var check = GetEntity<T_OH_RUNCONFIG>(e => e.ID != entity.ID && e.CONFIGTYPE == entity.CONFIGTYPE);
                if (check != null && check.ID != Guid.Empty)
                {
                    throw new Exception("已经设置了配置类型【" + entity.CONFIGTYPE.GetDescription() + "】的信息，操作失败！");
                }
                entity.Nav_Post = null;

                this.UnifiedCommit(() =>
                {
                    if (entity != null)
                        UpdateEntityNoCommit(entity);
                });

                return true;
            });
        }


        /// <summary>
        /// 排序分页查询数据
        /// </summary>
        /// <param name="pageFilter">分页过滤实体</param>
        /// <returns></returns>
        [HttpPost, Route("OrderPagedSuit")]
        public PagedActionResult<T_OH_RUNCONFIG> OrderPagedSuit([FromBody] KeywordPageFilter pageFilter)
        {
            return SafeGetPagedData(delegate (PagedActionResult<T_OH_RUNCONFIG> result)
            {
                PagedActionResult<T_OH_RUNCONFIG> orderPageEntities = GetOrderPageEntities<T_OH_RUNCONFIG>(null, pageFilter, null);
                if (orderPageEntities.Data != null && orderPageEntities.Data.Any())
                {
                    foreach (var item in orderPageEntities.Data)
                    {
                        item.STRDATE = item.DATE.ToString("MM-dd");
                    }
                }
                result.Data = orderPageEntities.Data.OrderBy(e => e.STRDATE);
                result.TotalCount = orderPageEntities.TotalCount;
            });
        }


        /// <summary>
        /// 获得单条实体数据
        /// </summary>
        /// <param name="filter">过滤实体</param>
        /// <returns></returns>
        [HttpPost, Route("GetSuit")]
        public JsonActionResult<T_OH_RUNCONFIG> GetSuit([FromBody] KeywordFilter filter)
        {
            return SafeExecute(() =>
            {
                var model = GetEntity<T_OH_RUNCONFIG>(null, filter, null);
                model.STRDATE = model.DATE.ToString("MM-dd HH:mm:ss");
                return model;
            });
        }
    }
}