//struct映射
func (s *commentRedisStore) CommentMapToStruct(commentInfo *model.CommentInfo, mapValue map[string]string) error {
if len(mapValue) == 0 {
return errors.New("len(mapValue) == 0")
}
decoder, _ := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
WeaklyTypedInput: true,
Result: commentInfo,
})
_ = decoder.Decode(mapValue)
//重填时间
if mapValue["createdAt"] != "" {
timeStr := mapValue["createdAt"]
commentInfo.CreatedAt, _ = time.Parse("2006-01-02T15:04:05Z07:00", timeStr)
}
if mapValue["updatedAt"] != "" {
timeStr := mapValue["updatedAt"]
commentInfo.UpdatedAt, _ = time.Parse("2006-01-02T15:04:05Z07:00", timeStr)
}
if mapValue["deletedAt"] != "" {
timeStr := mapValue["deletedAt"]
deletedAt, _ := time.Parse("2006-01-02T15:04:05Z07:00", timeStr)
commentInfo.DeletedAt = &deletedAt
} else {
commentInfo.DeletedAt = nil
}
return nil
}
type CommentInfo struct {
SN int64 `json:"sn"`
ID string `gorm:"-" json:"id"` // 评论外部编号
ArticleSN int64 `json:"articleSN"`
ArticleID string `gorm:"-" json:"articleID"` // 文章外部编号
UserID int `json:"userID"` // 用户id
Content string `json:"content"` // 评论内容
ReplyTo int `json:"replyTo"` // 被评论人id
ParentSN int64 `json:"parentSN"`
Parent string `gorm:"-" json:"parent"` // 父评论sn(空则为一级评论,否则为二级评论)
PlateSN int64 `json:"plateSN"`
Plate string `gorm:"-" json:"plate"` // 文章所属版块
CreatedAt time.Time `json:"createdAt"` // 创建时间
UpdatedAt time.Time `json:"updatedAt"` // 更新时间
DeletedAt *time.Time `json:"deletedAt"` // 删除时间
VoteUp int64 `json:"voteUp"` // 赞同数
VoteDown int64 `json:"voteDown"` // 反对数
RepliedTotal int64 `json:"repliedTotal"` // 被回复数
Status int64 `json:"status"` // 状态(正常显示1,用户主动假删除2)
ReviewStatus int64 `json:"reviewStatus"` // 审核状态(未审核1,通过2,审核不通过假删除3)
}
Plate string `gorm:"-" json:"plate"` // 文章所属版块
字段需要一致(大小写无所谓),否则上面的方法转不出来