repair_order_visit_add.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. 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 checkRepairOrderReturnVisitParam(req *pb_v1.RepairOrderReturnVisitRequest) 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.ReturnVisitLevel < 1:
  26. return status.Error(10003, "满意度不能为空")
  27. }
  28. return nil
  29. }
  30. func RepairOrderReturnVisit(ctx context.Context, req *pb_v1.RepairOrderReturnVisitRequest) (reply *pb_v1.RepairOrderReturnVisitReply, err error) {
  31. reply = &pb_v1.RepairOrderReturnVisitReply{}
  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. // 参数检查
  45. err = checkRepairOrderReturnVisitParam(req)
  46. if err != nil {
  47. return nil, err
  48. }
  49. dbname := utils.GetGardenDbName(req.GardenId)
  50. // 获取旧数据
  51. old := dbmodel.NewRepairOrder(dbname)
  52. where := map[string]interface{}{
  53. "id": req.Id,
  54. }
  55. if req.HouseholdUid > 0 {
  56. where["household_uid"] = req.HouseholdUid
  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 != RepairOrderStatusFinish {
  66. return nil, status.Error(10003, "工单未完结,不支持该操作")
  67. }
  68. // 工单流水线
  69. pipelineArray := []pb_v1.RepairOrderPipelineData{}
  70. json.Unmarshal([]byte(old.Pipeline), &pipelineArray)
  71. now := time.Now()
  72. // 更新新数据
  73. repairOrder := dbmodel.NewRepairOrder(dbname)
  74. values := map[string]interface{}{
  75. "return_visit_level": req.ReturnVisitLevel,
  76. "return_visit_content": req.ReturnVisitContent,
  77. "updated_at": now,
  78. }
  79. where = map[string]interface{}{
  80. "id": req.Id,
  81. }
  82. err = repairOrder.Update(database.DB(), where, values)
  83. if err != nil {
  84. return nil, errors.DataBaseError
  85. }
  86. return reply, nil
  87. }