add.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package msg_package
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "property-common/errors"
  9. dbmodel "property-common/model"
  10. pb_v1 "property-common/pb/v1"
  11. "time"
  12. "git.getensh.com/common/gopkgs/database"
  13. "git.getensh.com/common/gopkgs/logger"
  14. "go.uber.org/zap"
  15. "google.golang.org/grpc/status"
  16. )
  17. func checkMsgPackageAddParam(req *pb_v1.MsgPackageAddRequest) error {
  18. switch {
  19. case req.Name == "":
  20. return status.Error(10003, "套餐名称不能为空")
  21. case req.MsgCount == 0:
  22. return status.Error(10003, "短信条数不能为空")
  23. }
  24. return nil
  25. }
  26. //
  27. func MsgPackageAdd(ctx context.Context, req *pb_v1.MsgPackageAddRequest) (reply *pb_v1.MsgPackageAddReply, err error) {
  28. reply = &pb_v1.MsgPackageAddReply{}
  29. // 捕获各个task中的异常并返回给调用者
  30. defer func() {
  31. if r := recover(); r != nil {
  32. err = fmt.Errorf("%+v", r)
  33. e := &status.Status{}
  34. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  35. logger.Error("err",
  36. zap.String("system_err", err.Error()),
  37. zap.Stack("stacktrace"))
  38. }
  39. }
  40. }()
  41. // 参数检查
  42. err = checkMsgPackageAddParam(req)
  43. if err != nil {
  44. return nil, err
  45. }
  46. now := time.Now()
  47. p := &dbmodel.TMsgPackage{
  48. CreatedAt: now,
  49. UpdatedAt: now,
  50. Name: req.Name,
  51. Price: req.Price,
  52. Comment: req.Comment,
  53. MsgCount: req.MsgCount,
  54. }
  55. if err = p.Insert(database.DB()); err != nil {
  56. return nil, errors.DataBaseError
  57. }
  58. return reply, nil
  59. }