company.go 1.3 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 `json:"name,omitempty"` // 名称
	Code string `json:"code,omitempty"` // 编码(搜索使用,4位字母数字)
	Logo string `json:"logo,omitempty"` // 公司LOGO

	CreatedAt int64 `json:"createdAt,omitempty"`
	UpdatedAt int64 `json:"updatedAt,omitempty"`
	DeletedAt int64 `json:"deletedAt,omitempty"`
	Version   int   `json:"version,omitempty"`
}

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")
}