chance_favorite.go
4.7 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package models
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
"time"
"github.com/astaxie/beego/orm"
)
type ChanceFavorite struct {
Id int64 `orm:"column(id);pk" description:"点赞编号"`
UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"`
CompanyId int64 `orm:"column(company_id)" description:"company.id 公司编号"`
MarkFlag int `orm:"column(mark_flag)" description:"类型 1:点赞 2:收藏"`
SourceType int `orm:"column(source_type)" description:"来源类型 1:机会 2:评论"`
SourceId int64 `orm:"column(source_id)" description:"来源id 机会编号/评论编号"`
ChanceType int `orm:"column(chance_type)" description:"机会类型编号 - 附加 "`
EnableStatus int `orm:"column(enable_status)" description:"1:有效 0:无效"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"删除时间"`
ChanceId int64 `orm:"column(chance_id)" description:"机会编号"`
}
func (t *ChanceFavorite) TableName() string {
return "chance_favorite"
}
func init() {
orm.RegisterModel(new(ChanceFavorite))
}
// AddChanceFavorite insert a new ChanceFavorite into database and returns
// last inserted Id on success.
func AddChanceFavorite(m *ChanceFavorite) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetChanceFavoriteById retrieves ChanceFavorite by Id. Returns error if
// Id doesn't exist
func GetChanceFavoriteById(id int64) (v *ChanceFavorite, err error) {
o := orm.NewOrm()
v = &ChanceFavorite{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// UpdateChanceFavorite updates ChanceFavorite by Id and returns error if
// the record to be updated doesn't exist
func UpdateChanceFavoriteById(m *ChanceFavorite) (err error) {
o := orm.NewOrm()
v := ChanceFavorite{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num)
}
}
return
}
// DeleteChanceFavorite deletes ChanceFavorite by Id and returns error if
// the record to be deleted doesn't exist
func DeleteChanceFavorite(id int64) (err error) {
o := orm.NewOrm()
v := ChanceFavorite{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&ChanceFavorite{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
//按1.用户id 2.公司id 3.标记类型 4.机会类型编号 5.最后编号 6.页数
//获取用户点赞收藏机会
func GetChanceFavorites(userId, companyId int64, markFlag, sourceType int, sourceId int64, lastId int64, pageSize int) (v []*ChanceFavorite, total int, err error) {
sql := mybeego.NewSqlExutor().Table("chance_favorite").Order("create_at desc")
sql.Where(fmt.Sprintf("user_id=%d", userId))
sql.Where(fmt.Sprintf("company_id=%d", companyId))
if sourceType > 0 {
sql.Where(fmt.Sprintf("source_type=%d", sourceType))
}
if sourceId > 0 {
sql.Where(fmt.Sprintf("source_id=%d", sourceId))
}
if markFlag > 0 {
sql.Where(fmt.Sprintf("mark_flag&%d>0", markFlag))
}
if pageSize > 0 {
sql.Limit(0, pageSize)
}
if lastId > 0 {
sql.Where(fmt.Sprintf("id<%d", lastId))
}
if total, err = sql.Querys(&v); err == nil {
return
}
log.Error(sql.Strings())
return
}
//是否已经点赞/收藏 机会
func ExitsChanceFavorite(userId, companyId int64, sourceId int64, markFlag int) (exits bool, err error) {
sql := mybeego.NewSqlExutor().Table("chance_favorite")
sql.Where(fmt.Sprintf("source_id=%d", sourceId)).
Where(fmt.Sprintf("user_id=%d", userId)).
Where(fmt.Sprintf("company_id=%d", companyId)).
Where(fmt.Sprintf("enable_status=1"))
if markFlag > 0 {
sql.Where(fmt.Sprintf("(mark_flag&%d)>0", markFlag))
}
return sql.QueryExists()
}
//更新机会点赞/收藏状态
func UpdateChanceFavorite(userId, companyId int64, sourceId int64, markFlag int) (err error) {
o := orm.NewOrm()
sql := `update chance_favorite set mark_flag = mark_flag ^ ?
where user_id =? and company_id =? and source_id=? ` //
if _, err = o.Raw(sql, markFlag, userId, companyId, sourceId).Exec(); err == nil {
return
}
return
}
func GetChanceFavorite(userId, companyId int64, sourceId int64, sourceType int) (v *ChanceFavorite, err error) {
o := orm.NewOrm()
sql := `select * from chance_favorite where user_id =? and company_id =? and source_id=? and source_type=?`
if err = o.Raw(sql, userId, companyId, sourceId, sourceType).QueryRow(&v); err != nil {
return
}
return
}