using System;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using StaffManagement.Database;
using StaffManagement.Extensions;

namespace StaffManagement.Controllers
{
    /// <summary>
    /// api控制器基类
    /// </summary>
    [Area("api")]
    //[ApiController]
    [Route("api/[Controller]/[Action]")]
    [ServiceFilter(typeof(ApiExceptionFilterAttribute))]
    [ProducesResponseType(typeof(ApiErrorResult),500)]
    [ProducesResponseType(typeof(ApiErrorResult),400)]
    public abstract class ApiControllerBase<LoggerType>:ControllerBase<LoggerType>
    {
        public ApiControllerBase(ILogger<LoggerType> logger,IServiceProvider service) : base(logger,service) {
        }

        /// <summary>
        /// 记录保存请求和响应日志
        /// </summary>
        /// <typeparam name="TRequest">请求信息类型</typeparam>
        /// <typeparam name="TResponse">响应信息类型</typeparam>
        /// <param name="data">请求数据</param>
        /// <param name="result">响应数据</param>
        protected void SaveLogger<TRequest, TResponse>(TRequest data,TResponse result) {
            var requestStr = this.JsonSerialize(data);
            var responseStr = this.JsonSerialize(result);
            var logmsg = $"{this.Prefix}\n请求消息:{requestStr}\n响应消息{responseStr}";
            this.Logger.LogInformation(logmsg);
        }
    }
}
