code.go 1.09 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3
package res_m

import (
4
	"fmt"
hujiebin's avatar
hujiebin committed
5 6
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
7 8
	"gorm.io/gorm"
	"hilo-group/_const/enum/msg_e"
hujiebin's avatar
hujiebin committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
	"hilo-group/myerr"
)

type ResCode struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	Code          string
}

/**
 * true: 不存在成功, false: 已存在,不成功
 */
func CheckCode(model *domain.Model, code string) (bool, error) {
	var n int64
	if err := model.Db.Model(&ResCode{
		Code: code,
	}).Count(&n).Error; err != nil {
		return false, myerr.WrapErr(err)
	}
	if n == 0 {
		return true, nil
	} else {
		return false, nil
	}
}
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

func GetErrByLanguage(db *gorm.DB, msgId msg_e.MsgIdType, lang string, myErr *myerr.BusinessError, args ...interface{}) *myerr.BusinessError {
	var msg string
	if len(args) > 0 {
		msg = fmt.Sprintf(myErr.GetMsg(), args...)
	} else {
		msg = myErr.GetMsg()
	}
	if resMul, _ := GetResMultiTextBy(db, msgId, lang); resMul != nil {
		if len(args) > 0 {
			msg = fmt.Sprintf(resMul.Content, args...)
		} else {
			msg = resMul.Content
		}
	}
	return myerr.NewBusinessCodeNoCheck(myErr.GetCode(), msg, myerr.BusinessData{})
}