repair_class_add.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package repair
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "strings"
  9. "property-garden/errors"
  10. dbmodel "property-garden/model"
  11. pb_v1 "property-garden/pb/v1"
  12. "property-garden/utils"
  13. "time"
  14. "git.getensh.com/common/gopkgs/database"
  15. "git.getensh.com/common/gopkgs/logger"
  16. "go.uber.org/zap"
  17. "google.golang.org/grpc/status"
  18. )
  19. func checkRepairClassAddParam(req *pb_v1.RepairClassAddRequest) error {
  20. switch {
  21. case req.GardenId == 0:
  22. return status.Error(10003, "小区不能为空")
  23. case req.Name == "":
  24. return status.Error(10003, "名称不能为空")
  25. case req.Area == 0:
  26. return status.Error(10003, "区域不能为空")
  27. }
  28. return nil
  29. }
  30. func RepairClassAdd(ctx context.Context, req *pb_v1.RepairClassAddRequest) (reply *pb_v1.RepairClassAddReply, err error) {
  31. reply = &pb_v1.RepairClassAddReply{}
  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 = checkRepairClassAddParam(req)
  46. if err != nil {
  47. return nil, err
  48. }
  49. now := time.Now()
  50. dbname := utils.GetGardenDbName(req.GardenId)
  51. enable := int32(1)
  52. if !req.Enable {
  53. enable = 2
  54. }
  55. p := dbmodel.TRepairClass{
  56. Name:req.Name,
  57. Enable:enable,
  58. UpdatedAt:now,
  59. CreatedAt:now,
  60. Area:req.Area,
  61. }
  62. p.SetTable(dbname)
  63. err = p.Insert(database.DB())
  64. if err != nil {
  65. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  66. return nil, status.Error(10003, "分类名已存在")
  67. }
  68. return nil, errors.DataBaseError
  69. }
  70. reply.Id = p.ID
  71. return reply, nil
  72. }