正在显示
1 个修改的文件
包含
118 行增加
和
0 行删除
models/user_company.go
0 → 100644
1 | +package models | ||
2 | + | ||
3 | +import ( | ||
4 | + "errors" | ||
5 | + "fmt" | ||
6 | + "oppmg/common/log" | ||
7 | + "time" | ||
8 | + | ||
9 | + "github.com/astaxie/beego/orm" | ||
10 | +) | ||
11 | + | ||
12 | +type UserCompany struct { | ||
13 | + Id int64 `orm:"column(id);auto" description:"唯一标识"` | ||
14 | + CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"` | ||
15 | + UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"` | ||
16 | + ChanceTotal int `orm:"column(chance_total)" description:"发表机会数"` | ||
17 | + CommentTotal int `orm:"column(comment_total)" description:"发表评论总数"` | ||
18 | + CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"` | ||
19 | + UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"` | ||
20 | + Enable int8 `orm:"column(enable)"` | ||
21 | + DeleteAt time.Time `orm:"column(delete_at)"` | ||
22 | +} | ||
23 | + | ||
24 | +func (t *UserCompany) TableName() string { | ||
25 | + return "user_company" | ||
26 | +} | ||
27 | + | ||
28 | +//用户的公司是否有效 | ||
29 | +const ( | ||
30 | + USERCOMPANY_ENABLE_YES int8 = 1 //有效 | ||
31 | + USERCOMPANY_ENABLE_NO int8 = 2 // 无效 | ||
32 | +) | ||
33 | + | ||
34 | +func (t *UserCompany) IsEnable() bool { | ||
35 | + switch t.Enable { | ||
36 | + case USERCOMPANY_ENABLE_YES: | ||
37 | + return true | ||
38 | + case USERCOMPANY_ENABLE_NO: | ||
39 | + return false | ||
40 | + } | ||
41 | + return false | ||
42 | +} | ||
43 | + | ||
44 | +func (t *UserCompany) IsDelete() bool { | ||
45 | + log.Debug("deleteTime:%d", t.DeleteAt.Unix()) | ||
46 | + if t.DeleteAt.Unix() > 0 { | ||
47 | + return true | ||
48 | + } | ||
49 | + return false | ||
50 | +} | ||
51 | + | ||
52 | +func init() { | ||
53 | + orm.RegisterModel(new(UserCompany)) | ||
54 | +} | ||
55 | + | ||
56 | +// AddUserCompany insert a new UserCompany into database and returns | ||
57 | +// last inserted Id on success. | ||
58 | +func AddUserCompany(m *UserCompany, o orm.Ormer) (id int64, err error) { | ||
59 | + m.CreateAt = time.Now() | ||
60 | + m.DeleteAt = time.Unix(0, 0) | ||
61 | + m.UpdateAt = time.Now() | ||
62 | + m.Enable = USERCOMPANY_ENABLE_YES | ||
63 | + id, err = o.Insert(m) | ||
64 | + return | ||
65 | +} | ||
66 | + | ||
67 | +// UpdateUserCompany updates UserCompany by Id and returns error if | ||
68 | +// the record to be updated doesn't exist | ||
69 | +func UpdateUserCompanyById(m *UserCompany) (err error) { | ||
70 | + o := orm.NewOrm() | ||
71 | + v := UserCompany{Id: m.Id} | ||
72 | + // ascertain id exists in the database | ||
73 | + if err = o.Read(&v); err == nil { | ||
74 | + var num int64 | ||
75 | + if num, err = o.Update(m); err == nil { | ||
76 | + fmt.Println("Number of records updated in database:", num) | ||
77 | + } | ||
78 | + } | ||
79 | + return | ||
80 | +} | ||
81 | + | ||
82 | +func GetUserCompanyBy(userid int64, companyId int64) (*UserCompany, error) { | ||
83 | + o := orm.NewOrm() | ||
84 | + var data []*UserCompany | ||
85 | + _, err := o.QueryTable(&UserCompany{}). | ||
86 | + Filter("user_id", userid). | ||
87 | + Filter("company_id", companyId). | ||
88 | + Filter("delete_at", 0). | ||
89 | + All(&data) | ||
90 | + if err != nil { | ||
91 | + return nil, err | ||
92 | + } | ||
93 | + if len(data) == 0 { | ||
94 | + return nil, errors.New("UserCompany not found") | ||
95 | + } | ||
96 | + return data[0], nil | ||
97 | +} | ||
98 | + | ||
99 | +func ExistUserCompany(userid int64, companyId int64) bool { | ||
100 | + o := orm.NewOrm() | ||
101 | + ok := o.QueryTable(&UserCompany{}). | ||
102 | + Filter("user_id", userid). | ||
103 | + Filter("company_id", companyId). | ||
104 | + Filter("delete_at", 0). | ||
105 | + Exist() | ||
106 | + return ok | ||
107 | +} | ||
108 | + | ||
109 | +func EnableUserCompany(userid int64, companyid int64) error { | ||
110 | + o := orm.NewOrm() | ||
111 | + _, err := o.QueryTable(&UserCompany{}). | ||
112 | + Filter("user_id", userid). | ||
113 | + Filter("company_id", companyid).Update(orm.Params{ | ||
114 | + "enable": USERCOMPANY_ENABLE_YES, | ||
115 | + "delete_at": 0, | ||
116 | + }) | ||
117 | + return err | ||
118 | +} |
-
请 注册 或 登录 后发表评论