appointment_add.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package house_rent
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "property-household/errors"
  10. dbmodel "property-household/model"
  11. "property-household/pb"
  12. pb_v1 "property-household/pb/v1"
  13. "strings"
  14. "time"
  15. "git.getensh.com/common/gopkgs/database"
  16. "git.getensh.com/common/gopkgs/logger"
  17. "go.uber.org/zap"
  18. "google.golang.org/grpc/status"
  19. )
  20. func checkHouseRentAppointmentAddParam(req *pb_v1.HouseRentAppointmentAddRequest) error {
  21. switch {
  22. case req.GardenId < 1:
  23. return status.Error(10003, "小区不能为空")
  24. case req.Uid < 1:
  25. return status.Error(10003, "用户不能为空")
  26. case req.Name == "":
  27. return status.Error(10003, "名字不能为空")
  28. case req.RentId < 1:
  29. return status.Error(10003, "租房信息不能为空")
  30. case req.Phone == "":
  31. return status.Error(10003, "手机号不能为空")
  32. case req.Gender == 0:
  33. return status.Error(10003, "性别不能为空")
  34. }
  35. return nil
  36. }
  37. const (
  38. _ = iota
  39. HouseRentAppointmentWait
  40. HouseRentAppointmentAccess
  41. HouseRentAppointmentRefruse
  42. HouseRentAppointmentNotVisit
  43. HouseRentAppointmentVisited
  44. HouseRentAppointmentExpired
  45. )
  46. var HouseRentAppointmentStatusM = map[int]string{
  47. HouseRentAppointmentWait: "等待处理",
  48. HouseRentAppointmentAccess: "已受理",
  49. HouseRentAppointmentRefruse: "无法处理",
  50. HouseRentAppointmentNotVisit: "未到访",
  51. HouseRentAppointmentVisited: "已到访",
  52. }
  53. //
  54. func HouseRentAppointmentAdd(ctx context.Context, req *pb_v1.HouseRentAppointmentAddRequest) (reply *pb_v1.HouseRentAppointmentAddReply, err error) {
  55. reply = &pb_v1.HouseRentAppointmentAddReply{}
  56. // 捕获各个task中的异常并返回给调用者
  57. defer func() {
  58. if r := recover(); r != nil {
  59. err = fmt.Errorf("%+v", r)
  60. e := &status.Status{}
  61. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  62. logger.Error("err",
  63. zap.String("system_err", err.Error()),
  64. zap.Stack("stacktrace"))
  65. }
  66. }
  67. }()
  68. // 参数检查
  69. err = checkHouseRentAppointmentAddParam(req)
  70. if err != nil {
  71. return nil, err
  72. }
  73. now := time.Now()
  74. rent := dbmodel.THouseRent{}
  75. where := map[string]interface{}{
  76. "id": req.RentId,
  77. }
  78. err = rent.Find(database.DB(), where)
  79. if err != nil && err != gorm.ErrRecordNotFound {
  80. return nil, errors.DataBaseError
  81. }
  82. if rent.ID == 0 {
  83. return nil, errors.ErrRecordNotFound
  84. }
  85. p := &dbmodel.THouseRentAppointment{
  86. Uid: req.Uid,
  87. Name: req.Name,
  88. Phone: req.Phone,
  89. AppointmentTime: req.AppointmentTime,
  90. UpdatedAt: now,
  91. CreatedAt: now,
  92. Gender: req.Gender,
  93. HouseName: rent.HouseName,
  94. RentId: req.RentId,
  95. Area: rent.RoomArea,
  96. Layer: rent.Layer,
  97. Status: HouseRentAppointmentWait,
  98. GardenId: req.GardenId,
  99. }
  100. err = p.Insert(database.DB())
  101. if err != nil {
  102. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  103. return nil, status.Error(10003, "不能重复预约")
  104. }
  105. return nil, errors.DataBaseError
  106. }
  107. mreq := pb_v1.SystemMsgAddRequest{
  108. GardenId: req.GardenId,
  109. Content: "新的租房看房预约",
  110. Code: SystemMsgCodeRentAppointment,
  111. }
  112. pb.Garden.SystemMsgAdd(ctx, &mreq)
  113. reply.Id = p.ID
  114. return reply, nil
  115. }