repair_order_send.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package repair
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "property-garden/errors"
  10. "property-garden/impl/v1/public_msg"
  11. "property-garden/impl/v1/system_msg"
  12. dbmodel "property-garden/model"
  13. pb_v1 "property-garden/pb/v1"
  14. "property-garden/utils"
  15. "time"
  16. "git.getensh.com/common/gopkgs/database"
  17. "git.getensh.com/common/gopkgs/logger"
  18. "go.uber.org/zap"
  19. "google.golang.org/grpc/status"
  20. )
  21. func checkRepairOrderSendParam(req *pb_v1.RepairOrderSendRequest) error {
  22. switch {
  23. case req.Id == 0:
  24. return status.Error(10003, "id 不能为空")
  25. case req.GardenId == 0:
  26. return status.Error(10003, "小区不能为空")
  27. case req.CurrentUid < 1:
  28. return status.Error(10003, "当前操作人不能为空")
  29. case req.LastUid < 1:
  30. return status.Error(10003, "上级操作人不能为空")
  31. }
  32. return nil
  33. }
  34. func RepairOrderSend(ctx context.Context, req *pb_v1.RepairOrderSendRequest) (reply *pb_v1.RepairOrderSendReply, err error) {
  35. reply = &pb_v1.RepairOrderSendReply{}
  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 = checkRepairOrderSendParam(req)
  50. if err != nil {
  51. return nil, err
  52. }
  53. dbname := utils.GetGardenDbName(req.GardenId)
  54. // 获取旧数据
  55. old := dbmodel.NewRepairOrder(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 == RepairOrderStatusFinish {
  67. return nil, status.Error(10003, "工单已完结,不支持该操作")
  68. }
  69. // 工单流水线
  70. pipelineArray := []RepairOrderPipelineData{}
  71. json.Unmarshal([]byte(old.Pipeline), &pipelineArray)
  72. now := time.Now()
  73. pipeline := RepairOrderPipelineData{LastUid: req.LastUid,
  74. CurrentUid: req.CurrentUid, Status: RepairPipelineStatusSend,
  75. LastByCompany: req.ByCompany,
  76. Feedback: req.Feedback, HandleTime: now.Format("2006-01-02 15:04:05")}
  77. if old.Status == RepairOrderStatusSend {
  78. pipeline.Status = RepairPipelineStatusResend
  79. if req.Feedback == "" {
  80. return nil, status.Error(10003, "转单说明不能为空")
  81. }
  82. }
  83. pipelineArray = append(pipelineArray, pipeline)
  84. bytes, _ := json.Marshal(pipelineArray)
  85. // 更新新数据
  86. repairOrder := dbmodel.NewRepairOrder(dbname)
  87. values := map[string]interface{}{
  88. "status": RepairOrderStatusSend,
  89. "current_uid": req.CurrentUid,
  90. "last_uid": req.LastUid,
  91. "feedback": req.Feedback,
  92. "updated_at": now,
  93. "pipeline": string(bytes),
  94. "last_uid_is_company": req.ByCompany,
  95. }
  96. where = map[string]interface{}{
  97. "id": req.Id,
  98. }
  99. err = repairOrder.Update(database.DB(), where, values)
  100. if err != nil {
  101. return nil, errors.DataBaseError
  102. }
  103. if old.HouseholdUid > 0 {
  104. serviceInfo := public_msg.ServiceInfo{
  105. Name: "报事报修",
  106. State: "已派单",
  107. }
  108. public_msg.SendMsgWx(old.HouseholdUid, req.GardenId, serviceInfo)
  109. }
  110. mreq := pb_v1.SystemMsgAddRequest{
  111. GardenId: req.GardenId,
  112. Content: "报事报修派单",
  113. Code: system_msg.SystemMsgCodeRepairOrderSended,
  114. Uid: req.CurrentUid,
  115. }
  116. system_msg.SystemMsgAdd(ctx, &mreq)
  117. return reply, nil
  118. }