Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Commit

Permalink
[评论] 可以显示评论了
Browse files Browse the repository at this point in the history
  • Loading branch information
Thxzzzzz committed May 14, 2019
1 parent b763273 commit 9cbbca9
Show file tree
Hide file tree
Showing 13 changed files with 441 additions and 150 deletions.
17 changes: 17 additions & 0 deletions comments_infos.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CREATE TABLE `comments_infos` (
`id` BIGINT(24) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '评论主键id',
`product_id` BIGINT(24) UNSIGNED NOT NULL COMMENT '被评论产品id',
`user_id` BIGINT(24) UNSIGNED NOT NULL COMMENT '评论者id',
`content` varchar(512) DEFAULT NULL COMMENT '评论内容',
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`deleted_at` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `idx_comments_infos_deleted_at` (`deleted_at`) USING BTREE,
INDEX `idx_comments_infos_user_id` (`user_id`) USING BTREE,
INDEX `idx_comments_infos_product_id` (`product_id`) USING BTREE
)
COMMENT='评论主表'
COLLATE='utf8mb4_general_ci'
ENGINE=INNODB
;
17 changes: 17 additions & 0 deletions comments_replys.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CREATE TABLE `comments_replys` (
`id` BIGINT(24) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '评论主键id',
`comment_id` BIGINT(24) UNSIGNED NOT NULL COMMENT '评论主表id',
`user_id` BIGINT(24) UNSIGNED NOT NULL COMMENT '评论者id',
`content` varchar(512) DEFAULT NULL COMMENT '评论内容',
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`deleted_at` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `idx_comments_replys_deleted_at` (`deleted_at`) USING BTREE,
INDEX `idx_comments_replys_user_id` (`user_id`) USING BTREE,
INDEX `idx_comments_replys_comment_id` (`comment_id`) USING BTREE
)
COMMENT='评论回复表'
COLLATE='utf8mb4_general_ci'
ENGINE=INNODB
;
35 changes: 34 additions & 1 deletion controllers/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,39 @@ func (c *ProductController) GetSellerByProductId() {
// @Failure 400
// @router /getProductCountInfo [get]
func (c *ProductController) GetProductCountInfo() {
// 累计支持金额
// 最高筹集金额
// 累计支持人数
// 单项最高支持人数
countInfo, err := models.GetAllProductCountInfo()
if err != nil {
c.ResponseErrJson(&resultError.FormParamErr)
return
}
c.ResponseSuccessJson(countInfo)
}

//TODO 获取统计信息
// @Title 根据商品 ID 获取商家信息
// @Description 根据商品 ID 获取商家信息
// @Param product_id query int true "套餐ID"
// @Param page query int true "页码"
// @Param page_size query int true "每页数量"
// @Success 200
// @Failure 400
// @router /getCommentInfoByProductId [get]
func (c *ProductController) GetCommentInfoByProductId() {
form := forms.CommentListByProductForm{}
// 获取所有 query 数据组成的 map
values := c.Ctx.Request.URL.Query()
// 解析到 Struct 中
err := beego.ParseForm(values, &form)
if err != nil {
c.ResponseErrJson(err)
}
results, err := models.GetResultCommentInfosByProductId(&form)
if err != nil {
c.ResponseErrJson(&resultError.FormParamErr)
return
}
c.ResponseSuccessJson(results)
}
6 changes: 6 additions & 0 deletions forms/comment.go
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
package forms

// 请求评论信息表单
type CommentListByProductForm struct {
ProductId uint64 `json:"product_id" form:"product_id"`
PageForm
}
259 changes: 145 additions & 114 deletions funding.sql

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lastupdate.tmp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"C:\\GoPath\\src\\funding\\controllers":1557825931298529100}
{"C:\\GoPath\\src\\funding\\controllers":1557859750063378400}
121 changes: 121 additions & 0 deletions models/commentsInfos.go
Original file line number Diff line number Diff line change
@@ -1 +1,122 @@
package models

import (
"funding/forms"
"funding/objects"
"github.com/jinzhu/gorm"
"time"
)

// 主评论表
type CommentsInfo struct {
BaseModel
ProductId uint64 `json:"product_id"` // 产品 ID
UserId uint64 `json:"user_id"` // 用户 ID
IsSeller bool `json:"is_seller"` // 是否是对应卖家
Content string `json:"content"` // 评论内容
}

// 返回给前端的评论信息
type ResultCommentInfo struct {
ID uint64 `json:"id" gorm:"primary_key"`
ProductId uint64 `json:"product_id"` // 产品 ID
UserId uint64 `json:"user_id"` // 用户 ID
IsSeller bool `json:"is_seller"` // 是否是对应卖家
Content string `json:"content"` // 评论内容
CreatedAt time.Time `json:"created_at"` // 创建时间
Username string `json:"username"` // 用户名
IconUrl string `json:"icon_url"` // 用户头像
Replys []ResultCommentReply `json:"replys"` // 回复信息
}

///////////////////// 基本增删改查 /////////////////////

// 根据主评论的 ID 来获取主评论条目
func FindCommentsInfoById(commentsInfoId uint64) (*CommentsInfo, error) {
var ret CommentsInfo
err := db.First(&ret, commentsInfoId).Error
if err != nil {
return nil, err
}
return &ret, nil
}

// 根据用户 ID 来获取主评论列表
func FindCommentsInfosByUserId(userId uint64) ([]*CommentsInfo, error) {
var rets []*CommentsInfo
err := db.Where("user_id = ?", userId).Find(&rets).Error
return rets, err
}

// 根据产品 ID 来获取主评论列表
func FindCommentsInfosByProductId(productId uint64) ([]*CommentsInfo, error) {
var rets []*CommentsInfo
err := db.Where("product_id = ?", productId).Find(&rets).Error
return rets, err
}

// 新增主评论
func InsertCommentsInfo(commentsInfo *CommentsInfo) error {
err := db.Create(commentsInfo).Error
return err
}

//删除主评论条目 由于这里是软删除,所以只是把 delete_at 设置了一个值,实际上还存在数据库中,但并不能用 gorm 查到
func DeleteCommentsInfoById(id uint64) error {
err := db.Delete(CommentsInfo{}, "id = ?", id).Error
return err
}

//根据 commentsInfoID 来更新其他相应的字段
func UpdateCommentsInfo(commentsInfo *CommentsInfo) error {
var rec CommentsInfo
err := db.First(&rec, "id = ?", commentsInfo.ID).Error
if err != nil {
return err
}
err = db.Model(&rec).Updates(commentsInfo).Error
return err
}

///////////////////// EMD 基本增删改查 /////////////////////
// 根据产品 ID 获取对应产品的评论
const sqlGetResultCommentInfosByProductId = `
SELECT
c.*,u.username,u.icon_url
FROM
comments_infos c JOIN
users u ON c.user_id = u.id
WHERE
c.deleted_at IS NULL
AND u.deleted_at IS NULL
AND c.product_id = (?)
ORDER BY
id DESC
`

// 获取回复列表
func GetResultCommentInfosByProductId(form *forms.CommentListByProductForm) ([]ResultCommentInfo, error) {
results := []ResultCommentInfo{}

//TODO 先不做分页
//page, pageSize := 1, 10
//// 如果页码和每页数量大于 0
//if form.Page > 0 && form.PageSize > 0 {
// page = form.Page
// pageSize = form.PageSize
//}

err := db.Raw(sqlGetResultCommentInfosByProductId, form.ProductId).Scan(&results).Error
if err != nil {
return nil, resultError.NewFallFundingErr("查询评论时发生了错误")
}

for i, _ := range results {
replys, err := GetResultCommentReplaysByCommentId(results[i].ID)
if err != nil && gorm.IsRecordNotFoundError(err) {
continue
}
results[i].Replys = replys
}
return results, err
}
67 changes: 33 additions & 34 deletions models/commentsReplys.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package models

import "funding/objects"
import "time"

// 评论回复表
type CommentsReplay struct {
type CommentsReply struct {
BaseModel
CommentId uint64 `json:"comment_id"` // 产品 ID
UserId uint64 `json:"user_id"` // 用户 ID
Expand All @@ -12,85 +12,84 @@ type CommentsReplay struct {
}

// 返回给前端的回复信息
type ResultCommentReplay struct {
CommentsInfo
Username string `json:"username"` // 用户名
IconUrl string `json:"icon_url"` // 用户头像
type ResultCommentReply struct {
ID uint64 `json:"id" gorm:"primary_key"`
CommentId uint64 `json:"comment_id"` // 产品 ID
UserId uint64 `json:"user_id"` // 用户 ID
IsSeller bool `json:"is_seller"` // 是否是对应卖家
Content string `json:"content"` // 评论内容
CreatedAt time.Time `json:"created_at"` // 创建时间
Username string `json:"username"` // 用户名
IconUrl string `json:"icon_url"` // 用户头像
}

///////////////////// 基本增删改查 /////////////////////

// 根据回复的 ID 来获取回复条目
func FindCommentsReplayById(commentsReplayId uint64) (*CommentsReplay, error) {
var ret CommentsReplay
err := db.First(&ret, commentsReplayId).Error
func FindCommentsReplyById(commentsReplyId uint64) (*CommentsReply, error) {
var ret CommentsReply
err := db.First(&ret, commentsReplyId).Error
if err != nil {
return nil, err
}
return &ret, nil
}

// 根据用户 ID 来获取回复列表
func FindCommentsReplaysByUserId(userId uint64) ([]*CommentsReplay, error) {
var rets []*CommentsReplay
func FindCommentsReplysByUserId(userId uint64) ([]*CommentsReply, error) {
var rets []*CommentsReply
err := db.Where("user_id = ?", userId).Find(&rets).Error
return rets, err
}

// 根据主评论 ID 来获取回复列表
func FindCommentsReplaysByCommentId(commentId uint64) ([]*CommentsReplay, error) {
var rets []*CommentsReplay
func FindCommentsReplysByCommentId(commentId uint64) ([]*CommentsReply, error) {
var rets []*CommentsReply
err := db.Where("comment_id = ?", commentId).Find(&rets).Error
return rets, err
}

// 新增回复
func InsertCommentsReplay(commentsReplay *CommentsReplay) error {
err := db.Create(commentsReplay).Error
func InsertCommentsReply(commentsReply *CommentsReply) error {
err := db.Create(commentsReply).Error
return err
}

//删除回复条目 由于这里是软删除,所以只是把 delete_at 设置了一个值,实际上还存在数据库中,但并不能用 gorm 查到
func DeleteCommentsReplayById(id uint64) error {
err := db.Delete(CommentsReplay{}, "id = ?", id).Error
func DeleteCommentsReplyById(id uint64) error {
err := db.Delete(CommentsReply{}, "id = ?", id).Error
return err
}

//根据 commentsReplayID 来更新其他相应的字段
func UpdateCommentsReplay(commentsReplay *CommentsReplay) error {
var rec CommentsReplay
err := db.First(&rec, "id = ?", commentsReplay.ID).Error
//根据 commentsReplyID 来更新其他相应的字段
func UpdateCommentsReply(commentsReply *CommentsReply) error {
var rec CommentsReply
err := db.First(&rec, "id = ?", commentsReply.ID).Error
if err != nil {
return err
}
err = db.Model(&rec).Updates(commentsReplay).Error
err = db.Model(&rec).Updates(commentsReply).Error
return err
}

///////////////////// EMD 基本增删改查 /////////////////////
// 根据产品 ID 获取对应产品的评论
const sqlGetResultCommentReplaysByProductId = `
const sqlGetResultCommentReplaysByCommentId = `
SELECT
c.*,u.username,u.icon_url
FROM
comments_replays c JOIN
comments_replys c JOIN
users u ON c.user_id = u.id
WHERE
c.deleted_at IS NULL
AND u.deleted_at IS NULL
AND c.product_id = (?)
AND c.comment_id = (?)
ORDER BY
id DESC
`

func GetResultCommentReplaysByProductId(productId uint64) ([]ResultCommentInfo, error) {
results := []ResultCommentReplay{}
err := db.Raw(sqlSelectOrderListField, productId).Scan(&results).Error
if err != nil {
return nil, resultError.NewFallFundingErr("查询评论时发生了错误")
}
for _, comment := range results {

}
func GetResultCommentReplaysByCommentId(commentId uint64) ([]ResultCommentReply, error) {
results := []ResultCommentReply{}
err := db.Raw(sqlGetResultCommentReplaysByCommentId, commentId).Scan(&results).Error
return results, err
}
14 changes: 14 additions & 0 deletions models/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,20 @@ func GetCheckoutPkgInfoFromPkgId(pkgId uint64) (*resultModels.CheckoutPkgInfo, e
return &result, err
}

const sqlCountProduct = `
SUM(current_price) AS support_price_count,
MAX(current_price) AS max_support_price,
SUM(backers) AS backers_count,
MAX(backers) AS max_backers
`

// 统计产品信息
func GetAllProductCountInfo() (resultModels.ProductCountInfo, error) {
countInfo := resultModels.ProductCountInfo{}
err := db.Table("products").Select(sqlCountProduct).Scan(&countInfo).Error
return countInfo, err
}

// 获取产品的截止日期,这个可以用作购物车的失效处理,或者在获取购物车列表的时候就处理?
//func GetEndTimeListInProductId(productIds []uint64) ([]time.Time, error) {
// results := []time.Time{}
Expand Down
8 changes: 8 additions & 0 deletions resultModels/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ type CheckoutPkgInfo struct {
EndTime time.Time `json:"end_time"` // 截止时间
}

// 产品统计信息
type ProductCountInfo struct {
SupportPriceCount float64 `json:"support_price_count"` // 累计支持金额
MaxSupportPrice float64 `json:"max_support_price"` // 最高筹集金额
BackersCount uint64 `json:"backers_count"` // 累计支持人数
MaxBackers uint64 `json:"max_backers"` // 单项最高支持人数
}

//// 截止时间信息
//type EndTimeItem struct {
// ProductId uint64 `json:"product_id"` // 产品 Id
Expand Down
9 changes: 9 additions & 0 deletions routers/commentsRouter_controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ func init() {
Filters: nil,
Params: nil})

beego.GlobalControllerRouter["funding/controllers:ProductController"] = append(beego.GlobalControllerRouter["funding/controllers:ProductController"],
beego.ControllerComments{
Method: "GetCommentInfoByProductId",
Router: `/getCommentInfoByProductId`,
AllowHTTPMethods: []string{"get"},
MethodParams: param.Make(),
Filters: nil,
Params: nil})

beego.GlobalControllerRouter["funding/controllers:ProductController"] = append(beego.GlobalControllerRouter["funding/controllers:ProductController"],
beego.ControllerComments{
Method: "GetProductCountInfo",
Expand Down
Loading

0 comments on commit 9cbbca9

Please sign in to comment.