amount.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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-system/errors"
  10. dbmodel "property-system/model"
  11. pb_v1 "property-system/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 checkApplicationOrderAmountParam(req *pb_v1.ApplicationOrderAmountRequest) error {
  19. switch {
  20. case req.OrderId == 0:
  21. return status.Error(10003, "订单不能为空错误")
  22. }
  23. return nil
  24. }
  25. //
  26. func ApplicationOrderAmount(ctx context.Context, req *pb_v1.ApplicationOrderAmountRequest) (reply *pb_v1.ApplicationOrderAmountReply, err error) {
  27. reply = &pb_v1.ApplicationOrderAmountReply{}
  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. err = checkApplicationOrderAmountParam(req)
  41. if err != nil {
  42. return nil, err
  43. }
  44. now := time.Now()
  45. p := dbmodel.TApplicationOrder{}
  46. where := map[string]interface{}{
  47. "id": req.OrderId,
  48. }
  49. err = p.Find(database.DB(), where)
  50. if err != nil && err != gorm.ErrRecordNotFound {
  51. return nil, errors.DataBaseError
  52. }
  53. if p.ID == 0 {
  54. return nil, errors.ErrRecordNotFound
  55. }
  56. if p.Status != 1 {
  57. return nil, status.Error(10003, "当前状态不能改价")
  58. }
  59. values := map[string]interface{}{
  60. "amount": req.Amount,
  61. "updated_at": now,
  62. }
  63. err = p.Update(database.DB(), where, values)
  64. if err != nil {
  65. return nil, errors.DataBaseError
  66. }
  67. return reply, nil
  68. }