suggestion_order_add.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. "property-garden/errors"
  9. "property-garden/impl/v1/oss_utils"
  10. "property-garden/impl/v1/statistic"
  11. "property-garden/impl/v1/system_msg"
  12. dbmodel "property-garden/model"
  13. "property-garden/pb"
  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. const (
  23. SuggestionOrderStatusCreate = 1
  24. SuggestionOrderStatusSend = 2
  25. SuggestionOrderStatusFinish = 3
  26. )
  27. const (
  28. SuggestionPipelineStatusCreate = 1
  29. SuggestionPipelineStatusSend = 2
  30. SuggestionPipelineStatusFinish = 3
  31. SuggestionPipelineStatusBack = 4
  32. SuggestionPipelineStatusResend = 5
  33. )
  34. var SuggestionOrderStatusM = map[int32]string{
  35. SuggestionOrderStatusCreate: "未派单",
  36. SuggestionOrderStatusSend: "已派单",
  37. SuggestionOrderStatusFinish: "已完结",
  38. }
  39. var SuggestionPipelineStatusM = map[int32]string{
  40. SuggestionPipelineStatusCreate: "未派单",
  41. SuggestionPipelineStatusSend: "已派单",
  42. SuggestionPipelineStatusFinish: "已完结",
  43. SuggestionPipelineStatusBack: "退单",
  44. SuggestionPipelineStatusResend: "转单",
  45. }
  46. type SuggestionOrderPipelineData struct {
  47. // 处理意见
  48. Feedback string `json:"feedback"`
  49. // 处理时间
  50. HandleTime string `json:"handle_time"`
  51. LastUid int64 `json:"last_uid"`
  52. CurrentUid int64 `json:"current_uid"`
  53. // 上级处理人是公司账户
  54. LastByCompany bool `json:"last_by_company"`
  55. Status int32 `json:"status"`
  56. }
  57. func checkSuggestionOrderAddParam(req *pb_v1.SuggestionOrderAddRequest) error {
  58. switch {
  59. case req.GardenId == 0:
  60. return status.Error(10003, "小区不能为空")
  61. case req.SuggestionType < 1:
  62. return status.Error(10003, "投诉类型不能为空")
  63. case req.ApplyPeople == "":
  64. return status.Error(10003, "投诉人不能为空")
  65. case req.ApplyPeoplePhone != "":
  66. if !utils.VerifyMobileFormat(req.ApplyPeoplePhone) {
  67. return status.Error(10003, "手机格式错误")
  68. }
  69. case req.ApplyContent == "":
  70. return status.Error(10003, "投诉内容不能为空")
  71. }
  72. return nil
  73. }
  74. type UserInfo struct {
  75. Name string
  76. Phone string
  77. }
  78. func getUserInfos(ids []int64, gardenId int64) (map[int64]UserInfo, error) {
  79. ret := map[int64]UserInfo{}
  80. if len(ids) == 0 {
  81. return ret, nil
  82. }
  83. mreq := pb_v1.UserListRequest{
  84. GardenId: gardenId,
  85. Ids: ids,
  86. PageSize: -1,
  87. Page: -1,
  88. }
  89. mreply, err := pb.System.UserList(context.Background(), &mreq)
  90. if err != nil {
  91. return ret, err
  92. }
  93. for _, v := range mreply.List {
  94. ret[v.Id] = UserInfo{Name: v.RealName, Phone: v.Phone}
  95. }
  96. for _, v := range ids {
  97. if _, ok := ret[v]; !ok {
  98. ret[v] = UserInfo{}
  99. }
  100. }
  101. return ret, nil
  102. }
  103. func SuggestionOrderAdd(ctx context.Context, req *pb_v1.SuggestionOrderAddRequest) (reply *pb_v1.SuggestionOrderAddReply, err error) {
  104. reply = &pb_v1.SuggestionOrderAddReply{}
  105. // 捕获各个task中的异常并返回给调用者
  106. defer func() {
  107. if r := recover(); r != nil {
  108. err = fmt.Errorf("%+v", r)
  109. e := &status.Status{}
  110. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  111. logger.Error("err",
  112. zap.String("system_err", err.Error()),
  113. zap.Stack("stacktrace"))
  114. }
  115. }
  116. }()
  117. // 参数检查
  118. err = checkSuggestionOrderAddParam(req)
  119. if err != nil {
  120. return nil, err
  121. }
  122. now := time.Now()
  123. dbname := utils.GetGardenDbName(req.GardenId)
  124. pipeline := SuggestionOrderPipelineData{
  125. LastUid: req.LastUid,
  126. HandleTime: now.Format("2006-01-02 15:04:05"),
  127. Status: SuggestionPipelineStatusCreate,
  128. LastByCompany: req.ByCompany,
  129. }
  130. pipeArray := []SuggestionOrderPipelineData{pipeline}
  131. bytes, _ := json.Marshal(pipeArray)
  132. p := dbmodel.TSuggestionOrder{
  133. SuggestionType: req.SuggestionType,
  134. ApplyPeople: req.ApplyPeople,
  135. ApplyPeoplePhone: req.ApplyPeoplePhone,
  136. ApplyContent: req.ApplyContent,
  137. ApplyPic: utils.StringJoin(req.ApplyPic, ";"),
  138. LastUid: req.LastUid,
  139. Status: SuggestionOrderStatusCreate,
  140. Pipeline: string(bytes),
  141. CreatedAt: now,
  142. UpdatedAt: now,
  143. LastUidIsCompany: req.ByCompany,
  144. HouseholdUid: req.HouseholdUid,
  145. }
  146. p.SetTable(dbname)
  147. db := database.DB().Begin()
  148. err = p.Insert(db)
  149. if err != nil {
  150. db.Rollback()
  151. return nil, errors.DataBaseError
  152. }
  153. reply.Id = p.ID
  154. mreq := pb_v1.SystemMsgAddRequest{
  155. GardenId: req.GardenId,
  156. Content: "新的投诉建议",
  157. Code: system_msg.SystemMsgCodeSuggestionOrder,
  158. }
  159. statisticReq := pb_v1.RepairStatisticSetRequest{
  160. HandleType: statistic.HandleTypeSuggestion,
  161. TotalIncrease: int64(1),
  162. GardenId: req.GardenId,
  163. }
  164. err = statistic.RepairStatisticSetWithDb(&statisticReq, db)
  165. if err != nil {
  166. db.Rollback()
  167. return nil, err
  168. }
  169. if err := oss_utils.OssObjAdd(req.ApplyPic, nil); err != nil {
  170. db.Rollback()
  171. return nil, err
  172. }
  173. db.Commit()
  174. system_msg.SystemMsgAdd(ctx, &mreq)
  175. return reply, nil
  176. }