enable.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package application
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "property-common/errors"
  10. dbmodel "property-common/model"
  11. pb_v1 "property-common/pb/v1"
  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 checkApplicationEnableSetParam(req *pb_v1.ApplicationEnableSetRequest) error {
  19. switch {
  20. case req.Id == 0:
  21. return status.Error(10003, "id不能为空")
  22. }
  23. return nil
  24. }
  25. //
  26. func ApplicationEnableSet(ctx context.Context, req *pb_v1.ApplicationEnableSetRequest) (reply *pb_v1.ApplicationEnableSetReply, err error) {
  27. reply = &pb_v1.ApplicationEnableSetReply{}
  28. // 捕获各个task中的异常并返回给调用者
  29. defer func() {
  30. if r := recover(); r != nil {
  31. err = fmt.Errorf("%+v", r)
  32. e := &status.Status{}
  33. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  34. logger.Error("err",
  35. zap.String("system_err", err.Error()),
  36. zap.Stack("stacktrace"))
  37. }
  38. }
  39. }()
  40. // 参数检查
  41. err = checkApplicationEnableSetParam(req)
  42. if err != nil {
  43. return nil, err
  44. }
  45. now := time.Now()
  46. values := map[string]interface{}{
  47. "updated_at": now,
  48. "enable": 1,
  49. }
  50. if !req.Enable {
  51. values["enable"] = 2
  52. }
  53. where := map[string]interface{}{
  54. "id": req.Id,
  55. }
  56. p := dbmodel.TApplication{}
  57. err = p.Find(database.DB(), where)
  58. if err != nil && err != gorm.ErrRecordNotFound {
  59. return nil, errors.DataBaseError
  60. }
  61. if p.ID == 0 {
  62. return nil, errors.ErrRecordNotFound
  63. }
  64. err = p.Update(database.DB(), where, values)
  65. if err != nil {
  66. return nil, errors.DataBaseError
  67. }
  68. return reply, nil
  69. }