package businesserr

// ErrCode 定义错误码类型
type ErrCode string

type BusinessErr struct {
	Code    ErrCode `json:"code"`
	Message string  `json:"message"`
}

func (e *BusinessErr) Error() string {
	return e.Message
}

var (
	BatchNotSetStartedError = &BusinessErr{Code: ErrCode("400"), Message: "批次开始时间未设置"}
	BatchSetEndedError      = &BusinessErr{Code: ErrCode("400"), Message: "批次已结束时间未设置"}

	BatchNotStartedError = &BusinessErr{Code: ErrCode("400"), Message: "批次未开始"}
	BatchEndedError      = &BusinessErr{Code: ErrCode("400"), Message: "批次已结束"}
)

func (err *BusinessErr) HandleThirdErrCode() CmbAPIError {

	errCode := err.Code

	emp, ok := CmbAPIPublicErrorMap[errCode]
	if ok {
		for _, e := range emp {
			if e.Description == err.Message {
				return e
			}
		}
	}

	em, ok := CmbAPIErrorMap[errCode]
	if ok {
		for _, e := range em {
			if e.Description == err.Message {
				return e
			}
		}
	}

	return CmbAPIError{
		StatusCode:   499,
		ErrorCode:    errCode,
		Description:  err.Message,
		Hint:         "",
		ThirdErrCode: ThirdErrCodeDefault,
	}
}

func (err *BusinessErr) HandleMULTIThirdErrCode() CmbAPIError {

	errCode := err.Code

	emp, ok := CmbAPIPublicErrorMap[errCode]
	if ok {
		for _, e := range emp {
			if e.Description == err.Message {
				return e
			}
		}
	}

	em2, ok := CmbMULTIAPIErrorMap[errCode]
	if ok {
		for _, e := range em2 {
			if e.Description == err.Message {
				return e
			}
		}
	}

	return CmbAPIError{
		StatusCode:   499,
		ErrorCode:    errCode,
		Description:  err.Message,
		Hint:         "",
		ThirdErrCode: ThirdErrCodeDefault,
	}
}
