update.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package cpackage
  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 checkPackageUpdateParam(req *pb_v1.PackageUpdateRequest) error {
  19. switch {
  20. case req.Name == "":
  21. return status.Error(10003, "权限名称不能为空")
  22. case req.Desc == "":
  23. return status.Error(10003, "简介不能为空")
  24. case req.Id == 0:
  25. return status.Error(10003, "id不能为空")
  26. }
  27. return nil
  28. }
  29. //
  30. func PackageUpdate(ctx context.Context, req *pb_v1.PackageUpdateRequest) (reply *pb_v1.PackageUpdateReply, err error) {
  31. reply = &pb_v1.PackageUpdateReply{}
  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 = checkPackageUpdateParam(req)
  46. if err != nil {
  47. return nil, err
  48. }
  49. now := time.Now()
  50. values := map[string]interface{}{
  51. "updated_at": now,
  52. "name": req.Name,
  53. "desc": req.Desc,
  54. "price": req.Price,
  55. "house_count": req.HouseCount,
  56. }
  57. where := map[string]interface{}{
  58. "id": req.Id,
  59. }
  60. p := dbmodel.TPackage{}
  61. err = p.Find(database.DB(), where)
  62. if err != nil && err != gorm.ErrRecordNotFound {
  63. return nil, errors.DataBaseError
  64. }
  65. if p.ID == 0 {
  66. return nil, errors.ErrRecordNotFound
  67. }
  68. err = p.Update(database.DB(), where, values)
  69. if err != nil {
  70. return nil, errors.DataBaseError
  71. }
  72. return reply, nil
  73. }