52 lines
903 B
Go
52 lines
903 B
Go
package community
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
"yyjishu.com/rubbish-class/rubbish"
|
|
)
|
|
|
|
//swagger:response
|
|
type Community struct {
|
|
gorm.Model
|
|
Name string `gorm:"type:varchar(20);"`
|
|
}
|
|
|
|
var once sync.Once
|
|
|
|
type CommunityService struct {
|
|
}
|
|
|
|
func NewService() *CommunityService {
|
|
c := &CommunityService{}
|
|
once.Do(c.Init)
|
|
return c
|
|
}
|
|
|
|
//Map 社区KV获取
|
|
func (c *CommunityService) Map() (*map[string]uint, error) {
|
|
var cs []Community
|
|
if err := rubbish.DB.Find(&cs).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
data := make(map[string]uint)
|
|
for _, c := range cs {
|
|
data[c.Name] = c.ID
|
|
}
|
|
return &data, nil
|
|
}
|
|
|
|
//Create 创建社区信息
|
|
func (c *CommunityService) Create(model *Community) error {
|
|
if err := rubbish.DB.Create(model).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//Init 社区表初始化
|
|
func (c *CommunityService) Init() {
|
|
rubbish.DB.AutoMigrate(&Community{})
|
|
}
|