/
chat.go
63 lines (51 loc) · 1.71 KB
/
chat.go
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
package db
import (
"errors"
"strings"
"gorm.io/gorm"
)
type ChatType uint
const Q ChatType = 1
const A ChatType = 2
type Chat struct {
gorm.Model
Username string `gorm:"type:varchar(50);not null;comment:'用户名'" json:"username"` // 用户名
Source string `gorm:"type:varchar(50);comment:'用户来源:群聊名字,私聊'" json:"source"` // 对话来源
ChatType ChatType `gorm:"type:tinyint(1);default:1;comment:'类型:1问, 2答'" json:"chat_type"` // 状态
ParentContent uint `gorm:"default:0;comment:'父消息编号(编号为0时表示为首条)'" json:"parent_content"`
Content string `gorm:"type:varchar(128);comment:'内容'" json:"content"` // 问题或回答的内容
}
type ChatListReq struct {
Username string `json:"username" form:"username"`
Source string `json:"source" form:"source"`
}
// Add 添加资源
func (c Chat) Add() (uint, error) {
err := DB.Create(&c).Error
return c.ID, err
}
// Find 获取单个资源
func (c Chat) Find(filter map[string]interface{}, data *Chat) error {
return DB.Where(filter).First(&data).Error
}
// List 获取数据列表
func (c Chat) List(req ChatListReq) ([]*Chat, error) {
var list []*Chat
db := DB.Model(&Chat{}).Order("created_at ASC")
userName := strings.TrimSpace(req.Username)
if userName != "" {
db = db.Where("username = ?", userName)
}
source := strings.TrimSpace(req.Source)
if source != "" {
db = db.Where("source = ?", source)
}
err := db.Find(&list).Error
return list, err
}
// Exist 判断资源是否存在
func (c Chat) Exist(filter map[string]interface{}) bool {
var dataObj Chat
err := DB.Where(filter).First(&dataObj).Error
return !errors.Is(err, gorm.ErrRecordNotFound)
}