entity.go 2.75 KB
Newer Older
1 2 3 4 5 6 7 8 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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
package mysql

import "time"

type EntityI interface {
	GetID() ID
	//用于判断数据是否进行持久化
	IsLazyLoad() bool
	//默认值为false true:代表要移除数据
	CheckDel() bool
	//检查是否唯一键冲突,依旧更新
	CheckOnDuplicateKeyUPDATE() bool
	//检查是否唯一键冲突,则不插入
	CheckOnDuplicateKeyIGNORE() bool
	//更新乐观锁 默认值为false true:乐观锁更新
	CheckUpdateVersion() bool
	//更新条件.
	CheckUpdateCondition() bool
	//获取版本号
	GetUpdateVersionBefore() uint
	//更新情况
	GetUpdateCondition() string
	//save 动作排除字段
	GetOmit() []string
}
type Entity struct {
	ID                   ID                     `gorm:"primary_key"`
	CreatedTime          time.Time              `gorm:"->"`
	UpdatedTime          time.Time              `gorm:"->"`
	lazyLoad             bool                   `gorm:"-"`
	del                  bool                   `gorm:"-"`
	onDuplicateKeyUPDATE bool                   `gorm:"-"`
	onDuplicateKeyIGNORE bool                   `gorm:"-"`
	updateVersionFlag    bool                   `gorm:"-"`
	updateVersionBefore  uint                   `gorm:"-"`
	updateCondition      string                 `gorm:"-"`
	omit                 []string               `gorm:"-"` //更新排除
	updateColumns        map[string]interface{} `gorm:"-"` //更新字段
}

func (t *Entity) GetID() ID {
	return t.ID
}

func (t *Entity) IsLazyLoad() bool {
	return t.lazyLoad
}

func (t *Entity) SetLasyLoad() {
	t.lazyLoad = true
}

func (t *Entity) SetDel() {
	t.del = true
}

func (t *Entity) CheckDel() bool {
	return t.del
}

func (t *Entity) SetOnDuplicateKeyUPDATE() {
	t.onDuplicateKeyUPDATE = true
}

func (t *Entity) SetOnDuplicateKeyIGNORE() {
	t.onDuplicateKeyIGNORE = true
}

func (t *Entity) CheckOnDuplicateKeyUPDATE() bool {
	return t.onDuplicateKeyUPDATE
}

func (t *Entity) CheckOnDuplicateKeyIGNORE() bool {
	return t.onDuplicateKeyIGNORE
}

func (t *Entity) SetCheckUpdateVersionBefore(versionBefore uint) {
	t.updateVersionBefore = versionBefore
	t.updateVersionFlag = true
}

func (t *Entity) SetCheckUpdateCondition(condition string) {
	t.updateCondition = condition
}

func (t *Entity) CheckUpdateVersion() bool {
	return t.updateVersionFlag
}

func (t *Entity) CheckUpdateCondition() bool {
	return t.updateCondition != ""
}

func (t *Entity) GetUpdateCondition() string {
	return t.updateCondition
}

func (t *Entity) GetUpdateVersionBefore() uint {
	return t.updateVersionBefore
}

func (t *Entity) GetOmit() []string {
	return t.omit
}

func (t *Entity) SetOmit(omit []string) {
	t.omit = omit
}

func (t *Entity) SetUpdateColumns(updateColumns map[string]interface{}) {
	t.updateColumns = updateColumns
}

func (t *Entity) GetUpdateColumns() map[string]interface{} {
	return t.updateColumns
}