apply.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package house
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "property-household/errors"
  10. "property-household/impl/v1/oss_utils"
  11. dbmodel "property-household/model"
  12. "property-household/pb"
  13. pb_v1 "property-household/pb/v1"
  14. "property-household/utils"
  15. "strings"
  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. // 房屋状态 未入住 已入住
  23. const (
  24. HouseStatusWait = 1
  25. HouseStatusUsed = 2
  26. //HouseStatusRented = 3
  27. )
  28. // 认证状态
  29. const (
  30. HouseholdApproveStatusWait = 1
  31. HouseholdApproveStatusSuccess = 2
  32. HouseholdApproveStatusFail = 3
  33. HouseholdApproveStatusFailAndWait = 4
  34. )
  35. const (
  36. SystemMsgCodeHouseholdBindHouse = "1"
  37. SystemMsgCodeRepairOrder = "2"
  38. SystemMsgCodeRepairOrderSended = "3"
  39. SystemMsgCodeSuggestionOrder = "4"
  40. SystemMsgCodeSuggestionOrderSended = "5"
  41. SystemMsgCodeRent = "6"
  42. SystemMsgCodeRentAppointment = "7"
  43. )
  44. func checkHouseholdApplyParam(req *pb_v1.HouseholdApplyRequest) error {
  45. switch {
  46. case req.Phone == "":
  47. //return status.Error(10003, "手机号不能为空")
  48. case req.HouseId < 1:
  49. return status.Error(10003, "房屋不能为空")
  50. case req.Uid < 1:
  51. return status.Error(10003, "用户不能为空")
  52. case req.UserType < 1:
  53. return status.Error(10003, "业主类型不能为空")
  54. case req.IdType < 1:
  55. //return status.Error(10003, "证件类型不能为空,请先完成实名认证")
  56. case req.IdNumber == "":
  57. //return status.Error(10003, "证件号不能为空,请先完成实名认证")
  58. case req.Name == "":
  59. //return status.Error(10003, "姓名不能为空")
  60. case req.GardenId < 1:
  61. return status.Error(10003, "小区不能为空")
  62. }
  63. return nil
  64. }
  65. func GetHouseInfo(id int64, gardenId int64) (data *pb_v1.HouseInfoReply, err error) {
  66. mreq := &pb_v1.HouseInfoRequest{
  67. HouseId: id,
  68. GardenId: gardenId,
  69. }
  70. return pb.Garden.HouseInfo(context.Background(), mreq)
  71. }
  72. //
  73. func HouseholdApply(ctx context.Context, req *pb_v1.HouseholdApplyRequest) (reply *pb_v1.HouseholdApplyReply, err error) {
  74. reply = &pb_v1.HouseholdApplyReply{}
  75. // 捕获各个task中的异常并返回给调用者
  76. defer func() {
  77. if r := recover(); r != nil {
  78. err = fmt.Errorf("%+v", r)
  79. e := &status.Status{}
  80. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  81. logger.Error("err",
  82. zap.String("system_err", err.Error()),
  83. zap.Stack("stacktrace"))
  84. }
  85. }
  86. }()
  87. err = checkHouseholdApplyParam(req)
  88. if err != nil {
  89. return nil, err
  90. }
  91. info, err := GetHouseInfo(req.HouseId, req.GardenId)
  92. if err != nil {
  93. return nil, err
  94. }
  95. idNumber := ""
  96. if req.IdNumber != "" {
  97. idNumber = utils.CertEncrypt(idNumber)
  98. }
  99. // 检查用户
  100. user := dbmodel.TUser{}
  101. where := map[string]interface{}{
  102. "id": req.Uid,
  103. }
  104. err = user.Find(database.DB(), where)
  105. if err != nil && err != gorm.ErrRecordNotFound {
  106. return nil, errors.DataBaseError
  107. }
  108. if user.ID == 0 {
  109. return nil, errors.ErrRecordNotFound
  110. }
  111. // 检查是否与已审核的记录重复
  112. houseApproved := dbmodel.THouseApproved{}
  113. where = map[string]interface{}{
  114. "uid": req.Uid,
  115. "garden_id": req.GardenId,
  116. "house_id": req.HouseId,
  117. }
  118. count, err := houseApproved.Count(database.DB(), where, nil)
  119. if err != nil {
  120. return nil, errors.DataBaseError
  121. }
  122. if count > 0 {
  123. return nil, errors.ErrDuplicate
  124. }
  125. if user.Phone == "" || user.IdType < 1 || user.RealName == "" || user.IdNumber == "" {
  126. return nil, status.Error(10003, "请先实名认证")
  127. }
  128. now := time.Now()
  129. house := dbmodel.THouse{
  130. GardenId: info.GardenId,
  131. Uid: req.Uid,
  132. ApproveStatus: HouseholdApproveStatusWait,
  133. CreatedAt: now,
  134. UpdatedAt: now,
  135. UserType: req.UserType,
  136. HouseId: req.HouseId,
  137. BuildingNumber: info.BuildingNumber,
  138. UnitNumber: info.UnitNumber,
  139. HouseNumber: info.HouseNumber,
  140. UniqFlag: 1,
  141. BuildingId: info.BuildingId,
  142. UnitId: info.UnitId,
  143. IdType: user.IdType,
  144. IdNumber: user.IdNumber,
  145. Phone: user.Phone,
  146. Name: user.RealName,
  147. ApprovedAt: now,
  148. OpenId: user.OpenId,
  149. }
  150. if len(req.Appendix) > 0 {
  151. house.Appendix = strings.Join(req.Appendix, ";")
  152. }
  153. db := database.DB().Begin()
  154. err = house.Insert(db)
  155. if err != nil {
  156. db.Rollback()
  157. msg := strings.ToLower(err.Error())
  158. if strings.Contains(msg, "duplicate") {
  159. return nil, errors.ErrDuplicate
  160. }
  161. return nil, errors.DataBaseError
  162. }
  163. reply.Id = house.ID
  164. statisticReq := pb_v1.RepairStatisticSetRequest{
  165. HandleType: 3,
  166. TotalIncrease: int64(1),
  167. GardenId: req.GardenId,
  168. }
  169. if err := oss_utils.OssObjAdd(req.Appendix, nil); err != nil {
  170. db.Rollback()
  171. return nil, err
  172. }
  173. _, err = pb.Garden.RepairStatisticSet(ctx, &statisticReq)
  174. if err != nil {
  175. db.Rollback()
  176. return nil, err
  177. }
  178. db.Commit()
  179. mreq := pb_v1.SystemMsgAddRequest{
  180. GardenId: req.GardenId,
  181. Content: "新的住户入住申请",
  182. Code: SystemMsgCodeHouseholdBindHouse,
  183. }
  184. pb.Garden.SystemMsgAdd(ctx, &mreq)
  185. return reply, nil
  186. }