using System.Xml.Serialization;
using Hncore.Infrastructure.Data;
using Hncore.Infrastructure.Extension;
using Newtonsoft.Json;

namespace Hncore.Pass.PaymentCenter.WeiFuTong
{
    [XmlRoot("xml", Namespace = "")]
    public class WeiFuTongResponseBase : WeiFuTongDTOBase
    {
        /// <summary>
        /// 0表示成功，非0表示失败此字段是通信标识，非交易标识，交易是否成功需要查看 result_code 来判断
        /// </summary>
        [XmlElement("status")]
        public string Status { get; set; }

        /// <summary>
        /// 返回信息，如非空，为错误原因签名失败参数格式校验错误
        /// </summary>
        [XmlElement("message")]
        public string Message { get; set; }

        /// <summary>
        /// 业务结果 	0表示成功，非0表示失败
        /// </summary>
        [XmlElement("result_code")]
        public string ResultCode { get; set; }

        /// <summary>
        /// 错误代码
        /// </summary>
        [XmlElement("err_code")]
        public string ErrCode { get; set; }

        /// <summary>
        /// 错误代码描述
        /// </summary>
        [XmlElement("err_msg")]
        public string ErrMsg { get; set; }

        /// <summary>
        /// 可选值 UTF-8 ，默认为 UTF-8
        /// </summary>
        [JsonIgnore]
        [XmlElement("charset")]
        public string Charset { get; set; }

        public virtual bool HasError()
        {
            return Status != "0" || ResultCode != "0";
        }

        public virtual void HandleError()
        {
            if (Status != "0")
            {
                BusinessException.Throw($"支付平台通讯失败,status:{Status},message:{Message}");
            }

            if (ResultCode != "0")
            {
                BusinessException.Throw($"支付平台业务处理失败,result_code:{ResultCode},err_msg:{ErrMsg                                                                       }");
            }
        }
    }
}