order_add.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "property-system/errors"
  9. dbmodel "property-system/model"
  10. "property-system/parser"
  11. "property-system/pb"
  12. pb_v1 "property-system/pb/v1"
  13. "property-system/utils"
  14. "time"
  15. "git.getensh.com/common/gopkgs/database"
  16. "git.getensh.com/common/gopkgs/logger"
  17. "go.uber.org/zap"
  18. "google.golang.org/grpc/status"
  19. )
  20. func checkApplicationOrderAddParam(req *pb_v1.ApplicationOrderAddRequest) error {
  21. switch {
  22. case req.GardenId < 1:
  23. return status.Error(10003, "小区不能为空")
  24. case req.ApplicationId < 1:
  25. return status.Error(10003, "应用不能为空")
  26. }
  27. return nil
  28. }
  29. //
  30. func ApplicationOrderAdd(ctx context.Context, req *pb_v1.ApplicationOrderAddRequest) (reply *pb_v1.ApplicationOrderAddReply, err error) {
  31. reply = &pb_v1.ApplicationOrderAddReply{}
  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. err = checkApplicationOrderAddParam(req)
  45. if err != nil {
  46. return nil, err
  47. }
  48. now := time.Now()
  49. p := dbmodel.TApplicationOrder{}
  50. where := map[string]interface{}{
  51. "garden_id": req.GardenId,
  52. "application_id": req.ApplicationId,
  53. "status": 2,
  54. }
  55. count, err := p.Count(database.DB(), where, nil)
  56. if err != nil {
  57. return nil, errors.DataBaseError
  58. }
  59. if count > 0 {
  60. return nil, status.Error(10003, "应用已存在")
  61. }
  62. mreq := pb_v1.ApplicationInfoRequest{Id: req.ApplicationId}
  63. mreply, err := pb.Common.ApplicationInfo(ctx, &mreq)
  64. if err != nil {
  65. return nil, err
  66. }
  67. p.CreatedAt = now
  68. p.UpdatedAt = now
  69. p.ApplicationId = req.ApplicationId
  70. p.GardenId = req.GardenId
  71. p.Amount = mreply.Info.Price
  72. p.Cid = req.Cid
  73. p.Status = 1
  74. p.PayType = 1
  75. bytes, _ := json.Marshal(mreply.Info)
  76. p.ApplicationContent = string(bytes)
  77. db := database.DB().Begin()
  78. err = p.Insert(db)
  79. if err != nil {
  80. db.Rollback()
  81. return nil, errors.DataBaseError
  82. }
  83. // 价格为0 直接通过认证?
  84. /*if mreply.Info.Price == 0 {
  85. mreq := pb_v1.ApplicationOrderApproveRequest{OrderId: p.ID, Status: true, Feedback: "价格为零直接认证"}
  86. err = ApproveHandle(&mreq, db)
  87. if err != nil {
  88. db.Rollback()
  89. return nil, err
  90. }
  91. }*/
  92. db.Commit()
  93. reply.OrderId = p.ID
  94. reply.ApplicationDesc = mreply.Info.Desc
  95. reply.Amount = p.Amount
  96. reply.ApplicationName = mreply.Info.Name
  97. reply.PayType = 1
  98. reply.BankName = parser.Conf.Bank.BankName
  99. reply.BankAccount = parser.Conf.Bank.BankAccount
  100. reply.BankUserName = parser.Conf.Bank.BankUserName
  101. utils.RobotMsg(fmt.Sprintf("有新的应用订单,请关注"))
  102. return reply, nil
  103. }