suggestion_order_back.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package suggestion
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "property-garden/errors"
  10. dbmodel "property-garden/model"
  11. pb_v1 "property-garden/pb/v1"
  12. "property-garden/utils"
  13. "time"
  14. "git.getensh.com/common/gopkgs/database"
  15. "git.getensh.com/common/gopkgs/logger"
  16. "go.uber.org/zap"
  17. "google.golang.org/grpc/status"
  18. )
  19. func checkSuggestionOrderBackParam(req *pb_v1.SuggestionOrderBackRequest) error {
  20. switch {
  21. case req.Id == 0:
  22. return status.Error(10003, "id 不能为空")
  23. case req.GardenId == 0:
  24. return status.Error(10003, "小区不能为空")
  25. case req.LastUid < 1:
  26. return status.Error(10003, "当前操作人不能为空")
  27. //case req.NextUid < 1:
  28. // return status.Error(10003, "下级操作人不能为空")
  29. case req.Feedback == "":
  30. return status.Error(10003, "退单理由不能为空")
  31. }
  32. return nil
  33. }
  34. func SuggestionOrderBack(ctx context.Context, req *pb_v1.SuggestionOrderBackRequest) (reply *pb_v1.SuggestionOrderBackReply, err error) {
  35. reply = &pb_v1.SuggestionOrderBackReply{}
  36. // 捕获各个task中的异常并返回给调用者
  37. defer func() {
  38. if r := recover(); r != nil {
  39. err = fmt.Errorf("%+v", r)
  40. e := &status.Status{}
  41. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  42. logger.Error("err",
  43. zap.String("system_err", err.Error()),
  44. zap.Stack("stacktrace"))
  45. }
  46. }
  47. }()
  48. // 参数检查
  49. err = checkSuggestionOrderBackParam(req)
  50. if err != nil {
  51. return nil, err
  52. }
  53. dbname := utils.GetGardenDbName(req.GardenId)
  54. // 获取旧数据
  55. old := dbmodel.NewSuggestionOrder(dbname)
  56. where := map[string]interface{}{
  57. "id":req.Id,
  58. }
  59. err = old.Find(database.DB(), where)
  60. if err != nil && err != gorm.ErrRecordNotFound {
  61. return nil, errors.DataBaseError
  62. }
  63. if old.ID == 0 {
  64. return nil, errors.ErrRecordNotFound
  65. }
  66. if old.Status != SuggestionOrderStatusSend {
  67. return nil, status.Error(10003, "当前状态不支持该操作")
  68. }
  69. // 工单流水线
  70. pipelineArray := []SuggestionOrderPipelineData{}
  71. json.Unmarshal([]byte(old.Pipeline), &pipelineArray)
  72. now := time.Now()
  73. pipeline := SuggestionOrderPipelineData{LastUid:req.LastUid,
  74. CurrentUid:0, Status:SuggestionPipelineStatusBack,
  75. LastByCompany:req.ByCompany,
  76. Feedback:req.Feedback, HandleTime:now.Format("2006-01-02 15:04:05")}
  77. pipelineArray = append(pipelineArray, pipeline)
  78. bytes, _ := json.Marshal(pipelineArray)
  79. // 更新新数据
  80. repairOrder := dbmodel.NewSuggestionOrder(dbname)
  81. values := map[string]interface{}{
  82. "status":SuggestionOrderStatusCreate,
  83. "last_uid":req.LastUid,
  84. "current_uid":0,
  85. "feedback": req.Feedback,
  86. "updated_at":now,
  87. "pipeline":string(bytes),
  88. "last_uid_is_company":req.ByCompany,
  89. }
  90. where = map[string]interface{}{
  91. "id":req.Id,
  92. }
  93. err = repairOrder.Update(database.DB(), where, values)
  94. if err != nil {
  95. return nil, errors.DataBaseError
  96. }
  97. return reply, nil
  98. }