suggestion_order_finish.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. "property-garden/impl/v1/oss_utils"
  11. "property-garden/impl/v1/public_msg"
  12. "property-garden/impl/v1/statistic"
  13. dbmodel "property-garden/model"
  14. pb_v1 "property-garden/pb/v1"
  15. "property-garden/utils"
  16. "time"
  17. "git.getensh.com/common/gopkgs/database"
  18. "git.getensh.com/common/gopkgs/logger"
  19. "go.uber.org/zap"
  20. "google.golang.org/grpc/status"
  21. )
  22. func checkSuggestionOrderFinishParam(req *pb_v1.SuggestionOrderFinishRequest) error {
  23. switch {
  24. case req.Id == 0:
  25. return status.Error(10003, "id 不能为空")
  26. case req.GardenId == 0:
  27. return status.Error(10003, "小区不能为空")
  28. case req.LastUid < 1:
  29. return status.Error(10003, "当前操作人不能为空")
  30. }
  31. return nil
  32. }
  33. func SuggestionOrderFinish(ctx context.Context, req *pb_v1.SuggestionOrderFinishRequest) (reply *pb_v1.SuggestionOrderFinishReply, err error) {
  34. reply = &pb_v1.SuggestionOrderFinishReply{}
  35. // 捕获各个task中的异常并返回给调用者
  36. defer func() {
  37. if r := recover(); r != nil {
  38. err = fmt.Errorf("%+v", r)
  39. e := &status.Status{}
  40. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  41. logger.Error("err",
  42. zap.String("system_err", err.Error()),
  43. zap.Stack("stacktrace"))
  44. }
  45. }
  46. }()
  47. // 参数检查
  48. err = checkSuggestionOrderFinishParam(req)
  49. if err != nil {
  50. return nil, err
  51. }
  52. dbname := utils.GetGardenDbName(req.GardenId)
  53. // 获取旧数据
  54. old := dbmodel.NewSuggestionOrder(dbname)
  55. where := map[string]interface{}{
  56. "id": req.Id,
  57. }
  58. err = old.Find(database.DB(), where)
  59. if err != nil && err != gorm.ErrRecordNotFound {
  60. return nil, errors.DataBaseError
  61. }
  62. if old.ID == 0 {
  63. return nil, errors.ErrRecordNotFound
  64. }
  65. if old.Status != SuggestionOrderStatusSend {
  66. return nil, status.Error(10003, "当前状态不支持该操作")
  67. }
  68. // 工单流水线
  69. pipelineArray := []SuggestionOrderPipelineData{}
  70. json.Unmarshal([]byte(old.Pipeline), &pipelineArray)
  71. now := time.Now()
  72. pipeline := SuggestionOrderPipelineData{LastUid: req.LastUid,
  73. Status: SuggestionPipelineStatusFinish,
  74. LastByCompany: req.ByCompany,
  75. Feedback: req.Feedback, HandleTime: now.Format("2006-01-02 15:04:05")}
  76. pipelineArray = append(pipelineArray, pipeline)
  77. bytes, _ := json.Marshal(pipelineArray)
  78. // 更新新数据
  79. repairOrder := dbmodel.NewSuggestionOrder(dbname)
  80. values := map[string]interface{}{
  81. "status": SuggestionOrderStatusFinish,
  82. "last_uid": req.LastUid,
  83. "feedback": req.Feedback,
  84. "updated_at": now,
  85. "pipeline": string(bytes),
  86. "handle_pic": utils.StringJoin(req.HandlePic, ";"),
  87. "last_uid_is_company": req.ByCompany,
  88. "current_uid": 0,
  89. }
  90. if !req.ByCompany {
  91. values["current_uid"] = req.LastUid
  92. }
  93. where = map[string]interface{}{
  94. "id": req.Id,
  95. }
  96. db := database.DB().Begin()
  97. err = repairOrder.Update(db, where, values)
  98. if err != nil {
  99. db.Rollback()
  100. return nil, errors.DataBaseError
  101. }
  102. statisticReq := pb_v1.RepairStatisticSetRequest{
  103. HandleType: statistic.HandleTypeSuggestion,
  104. FinishIncrease: 1,
  105. GardenId: req.GardenId,
  106. }
  107. err = statistic.RepairStatisticSetWithDb(&statisticReq, db)
  108. if err != nil {
  109. db.Rollback()
  110. return nil, err
  111. }
  112. if err := oss_utils.OssObjAdd(req.HandlePic, nil); err != nil {
  113. db.Rollback()
  114. return nil, err
  115. }
  116. db.Commit()
  117. if old.HouseholdUid > 0 {
  118. serviceInfo := public_msg.ServiceInfo{
  119. Name: "投诉建议",
  120. State: "已完成",
  121. }
  122. public_msg.SendMsgWx(old.HouseholdUid, req.GardenId, serviceInfo)
  123. }
  124. return reply, nil
  125. }