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
{
    /// 产品
    [Route("api/FM/SyncTask")]
    [APT.Infrastructure.Api.RootOrg]
    public class SyncTaskController : AuthorizeApiController
    {
        /// 
        /// 查询
        /// 
        /// 
        /// 
        [HttpPost, Route("Entities")]
        public JsonActionResult> Entities([FromBody] KeywordFilter filter)
        {
            return WitEntities(null, filter);
        }
        /// 
        /// 查询
        /// 
        /// 
        /// 
        /// 
        [HttpPost, Route("OrderEntities")]
        public JsonActionResult> OrderEntities([FromBody] KeywordFilter filter)
        {
            return WitOrderEntities(null, filter);
        }
        /// 
        /// 查询
        /// 
        /// 
        /// 
        [HttpPost, Route("Paged")]
        public PagedActionResult Paged([FromBody] KeywordPageFilter pageFilter)
        {
            return WitPaged(null, pageFilter);
        }
        /// 
        /// 查询
        /// 
        /// 
        /// 
        [HttpPost, Route("OrderPaged")]
        public PagedActionResult OrderPaged([FromBody] KeywordPageFilter pageFilter)
        {
            var model = WitOrderPaged(null, pageFilter);
            return model;
        }
        /// 
        /// 删除
        /// 
        /// 
        /// 
        [HttpGet, Route("Delete")]
        public JsonActionResult Delete(string id)
        {
            return WitRealDelete(id);
        }
        /// 
        /// 更新
        /// 
        /// 
        /// 
        [HttpPost, Route("Update")]
        public JsonActionResult Update([FromBody] T_FM_SYNC_TASK entity)
        {
            return WitUpdate(entity);
        }
        /// 
        /// 更新
        /// 
        /// 
        /// 
        [HttpPost, Route("BantchUpdate")]
        public JsonActionResult BantchUpdate([FromBody] List entitys)
        {
            return WitBantchUpdate(entitys);
        }
        /// 
        /// 批量删除
        /// 
        /// 
        /// 
        [HttpGet, Route("BatchDelete")]
        public JsonActionResult BatchDelete(string ids)
        {
            return WitRealBatchDelete(ids);
        }
        /// 
        /// 获得单条实体数据
        /// 
        /// 
        /// 
        [HttpPost, Route("Get")]
        public JsonActionResult Get([FromBody] KeywordFilter filter)
        {
            return WitEntity(null, filter);
        }
        /// 
        /// 查询(redis)
        /// 
        /// 
        /// 
        /// 
        [HttpPost, Route("RedisEntities")]
        public JsonActionResult> RedisEntities([FromBody] KeywordFilter filter)
        {
            return SafeExecute>(() =>
            {
                if (filter.OrgId == null)
                {
                    return this.GetEntities(i => i.ENABLE_STATUS == (int)FMEnableStatusEnum.启用
                    && i.TASK_START_TIME < DateTime.Now, filter);
                }
                else
                {
                    return this.GetEntitiesByRedis(i => i.ENABLE_STATUS == (int)FMEnableStatusEnum.启用
                    && i.TASK_START_TIME < DateTime.Now, filter);
                }
            });
        }
        /// 
        /// 更新
        /// 
        /// 
        /// 
        [HttpPost, Route("Add")]
        public JsonActionResult 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);
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        [HttpPost, Route("UpdateSyncStatus")]
        public JsonActionResult UpdateSyncStatus([FromBody] KeywordFilter filter)
        {
            return SafeExecute(() =>
            {
                var task = this.GetEntity(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);
            });
        }
    }
}