repair_order_info.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. "strings"
  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 checkRepairOrderInfoParam(req *pb_v1.RepairOrderInfoRequest) error {
  20. switch {
  21. case req.GardenId == 0:
  22. return status.Error(10003, "小区不能为空")
  23. case req.Id == 0:
  24. return status.Error(10003, "id 不能为空")
  25. }
  26. return nil
  27. }
  28. func RepairOrderInfo(ctx context.Context, req *pb_v1.RepairOrderInfoRequest) (reply *pb_v1.RepairOrderInfoReply, err error) {
  29. reply = &pb_v1.RepairOrderInfoReply{}
  30. // 捕获各个task中的异常并返回给调用者
  31. defer func() {
  32. if r := recover(); r != nil {
  33. err = fmt.Errorf("%+v", r)
  34. e := &status.Status{}
  35. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  36. logger.Error("err",
  37. zap.String("system_err", err.Error()),
  38. zap.Stack("stacktrace"))
  39. }
  40. }
  41. }()
  42. // 参数检查
  43. err = checkRepairOrderInfoParam(req)
  44. if err != nil {
  45. return nil, err
  46. }
  47. dbname := utils.GetGardenDbName(req.GardenId)
  48. p := dbmodel.NewRepairOrder(dbname)
  49. where := map[string]interface{}{
  50. "id": req.Id,
  51. }
  52. err = p.Find(database.DB(), where)
  53. if err != nil && err != gorm.ErrRecordNotFound {
  54. return nil, errors.DataBaseError
  55. }
  56. if p.ID == 0 {
  57. return nil, errors.ErrRecordNotFound
  58. }
  59. // 处理流水线
  60. pipeline := []RepairOrderPipelineData{}
  61. json.Unmarshal([]byte(p.Pipeline), &pipeline)
  62. uids := []int64{}
  63. for _, v := range pipeline{
  64. if v.CurrentUid > 0 {
  65. uids = append(uids, v.CurrentUid)
  66. }
  67. if !v.LastByCompany && v.LastUid > 0 {
  68. uids = append(uids, v.LastUid)
  69. }
  70. }
  71. userInfos, err := getUserInfos(uids, req.GardenId)
  72. if err != nil {
  73. return nil, err
  74. }
  75. classInfos, err := getClassName([]int64{p.ClassId}, dbname)
  76. if err != nil {
  77. return nil, err
  78. }
  79. rspPipeline := make([]*pb_v1.RepairOrderPipelineData, len(pipeline))
  80. for i, v := range pipeline {
  81. item := &pb_v1.RepairOrderPipelineData{
  82. // 处理意见
  83. Feedback:v.Feedback,
  84. // 处理时间
  85. HandleTime:v.HandleTime,
  86. Status:RepairPipelineStatusM[v.Status],
  87. }
  88. switch v.Status {
  89. case RepairPipelineStatusCreate:
  90. item.User = p.ApplyPeople
  91. item.Feedback = p.ApplyContent
  92. item.Phone = p.ApplyPeoplePhone
  93. case RepairPipelineStatusSend:
  94. item.User = userInfos[v.CurrentUid].Name
  95. item.Phone = userInfos[v.CurrentUid].Phone
  96. case RepairPipelineStatusFinish:
  97. if !v.LastByCompany {
  98. item.User = userInfos[v.LastUid].Name
  99. item.Phone = userInfos[v.LastUid].Phone
  100. }
  101. case RepairPipelineStatusBack:
  102. if !v.LastByCompany {
  103. item.User = userInfos[v.LastUid].Name
  104. item.Phone = userInfos[v.LastUid].Phone
  105. }
  106. case RepairPipelineStatusResend:
  107. item.User = userInfos[v.CurrentUid].Name
  108. item.Phone = userInfos[v.CurrentUid].Phone
  109. }
  110. rspPipeline[i] = item
  111. }
  112. // 处理基本信息
  113. item := &pb_v1.RepairOrderItem{
  114. // 分类id
  115. ClassId: p.ClassId,
  116. // 报修人
  117. ApplyPeople: p.ApplyPeople,
  118. // 报修人电话
  119. ApplyPeoplePhone: p.ApplyPeoplePhone,
  120. // 预约时间
  121. Appointment: p.Appointment.Format("2006-01-02 15:04:05"),
  122. // 房屋id
  123. HouseId: p.HouseId,
  124. // 处理人
  125. CurrentUser: userInfos[p.CurrentUid].Name,
  126. // 报修内容
  127. ApplyContent: p.ApplyContent,
  128. // 报修图片
  129. ApplyPic: strings.Split(p.ApplyPic, ";"),
  130. // 回访内容
  131. ReturnVisitContent: p.ReturnVisitContent,
  132. // 回访满意度
  133. ReturnVisitLevel: p.ReturnVisitLevel,
  134. // 楼栋id
  135. BuildingId: p.BuildingId,
  136. // 单元id
  137. UnitId: p.UnitId,
  138. // 房屋全称
  139. HouseName: p.HouseName,
  140. // 报修时间
  141. CreatedAt: p.CreatedAt.Format("2006-01-02 15:04:05"),
  142. // 维修时间
  143. RepairTime: "",
  144. // 分类名称
  145. ClassName: classInfos[p.ClassId].Name,
  146. Id: p.ID,
  147. Status:p.Status,
  148. Area:classInfos[p.ClassId].Area,
  149. RepairPic:strings.Split(p.RepairPic, ";"),
  150. }
  151. if !p.LastUidIsCompany {
  152. item.LastUser = userInfos[p.LastUid].Name
  153. }
  154. if p.Status == RepairOrderStatusFinish {
  155. item.RepairTime = p.RepairTime.Format("2006-01-02 15:04:05")
  156. }
  157. if p.Appointment.Unix() < 1136131200 {
  158. item.Appointment = ""
  159. }
  160. reply.List = rspPipeline
  161. reply.BaseInfo = item
  162. return reply, nil
  163. }