company.go 1.2 KB
package models

import (
	"fmt"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
	"gorm.io/gorm"
	"time"
)

type Company struct {
	Id   int64  // 唯一标识
	Name string // 名称
	Code string `gorm:"uniqueIndex:idx_company_code"` // 编码(搜索使用,4位字母数字)
	Logo string // 公司LOGO

	CreatedAt int64
	UpdatedAt int64
	DeletedAt int64
	Version   int
}

func (m *Company) TableName() string {
	return "company"
}

func (m *Company) BeforeCreate(tx *gorm.DB) (err error) {
	m.CreatedAt = time.Now().Unix()
	m.UpdatedAt = time.Now().Unix()
	return
}

func (m *Company) BeforeUpdate(tx *gorm.DB) (err error) {
	m.UpdatedAt = time.Now().Unix()
	return
}

func (m *Company) CacheKeyFunc() string {
	if m.Id == 0 {
		return ""
	}
	return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}

func (m *Company) CacheKeyFuncByObject(obj interface{}) string {
	if v, ok := obj.(*Company); ok {
		return v.CacheKeyFunc()
	}
	return ""
}

func (m *Company) CachePrimaryKeyFunc() string {
	if len("") == 0 {
		return ""
	}
	return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
}