del.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package vote
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "git.getensh.com/common/gopkgs/database"
  9. "git.getensh.com/common/gopkgs/logger"
  10. "go.uber.org/zap"
  11. "google.golang.org/grpc/status"
  12. "gorm.io/gorm"
  13. "property-garden/errors"
  14. "property-garden/impl/v1/oss_utils"
  15. dbmodel "property-garden/model"
  16. "property-garden/parser"
  17. pb_v1 "property-garden/pb/v1"
  18. "property-garden/utils"
  19. "strings"
  20. )
  21. func checkVoteDelParam(req *pb_v1.VoteDelRequest) error {
  22. switch {
  23. case req.GardenId == 0:
  24. return status.Error(10003, "小区不能为空")
  25. case req.Id < 1:
  26. return status.Error(10003, "id 不能为空")
  27. }
  28. return nil
  29. }
  30. func VoteDel(ctx context.Context, req *pb_v1.VoteDelRequest) (reply *pb_v1.VoteDelReply, err error) {
  31. reply = &pb_v1.VoteDelReply{}
  32. // 捕获各个task中的异常并返回给调用者
  33. defer func() {
  34. if r := recover(); r != nil {
  35. err = fmt.Errorf("%+v", r)
  36. e := &status.Status{}
  37. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  38. logger.Error("err",
  39. zap.String("system_err", err.Error()),
  40. zap.Stack("stacktrace"))
  41. }
  42. }
  43. }()
  44. // 参数检查
  45. err = checkVoteDelParam(req)
  46. if err != nil {
  47. return nil, err
  48. }
  49. dbname := utils.GetGardenDbName(req.GardenId)
  50. // 获取旧数据
  51. // 获取旧数据
  52. old := dbmodel.NewVote(dbname)
  53. where := map[string]interface{}{
  54. "id": req.Id,
  55. }
  56. err = old.Find(database.DB(), where)
  57. if err != nil && err != gorm.ErrRecordNotFound {
  58. return nil, errors.DataBaseError
  59. }
  60. if old.ID == 0 {
  61. return nil, errors.ErrRecordNotFound
  62. }
  63. // 删除数据
  64. p := dbmodel.NewVote(dbname)
  65. db := database.DB().Begin()
  66. err = p.Delete(db, where)
  67. if err != nil {
  68. db.Rollback()
  69. return nil, errors.DataBaseError
  70. }
  71. collection := parser.Session.DB(dbmodel.VoteMgoDb).C(dbmodel.VoteCollection(req.GardenId, req.Id))
  72. err = collection.DropCollection()
  73. if err != nil {
  74. db.Rollback()
  75. logger.Error("mgo",
  76. zap.String("error", err.Error()))
  77. return nil, errors.DataBaseError
  78. }
  79. if err := oss_utils.OssObjAdd(nil, strings.Split(old.Pics, ";")); err != nil {
  80. db.Rollback()
  81. return nil, err
  82. }
  83. db.Commit()
  84. return reply, nil
  85. }