enable.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package material
  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. "property-common/errors"
  13. dbmodel "property-common/model"
  14. pb_v1 "property-common/pb/v1"
  15. "time"
  16. )
  17. func checkMaterialEnableParam(req *pb_v1.MaterialEnableRequest) error {
  18. switch {
  19. case req.Id == 0:
  20. return status.Error(10003, "id不能为空")
  21. case req.Enable == 0:
  22. return status.Error(10003, "是否发布")
  23. }
  24. return nil
  25. }
  26. //
  27. func MaterialEnable(ctx context.Context, req *pb_v1.MaterialEnableRequest) (reply *pb_v1.MaterialEnableReply, err error) {
  28. reply = &pb_v1.MaterialEnableReply{}
  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 = checkMaterialEnableParam(req)
  43. if err != nil {
  44. return nil, err
  45. }
  46. now := time.Now()
  47. p := &dbmodel.TMaterial{}
  48. where := map[string]interface{}{
  49. "id": req.Id,
  50. }
  51. values := map[string]interface{}{
  52. "enable": req.Enable,
  53. "updated_at": now,
  54. }
  55. err = p.Update(database.DB(), where, values)
  56. if err != nil {
  57. return nil, errors.DataBaseError
  58. }
  59. return reply, nil
  60. }