-
Notifications
You must be signed in to change notification settings - Fork 48
/
model.go
2141 lines (1804 loc) · 61.2 KB
/
model.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package model
import (
"errors"
"fmt"
//_ "github.com/lib/pq"
"github.com/lunny/xorm"
_ "github.com/mattn/go-sqlite3"
"os"
"sdc/helper"
"strconv"
"strings"
"time"
)
const (
//dbtype = "pgsql"
dbtype = "sqlite"
)
var (
Engine *xorm.Engine
)
type User struct {
Id int64
Pid int64 //用在归属地 归属学校 归属组织 等方面
Email string `xorm:"index"`
Password string `xorm:"index"`
Username string `xorm:"index"`
Nickname string `xorm:"index"`
Realname string `xorm:"index"`
Content string `xorm:"text index"`
Avatar string
AvatarLarge string
AvatarMedium string
AvatarSmall string
Birth time.Time
Province string
City string
Company string
Address string
Postcode string
Mobile string
Website string
Sex int64
Qq string
Msn string
Weibo string
Ctype int64 `xorm:"index"`
Role int64 `xorm:"index"`
Created time.Time `xorm:"created index"`
Updated time.Time `xorm:"updated index"`
Hotness float64 `xorm:"index"`
Hotup int64 `xorm:"index"`
Hotdown int64 `xorm:"index"`
Hotscore int64 `xorm:"index"` //Hotup - Hotdown
Hotvote int64 `xorm:"index"` //Hotup + Hotdown
Views int64 `xorm:"index"`
LastLoginTime time.Time
LastLoginIp string
LoginCount int64 `xorm:"index"`
}
//category,Pid:root
type Category struct {
Id int64
Pid int64 `xorm:"index"`
Uid int64 `xorm:"index"`
Order int64
Ctype int64 `xorm:"index"`
Title string `xorm:"index"`
Content string `xorm:"text index"`
Attachment string `xorm:"text"`
Created time.Time `xorm:"created index"`
Updated time.Time `xorm:"updated index"`
Hotness float64 `xorm:"index"`
Hotup int64 `xorm:"index"`
Hotdown int64 `xorm:"index"`
Hotscore int64 `xorm:"index"` //Hotup - Hotdown
Hotvote int64 `xorm:"index"` //Hotup + Hotdown
Views int64 `xorm:"index"`
Author string `xorm:"index"` //这里指本分类创建者
NodeTime time.Time
NodeCount int64 `xorm:"index"`
NodeLastUserId int64
}
//node,Pid:category
type Node struct {
Id int64
Pid int64 `xorm:"index"`
Uid int64 `xorm:"index"`
Order int64
Ctype int64 `xorm:"index"`
Title string `xorm:"index"`
Content string `xorm:"text index"`
Attachment string `xorm:"text"`
Created time.Time `xorm:"created index"`
Updated time.Time `xorm:"updated index"`
Hotness float64 `xorm:"index"`
Hotup int64 `xorm:"index"`
Hotdown int64 `xorm:"index"`
Hotscore int64 `xorm:"index"` //Hotup - Hotdown
Hotvote int64 `xorm:"index"` //Hotup + Hotdown
Views int64 `xorm:"index"`
Author string `xorm:"index"` //节点的创建者
TopicTime time.Time
TopicCount int64 `xorm:"index"`
TopicLastUserId int64
}
//由于cid nid uid都可以是topic的上级所以默认不设置pid字段,这里默认Pid是nid
type Topic struct {
Id int64
Cid int64 `xorm:"index"`
Nid int64 `xorm:"index"`
Uid int64 `xorm:"index"`
Order int64
Ctype int64 `xorm:"index"`
Title string `xorm:"index"`
Content string `xorm:"text index"`
Attachment string `xorm:"text index"`
Thumbnails string `xorm:"index"` //Original remote file
ThumbnailsLarge string `xorm:"index"` //200x300
ThumbnailsMedium string `xorm:"index"` //200x150
ThumbnailsSmall string `xorm:"index"` //70x70
Tags string `xorm:"index"`
Created time.Time `xorm:"created index"`
Updated time.Time `xorm:"index"`
Hotness float64 `xorm:"index"`
Hotup int64 `xorm:"index"`
Hotdown int64 `xorm:"index"`
Hotscore int64 `xorm:"index"` //Hotup - Hotdown
Hotvote int64 `xorm:"index"` //Hotup + Hotdown
Views int64 `xorm:"index"`
Author string `xorm:"index"`
Category string `xorm:"index"`
Node string `xorm:"index"`
ReplyTime time.Time
ReplyCount int64 `xorm:"index"`
ReplyLastUserId int64
ReplyLastUsername string
ReplyLastNickname string
}
//由于cid nid uid都可以是topic的上级所以默认不设置pid字段,这里默认Pid是nid
type Question struct {
Id int64
Cid int64 `xorm:"index"`
Nid int64 `xorm:"index"`
Uid int64 `xorm:"index"`
Order int64
Ctype int64 `xorm:"index"`
Title string `xorm:"index"`
Content string `xorm:"text index"`
Attachment string `xorm:"text index"`
Thumbnails string `xorm:"index"` //Original remote file
ThumbnailsLarge string `xorm:"index"` //200x300
ThumbnailsMedium string `xorm:"index"` //200x150
ThumbnailsSmall string `xorm:"index"` //70x70
Tags string `xorm:"index"`
Created time.Time `xorm:"created index"`
Updated time.Time `xorm:"index"`
Hotness float64 `xorm:"index"`
Hotup int64 `xorm:"index"`
Hotdown int64 `xorm:"index"`
Hotscore int64 `xorm:"index"` //Hotup - Hotdown
Hotvote int64 `xorm:"index"` //Hotup + Hotdown
Views int64 `xorm:"index"`
Author string `xorm:"index"`
Category string `xorm:"index"`
Node string `xorm:"index"`
ReplyTime time.Time
ReplyCount int64 `xorm:"index"`
ReplyHotscore int64 `xorm:"index"` //Hotup - Hotdown
ReplyLastUserId int64
ReplyLastUsername string
ReplyLastNickname string
FavoriteCount int64 `xorm:"index"`
}
//reply,Pid:topic
type Reply struct {
Id int64
Uid int64 `xorm:"index"`
Pid int64 `xorm:"index"` //Topic id
Order int64
Ctype int64 `xorm:"index"`
Content string `xorm:"text index"`
Attachment string `xorm:"text"`
Created time.Time `xorm:"created index"`
Updated time.Time `xorm:"index"`
Hotness float64 `xorm:"index"`
Hotup int64 `xorm:"index"`
Hotdown int64 `xorm:"index"`
Hotscore int64 `xorm:"index"` //Hotup - Hotdown
Hotvote int64 `xorm:"index"` //Hotup + Hotdown
Views int64 `xorm:"index"`
Author string `xorm:"index"`
AuthorSignature string `xorm:"index"`
Email string `xorm:"index"`
Website string `xorm:"index"`
ReplyTime time.Time
ReplyCount int64 `xorm:"index"`
ReplyLastUserId int64
ReplyLastUsername string
ReplyLastNickname string
}
type File struct {
Id int64
Uid int64 `xorm:"index"`
Cid int64 `xorm:"index"`
Nid int64 `xorm:"index"`
Pid int64 `xorm:"index"`
Order int64
Ctype int64 `xorm:"index"`
Filename string
Content string `xorm:"text index"`
Hash string
Location string `xorm:"index"`
Url string `xorm:"index"`
Size int64
Created time.Time `xorm:"created index"`
Updated time.Time `xorm:"updated index"`
Hotness float64 `xorm:"index"`
Hotup int64 `xorm:"index"`
Hotdown int64 `xorm:"index"`
Hotscore int64 `xorm:"index"` //Hotup - Hotdown
Hotvote int64 `xorm:"index"` //Hotup + Hotdown
Views int64 `xorm:"index"`
ReplyTime time.Time
ReplyCount int64 `xorm:"index"`
ReplyLastUserId int64
}
type Image struct {
Id int64
Uid int64 `xorm:"index"`
Cid int64 `xorm:"index"`
Nid int64 `xorm:"index"`
Pid int64 `xorm:"index"`
Order int64
Ctype int64 `xorm:"index"`
Fingerprint string `xorm:"index"`
Filename string `xorm:"index"`
Content string `xorm:"text index"`
Hash string `xorm:"index"`
Location string `xorm:"index"`
Url string `xorm:"index"`
Size int64
Width int64
Height int64
Created time.Time `xorm:"created index"`
Updated time.Time `xorm:"updated index"`
Hotness float64 `xorm:"index"`
Hotup int64 `xorm:"index"`
Hotdown int64 `xorm:"index"`
Hotscore int64 `xorm:"index"` //Hotup - Hotdown
Hotvote int64 `xorm:"index"` //Hotup + Hotdown
Views int64 `xorm:"index"`
Author string `xorm:"index"`
ReplyTime time.Time
ReplyCount int64 `xorm:"index"`
ReplyLastUserId int64
}
type Timeline struct {
Id int64
Cid int64 `xorm:"index"`
Nid int64 `xorm:"index"`
Uid int64 `xorm:"index"`
Order int64
Ctype int64 `xorm:"index"`
Title string `xorm:"index"`
Content string `xorm:"text index"`
Attachment string `xorm:"text index"`
Created time.Time `xorm:"created index"`
Updated time.Time `xorm:"updated index"`
Hotness float64 `xorm:"index"`
Hotup int64 `xorm:"index"`
Hotdown int64 `xorm:"index"`
Hotscore int64 `xorm:"index"` //Hotup - Hotdown
Hotvote int64 `xorm:"index"` //Hotup + Hotdown
Views int64 `xorm:"index"`
Author string `xorm:"index"`
AuthorSignature string `xorm:"index"`
Category string `xorm:"index"`
Node string `xorm:"index"`
ReplyTime time.Time
ReplyCount int64 `xorm:"index"`
ReplyLastUserId int64
ReplyLastUsername string
ReplyLastNickname string
}
type Ads struct {
Id int64
Cid int64 `xorm:"index"`
Nid int64 `xorm:"index"`
Uid int64 `xorm:"index"`
Order int64
Ctype int64 `xorm:"index"`
Title string `xorm:"index"`
Content string `xorm:"text index"`
Attachment string `xorm:"text"`
Begintime time.Time
Endtime time.Time
Created time.Time `xorm:"created index"`
Updated time.Time `xorm:"updated index"`
Hotness float64 `xorm:"index"`
Hotup int64 `xorm:"index"`
Hotdown int64 `xorm:"index"`
Hotscore int64 `xorm:"index"` //Hotup - Hotdown
Hotvote int64 `xorm:"index"` //Hotup + Hotdown
Views int64 `xorm:"index"`
Author string `xorm:"index"`
Category string `xorm:"index"`
Node string `xorm:"index"`
ReplyTime time.Time
ReplyCount int64 `xorm:"index"`
ReplyLastUserId int64
ReplyLastUsername string
ReplyLastNickname string
}
type QuestionMark struct {
Id int64
Uid int64 `xorm:"index"`
Qid int64 `xorm:"index"`
}
type AnswerMark struct {
Id int64
Uid int64 `xorm:"index"`
Aid int64 `xorm:"index"`
}
func init() {
SetEngine()
CreatTables()
initData()
}
func ConDb() (*xorm.Engine, error) {
switch {
case dbtype == "sqlite":
return xorm.NewEngine("sqlite3", "./data/sqlite.db")
case dbtype == "mysql":
return xorm.NewEngine("mysql", "user=mysql password=^%*&^%#@* dbname=mysql")
case dbtype == "pgsql":
// "user=postgres password=^%*&^%#@* dbname=pgsql sslmode=disable maxcons=10 persist=true"
//return xorm.NewEngine("postgres", "host=110.76.39.205 user=postgres password=^%*&^%#@* dbname=pgsql sslmode=disable")
return xorm.NewEngine("postgres", "user=postgres password=^%*&^%#@* dbname=sdc sslmode=disable")
//return xorm.NewEngine("postgres", "host=127.0.0.1 port=6432 user=postgres password=^%*&^%#@* dbname=sdc sslmode=disable")
}
return nil, errors.New("尚未设定数据库连接")
}
func SetEngine() (*xorm.Engine, error) {
var err error
Engine, err = ConDb()
//Engine.Mapper = xorm.SameMapper{}
Engine.ShowDebug = true
Engine.ShowErr = true
Engine.ShowSQL = true
//Engine.SetMaxConns(10)
//Engine.Pool.SetMaxConns(5)
cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 10000)
Engine.SetDefaultCacher(cacher)
f, err := os.Create("./logs/sql.log")
if err != nil {
println(err.Error())
panic("OMG!")
}
Engine.Logger = f
return Engine, err
}
func CreatTables() error {
return Engine.Sync(&User{}, &Category{}, &Node{}, &Topic{}, &Reply{}, &Question{}, &QuestionMark{}, &AnswerMark{}, &File{}, &Image{}, &Timeline{}) //Engine.CreateTables(&User{}, &Category{}, &Node{}, &Topic{}, &Reply{}, &Kv{}, &File{}, &Image{}, &Timeline{})
}
func initData() {
//用户等级划分:正数是普通用户,负数是管理员各种等级划分,为0则尚未注册
if usr, err := GetUserByRole(-1000); usr == nil && err == nil {
if id, err := AddUser("root@sudochina.com", "root", "root", "root", helper.Encrypt_hash("rootpass", nil), -1000); err == nil {
fmt.Println("Default Email:root@sudochina.com ,Username:root ,Password:rootpass Userid:", id)
} else {
fmt.Print("create root got errors:", err)
}
}
fmt.Println("The sdc system has started!")
}
func GetUserByRole(role int64) (*User, error) {
user := new(User)
if has, err := Engine.Where("role=?", role).Get(user); has {
return user, err
} else {
return nil, err
}
}
func GetUsersOnHotness(offset int, limit int, path string) (*[]User, error) {
users := new([]User)
err := Engine.Limit(limit, offset).Desc(path).Desc("hotness").Find(users)
return users, err
}
func GetUserByUsername(username string) (*User, error) {
user := new(User)
if has, err := Engine.Where("username=?", username).Get(user); has {
return user, err
} else {
return nil, err
}
}
func GetUserByNickname(nickname string) (*User, error) {
user := &User{Nickname: nickname}
has, err := Engine.Get(user)
if has {
return user, err
} else {
return nil, err
}
}
//返回值尽量返回指针 不然会出现诡异的问题
func GetUserByEmail(email string) (*User, error) {
var user = &User{Email: email}
has, err := Engine.Get(user)
if has {
return user, err
} else {
return nil, err
}
}
func GetUser(id int64) (*User, error) {
user := new(User)
has, err := Engine.Id(id).Get(user)
if has {
return user, err
} else {
return nil, err
}
}
func SearchTopic(content string, offset int, limit int, path string) (*[]Topic, error) {
//排序首先是热值优先,然后是时间优先。
if content != "" {
keyword := "%" + content + "%"
tps := new([]Topic)
err := Engine.Where("title like ? or content like ?", keyword, keyword).Limit(limit, offset).Desc(path, "views", "reply_count", "created").Find(tps)
return tps, err
}
return nil, errors.New("搜索内容为空!")
}
func SearchQuestion(content string, offset int, limit int, path string) (*[]Question, error) {
//排序首先是热值优先,然后是时间优先。
if content != "" {
keyword := "%" + content + "%"
qst := new([]Question)
err := Engine.Where("title like ? or content like ? or tags like ?", keyword, keyword, keyword).Limit(limit, offset).Desc(path, "views", "reply_count", "created").Find(qst)
return qst, err
}
return nil, errors.New("搜索内容为空!")
}
func SearchNode(content string, offset int, limit int, path string) (*[]Node, error) {
//排序首先是热值优先,然后是时间优先。
if content != "" {
keyword := "%" + content + "%"
nds := new([]Node)
err := Engine.Where("title like ? or content like ?", keyword, keyword).Limit(limit, offset).Desc(path, "views", "topic_count", "created").Find(nds)
return nds, err
}
return nil, errors.New("搜索内容为空!")
}
func SearchCategory(content string, offset int, limit int, path string) (*[]Category, error) {
//排序首先是热值优先,然后是时间优先。
if content != "" {
keyword := "%" + content + "%"
cats := new([]Category)
err := Engine.Where("title like ? or content like ?", keyword, keyword).Limit(limit, offset).Desc(path, "views", "node_count", "created").Find(cats)
return cats, err
}
return nil, errors.New("搜索内容为空!")
}
func AddUser(email string, username string, nickname string, realname string, password string, role int64) (int64, error) {
usr := new(User)
usr = &User{Email: email, Password: password, Username: username, Nickname: nickname, Realname: realname, Role: role, Created: time.Now()}
if _, err := Engine.Insert(usr); err == nil {
return usr.Id, err
} else {
return -1, err
}
}
func AddNode(title string, content string, cid int64, uid int64) (int64, error) {
nd := new(Node)
nd = &Node{Pid: cid, Uid: uid, Title: title, Content: content, Created: time.Now()}
if _, err := Engine.Insert(nd); err == nil {
return nd.Id, err
} else {
return -1, err
}
}
func AddTopic(title string, content string, cid int64, nid int64, uid int64) (int64, error) {
cat, ea := GetCategory(cid)
if ea != nil {
return -1, ea
}
nd, eb := GetNode(nid)
if eb != nil {
return -1, eb
}
usr, ec := GetUser(uid)
if ec != nil {
return -1, ec
}
if ea == nil && eb == nil && ec == nil {
tp := new(Topic)
tp = &Topic{Cid: cid, Nid: nid, Uid: uid, Title: title, Content: content, Author: usr.Username,
Category: cat.Title, Node: nd.Title, Created: time.Now()}
if _, err := Engine.Insert(tp); err == nil {
return tp.Id, err
} else {
return -1, err
}
}
return -1, errors.New("AddTopic发生错误!")
}
func AddTimeline(title string, content string, cid int64, nid int64, uid int64, author string, author_signature string) (int64, error) {
tml := new(Timeline)
tml = &Timeline{Cid: cid, Nid: nid, Uid: uid, Title: title, Content: content, Author: author, AuthorSignature: author_signature, Created: time.Now()}
if _, err := Engine.Insert(tml); err == nil {
return tml.Id, err
} else {
return -1, err
}
}
func DelTimeline(lid int64) error {
if row, err := Engine.Id(lid).Delete(new(Timeline)); err != nil || row == 0 {
fmt.Println("row:::", row)
fmt.Println("删除时光记录错误:", err) //此时居然为空!
return errors.New("删除时光记录错误!") //错误还要我自己构造?!
} else {
return nil
}
}
func GetTimeline(lid int64) (*Timeline, error) {
tl := new(Timeline)
_, err := Engine.Where("id=?", lid).Get(tl)
return tl, err
}
func GetTimelines(offset int, limit int, path string, uid int64) (*[]Timeline, error) {
tls := new([]Timeline)
err := errors.New("")
if uid == 0 {
err = Engine.Limit(limit, offset).Desc(path).Find(tls)
} else {
if err = Engine.Where("uid=?", uid).Limit(limit, offset).Desc(path).Find(tls); err != nil {
err = Engine.Where("uid=?", uid).NoCache().Limit(limit, offset).Desc(path).Find(tls)
}
}
return tls, err
}
func GetTopicsByHotnessNodes(nodelimit int, topiclimit int) []*[]Topic {
//找出最热的节点:views优先 然后按 hotness排序 大概找出5到10个节点
//按上面找的节点读取下级话题
nds, _ := GetNodes(0, nodelimit, "hotness")
topics := make([]*[]Topic, 0)
if len(*nds) > 0 {
i := 0
for _, v := range *nds {
i = i + 1
tps := GetTopicsByNid(v.Id, 0, topiclimit, 0, "views")
if len(*tps) != 0 {
topics = append(topics, tps)
}
if i == len(*nds)-1 {
break
}
}
}
return topics
}
func GetTopicsByScoreNodes(nodelimit int, topiclimit int) []*[]Topic {
//找出最热的节点:views优先 然后按 hotness排序 大概找出5到10个节点
//按上面找的节点读取下级话题
nds, _ := GetNodes(0, nodelimit, "hotscore")
topics := make([]*[]Topic, 0)
if len(*nds) > 0 {
i := 0
for _, v := range *nds {
i = i + 1
tps := GetTopicsByNid(v.Id, 0, topiclimit, 0, "views")
if len(*tps) != 0 {
topics = append(topics, tps)
}
if i == len(*nds) {
break
}
}
}
return topics
}
func GetTopicsByHotnessCategory(catlimit int, topiclimit int) []*[]Topic {
//找出最热的分类:views优先 然后按 hotness排序 大概找出5到10个节点
//按上面找的节点读取下级话题
cats, _ := GetCategorys(0, catlimit, "hotness")
topics := make([]*[]Topic, 0)
if len(cats) > 0 {
i := 0
for _, v := range cats {
i = i + 1
//(cid int64, offset int, limit int, ctype int64, path string)
tps := GetTopicsByCid(v.Id, 0, topiclimit, 0, "views")
if len(*tps) != 0 {
topics = append(topics, tps)
}
if i == len(cats) {
break
}
}
}
return topics
}
func GetImagesByHotnessFingerprint(usrlimit int, imagelimit int) []*[]Image {
//获取最热的用户排名
users, _ := GetUsersOnHotness(0, usrlimit, "views")
images := make([]*[]Image, 0)
if len(*users) > 0 {
i := 0
for _, v := range *users {
i = i + 1
imgs, _ := GetImagesOnViewsHotnessFingerprintByUidExcludeCid(v.Id, 10) //按用户id取图 并排除头像
if len(*imgs) != 0 {
images = append(images, imgs)
}
if i == len(*users) {
break
}
}
}
return images
}
func SetTimelineContentByRid(lid int64, Content string) error {
if row, err := Engine.Table(&Timeline{}).Where("id=?", lid).Update(map[string]interface{}{"content": Content}); err != nil || row == 0 {
fmt.Println("SetTimelineContentByRid row:::", row)
fmt.Println("SetTimelineContentByRid出现错误:", err)
return err
} else {
return nil
}
}
func GetNode(id int64) (*Node, error) {
nd := new(Node)
has, err := Engine.Id(id).Get(nd)
if has {
return nd, err
} else {
return nil, err
}
}
func GetNodeByTitle(title string) (*Node, error) {
nd := new(Node)
nd.Title = title
has, err := Engine.Get(nd)
if has {
return nd, err
} else {
return nil, err
}
}
func GetNodes(offset int, limit int, path string) (*[]Node, error) {
nds := new([]Node)
err := Engine.Limit(limit, offset).Desc(path).Find(nds)
return nds, err
}
func AddCategory(title string, content string) (int64, error) {
cat := new(Category)
cat = &Category{Title: title, Content: content, Created: time.Now()}
if _, err := Engine.Insert(cat); err == nil {
return cat.Id, err
} else {
return -1, err
}
}
func SetQuestionMark(uid int64, qid int64) (int64, error) {
qstm := new(QuestionMark)
qstm = &QuestionMark{Uid: uid, Qid: qid}
rows, err := Engine.Insert(qstm)
return rows, err
}
func SetAnswerMark(uid int64, aid int64) (int64, error) {
ansm := new(AnswerMark)
ansm = &AnswerMark{Uid: uid, Aid: aid}
rows, err := Engine.Insert(ansm)
return rows, err
}
func IsQuestionMark(uid int64, qid int64) bool {
qstm := new(QuestionMark)
if has, err := Engine.Where("uid=? and qid=?", uid, qid).Get(qstm); err != nil {
fmt.Println(err)
return false
} else {
if has {
if qstm.Uid == uid {
return true
} else {
return false
}
} else {
return false
}
}
}
func IsAnswerMark(uid int64, aid int64) bool {
ansm := new(AnswerMark)
if has, err := Engine.Where("uid=? and aid=?", uid, aid).Get(ansm); err != nil {
return false
} else {
if has {
if ansm.Uid == uid {
return true
} else {
return false
}
} else {
return false
}
}
}
func GetCategorys(offset int, limit int, path string) (cate []*Category, err error) {
err = Engine.Limit(limit, offset).Desc(path).Find(&cate)
return cate, err
}
func GetCategory(id int64) (*Category, error) {
cat := new(Category)
has, err := Engine.Id(id).Get(cat)
if has {
return cat, err
} else {
return nil, err
}
}
func GetTopic(id int64) (*Topic, error) {
tp := new(Topic)
has, err := Engine.Id(id).Get(tp)
if has {
return tp, err
} else {
return nil, err
}
}
func GetQuestion(id int64) (*Question, error) {
qs := new(Question)
has, err := Engine.Id(id).Get(qs)
if has {
return qs, err
} else {
return nil, err
}
}
func GetAnswer(id int64) (*Reply, error) {
ans := new(Reply)
has, err := Engine.Id(id).Get(ans)
if has {
return ans, err
} else {
return nil, err
}
}
func GetTopics(offset int, limit int, path string) (*[]Topic, error) {
tps := new([]Topic)
err := Engine.Limit(limit, offset).Desc(path).Find(tps)
return tps, err
}
func GetQuestions(offset int, limit int, path string) (*[]Question, error) {
tps := new([]Question)
if path == "unanswered" {
err := Engine.Where("reply_count=? or ctype=?", 0, 0).Limit(limit, offset).Desc("id").Find(tps)
return tps, err
} else {
err := Engine.Limit(limit, offset).Desc(path).Find(tps)
return tps, err
}
}
func GetTopicsCount(offset int, limit int, path string) (int64, error) {
total, err := Engine.Limit(limit, offset).Count(&Topic{})
return total, err
}
func GetQuestionsCount(offset int, limit int, path string) (int64, error) {
if path == "unanswered" {
total, err := Engine.Where("reply_count=? or ctype=?", 0, 0).Limit(limit, offset).Count(&Question{})
return total, err
} else {
total, err := Engine.Limit(limit, offset).Count(&Question{})
return total, err
}
}
func GetTopicsByCategoryCount(category string, offset int, limit int, path string) (int64, error) {
total, err := Engine.Where("category=?", category).Limit(limit, offset).Count(&Topic{})
return total, err
}
//大数据下会出现极其严重的性能问题 亟待改善
func GetTopicsByCid(cid int64, offset int, limit int, ctype int64, path string) *[]Topic {
//排序首先是热值优先,然后是时间优先。
tps := new([]Topic)
switch {
case path == "asc":
if ctype != 0 {
Engine.Where("cid=? and ctype=?", cid, ctype).Limit(limit, offset).Asc("id").Find(tps)
} else {
Engine.Where("cid=?", cid).Limit(limit, offset).Asc("id").Find(tps)
}
case path == "views" || path == "reply_count":
if ctype != 0 {
Engine.Where("cid=? and ctype=?", cid, ctype).Desc(path).Limit(limit, offset).Find(tps)
} else {
if cid == 0 {
Engine.Desc(path).Limit(limit, offset).Find(tps)
} else {
Engine.Where("cid=?", cid).Desc(path).Limit(limit, offset).Find(tps)
}
}
default:
if ctype != 0 {
Engine.Where("cid=? and ctype=?", cid, ctype).Limit(limit, offset).Desc(path, "views", "reply_count", "created").Find(tps)
} else {
if cid == 0 {
Engine.Limit(limit, offset).Desc(path, "views", "reply_count", "created").Find(tps)
} else {
Engine.Where("cid=?", cid).Limit(limit, offset).Desc(path, "views", "reply_count", "created").Find(tps)
}
}
}
return tps
}
func GetQuestionsByCid(cid int64, offset int, limit int, ctype int64, path string) *[]Question {
//排序首先是热值优先,然后是时间优先。
tps := new([]Question)
switch {
case path == "asc":
if ctype != 0 {
Engine.Where("cid=? and ctype=?", cid, ctype).Limit(limit, offset).Asc("id").Find(tps)
} else {
Engine.Where("cid=?", cid).Limit(limit, offset).Asc("id").Find(tps)
}
case path == "views" || path == "reply_count":
if ctype != 0 {
Engine.Where("cid=? and ctype=?", cid, ctype).Desc(path).Limit(limit, offset).Find(tps)
} else {
if cid == 0 {
Engine.Desc(path).Limit(limit, offset).Find(tps)
} else {
Engine.Where("cid=?", cid).Desc(path).Limit(limit, offset).Find(tps)
}
}
default:
if ctype != 0 {
Engine.Where("cid=? and ctype=?", cid, ctype).Limit(limit, offset).Desc(path, "views", "reply_count", "created").Find(tps)
} else {
if cid == 0 {
Engine.Limit(limit, offset).Desc(path, "views", "reply_count", "created").Find(tps)
} else {
Engine.Where("cid=?", cid).Limit(limit, offset).Desc(path, "views", "reply_count", "created").Find(tps)