msg.go 3.25 KB
Newer Older
chenweijian's avatar
chenweijian committed
1 2 3
package msg

import (
chenweijian's avatar
chenweijian committed
4 5
	"fmt"
	"git.hilo.cn/hilo-common/_const/common"
chenweijian's avatar
chenweijian committed
6 7 8
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/internal/enum/msg_e"
	"git.hilo.cn/hilo-common/internal/model/msg_m"
chenweijian's avatar
chenweijian committed
9
	"git.hilo.cn/hilo-common/internal/model/res_m"
chenweijian's avatar
chenweijian committed
10
	"git.hilo.cn/hilo-common/internal/model/user_m"
chenweijian's avatar
chenweijian committed
11
	"git.hilo.cn/hilo-common/myerr"
chenweijian's avatar
chenweijian committed
12
	"git.hilo.cn/hilo-common/resource/mysql"
chenweijian's avatar
chenweijian committed
13
	"git.hilo.cn/hilo-common/utils"
chenweijian's avatar
chenweijian committed
14
	"gorm.io/gorm"
chenweijian's avatar
chenweijian committed
15 16 17 18 19
)

// 发送小助手消息
func SendLittleAssistantMsg(model *domain.Model, userId mysql.ID, t msg_e.MsgUserRecordType, diamondIncome mysql.Str,
	dayNum mysql.Str, propertyUrl mysql.Str, beanNum mysql.Str, groupCode mysql.Str, users ...*user_m.User) (err error) {
chenweijian's avatar
chenweijian committed
20
	defer utils.CheckGoPanic()
chenweijian's avatar
chenweijian committed
21 22 23 24 25 26 27 28 29 30
	var user *user_m.User
	if len(users) > 0 {
		user = users[0]
	} else {
		user, err = user_m.GetUser(model, userId)
		if err != nil {
			model.Log.Errorf("SendLittleAssistantMsg GetUser userId:%v, err:%v", userId, err)
			return err
		}
	}
chenweijian's avatar
chenweijian committed
31 32 33 34 35
	if t > 0 {
		if err = msg_m.NewUserRecord(model, user.ID, t, user.Nick, user.ID, diamondIncome, dayNum, propertyUrl, beanNum, groupCode).Persistent(); err != nil {
			model.Log.Errorf("SendLittleAssistantMsg NewUserRecord err:%v", err)
			return err
		}
chenweijian's avatar
chenweijian committed
36
	}
hujiebin's avatar
hujiebin committed
37 38 39 40 41 42 43 44
	// 异步emas提醒
	go func() {
		defer utils.CheckGoPanic()
		err = msg_m.SendEmasMsgAssistant(domain.CreateModelContext(model.MyContext), user.ExternalId, user.DeviceType)
		if err != nil {
			model.Log.Errorf("SendLittleAssistantMsg SendEmasMsgAssistant userId:%v, err:%v", userId, err)
		}
	}()
chenweijian's avatar
chenweijian committed
45 46
	return nil
}
chenweijian's avatar
chenweijian committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

// 错误toast翻译
func GetErrByLanguage(model *domain.Model, msgId common.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, _ := res_m.GetResMultiTextBy(model, 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{})
}
chenweijian's avatar
chenweijian committed
65

chenweijian's avatar
chenweijian committed
66 67 68 69 70 71 72 73 74 75 76
// 生成弹窗错误
func NewAlertErrByLanguage(model *domain.Model, msgId1, msgId2 common.MsgIdType, lang string, args1 []interface{}, args2 []interface{}) *myerr.BusinessError {
	var title, content string
	if resMul, _ := res_m.GetResMultiTextBy(model, msgId1, lang); resMul != nil {
		if len(args1) > 0 {
			title = fmt.Sprintf(resMul.Content, args1...)
		} else {
			title = resMul.Content
		}
	}
	if resMul, _ := res_m.GetResMultiTextBy(model, msgId2, lang); resMul != nil {
chenweijian's avatar
chenweijian committed
77
		if len(args2) > 0 {
chenweijian's avatar
chenweijian committed
78 79 80 81 82 83 84 85
			content = fmt.Sprintf(resMul.Content, args2...)
		} else {
			content = resMul.Content
		}
	}
	return myerr.NewBusinessCodeNoCheck(1020, "", myerr.BusinessData{Title: title, Detail: content})
}

chenweijian's avatar
chenweijian committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
func GetResMultiTextBy(model *domain.Model, msgId uint, Language mysql.Str) (string, error) {
	r := res_m.ResMultiText{}
	if err := model.DB().Where(&res_m.ResMultiText{
		MsgId:    msgId,
		Language: Language,
	}).First(&r).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			if err := model.DB().Where(&res_m.ResMultiText{
				MsgId:    msgId,
				Language: common.DEFAULT_LANG,
			}).First(&r).Error; err != nil {
				return "", myerr.WrapErr(err)
			}
		} else {
			return "", myerr.WrapErr(err)
		}

	}
	return r.Content, nil
}