package enums

import "SSO/utils"

const ( // ErrorCode
	// 登录注册错误 600+
	ErrorCodeLoginTimeout           = 601
	ErrorCodeNotLogin               = 602
	ErrorCodePermissionDenied       = 603
	ErrorCodeMobileRegistered       = 604
	ErrorCodePasswordEmpty          = 605
	ErrorCodeVerificationCodeWrong  = 606
	ErrorCodeRegisterFail           = 607
	ErrorCodeInvalidToken           = 608
	ErrorCodeAccountOrPasswordWrong = 609
	ErrorCodeMissingOrg             = 610
	ErrorCodeMissingOrgApp          = 611
	ErrorCodeAccountOrVerCodeWrong  = 612
	ErrorCodeMobileNotExit          = 613

	// 数据验证错误 700+
	ErrorCodeMobileFormat   = 701
	ErrorCodeTelphoneFormat = 702
	ErrorCodeParamWrong     = 703

	// 数据库操作错误 800+
	ErrorCodeDBCreate         = 801
	ErrorCodeDBUpdate         = 802
	ErrorCodeDBDelete         = 803
	ErrorCodeDBSelectNoResult = 804
	ErrorCodeDataException    = 805

	// 业务逻辑操作错误 900+
	ErrorCodeRepeatCreateOrg                = 901
	ErrorCodeRepeatCreateApp                = 902
	ErrorCodeNeverCreateTypeApp             = 903
	ErrorCodeContactSuperAdminCreateTypeApp = 904
)

var ErrCodeMsgs = map[int]string{
	// 登录注册错误
	ErrorCodeLoginTimeout:           "登录超时",
	ErrorCodeNotLogin:               "未登录",
	ErrorCodePermissionDenied:       "权限不足",
	ErrorCodeMobileRegistered:       "手机号已被注册",
	ErrorCodePasswordEmpty:          "密码为空",
	ErrorCodeVerificationCodeWrong:  "验证码错误",
	ErrorCodeRegisterFail:           "注册失败",
	ErrorCodeInvalidToken:           "令牌无效",
	ErrorCodeAccountOrPasswordWrong: "账号或密码错误",
	ErrorCodeMissingOrg:             "未创建机构",
	ErrorCodeMissingOrgApp:          "未创建任何应用",
	ErrorCodeAccountOrVerCodeWrong:  "账号或验证码错误",
	ErrorCodeMobileNotExit:          "手机号不存在",

	// 数据验证错误
	ErrorCodeMobileFormat:   "手机号格式错误",
	ErrorCodeTelphoneFormat: "电话格式错误",
	ErrorCodeParamWrong:     "参数错误",

	// 数据库操作错误
	ErrorCodeDBCreate:         "数据库创建出错",
	ErrorCodeDBUpdate:         "数据库更新出错",
	ErrorCodeDBDelete:         "数据库删除出错",
	ErrorCodeDBSelectNoResult: "查询无结果",
	ErrorCodeDataException:    "数据异常",

	// 业务逻辑操作错误
	ErrorCodeRepeatCreateOrg:                "该账号已创建过机构了",
	ErrorCodeRepeatCreateApp:                "该账号已创建过此种应用了",
	ErrorCodeNeverCreateTypeApp:             "未创建此类应用",
	ErrorCodeContactSuperAdminCreateTypeApp: "请联系超级管理员开通此种应用",
}

type SGJError struct {
	Code int
}

func (e *SGJError) Error() string {
	value, ok := ErrCodeMsgs[e.Code]
	if ok {
		return value
	} else {
		return "未知错误"
	}
}

func MakeSuccessResponseJSON(data map[string]interface{}) map[string]interface{} {
	json := make(map[string]interface{})
	if data != nil {
		json["data"] = data
	} else {
		json["data"] = make(map[string]interface{})
	}
	json["state"] = 1
	json["code"] = 0
	return json
}

func MakeFailResponseJSON(errMsg string, errCode int) map[string]interface{} {
	json := make(map[string]interface{})
	json["msg"] = errMsg
	json["code"] = errCode
	json["state"] = 0
	return json
}

func MakeFailResponseJSONWithSGJError(err *SGJError) map[string]interface{} {
	if err == nil {
		utils.WarningLog("MakeFailResponseJSONWithRequestError 参数err 不能为空")
		return nil
	}
	return MakeFailResponseJSON(err.Error(), err.Code)
}

func MakeFailResponseJSONWithSGJErrorCode(code int) map[string]interface{} {
	err := &SGJError{Code: code}
	return MakeFailResponseJSON(err.Error(), err.Code)
}
