// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package neighbor import ( "context" "encoding/json" "fmt" "gorm.io/gorm" "property-garden/errors" "property-garden/impl/v1/oss_utils" dbmodel "property-garden/model" pb_v1 "property-garden/pb/v1" "property-garden/utils" "time" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" ) func checkNeighborCommentAddParam(req *pb_v1.NeighborCommentAddRequest) error { switch { case req.GardenId == 0: return status.Error(10003, "小区不能为空") case req.Pid == 0 && req.ArticleId == 0: return status.Error(10003, "父id和文章id不能同时为空") case req.Pid > 0 && req.ArticleId > 0: return status.Error(10003, "父id和文章id智能选其一") case req.Content == "": return status.Error(10003, "内容不能为空") case req.Uid == 0: return status.Error(10003, "uid 不能为空") } return nil } func articleCommentAdd(req *pb_v1.NeighborCommentAddRequest, db *gorm.DB, dbname string) (err error) { article := dbmodel.NewNeighborArticle(dbname) where := map[string]interface{}{ "id": req.ArticleId, } err = article.Find(db, where) if err != nil && err != gorm.ErrRecordNotFound { return errors.DataBaseError } if article.ID == 0 { return errors.ErrRecordNotFound } now := time.Now() p := dbmodel.TNeighborComment{ ArticleId: req.ArticleId, ClassId: article.ClassId, Pid: 0, Content: req.Content, Pics: utils.StringJoin(req.Pics, ","), Uid: req.Uid, SubCommentCount: 0, CreatedAt: now, UpdatedAt: now, Likes: 0, UserIcon: req.UserIcon, NickName: req.NickName, } p.SetTable(dbname) err = p.Insert(db) if err != nil { return errors.DataBaseError } values := map[string]interface{}{ "comment_count": gorm.Expr("comment_count + ?", 1), } err = article.Update(db, where, values) if err != nil { return errors.DataBaseError } return nil } func subCommentAdd(req *pb_v1.NeighborCommentAddRequest, db *gorm.DB, dbname string) (err error) { comment := dbmodel.NewNeighborComment(dbname) where := map[string]interface{}{ "id": req.Pid, } err = comment.Find(db, where) if err != nil && err != gorm.ErrRecordNotFound { return errors.DataBaseError } if comment.ID == 0 { return errors.ErrRecordNotFound } now := time.Now() p := dbmodel.TNeighborComment{ ArticleId: comment.ArticleId, ClassId: comment.ClassId, Pid: req.Pid, Content: req.Content, Pics: utils.StringJoin(req.Pics, ";"), Uid: req.Uid, SubCommentCount: 0, CreatedAt: now, UpdatedAt: now, Likes: 0, UserIcon: req.UserIcon, } p.SetTable(dbname) err = p.Insert(db) if err != nil { return errors.DataBaseError } values := map[string]interface{}{ "sub_comment_count": gorm.Expr("sub_comment_count + ?", 1), } err = comment.Update(db, where, values) if err != nil { return errors.DataBaseError } return nil } func NeighborCommentAdd(ctx context.Context, req *pb_v1.NeighborCommentAddRequest) (reply *pb_v1.NeighborCommentAddReply, err error) { reply = &pb_v1.NeighborCommentAddReply{} // 捕获各个task中的异常并返回给调用者 defer func() { if r := recover(); r != nil { err = fmt.Errorf("%+v", r) e := &status.Status{} if er := json.Unmarshal([]byte(err.Error()), e); er != nil { logger.Error("err", zap.String("system_err", err.Error()), zap.Stack("stacktrace")) } } }() // 参数检查 err = checkNeighborCommentAddParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) db := database.DB().Begin() if req.Pid > 0 { err = subCommentAdd(req, db, dbname) } else { err = articleCommentAdd(req, db, dbname) } if err != nil { db.Rollback() return nil, err } if err := oss_utils.OssObjAdd(req.Pics, nil); err != nil { db.Rollback() return nil, err } db.Commit() return reply, nil }