11package dbs
22
3- import "time"
3+ import (
4+ "bytes"
5+ "jugglechat-server/storages/dbs/dbcommons"
6+ "jugglechat-server/storages/models"
7+ "time"
8+ )
49
510type PostCommentDao struct {
611 ID int64 `gorm:"primary_key"`
712 CommentId string `gorm:"comment_id"`
813 PostId string `gorm:"post_id"`
914 ParentCommentId string `gorm:"parent_comment_id"`
1015 ParentUserId string `gorm:"parent_user_id"`
11- Content string `gorm:"content "`
16+ Text string `gorm:"text "`
1217 IsDelete int `gorm:"is_delete"`
1318 UserId string `gorm:"user_id"`
14- CreatedTime time. Time `gorm:"created_time"`
19+ CreatedTime int64 `gorm:"created_time"`
1520 UpdatedTime time.Time `gorm:"updated_time"`
1621 Status int `gorm:"status"`
1722 AppKey string `gorm:"app_key"`
@@ -20,3 +25,88 @@ type PostCommentDao struct {
2025func (comment PostCommentDao ) TableName () string {
2126 return "postcomments"
2227}
28+
29+ func (comment PostCommentDao ) Create (item models.PostComment ) error {
30+ return dbcommons .GetDb ().Create (& PostCommentDao {
31+ CommentId : item .CommentId ,
32+ PostId : item .PostId ,
33+ ParentCommentId : item .ParentCommentId ,
34+ ParentUserId : item .ParentUserId ,
35+ Text : item .Text ,
36+ IsDelete : item .IsDelete ,
37+ UserId : item .UserId ,
38+ CreatedTime : item .CreatedTime ,
39+ UpdatedTime : time .Now (),
40+ Status : item .Status ,
41+ AppKey : item .AppKey ,
42+ }).Error
43+ }
44+
45+ func (comment PostCommentDao ) FindByIds (appkey string , commentIds []string ) (map [string ]* models.PostComment , error ) {
46+ var items []* PostCommentDao
47+ err := dbcommons .GetDb ().Where ("app_key=? and comment_id in (?)" , appkey , commentIds ).Find (& items ).Error
48+ if err != nil {
49+ return nil , err
50+ }
51+ ret := map [string ]* models.PostComment {}
52+ for _ , item := range items {
53+ ret [item .CommentId ] = & models.PostComment {
54+ ID : item .ID ,
55+ CommentId : item .CommentId ,
56+ PostId : item .PostId ,
57+ ParentCommentId : item .ParentCommentId ,
58+ ParentUserId : item .ParentUserId ,
59+ Text : item .Text ,
60+ IsDelete : item .IsDelete ,
61+ UserId : item .UserId ,
62+ CreatedTime : item .CreatedTime ,
63+ UpdatedTime : time .Now (),
64+ Status : item .Status ,
65+ AppKey : item .AppKey ,
66+ }
67+ }
68+ return ret , nil
69+ }
70+
71+ func (comment PostCommentDao ) QryPostComments (appkey , postId string , startTime , limit int64 , isPosttive bool ) ([]* models.PostComment , error ) {
72+ var items []* PostCommentDao
73+ conditionBuf := bytes.Buffer {}
74+ params := []interface {}{}
75+ conditionBuf .WriteString ("app_key=? and post_id=?" )
76+ params = append (params , appkey )
77+ params = append (params , postId )
78+ orderStr := "created_time desc"
79+ if isPosttive {
80+ orderStr = "created_time asc"
81+ conditionBuf .WriteString (" and created_time>?" )
82+ params = append (params , startTime )
83+ } else {
84+ if startTime <= 0 {
85+ startTime = time .Now ().UnixMilli ()
86+ }
87+ conditionBuf .WriteString (" and created_time<?" )
88+ params = append (params , startTime )
89+ }
90+ err := dbcommons .GetDb ().Where (conditionBuf .String (), params ... ).Order (orderStr ).Limit (limit ).Find (& items ).Error
91+ if err != nil {
92+ return nil , err
93+ }
94+ ret := []* models.PostComment {}
95+ for _ , item := range items {
96+ ret = append (ret , & models.PostComment {
97+ ID : item .ID ,
98+ CommentId : item .CommentId ,
99+ PostId : item .PostId ,
100+ ParentCommentId : item .ParentCommentId ,
101+ ParentUserId : item .ParentUserId ,
102+ Text : item .Text ,
103+ IsDelete : item .IsDelete ,
104+ UserId : item .UserId ,
105+ CreatedTime : item .CreatedTime ,
106+ UpdatedTime : time .Now (),
107+ Status : item .Status ,
108+ AppKey : item .AppKey ,
109+ })
110+ }
111+ return ret , nil
112+ }
0 commit comments