household_sync.go 4.3 KB

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