using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dnc.Api.Throttle;
using Mall.Common.API;
using Mall.WebApi.Filter;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Mall.WebApi.Controllers.CAP
{
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class TestController : BaseController
    {
        private readonly DotNetCore.CAP.ICapPublisher _capBus;
        public TestController(DotNetCore.CAP.ICapPublisher capPublisher)
        {
            this._capBus = capPublisher;
            
        }

        [HttpPost]
        [AllowAnonymous] [RateValve(Policy = Policy.Ip, Limit = 10, Duration = 60)]
        public ApiResult Get()
        {
            ApiResult apiResult = new ApiResult()
            {
                message = "操作成功",
                resultCode = 1,
                data = DateTime.Now
            };
            _capBus.Publish("show.time", Common.Plugin.JsonHelper.Serialize(apiResult));
          
            return apiResult;
        }

        [NonAction]
        [AllowAnonymous] [RateValve(Policy = Policy.Ip, Limit = 10, Duration = 60)]
        [DotNetCore.CAP.CapSubscribe("show.time")]
        public void CheckReceiveMessage(string result)
        {
            //string str = "";
            Console.WriteLine(result);
        }
    }
}