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

using APT.Utility;
using APT.BaseData.Domain.Enums;

namespace APT.FM.WebApi.Controllers.Api.FM
{
    /// <summary>产品</summary>
    [Route("api/FM/SyncTask")]
    [APT.Infrastructure.Api.RootOrg]
    public class SyncTaskController : AuthorizeApiController<T_FM_SYNC_TASK>
    {
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("Entities")]
        public JsonActionResult<IEnumerable<T_FM_SYNC_TASK>> Entities([FromBody] KeywordFilter filter)
        {
            return WitEntities(null, filter);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        /// 
        [HttpPost, Route("OrderEntities")]
        public JsonActionResult<IEnumerable<T_FM_SYNC_TASK>> OrderEntities([FromBody] KeywordFilter filter)
        {
            return WitOrderEntities(null, filter);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("Paged")]
        public PagedActionResult<T_FM_SYNC_TASK> Paged([FromBody] KeywordPageFilter pageFilter)
        {
            return WitPaged(null, pageFilter);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("OrderPaged")]
        public PagedActionResult<T_FM_SYNC_TASK> OrderPaged([FromBody] KeywordPageFilter pageFilter)
        {

            var model = WitOrderPaged(null, pageFilter);
            return model;

        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("Delete")]
        public JsonActionResult<bool> Delete(string id)
        {
            return WitRealDelete(id);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Route("Update")]
        public JsonActionResult<bool> Update([FromBody] T_FM_SYNC_TASK entity)
        {
            return WitUpdate(entity);
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Route("BantchUpdate")]
        public JsonActionResult<bool> BantchUpdate([FromBody] List<T_FM_SYNC_TASK> entitys)
        {
            return WitBantchUpdate(entitys);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet, Route("BatchDelete")]
        public JsonActionResult<bool> BatchDelete(string ids)
        {
            return WitRealBatchDelete(ids);
        }


        /// <summary>
        /// 获得单条实体数据
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("Get")]
        public JsonActionResult<T_FM_SYNC_TASK> Get([FromBody] KeywordFilter filter)
        {
            return WitEntity(null, filter);
        }

        /// <summary>
        /// 查询(redis)
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        /// 
        [HttpPost, Route("RedisEntities")]
        public JsonActionResult<IEnumerable<T_FM_SYNC_TASK>> RedisEntities([FromBody] KeywordFilter filter)
        {
            return SafeExecute<IEnumerable<T_FM_SYNC_TASK>>(() =>
            {
                if (filter.OrgId == null)
                {
                    return this.GetEntities<T_FM_SYNC_TASK>(i => i.ENABLE_STATUS == (int)FMEnableStatusEnum.启用
                    && i.TASK_START_TIME < DateTime.Now, filter);
                }
                else
                {
                    return this.GetEntitiesByRedis<T_FM_SYNC_TASK>(i => i.ENABLE_STATUS == (int)FMEnableStatusEnum.启用
                    && i.TASK_START_TIME < DateTime.Now, filter);
                }

            });
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Route("Add")]
        public JsonActionResult<bool> Add([FromBody] T_FM_SYNC_TASK entity)
        {
            //entity.NEXT_START_TIME = entity.TASK_START_TIME.AddDays(entity.PERIODIC_TIME);
            var dt = entity.TASK_START_TIME;
            entity.NEXT_START_TIME = (entity.SYNC_UNIT == (int)TimeUnitEnum.秒) ? dt.AddSeconds(entity.PERIODIC_TIME) : (entity.SYNC_UNIT == (int)TimeUnitEnum.分 ? dt.AddMinutes(entity.PERIODIC_TIME) : (entity.SYNC_UNIT == (int)TimeUnitEnum.时 ? dt.AddHours(entity.PERIODIC_TIME) : dt.AddDays(entity.PERIODIC_TIME)));
            return WitUpdate(entity);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("UpdateSyncStatus")]
        public JsonActionResult<bool> UpdateSyncStatus([FromBody] KeywordFilter filter)
        {
            return SafeExecute<bool>(() =>
            {
                var task = this.GetEntity<T_FM_SYNC_TASK>(filter.Keyword);
                if (task == null) return false;

                if (!string.IsNullOrEmpty(filter.Parameter1))
                {
                    var isSynch = LibUtils.ToBoolean(filter.Parameter1);
                    if (isSynch && task.IS_SYNCING.HasValue && task.IS_SYNCING.Value)
                        throw new Exception("同步任务[" + task.NAME + "]已在同步中，请稍后重试");
                    task.IS_SYNCING = isSynch;
                }
                if (!string.IsNullOrEmpty(filter.Parameter2))
                    task.CURR_TASK_START_TIME = LibUtils.ToDateTime(filter.Parameter2);
                if (!string.IsNullOrEmpty(filter.Parameter3))
                    task.TASK_END_TIME = LibUtils.ToDateTime(filter.Parameter3).Value;
                if (!string.IsNullOrEmpty(filter.Parameter4))
                    task.NEXT_START_TIME = LibUtils.ToDateTime(filter.Parameter4).Value;
                if (!string.IsNullOrEmpty(filter.Parameter5))
                    task.EXECUTE_NUM = LibUtils.ToInt(filter.Parameter5);
                if (!string.IsNullOrEmpty(filter.Parameter6))
                    task.UPDATE_SUCCES_TIME = LibUtils.ToDateTime(filter.Parameter6);
                return this.UpdateEntity(task);
            });
        }
    }
}
