model.go 3.07 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3
package domain

import (
hujiebin's avatar
hujiebin committed
4
	"fmt"
hujiebin's avatar
hujiebin committed
5 6 7 8
	"git.hilo.cn/hilo-common/mycontext"
	"git.hilo.cn/hilo-common/resource/mysql"
	"git.hilo.cn/hilo-common/resource/redisCli"
	"gorm.io/gorm"
hujiebin's avatar
hujiebin committed
9 10
	"gorm.io/gorm/clause"
	"time"
hujiebin's avatar
hujiebin committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
)

type Model struct {
	*CtxAndDb `gorm:"-"`
}

func CreateModel(ctxAndDb *CtxAndDb) *Model {
	return &Model{CtxAndDb: ctxAndDb}
}

func CreateModelContext(myContext *mycontext.MyContext) *Model {
	return &Model{
		CtxAndDb: &CtxAndDb{
			Db:        mysql.Db,
			MyContext: myContext,
			Redis:     redisCli.GetRedis(),
		},
	}
}

func CreateModelNil() *Model {
	return &Model{
		CtxAndDb: &CtxAndDb{
			Db:        mysql.Db,
			MyContext: mycontext.CreateMyContext(nil),
			Redis:     redisCli.GetRedis(),
		},
	}
}

func (m *Model) DB() *gorm.DB {
	return m.Db.WithContext(m)
}

hujiebin's avatar
hujiebin committed
45 46 47 48 49 50 51 52 53 54
//获取traceid
func (m *Model) GetTraceId() string {
	if traceIdTemp, ok := m.Cxt[mycontext.TRACEID]; ok {
		if traceId, ok := traceIdTemp.(string); ok {
			return traceId
		}
	}
	return ""
}

hujiebin's avatar
hujiebin committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68
// 包装事务
// 注意:需要使用新的model
func (m *Model) Transaction(f func(*Model) error) error {
	// 公用context
	// 新的db
	txModel := CreateModelContext(m.MyContext)
	txModel.Db = m.Db.Begin().WithContext(m)
	err := f(txModel)
	if err != nil {
		txModel.Db.Rollback()
		return err
	}
	return txModel.Db.Commit().Error
}
hujiebin's avatar
hujiebin committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

func Persistent(db *gorm.DB, t mysql.EntityI) error {
	if t == nil {
		return nil
	}
	if t.IsLazyLoad() {
		return nil
	}
	//删除
	if t.CheckDel() {
		tx := db.Delete(t)
		if err := tx.Error; err != nil {
			return err
		}
		if tx.RowsAffected == 0 {
			return fmt.Errorf("gorm delete.RowsAffected = 0")
		}
		//增加缓存行为记录(删除)

	} else if t.GetID() == 0 {
		//新增
		if t.CheckOnDuplicateKeyUPDATE() {
			if err := db.Set("gorm:insert_option", fmt.Sprintf("ON DUPLICATE KEY UPDATE `created_time` = '%s'", time.Now())).Create(t).Error; err != nil {
				return err
			}
		} else if t.CheckOnDuplicateKeyIGNORE() {
			if err := db.Clauses(clause.Insert{Modifier: "IGNORE"}).Create(t).Error; err != nil {
				return err
			}
		} else {
			if err := db.Create(t).Error; err != nil {
				return err
			}
		}
		//增加缓存行为记录(新增)
	} else {
		//fixme: 更新条件,目前是互斥的,应该改成且。
		//更新
		if t.CheckUpdateVersion() {
			//版本号。乐观锁更新,注意,空值不更新
			tx := db.Model(t).Where("version = ? ", t.GetUpdateVersionBefore()).Updates(t)
			if err := tx.Error; err != nil {
				return err
			}
			if tx.RowsAffected == 0 {
				return fmt.Errorf("gorm version update.RowsAffected = 0")
			}
		} else if t.CheckUpdateCondition() {
			//条件更新
			tx := db.Model(t).Where(t.GetUpdateCondition()).Updates(t)
			if err := tx.Error; err != nil {
				return err
			}
			if tx.RowsAffected == 0 {
				return fmt.Errorf("gorm condition update.RowsAffected = 0")
			}
		} else if len(t.GetOmit()) > 0 {
			if err := db.Model(t).Omit(t.GetOmit()...).Save(t).Error; err != nil {
				return err
			}
		} else {
			if err := db.Model(t).Save(t).Error; err != nil {
				return err
			}
		}
		//增加缓存行为记录(更新)
	}
	return nil
}