approve.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. "git.getensh.com/common/gopkgs/database"
  9. "git.getensh.com/common/gopkgs/logger"
  10. "go.uber.org/zap"
  11. "google.golang.org/grpc/status"
  12. "gorm.io/gorm"
  13. "property-household/errors"
  14. "property-household/impl/v1/public_msg"
  15. dbmodel "property-household/model"
  16. "property-household/pb"
  17. pb_v1 "property-household/pb/v1"
  18. "time"
  19. )
  20. func checkHouseholdApproveParam(req *pb_v1.HouseholdApproveRequest) error {
  21. switch {
  22. case req.Id < 1:
  23. return status.Error(10003, "id不能为空")
  24. }
  25. return nil
  26. }
  27. const (
  28. HandleTypeRepair = 1
  29. HandleTypeSuggestion = 2
  30. HandleTypeHousehold = 3
  31. )
  32. //
  33. func HouseholdApprove(ctx context.Context, req *pb_v1.HouseholdApproveRequest) (reply *pb_v1.HouseholdApproveReply, err error) {
  34. reply = &pb_v1.HouseholdApproveReply{}
  35. // 捕获各个task中的异常并返回给调用者
  36. defer func() {
  37. if r := recover(); r != nil {
  38. err = fmt.Errorf("%+v", r)
  39. e := &status.Status{}
  40. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  41. logger.Error("err",
  42. zap.String("system_err", err.Error()),
  43. zap.Stack("stacktrace"))
  44. }
  45. }
  46. }()
  47. err = checkHouseholdApproveParam(req)
  48. if err != nil {
  49. return nil, err
  50. }
  51. p := dbmodel.THouse{}
  52. where := map[string]interface{}{
  53. "id": req.Id,
  54. }
  55. err = p.Find(database.DB(), where)
  56. if err != nil && err != gorm.ErrRecordNotFound {
  57. return nil, errors.DataBaseError
  58. }
  59. if p.ID == 0 {
  60. return nil, errors.ErrRecordNotFound
  61. }
  62. if p.ApproveStatus != HouseholdApproveStatusWait {
  63. return nil, status.Error(10003, "当前状态不能审核")
  64. }
  65. u := dbmodel.TUser{}
  66. filter := map[string]interface{}{
  67. "id": p.Uid,
  68. }
  69. err = u.Find(database.DB(), filter)
  70. if err != nil && err != gorm.ErrRecordNotFound {
  71. return nil, errors.DataBaseError
  72. }
  73. if u.ID == 0 {
  74. return nil, errors.ErrRecordNotFound
  75. }
  76. now := time.Now()
  77. values := map[string]interface{}{
  78. "feedback": req.Feedback,
  79. "approve_status": HouseholdApproveStatusSuccess,
  80. "approved_at": now,
  81. "updated_at": now,
  82. }
  83. if !req.Status {
  84. values["approve_status"] = HouseholdApproveStatusFail
  85. values["uniq_flag"] = nil
  86. }
  87. db := database.DB().Begin()
  88. insertId := int64(0)
  89. if req.Status {
  90. err = p.Delete(db, where)
  91. if err != nil {
  92. db.Rollback()
  93. return nil, errors.DataBaseError
  94. }
  95. approvedHouse := dbmodel.THouseApproved{
  96. GardenId: p.GardenId,
  97. Uid: p.Uid,
  98. CreatedAt: p.CreatedAt,
  99. UpdatedAt: now,
  100. UserType: p.UserType,
  101. HouseId: p.HouseId,
  102. BuildingNumber: p.BuildingNumber,
  103. UnitNumber: p.UnitNumber,
  104. HouseNumber: p.HouseNumber,
  105. BuildingId: p.BuildingId,
  106. UnitId: p.UnitId,
  107. Appendix: p.Appendix,
  108. Feedback: req.Feedback,
  109. Phone: p.Phone,
  110. Name: p.Name,
  111. IdType: p.IdType,
  112. IdNumber: p.IdNumber,
  113. ApprovedAt: now,
  114. OpenId: u.OpenId,
  115. PublicOpenId: u.PublicOpenId,
  116. }
  117. err = approvedHouse.Insert(db)
  118. insertId = approvedHouse.ID
  119. } else {
  120. err = p.Update(db, where, values)
  121. if err == nil {
  122. // 业主审核统计数增加
  123. statisticReq := pb_v1.RepairStatisticSetRequest{
  124. HandleType: HandleTypeHousehold,
  125. FinishIncrease: int64(1),
  126. GardenId: p.GardenId,
  127. }
  128. _, err = pb.Garden.RepairStatisticSet(ctx, &statisticReq)
  129. }
  130. }
  131. if err != nil {
  132. db.Rollback()
  133. return nil, errors.DataBaseError
  134. }
  135. if req.Status {
  136. mreq := &pb_v1.HouseholdSyncRequest{
  137. GardenId: p.GardenId,
  138. OpenId: p.OpenId,
  139. Uid: p.Uid,
  140. CreatedAt: p.CreatedAt.Unix(),
  141. UpdatedAt: now.Unix(),
  142. UserType: p.UserType,
  143. HouseId: p.HouseId,
  144. BuildingNumber: p.BuildingNumber,
  145. UnitNumber: p.UnitNumber,
  146. HouseNumber: p.HouseNumber,
  147. BuildingId: p.BuildingId,
  148. UnitId: p.UnitId,
  149. Appendix: p.Appendix,
  150. Feedback: req.Feedback,
  151. Phone: p.Phone,
  152. Name: p.Name,
  153. IdType: p.IdType,
  154. IdNumber: p.IdNumber,
  155. ApprovedAt: now.Unix(),
  156. Id: insertId,
  157. PublicOpenId: u.PublicOpenId,
  158. }
  159. mreq.HouseholdUser = &pb_v1.HouseholdUserData{
  160. Id: u.ID,
  161. Phone: u.Phone,
  162. IdNumber: u.IdNumber,
  163. IdType: u.IdType,
  164. OpenId: u.OpenId,
  165. UnionId: u.UnionId,
  166. NickName: u.NickName,
  167. RealName: u.RealName,
  168. Avatar: u.Avatar,
  169. PublicOpenId: u.PublicOpenId,
  170. }
  171. _, err = pb.Garden.HouseholdSync(ctx, mreq)
  172. if err != nil {
  173. db.Rollback()
  174. return nil, err
  175. }
  176. }
  177. db.Commit()
  178. householdGardensDelFromRedis(p.Uid)
  179. serviceInfo := public_msg.ServiceInfo{
  180. Name: "房屋认证",
  181. State: "已通过",
  182. }
  183. if !req.Status {
  184. serviceInfo.State = "未通过(" + req.Feedback + ")"
  185. }
  186. public_msg.SendMsgWx(p.Uid, p.GardenId, serviceInfo)
  187. return reply, nil
  188. }