repair_order_add.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/house"
  11. "property-garden/impl/v1/oss_utils"
  12. "property-garden/impl/v1/statistic"
  13. "property-garden/impl/v1/system_msg"
  14. dbmodel "property-garden/model"
  15. "property-garden/pb"
  16. pb_v1 "property-garden/pb/v1"
  17. "property-garden/utils"
  18. "time"
  19. "git.getensh.com/common/gopkgs/database"
  20. "git.getensh.com/common/gopkgs/logger"
  21. "go.uber.org/zap"
  22. "google.golang.org/grpc/status"
  23. )
  24. const (
  25. RepairOrderStatusCreate = 1
  26. RepairOrderStatusSend = 2
  27. RepairOrderStatusFinish = 3
  28. )
  29. const (
  30. RepairPipelineStatusCreate = 1
  31. RepairPipelineStatusSend = 2
  32. RepairPipelineStatusFinish = 3
  33. RepairPipelineStatusBack = 4
  34. RepairPipelineStatusResend = 5
  35. )
  36. var RepairOrderStatusM = map[int32]string{
  37. RepairOrderStatusCreate: "未派单",
  38. RepairOrderStatusSend: "已派单",
  39. RepairOrderStatusFinish: "已完结",
  40. }
  41. var RepairPipelineStatusM = map[int32]string{
  42. RepairPipelineStatusCreate: "未派单",
  43. RepairPipelineStatusSend: "已派单",
  44. RepairPipelineStatusFinish: "已完结",
  45. RepairPipelineStatusBack: "退单",
  46. RepairPipelineStatusResend: "转单",
  47. }
  48. type RepairOrderPipelineData struct {
  49. // 处理意见
  50. Feedback string `json:"feedback"`
  51. // 处理时间
  52. HandleTime string `json:"handle_time"`
  53. LastUid int64 `json:"last_uid"`
  54. CurrentUid int64 `json:"current_uid"`
  55. // 上级处理人是公司账户
  56. LastByCompany bool `json:"last_by_company"`
  57. Status int32 `json:"status"`
  58. }
  59. func checkRepairOrderAddParam(req *pb_v1.RepairOrderAddRequest) error {
  60. switch {
  61. case req.GardenId == 0:
  62. return status.Error(10003, "小区不能为空")
  63. case req.ClassId < 1:
  64. return status.Error(10003, "报修类型不能为空")
  65. case req.ApplyPeople == "":
  66. return status.Error(10003, "报修人不能为空")
  67. case req.ApplyPeoplePhone == "" || !utils.VerifyMobileFormat(req.ApplyPeoplePhone):
  68. return status.Error(10003, "手机格式错误")
  69. case req.ApplyContent == "":
  70. return status.Error(10003, "报修内容不能为空")
  71. }
  72. return nil
  73. }
  74. func getHouseInfo(id int64, gardenId int64) (houseInfo *pb_v1.HouseItem, err error) {
  75. // 获取房屋信息
  76. mreq := &pb_v1.HouseListRequest{
  77. GardenId: gardenId,
  78. HouseId: id,
  79. PageSize: -1,
  80. Page: -1,
  81. }
  82. mreply, err := house.HouseList(context.Background(), mreq)
  83. if err != nil {
  84. return nil, err
  85. }
  86. if len(mreply.List) == 0 {
  87. return nil, status.Error(10003, "房屋未录入")
  88. }
  89. return mreply.List[0], nil
  90. }
  91. type UserInfo struct {
  92. Name string
  93. Phone string
  94. }
  95. func getUserInfos(ids []int64, gardenId int64) (map[int64]UserInfo, error) {
  96. ret := map[int64]UserInfo{}
  97. if len(ids) == 0 {
  98. return ret, nil
  99. }
  100. mreq := pb_v1.UserListRequest{
  101. GardenId: gardenId,
  102. Ids: ids,
  103. PageSize: -1,
  104. Page: -1,
  105. }
  106. mreply, err := pb.System.UserList(context.Background(), &mreq)
  107. if err != nil {
  108. return ret, err
  109. }
  110. for _, v := range mreply.List {
  111. ret[v.Id] = UserInfo{Name: v.RealName, Phone: v.Phone}
  112. }
  113. for _, v := range ids {
  114. if _, ok := ret[v]; !ok {
  115. ret[v] = UserInfo{}
  116. }
  117. }
  118. return ret, nil
  119. }
  120. func RepairOrderAdd(ctx context.Context, req *pb_v1.RepairOrderAddRequest) (reply *pb_v1.RepairOrderAddReply, err error) {
  121. reply = &pb_v1.RepairOrderAddReply{}
  122. // 捕获各个task中的异常并返回给调用者
  123. defer func() {
  124. if r := recover(); r != nil {
  125. err = fmt.Errorf("%+v", r)
  126. e := &status.Status{}
  127. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  128. logger.Error("err",
  129. zap.String("system_err", err.Error()),
  130. zap.Stack("stacktrace"))
  131. }
  132. }
  133. }()
  134. // 参数检查
  135. err = checkRepairOrderAddParam(req)
  136. if err != nil {
  137. return nil, err
  138. }
  139. now := time.Now()
  140. dbname := utils.GetGardenDbName(req.GardenId)
  141. class := dbmodel.NewRepairClass(dbname)
  142. where := map[string]interface{}{
  143. "id": req.ClassId,
  144. }
  145. err = class.Find(database.DB(), where)
  146. if err != nil && err != gorm.ErrRecordNotFound {
  147. return nil, errors.DataBaseError
  148. }
  149. if class.ID == 0 {
  150. return nil, errors.ErrRecordNotFound
  151. }
  152. if class.Enable == 2 {
  153. return nil, status.Error(10003, "报修类型未启用")
  154. }
  155. pipeline := RepairOrderPipelineData{
  156. LastUid: req.LastUid,
  157. HandleTime: now.Format("2006-01-02 15:04:05"),
  158. Status: RepairPipelineStatusCreate,
  159. LastByCompany: req.ByCompany,
  160. }
  161. pipeArray := []RepairOrderPipelineData{pipeline}
  162. bytes, _ := json.Marshal(pipeArray)
  163. p := dbmodel.TRepairOrder{
  164. ClassId: req.ClassId,
  165. ApplyPeople: req.ApplyPeople,
  166. ApplyPeoplePhone: req.ApplyPeoplePhone,
  167. Appointment: time.Unix(req.Appointment, 0),
  168. ApplyContent: req.ApplyContent,
  169. ApplyPic: utils.StringJoin(req.ApplyPic, ";"),
  170. LastUid: req.LastUid,
  171. Status: RepairOrderStatusCreate,
  172. Pipeline: string(bytes),
  173. CreatedAt: now,
  174. UpdatedAt: now,
  175. LastUidIsCompany: req.ByCompany,
  176. HouseholdUid: req.HouseholdUid,
  177. }
  178. if req.HouseId > 0 {
  179. houseInfo, err := getHouseInfo(req.HouseId, req.GardenId)
  180. if err != nil {
  181. return nil, err
  182. }
  183. p.HouseId = req.HouseId
  184. p.BuildingId = houseInfo.BuildingId
  185. p.UnitId = houseInfo.UnitId
  186. p.HouseName = houseInfo.HouseName
  187. }
  188. p.SetTable(dbname)
  189. db := database.DB().Begin()
  190. err = p.Insert(db)
  191. if err != nil {
  192. db.Rollback()
  193. return nil, errors.DataBaseError
  194. }
  195. reply.Id = p.ID
  196. mreq := pb_v1.SystemMsgAddRequest{
  197. GardenId: req.GardenId,
  198. Content: "新的报事报修",
  199. Code: system_msg.SystemMsgCodeRepairOrder,
  200. }
  201. statisticReq := pb_v1.RepairStatisticSetRequest{
  202. HandleType: statistic.HandleTypeRepair,
  203. TotalIncrease: int64(1),
  204. GardenId: req.GardenId,
  205. }
  206. err = statistic.RepairStatisticSetWithDb(&statisticReq, db)
  207. if err != nil {
  208. db.Rollback()
  209. return nil, err
  210. }
  211. if err := oss_utils.OssObjAdd(req.ApplyPic, nil); err != nil {
  212. db.Rollback()
  213. return nil, err
  214. }
  215. db.Commit()
  216. system_msg.SystemMsgAdd(ctx, &mreq)
  217. return reply, nil
  218. }