using FactorySystemModel.EnumModel;
using System;

namespace FactorySystemModel.ResponseModel
{
    public class ApiResult
    {
        #region 初始化构造函数
        /// <summary>
        /// 初始化构造函数
        /// </summary>
        public ApiResult()
        {
            Ticket = "";
            Code = (int)Constant.ApiResultCode.成功;
            Message = Constant.ApiResultCode.成功.ToString();
        }
        /// <summary>
        /// 初始化构造函数，成功时调用
        /// </summary>
        /// <param name="data">接口返回内容</param>
        public ApiResult(object data)
        {
            Code = (int)Constant.ApiResultCode.成功;
            Message = Constant.ApiResultCode.成功.ToString();
            Data = data ?? "";
        }
        #endregion

        #region 实体字段定义
        /// <summary>
        /// 接口编码，成功=200,失败=-1,异常=-999
        /// </summary>
        public int Code { get; set; }
        /// <summary>
        /// 接口消息
        /// </summary>
        public string Message { get; set; }
        /// <summary>
        /// 接口消息
        /// </summary>
        public string Ticket { get; set; }
        /// <summary>
        /// 接口返回数据
        /// </summary>
        public object Data { get => data ?? ""; set => data = value; }

        private object data;
        #endregion

        #region 错误时返回实体方法
        /// <summary>
        /// 系统错误
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public ApiResult Error(Exception ex)
        {
            this.Code = (int)Constant.ApiResultCode.异常;
            this.Message = "数据异常：" + ex.Message.ToString();
            return this;
        }

        /// <summary>
        /// 自定义错误
        /// </summary>
        public ApiResult Error(string message)
        {
            this.Code = (int)Constant.ApiResultCode.异常;
            this.Message = message;
            return this;
        }

        /// <summary>
        /// 自定义错误
        /// </summary>
        /// <param name="Code"></param>
        /// <param name="Message"></param>
        /// <returns></returns>
        public ApiResult CustomError(int Code, string Message)
        {
            this.Code = Code;
            this.Message = Message;
            return this;
        }
        /// <summary>
        /// 返回失败
        /// </summary>
        /// <returns></returns>
        public ApiResult FailError()
        {
            this.Code = (int)Constant.ApiResultCode.失败;
            this.Message = Constant.ApiResultCode.失败.ToString();
            return this;
        }
        #endregion
    }
}
