add.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package system_msg
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "property-garden/errors"
  9. dbmodel "property-garden/model"
  10. pb_v1 "property-garden/pb/v1"
  11. "property-garden/utils"
  12. "time"
  13. "git.getensh.com/common/gopkgs/database"
  14. "git.getensh.com/common/gopkgs/logger"
  15. "go.uber.org/zap"
  16. "google.golang.org/grpc/status"
  17. )
  18. func checkSystemMsgAddParam(req *pb_v1.SystemMsgAddRequest) error {
  19. switch {
  20. case req.GardenId < 1:
  21. return status.Error(10003, "小区不能为空")
  22. case req.Content == "":
  23. return status.Error(10003, "内容不能为空")
  24. case req.Code == "":
  25. return status.Error(10003, "消息code不能为空")
  26. }
  27. return nil
  28. }
  29. const (
  30. SystemMsgCodeHouseholdBindHouse = "1"
  31. SystemMsgCodeRepairOrder = "2"
  32. SystemMsgCodeRepairOrderSended = "3"
  33. SystemMsgCodeSuggestionOrder = "4"
  34. SystemMsgCodeSuggestionOrderSended = "5"
  35. SystemMsgCodeRent = "6"
  36. SystemMsgCodeRentAppointment = "7"
  37. SystemMsgCodeFaceAdd = "8"
  38. )
  39. //
  40. func SystemMsgAdd(ctx context.Context, req *pb_v1.SystemMsgAddRequest) (reply *pb_v1.SystemMsgAddReply, err error) {
  41. reply = &pb_v1.SystemMsgAddReply{}
  42. // 捕获各个task中的异常并返回给调用者
  43. defer func() {
  44. if r := recover(); r != nil {
  45. err = fmt.Errorf("%+v", r)
  46. e := &status.Status{}
  47. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  48. logger.Error("err",
  49. zap.String("system_err", err.Error()),
  50. zap.Stack("stacktrace"))
  51. }
  52. }
  53. }()
  54. // 参数检查
  55. err = checkSystemMsgAddParam(req)
  56. if err != nil {
  57. return nil, err
  58. }
  59. dbname := utils.GetGardenDbName(req.GardenId)
  60. now := time.Now()
  61. p := &dbmodel.TMsg{
  62. Code: req.Code,
  63. Content: req.Content,
  64. UpdatedAt: now,
  65. CreatedAt: now,
  66. SystemUid: req.Uid,
  67. //GardenId:req.GardenId,
  68. }
  69. p.SetTable(dbname)
  70. err = p.Insert(database.DB())
  71. if err != nil {
  72. return nil, errors.DataBaseError
  73. }
  74. return reply, nil
  75. }