Эх сурвалжийг харах

留言列表,删除留言

xiexiaoyuan 3 жил өмнө
parent
commit
8e69196c47

+ 83 - 0
controller/comment/comment.go

@@ -6,6 +6,8 @@ import (
 	reqComment "hongze/hongze_yb/models/request/comment"
 	commentService "hongze/hongze_yb/services/comment"
 	userService "hongze/hongze_yb/services/user"
+	"hongze/hongze_yb/utils"
+	"strconv"
 )
 
 // Comment 发布留言
@@ -41,3 +43,84 @@ func Delete(c *gin.Context) {
 	response.Ok("删除留言成功", c)
 	return
 }
+
+// HotList 获取精选留言
+func HotList(c *gin.Context)  {
+	reqReportId := c.DefaultQuery("report_id", "")
+	reqReportChapterId := c.DefaultQuery("report_chapter_id", "")
+	reqPageIndex := c.DefaultQuery("current_index", "1")
+	reqPageSize := c.DefaultQuery("page_size", strconv.Itoa(utils.PageSize20))
+
+	pageIndex, err := strconv.Atoi(reqPageIndex)
+	if err != nil {
+		response.Fail("请输入正确的条数限制", c)
+		return
+	}
+	pageSize, err := strconv.Atoi(reqPageSize)
+	if err != nil {
+		response.Fail("请输入正确的页码", c)
+		return
+	}
+
+	if reqReportId == ""{
+		response.Fail("请输入报告ID", c)
+		return
+	}
+	reportId, err := strconv.Atoi(reqReportId)
+	if err != nil {
+		response.Fail("报告ID格式有误", c)
+		return
+	}
+	if reqReportChapterId == "" {
+		response.Fail("请输入章节ID", c)
+		return
+	}
+	reportChapterId, err := strconv.Atoi(reqReportChapterId)
+	if err != nil {
+		response.Fail("章节ID格式有误", c)
+		return
+	}
+	userinfo := userService.GetInfoByClaims(c)
+
+	list, err := commentService.List(userinfo, reportId, reportChapterId, true, pageIndex, pageSize)
+	if err != nil {
+		response.Fail(err.Error(), c)
+		return
+	}
+	response.OkData("查询成功", list, c )
+	return
+}
+
+// MyList 获取我的留言
+func MyList(c *gin.Context)  {
+	reqReportId := c.DefaultQuery("report_id", "")
+	reqReportChapterId := c.DefaultQuery("report_chapter_id", "")
+	if reqReportId == ""{
+		response.Fail("请输入报告ID", c)
+		return
+	}
+	reportId, err := strconv.Atoi(reqReportId)
+	if err != nil {
+		response.Fail("报告ID格式有误", c)
+		return
+	}
+	if reqReportChapterId == "" {
+		response.Fail("请输入章节ID", c)
+		return
+	}
+	reportChapterId, err := strconv.Atoi(reqReportChapterId)
+	if err != nil {
+		response.Fail("章节ID格式有误", c)
+		return
+	}
+
+	userinfo := userService.GetInfoByClaims(c)
+
+	list, err := commentService.List(userinfo, reportId, reportChapterId, false, 0, 0)
+	if err != nil {
+		response.Fail(err.Error(), c)
+		return
+	}
+	response.OkData("查询成功", list, c )
+	return
+}

+ 22 - 19
models/response/comment.go

@@ -2,28 +2,31 @@ package response
 
 import "time"
 
+type RespCommentList struct {
+	List       []*RespCommentItem   `json:"list"`
+	Paging     *PagingItem  		`json:"paging"`
+}
 type RespCommentItem struct {
-	CommentId         uint64 `description:"评论ID"`
-	UserId            uint64 `description:"用户id"`
-	ReportId          uint `description:"报告ID"`
-	ReportChapterId   uint `description:"报告章节ID"`
-	Content           string `description:"留言内容"`
-	IsTop             int8 `description:"是否置顶(0-未置顶,1-置顶)"`
-	IsHot             int8 `description:"是否设置精选(0-未设置,1-已设置)"`
-	HotTopTime        time.Time `description:"设置精选或者设置置顶的时间"`
-	UserName          string `description:"昵称"`
-	UserImg           string `description:"头像"`
-	CreateTime        time.Time `description:"创建时间"`
-	ReplayList        []*ReplayItem
+	CommentId         uint64 `description:"留言ID" json:"comment_id"`
+	UserId            uint64 `description:"用户id" json:"user_id"`
+	Content           string `description:"留言内容" json:"content"`
+	IsTop             int8 `description:"是否置顶(0-未置顶,1-置顶)json:"is_top""`
+	IsHot             int8 `description:"是否设置精选(0-未设置,1-已设置)json:"is_hot""`
+	HotTopTime        time.Time `description:"设置精选或者设置置顶的时间" json:"hot_top_time"`
+	UserName          string `description:"用户昵称" json:"user_name"`
+	UserImg           string `description:"用户头像" json:"user_img"`
+	CreateTime        time.Time `description:"留言创建时间" json:"create_time"`
+	ReplyList        []*ReplyItem
 }
 
-type ReplayItem struct {
-	AdminId           uint64 `description:"发布留言回复的管理员ID"`
-	AdminName         string `description:"系统昵称"`
-	AdminImg          string `description:"系统头像"`
-	Content           string `description:"回复内容"`
-	ReplyCommentId    uint64 `description:"回复的留言ID"`
-	CreateTime        time.Time `description:"创建时间"`
+type ReplyItem struct {
+	CommentId         uint64 `description:"留言ID" json:"comment_id"`
+	AdminId           uint64 `description:"发布留言回复的管理员ID" json:"admin_id"`
+	AdminName         string `description:"系统昵称" json:"admin_name"`
+	AdminImg          string `description:"系统头像" json:"admin_img"`
+	Content           string `description:"回复内容" json:"content"`
+	ReplyCommentId    uint64 `description:"回复的留言ID" json:"reply_comment_id"`
+	CreateTime        time.Time `description:"留言创建时间" json:"create_time"`
 }
 
 type RespCommentAdd struct {

+ 16 - 8
models/tables/yb_comment/query.go

@@ -6,11 +6,11 @@ import (
 )
 
 
-// GetListByUserId 获取用户的留言列表
-func GetListByUserId(userId uint64, reportId, reportChapterId int) (list []*YbComment, err error) {
+// GetListByUserIdReportId 获取用户的留言列表
+func GetListByUserIdReportId(userId uint64, reportId, reportChapterId int) (list []*YbComment, err error) {
 	err = global.DEFAULT_MYSQL.Model(YbComment{}).
-		Where("user_id = ? and report_id = ? and report_chapter_id = ?", userId, reportId, reportChapterId).
-		Order("create_time desc, id desc").
+		Where("user_id = ? and report_id = ? and report_chapter_id = ? and enabled = 1 ", userId, reportId, reportChapterId).
+		Order("create_time desc, comment_id desc").
 		Scan(&list).Error
 	if err == utils.ErrNoRow {
 		err = nil
@@ -22,18 +22,26 @@ func GetListByUserId(userId uint64, reportId, reportChapterId int) (list []*YbCo
 func GetHotListByReportId(reportId, reportChapterId int, offset, limit int) (list []*YbComment, err error) {
 	err = global.DEFAULT_MYSQL.Model(YbComment{}).
 		Where("report_id = ? and report_chapter_id = ? and enabled = 1 and is_hot = 1 and type = 1", reportId, reportChapterId).
-		Order("hot_top_time desc, id desc").
+		Order("is_top desc, hot_top_time desc, comment_id desc").
 		Offset(offset).
 		Limit(limit).
 		Scan(&list).Error
 	return
 }
 
-// GetReplayListByReplyCommentId 获取报告的精选留言的回复列表
-func GetReplayListByReplyCommentId(reportId, reportChapterId int, replyCommentIds []int) (list []*YbComment, err error) {
+// GetHostListTotalByReportId 获取精选留言总条数
+func GetHostListTotalByReportId(reportId, reportChapterId int)(total int64, err error)  {
+	err = global.DEFAULT_MYSQL.Model(YbComment{}).
+		Where("report_id = ? and report_chapter_id = ? and enabled = 1 and is_hot = 1 and type = 1", reportId, reportChapterId).
+		Count(&total).Error
+	return
+}
+
+// GetReplyListByReplyCommentId 获取报告的精选留言的回复列表
+func GetReplyListByReplyCommentId(reportId, reportChapterId int, replyCommentIds []uint64) (list []*YbComment, err error) {
 	err = global.DEFAULT_MYSQL.Model(YbComment{}).
 		Where("report_id = ? and report_chapter_id = ? and enabled = 1 and type = 2 and reply_comment_id in ?", reportId, reportChapterId, replyCommentIds).
-		Order("create_time desc, id desc").
+		Order("create_time desc, comment_id desc").
 		Scan(&list).Error
 	return
 }

+ 32 - 0
models/tables/yb_comment/update.go

@@ -8,4 +8,36 @@ func (yc *YbComment) Update(updateCols []string) (err error) {
 	return
 }
 
+// Delete 软删除,隐藏评论
+func Delete(userId uint64, commentId uint64)(err error) {
+	tx := global.DEFAULT_MYSQL.Begin()
+	defer func() {
+		if err != nil {
+			tx.Rollback()
+		} else {
+			tx.Commit()
+		}
+	}()
+	sql1 := ` UPDATE yb_comment
+			SET
+            	enabled = 0,
+				modify_time = NOW(),
+				type = 1,
+				user_id = ?
+			WHERE comment_id = ?`
+	err = tx.Exec(sql1, userId, commentId).Error
+	if err != nil {
+		return
+	}
+	// 删除相关联的回复
+	sql2 := ` UPDATE yb_comment
+			SET
+            	enabled = 0,
+				type = 2,
+				modify_time = NOW()
+			WHERE reply_comment_id = ?`
+	err = tx.Exec(sql2, commentId).Error
+	return
+}
+
 

+ 2 - 0
routers/comment.go

@@ -10,4 +10,6 @@ func InitComment(r *gin.Engine)  {
 	rGroup := r.Group("/api/comment").Use(middleware.Token())
 	rGroup.POST("/set", comment.Comment)
 	rGroup.POST("/del", comment.Delete)
+	rGroup.GET("/hot", comment.HotList)
+	rGroup.GET("/my", comment.MyList)
 }

+ 90 - 4
services/comment/comment.go

@@ -128,16 +128,102 @@ func Delete(user user.UserInfo, req *reqComment.ReqDel) (err error) {
 		err = errors.New("不允许删除他人的留言")
 		return
 	}
+	if commentInfo.Type != 1 {
+		err = errors.New("不允许删除回复")
+		return
+	}
 	if commentInfo.Enabled == 0 {
 		return
 	}
-	commentInfo.Enabled = 0
-	commentInfo.ModifyTime = time.Now()
-	err = commentInfo.Update([]string{"enabled", "modify_time"})
+	err = yb_comment.Delete(user.UserID, commentInfo.CommentId)
 	if err != nil {
 		errMsg = err.Error()
-		err = errors.New("更新留言出错")
+		err = errors.New("删除留言出错")
 		return
 	}
 	return
 }
+
+// List 查询精选留言列表或我的留言列表
+func List(user user.UserInfo,  reportId, reportChapterId int, hotFlag bool, pageIndex, pageSize int) (ret response.RespCommentList, err error) {
+	var errMsg string
+	defer func() {
+		if err != nil {
+			global.LOG.Critical(fmt.Sprintf("GetReportList: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
+		}
+	}()
+
+	offset := (pageIndex -1)*pageSize
+	var commentList []*yb_comment.YbComment
+	var total int64
+	//查询精选留言
+	if hotFlag {
+		commentList, err = yb_comment.GetHotListByReportId(reportId, reportChapterId, offset, pageSize)
+		if err != nil {
+			errMsg = err.Error()
+			err = errors.New("查询精选留言列表出错")
+			return
+		}
+		total, err = yb_comment.GetHostListTotalByReportId(reportId, reportChapterId)
+		if err != nil {
+			errMsg = err.Error()
+			err = errors.New("查询精选留言总数出错")
+			return
+		}
+	} else {
+		//查询个人留言
+		commentList, err = yb_comment.GetListByUserIdReportId(user.UserID, reportId, reportChapterId)
+		if err != nil {
+			errMsg = err.Error()
+			err = errors.New("查询精选留言列表出错")
+			return
+		}
+	}
+
+	var commentIds []uint64
+	for _, v := range commentList {
+		commentIds = append(commentIds, v.CommentId)
+	}
+	// 查询所有的回复列表
+	replyListMap := make(map[uint64][]*response.ReplyItem)
+	if len(commentIds) > 0 {
+		replyList, tErr := yb_comment.GetReplyListByReplyCommentId(reportId, reportChapterId, commentIds)
+		if tErr != nil {
+			errMsg = tErr.Error()
+			err = errors.New("查询回复出错")
+			return
+		}
+		for _, v := range replyList {
+			t := new(response.ReplyItem)
+			t.CommentId = v.CommentId
+			t.Content = v.Content
+			t.AdminId = v.AdminId
+			t.CreateTime = v.CreateTime
+			t.AdminName = ""
+			t.AdminImg = ""
+			replyListMap[v.ReplyCommentId] = append(replyListMap[v.ReplyCommentId], t)
+		}
+	}
+	var list []*response.RespCommentItem
+	for _, v := range commentList {
+		tmp := new(response.RespCommentItem)
+		tmp.CommentId = v.CommentId
+		tmp.UserId = v.UserId
+		tmp.UserName = ""
+		tmp.UserImg = ""
+		tmp.Content = v.Content
+		tmp.IsHot = v.IsHot
+		tmp.IsTop = v.IsTop
+		tmp.HotTopTime = v.HotTopTime
+		tmp.CreateTime = v.CreateTime
+		if existList, ok := replyListMap[v.CommentId]; ok {
+			tmp.ReplyList = existList
+		}
+		list = append(list, tmp)
+	}
+	ret.List = list
+	if hotFlag {
+		ret.Paging = response.GetPaging(pageIndex, pageSize, int(total))
+	}
+	return
+}