msg.go 2.47 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 37 38 39 40 41 42 43
	}
	err = msg_m.SendEmasMsgAssistant(model, user.ExternalId, user.DeviceType)
	if err != nil {
		model.Log.Errorf("SendLittleAssistantMsg SendEmasMsgAssistant userId:%v, err:%v", userId, err)
		return err
	}
	return nil
}
chenweijian's avatar
chenweijian committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

// 错误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
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

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
}