using System.Net;
using System.Web.Http;
using DigitalSchoolApi.Models;
using Newtonsoft.Json;

namespace DigitalSchoolApi.Controllers
{
    /// <summary>
    /// 基础API
    /// </summary>
    public class BaseController : ApiController
    {
        /// <summary>
        /// 请求成功
        /// </summary>
        /// <param name="result">操作结果</param>
        /// <param name="info">自定义消息</param>
        /// <param name="code">http状态码</param>
        /// <param name="data">返回数据</param>
        /// <returns></returns>
        protected IHttpActionResult ResultInfo(object data = null, HttpStatusCode code = HttpStatusCode.OK, Result result = Result.Success, string info = "")
        {
            if (string.IsNullOrEmpty(info))
            {
                switch (result)
                {

                    case Result.Fail:
                        info = "操作失败";
                        break;
                    case Result.NotFound:
                        info = "未找到相关数据";
                        break;
                    default:
                        info = "请求成功";
                        break;
                }
            }
            return Json(new OperateResult<object> { StatusCode = code, Result = result, Message = info, Data = data },
                new JsonSerializerSettings()
                {
                    NullValueHandling = NullValueHandling.Ignore,DateFormatString = "yyyy-MM-dd HH:mm:ss"
                });
        }
    }
}
