key_info_change_approve.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package garden
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "property-system/errors"
  10. dbmodel "property-system/model"
  11. "property-system/pb"
  12. pb_v1 "property-system/pb/v1"
  13. "time"
  14. "git.getensh.com/common/gopkgs/database"
  15. "git.getensh.com/common/gopkgs/logger"
  16. "go.uber.org/zap"
  17. "google.golang.org/grpc/status"
  18. )
  19. func checkGardenKeyInfoApproveParam(req *pb_v1.GardenKeyInfoApproveRequest) error {
  20. switch {
  21. case req.Id < 1:
  22. return status.Error(10003, "id不能为空")
  23. }
  24. return nil
  25. }
  26. //
  27. func GardenKeyInfoApprove(ctx context.Context, req *pb_v1.GardenKeyInfoApproveRequest) (reply *pb_v1.GardenKeyInfoApproveReply, err error) {
  28. reply = &pb_v1.GardenKeyInfoApproveReply{}
  29. // 捕获各个task中的异常并返回给调用者
  30. defer func() {
  31. if r := recover(); r != nil {
  32. err = fmt.Errorf("%+v", r)
  33. e := &status.Status{}
  34. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  35. logger.Error("err",
  36. zap.String("system_err", err.Error()),
  37. zap.Stack("stacktrace"))
  38. }
  39. }
  40. }()
  41. err = checkGardenKeyInfoApproveParam(req)
  42. if err != nil {
  43. return nil, err
  44. }
  45. // 获取修改信息
  46. p := &dbmodel.TGardenChange{}
  47. where := map[string]interface{}{
  48. "id": req.Id,
  49. }
  50. err = p.Find(database.DB(), where)
  51. if err != nil && err != gorm.ErrRecordNotFound {
  52. return nil, errors.DataBaseError
  53. }
  54. if p.ID == 0 {
  55. return nil, errors.GardenNotExist
  56. }
  57. if p.Status > 1 {
  58. return nil, status.Error(10003, "已审核,不能重复审批")
  59. }
  60. gardenId := p.GardenId
  61. // 拒绝申请
  62. if !req.Status {
  63. gChange := dbmodel.TGardenChange{}
  64. where = map[string]interface{}{
  65. "id":req.Id,
  66. }
  67. values := map[string]interface{}{
  68. "status": 3,
  69. "approved_at": time.Now(),
  70. "feedback": req.Feedback,
  71. }
  72. err = gChange.Update(database.DB(), where, values)
  73. if err != nil {
  74. return nil, errors.DataBaseError
  75. }
  76. return reply, nil
  77. }
  78. // 组装申请信息
  79. addrChange := false
  80. nameChange := false
  81. gardenName := ""
  82. array := []KeyInfoChangeContent{}
  83. json.Unmarshal([]byte(p.Content), &array)
  84. values := map[string]interface{}{}
  85. for _, v := range array {
  86. values[v.Key] = v.NewValue
  87. if v.Key == "committee_code" {
  88. addrChange = true
  89. }
  90. if v.Key == "garden_name" {
  91. nameChange = true
  92. gardenName = v.NewValue
  93. }
  94. }
  95. // 检查 不能修改省市区街道社区是否可以修改
  96. if addrChange {
  97. mreq := pb_v1.GardenHouseIsInRequest{GardenId: req.Id}
  98. mreply, err := pb.Garden.GardenHouseIsIn(ctx, &mreq)
  99. if err != nil {
  100. return nil, err
  101. }
  102. if mreply.In {
  103. // 自动拒绝
  104. p := dbmodel.TGardenChange{}
  105. values := map[string]interface{}{
  106. "status": 3,
  107. "approved_at": time.Now(),
  108. "feedback": "已入驻房屋 不能修改省市区街道社区",
  109. }
  110. err = p.Update(database.DB(), where, values)
  111. if err != nil {
  112. return nil, errors.DataBaseError
  113. }
  114. }
  115. }
  116. db := database.DB().Begin()
  117. defer func() {
  118. if err != nil {
  119. db.Rollback()
  120. }
  121. }()
  122. // 更新小区
  123. garden := dbmodel.TGardenApproved{}
  124. where = map[string]interface{}{
  125. "id":p.GardenId,
  126. }
  127. err = garden.Update(db, where, values)
  128. if err != nil {
  129. return nil, errors.DataBaseError
  130. }
  131. // 通过申请
  132. gChange := dbmodel.TGardenChange{}
  133. where = map[string]interface{}{
  134. "id":req.Id,
  135. }
  136. values = map[string]interface{}{
  137. "status": 2,
  138. "approved_at": time.Now(),
  139. "feedback": req.Feedback,
  140. }
  141. err = gChange.Update(db, where, values)
  142. if err != nil {
  143. return nil, errors.DataBaseError
  144. }
  145. // 拒绝住户申请
  146. if addrChange {
  147. if err = RefuseHouseApply(req.Id, 0, 0, 0); err != nil {
  148. return nil, err
  149. }
  150. }
  151. // 同步小区名信息到租房信息表
  152. if nameChange {
  153. mreq := pb_v1.GardenHouseRentChangeFieldRequest{
  154. GardenId: gardenId,
  155. GardenName: gardenName,
  156. }
  157. _, err = pb.Garden.GardenHouseRentChangeField(ctx, &mreq)
  158. if err != nil {
  159. return nil, err
  160. }
  161. }
  162. db.Commit()
  163. return reply, nil
  164. }