using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using MadRunFabric.Common;
using Microsoft.AspNetCore.Authorization;
using DistributionApi.IRepositories;
using Microsoft.Extensions.Logging;
using Api.SignToken;
using MadRunFabric.Model;
using MongoDB.Bson;
using System.Security.Claims;

namespace DistributionApi.Controllers
{
    /// <summary>
    /// 自动定时计划
    /// </summary>
    [Authorize]
    [ApiVersion("6.0")]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class HangfirePlanTimeingController : BaseController
    {
        private readonly ILogger<HangfirePlanTimeingController> _logger;
        private readonly IHangfirePlan_DistributionRepository _hangfireplan_distributionrepository;
        private readonly IHangfireService _hangfireService;
        public HangfirePlanTimeingController(
            ILogger<HangfirePlanTimeingController> logger,
            IHangfirePlan_DistributionRepository hangfireplan_distributionrepository, 
            IHangfireService hangfireService
            )
        {
            _logger = logger;
            _hangfireplan_distributionrepository = hangfireplan_distributionrepository;
            _hangfireService = hangfireService;
        }

        /// <summary>
        /// 自动定时计划 - 自动分配
        /// 配送岗    抢单时效     3分钟（某单子此时间内没人抢，系统强制分配）
        /// 配送岗 待配送工单滞留时效     30分钟（超时就记一次超时违规记录，可以有违规申诉）
        /// </summary>
        /// <param name="wotype"></param>
        /// <param name="plancycle"></param>
        /// <param name="planinterval"></param>
        /// <returns></returns>
        [HttpPost("addautoassign")]
        public async Task<IActionResult> AddAutoAssignAsync(int plancycle, int planinterval)
        {
            var usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;

            var model = new HangfirePlan_Distribution();
            model.id = ObjectId.GenerateNewId().ToString();
            model.wotype = 2;
            model.plantype = (int)EnumDistributionPlanType.timing;
            model.plancycle = plancycle;
            model.planinterval = planinterval;
            model.planstate = (int)EnumDistributionPlanState.start;//默认计划开启
            model.createby = usercode;
            model.createtime = DateTime.Now;
            model.isdelete = 0;
            bool b = await _hangfireplan_distributionrepository.Add(model);
            if (b)
            {
                SetTask(model.id, model.wotype, model.plancycle, model.planinterval);
                return Success("添加成功");
            }
            return Error("添加失败");
        }

        ///// <summary>
        ///// 获取计划定时事件详情
        ///// </summary>
        ///// <param name="planid"></param>
        ///// <returns></returns>
        //[AllowAnonymous]
        //[HttpPost("gethangfireplaninfo")]
        //public async Task<IActionResult> GetHangfirePlanInfo(string planid)
        //{
        //    var model = await _hangfireplan_distributionrepository.GetSingle(planid);
        //    if (model!=null)
        //    {
        //        return Success("添加成功", model);
        //    }
        //    return Error("添加失败");
        //}

        #region 设置定时任务
        private void SetTask(string id, int wotype, int plancycle, int planinterval)
        {
            _hangfireService.SaveAutoAssign(id, wotype, plancycle, planinterval);
        }
        #endregion
    }
}