package errors

import (
	"encoding/json"

	"gd_management_gateway/common.in/jsonrpc2"
)

var (
	// 通用错误
	SystemError         = jsonrpc2.NewJsonError(10001, "系统错误")
	BadParaFormat       = jsonrpc2.NewJsonError(10002, "请求参数格式不对")
	ServiceError        = jsonrpc2.NewJsonError(10003, "内部服务错误")
	NoAuthAccess        = jsonrpc2.NewJsonError(10004, "无访问权限")
	IpNoAuthAccess      = jsonrpc2.NewJsonError(10005, "IP无访问权限")
	UserTokenillegality = jsonrpc2.NewJsonError(10006, "用户Token非法错误")
	UserTokenExpire     = jsonrpc2.NewJsonError(10007, "用户Token已过期")
	AuthNotAllow        = jsonrpc2.NewJsonError(10008, "用户权限不足")
	// 业务错误
	ArgsError        = jsonrpc2.NewJsonError(20001, "参数错误")
	DataBaseError    = jsonrpc2.NewJsonError(20002, "数据库操作失败")
	VendorError      = jsonrpc2.NewJsonError(20003, "第三方发生错误")
	DataBaseNoRecord = jsonrpc2.NewJsonError(20004, "没有找到相关记录")

	// 01 user  02 vehicle  03 service
	// 业务错误-01 user
	AppKeyError               = jsonrpc2.NewJsonError(20101, "AppKey错误")
	AppKeyExpired             = jsonrpc2.NewJsonError(20102, "AppKey过期")
	SignError                 = jsonrpc2.NewJsonError(20103, "签名异常")
	TokenExpired              = jsonrpc2.NewJsonError(20104, "token已过期")
	ApiUseExpired             = jsonrpc2.NewJsonError(20105, "API接口使用已到期")
	BeyondRequestLimit        = jsonrpc2.NewJsonError(20106, "超出请求次数次数限制")
	BeyondRequestByDayLimit   = jsonrpc2.NewJsonError(20107, "超出本日请求次数次数限制")
	BeyondRequestByWeekLimit  = jsonrpc2.NewJsonError(20108, "超出本周请求次数次数限制")
	BeyondRequestByMonthLimit = jsonrpc2.NewJsonError(20109, "超出本月请求次数次数限制")
	BeyondRequestByYearLimit  = jsonrpc2.NewJsonError(20110, "超出本年请求次数次数限制")
	AccountLock               = jsonrpc2.NewJsonError(20111, "账号登录频繁，请稍后再试")
	PasswordError             = jsonrpc2.NewJsonError(20112, "密码最低长度8位，至少包含两种字符（字母， 数字）")

	// 业务错误-02 vehicle
	VinPlateError = jsonrpc2.NewJsonError(20201, "vin码和车牌号码不匹配")

	// 业务错误-03 service
	ViolationCityMaintain              = jsonrpc2.NewJsonError(20301, "查询的城市正在维护中")
	C300CityNotSupport                 = jsonrpc2.NewJsonError(20302, "车牌城市不支持估值查询")
	AnnalExamNotStationsForCity        = jsonrpc2.NewJsonError(20303, "年检城市没有检测站")
	ServiceNoRecordError               = jsonrpc2.NewJsonError(20304, "没有该服务类型")
	OrderExistError                    = jsonrpc2.NewJsonError(20305, "不能重复创建订单")
	VehiclePlateNumberError            = jsonrpc2.NewJsonError(20306, "车牌号有误")
	VehicleEnginNoError                = jsonrpc2.NewJsonError(20307, "发动机号有误")
	VehicleVinError                    = jsonrpc2.NewJsonError(20308, "车架号有误")
	VehicleTypeNotSupport              = jsonrpc2.NewJsonError(20309, "不支持的车辆类型")
	VehicleInfoNotExist                = jsonrpc2.NewJsonError(20310, "车辆信息不存在")
	ViolationQueryNotSupport           = jsonrpc2.NewJsonError(20311, "查询的省市未开通")
	ViolationQueryNotSupportOtherPlace = jsonrpc2.NewJsonError(20312, "该省份不支持异地车辆违法查询")
	FileErr                            = jsonrpc2.NewJsonError(20313, "文件不能为空")
	ExtErr                             = jsonrpc2.NewJsonError(20314, "后缀名不符合上传要求")
	FileTypeErr                        = jsonrpc2.NewJsonError(20315, "文件类型错误")
	FileSizeErr                        = jsonrpc2.NewJsonError(20316, "文件太大")
	FileOpenFailed                     = jsonrpc2.NewJsonError(20317, "文件打开失败")
	ExportPdfFailed                    = jsonrpc2.NewJsonError(20318, "pdf 生成失败")
	OrderStatusNoSupport               = jsonrpc2.NewJsonError(20319, "不支持的订单状态")
)

func ErrorTransform(errReturn error) error {
	var e jsonrpc2.Error

	err := json.Unmarshal([]byte(errReturn.Error()), &e)
	if err != nil {
		return ServiceError
	}
	/*switch e.Code {
	case 20003:
		return ServiceError
	default:
		return errReturn
	}*/
	return errReturn
}
