household_sync.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package household
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "property-garden/errors"
  10. "property-garden/impl/v1/statistic"
  11. dbmodel "property-garden/model"
  12. "property-garden/pb"
  13. pb_v1 "property-garden/pb/v1"
  14. "property-garden/utils"
  15. "time"
  16. "git.getensh.com/common/gopkgs/database"
  17. "git.getensh.com/common/gopkgs/logger"
  18. "go.uber.org/zap"
  19. "google.golang.org/grpc/status"
  20. )
  21. func checkHouseholdSyncParam(req *pb_v1.HouseholdSyncRequest) error {
  22. if req.HouseId < 1 ||
  23. req.UnitNumber < 1 ||
  24. req.HouseNumber == "" ||
  25. req.BuildingNumber == "" ||
  26. req.GardenId < 1 ||
  27. req.BuildingId < 1 ||
  28. req.Uid < 1 ||
  29. req.UserType < 1 {
  30. return errors.ParamsError
  31. }
  32. return nil
  33. }
  34. //
  35. func HouseholdSync(ctx context.Context, req *pb_v1.HouseholdSyncRequest) (reply *pb_v1.HouseholdSyncReply, err error) {
  36. reply = &pb_v1.HouseholdSyncReply{}
  37. // 捕获各个task中的异常并返回给调用者
  38. defer func() {
  39. if r := recover(); r != nil {
  40. err = fmt.Errorf("%+v", r)
  41. e := &status.Status{}
  42. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  43. logger.Error("err",
  44. zap.String("system_err", err.Error()),
  45. zap.Stack("stacktrace"))
  46. }
  47. }
  48. }()
  49. err = checkHouseholdSyncParam(req)
  50. if err != nil {
  51. return nil, err
  52. }
  53. dbname := utils.GetGardenDbName(req.GardenId)
  54. // 更新房屋入驻状态
  55. house := dbmodel.NewHouse(dbname)
  56. where := map[string]interface{}{
  57. "id": req.HouseId,
  58. }
  59. values := map[string]interface{}{
  60. "status": 2,
  61. }
  62. db := database.DB().Begin()
  63. defer func() {
  64. if err != nil {
  65. db.Rollback()
  66. }
  67. }()
  68. err = house.Update(db, where, values)
  69. if err != nil {
  70. return nil, errors.DataBaseError
  71. }
  72. // 插入房屋申请记录
  73. p := dbmodel.THouseApprovedGarden{
  74. OpenId: req.OpenId,
  75. Uid: req.Uid,
  76. CreatedAt: time.Unix(req.CreatedAt, 0),
  77. UpdatedAt: time.Unix(req.UpdatedAt, 0),
  78. UserType: req.UserType,
  79. HouseId: req.HouseId,
  80. BuildingNumber: req.BuildingNumber,
  81. UnitNumber: req.UnitNumber,
  82. HouseNumber: req.HouseNumber,
  83. BuildingId: req.BuildingId,
  84. UnitId: req.UnitId,
  85. Appendix: req.Appendix,
  86. Feedback: req.Feedback,
  87. Phone: req.Phone,
  88. IdNumber: req.IdNumber,
  89. IdType: req.IdType,
  90. Name: req.Name,
  91. ApprovedAt: time.Unix(req.ApprovedAt, 0),
  92. ID: req.Id,
  93. PublicOpenId: req.PublicOpenId,
  94. }
  95. p.SetTable(dbname)
  96. err = p.Insert(db)
  97. if err != nil {
  98. return nil, errors.DataBaseError
  99. }
  100. statisticReq := pb_v1.RepairStatisticSetRequest{
  101. HandleType: statistic.HandleTypeHousehold,
  102. FinishIncrease: int64(1),
  103. GardenId: req.GardenId,
  104. }
  105. err = statistic.RepairStatisticSetWithDb(&statisticReq, db)
  106. if err != nil {
  107. return nil, err
  108. }
  109. dReq := pb_v1.HouseholdGateSyncCheckRequest{GardenId: req.GardenId, Uid: req.Uid, UnitId: req.UnitId}
  110. _, err = pb.Device.HouseholdGateSyncCheck(ctx, &dReq)
  111. if err != nil {
  112. return nil, err
  113. }
  114. // 修改或插入业主信息
  115. u := dbmodel.NewHouseholdUser(dbname)
  116. where = map[string]interface{}{
  117. "id": req.HouseholdUser.Id,
  118. }
  119. err = u.Find(db, where)
  120. if err != nil && err != gorm.ErrRecordNotFound {
  121. return nil, errors.DataBaseError
  122. }
  123. u.SetTable(dbname)
  124. if u.ID == 0 {
  125. u.ID = req.HouseholdUser.Id
  126. u.Phone = req.HouseholdUser.Phone
  127. u.IdNumber = req.HouseholdUser.IdNumber
  128. u.IdType = req.HouseholdUser.IdType
  129. u.OpenId = req.HouseholdUser.OpenId
  130. u.UnionId = req.HouseholdUser.UnionId
  131. u.NickName = req.HouseholdUser.NickName
  132. u.RealName = req.HouseholdUser.RealName
  133. u.Avatar = req.HouseholdUser.Avatar
  134. u.CreatedAt = time.Now()
  135. u.UpdatedAt = time.Now()
  136. u.PublicOpenId = req.HouseholdUser.PublicOpenId
  137. err = u.Insert(db)
  138. if err == nil {
  139. mreq := pb_v1.ObjStatisticSetRequest{
  140. ObjType: statistic.ObjTypeUser,
  141. TotalIncrease: int64(1),
  142. GardenId: req.GardenId,
  143. }
  144. err = statistic.ObjStatisticSetWithDb(&mreq, db)
  145. }
  146. } else {
  147. values := map[string]interface{}{
  148. "phone": req.HouseholdUser.Phone,
  149. "id_number": req.HouseholdUser.IdNumber,
  150. "id_type": req.HouseholdUser.IdType,
  151. "open_id": req.HouseholdUser.OpenId,
  152. "union_id": req.HouseholdUser.UnionId,
  153. "nick_name": req.HouseholdUser.NickName,
  154. "real_name": req.HouseholdUser.RealName,
  155. "avatar": req.HouseholdUser.Avatar,
  156. "public_open_id": req.HouseholdUser.PublicOpenId,
  157. }
  158. err = u.Update(db, where, values)
  159. }
  160. if err != nil {
  161. return nil, errors.DataBaseError
  162. }
  163. db.Commit()
  164. return reply, nil
  165. }