user_company.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
144
145
146
147
148
149
150
151
package models
import (
"fmt"
"time"
"github.com/astaxie/beego/orm"
)
type UserCompany struct {
Id int64 `orm:"column(id)" description:"唯一标识"`
CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"`
UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"`
NickName string `orm:"column(nick_name);size(100)" description:"昵称"`
ChanceTotal int `orm:"column(chance_total)" description:"发表机会数"`
CommentTotal int `orm:"column(comment_total)" description:"发表评论总数"`
CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
Enable int8 `orm:"column(enable)" description:"有效状态"`
}
func (t *UserCompany) TableName() string {
return "user_company"
}
func init() {
orm.RegisterModel(new(UserCompany))
}
// AddUserCompany insert a new UserCompany into database and returns
// last inserted Id on success.
func AddUserCompany(m *UserCompany) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetUserCompanyById retrieves UserCompany by Id. Returns error if
// Id doesn't exist
func GetUserCompanyById(id int64) (v *UserCompany, err error) {
o := orm.NewOrm()
v = &UserCompany{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// UpdateUserCompany updates UserCompany by Id and returns error if
// the record to be updated doesn't exist
func UpdateUserCompanyById(m *UserCompany) (err error) {
o := orm.NewOrm()
v := UserCompany{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
}
// DeleteUserCompany deletes UserCompany by Id and returns error if
// the record to be deleted doesn't exist
func DeleteUserCompany(id int64) (err error) {
o := orm.NewOrm()
v := UserCompany{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&UserCompany{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
// GetUserAuthById retrieves UserAuth by Id. Returns error if
// Id doesn't exist
func GetUserCompanyByUserId(uid int64, companyId int64) (v *UserCompany, err error) {
o := orm.NewOrm()
sql := "select * from user_company where user_id=? and company_id=? and enable=1" //
if err = o.Raw(sql, uid, companyId).QueryRow(&v); err == nil {
return v, nil
}
return nil, err
}
//按用户公司编号 公司编号获取用户信息
//获取公司信息
func GetUserCompanyBy(id int64, companyId int64) (v *UserCompany, err error) {
o := orm.NewOrm()
sql := "select * from user_company where id=? and company_id=? " //and enable=1
if err = o.Raw(sql, id, companyId).QueryRow(&v); err == nil {
return v, nil
}
return nil, err
}
//按用户编号
//获取用户第一个有效公司
func GetUserCompanysFirst(uid int64) (v *UserCompany, err error) {
o := orm.NewOrm()
sql := `select a.* from user_company a inner join company b on a.company_id=b.id where a.user_id=? and a.enable=1 and b.enable=1
order by a.create_at desc limit 1` //
if err = o.Raw(sql, uid).QueryRow(&v); err == nil {
return v, nil
}
return nil, err
}
func GetUserCompanyIdAll(companyId int) (v []int64, err error) {
o := orm.NewOrm()
sql := "select id from user_company where company_id=? and enable=1" //and enable=1
if _, err = o.Raw(sql, companyId).QueryRows(&v); err == nil {
return v, nil
}
return nil, err
}
//获取用户所有的公司列表
//@uid 表user.id
func GetUserAllCompany(uid int64) (v []*UserCompany, err error) {
o := orm.NewOrm()
sql := "select a.* from user_company a inner join company b on a.company_id=b.id where a.user_id=? and a.enable=1 and b.enable=1" //and enable=1
if _, err = o.Raw(sql, uid).QueryRows(&v); err == nil {
return v, nil
}
return nil, err
}
//获取用户Id列表
//@key CompanyId NickName
func GetUserCompanyIdAllBy(options map[string]interface{}) (v []int64, err error) {
o := orm.NewOrm()
sql := "select id from user_company where 1=1 "
if _, ok := options["CompanyId"]; ok {
sql += fmt.Sprintf(" and company_id=%v ", options["CompanyId"])
}
//if _, ok := options["NickName"]; ok {
// sql += fmt.Sprintf(" and nick_name like '%%%v%%' ", options["NickName"])
//}
if _, ok := options["NickName"]; ok {
sql += fmt.Sprintf(" and user_id in (select id from user where nick_name like '%%%v%%') ", options["NickName"])
}
if _, err = o.Raw(sql).QueryRows(&v); err == nil {
return v, nil
}
return nil, err
}