refuse.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "property-household/errors"
  13. dbmodel "property-household/model"
  14. "property-household/pb"
  15. pb_v1 "property-household/pb/v1"
  16. "time"
  17. )
  18. func checkHouseholdRefuseParam(req *pb_v1.HouseholdRefuseRequest) error {
  19. switch {
  20. case req.GardenId < 1:
  21. return status.Error(10003, "小区不能为空")
  22. case req.HouseId < 1 && req.UnitId < 1 && req.BuildingId < 1:
  23. return status.Error(10003, "房屋,单元,楼栋不能全空")
  24. }
  25. return nil
  26. }
  27. //
  28. func HouseholdRefuse(ctx context.Context, req *pb_v1.HouseholdRefuseRequest) (reply *pb_v1.HouseholdRefuseReply, err error) {
  29. reply = &pb_v1.HouseholdRefuseReply{}
  30. // 捕获各个task中的异常并返回给调用者
  31. defer func() {
  32. if r := recover(); r != nil {
  33. err = fmt.Errorf("%+v", r)
  34. e := &status.Status{}
  35. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  36. logger.Error("err",
  37. zap.String("system_err", err.Error()),
  38. zap.Stack("stacktrace"))
  39. }
  40. }
  41. }()
  42. err = checkHouseholdRefuseParam(req)
  43. if err != nil {
  44. return nil, err
  45. }
  46. p := &dbmodel.THouse{}
  47. where := map[string]interface{}{
  48. "approve_status":HouseholdApproveStatusWait,
  49. }
  50. if req.HouseId > 0 {
  51. where["house_id"] = req.HouseId
  52. }
  53. if req.BuildingId > 0 {
  54. where["building_id"] = req.BuildingId
  55. }
  56. if req.UnitId > 0 {
  57. where["unit_id"] = req.UnitId
  58. }
  59. if req.GardenId > 0 {
  60. where["garden_id"] = req.GardenId
  61. }
  62. now := time.Now()
  63. values := map[string]interface{}{
  64. "approve_status":HouseholdApproveStatusFail,
  65. "feedback":req.Feedback,
  66. "approved_at":now,
  67. "updated_at":now,
  68. "uniq_flag":nil,
  69. }
  70. db := database.DB().Begin()
  71. affected, err := p.UpdateAffected(db, where, values)
  72. if err != nil {
  73. db.Rollback()
  74. return nil, errors.DataBaseError
  75. }
  76. if affected > 0 {
  77. statisticReq := pb_v1.RepairStatisticSetRequest{
  78. HandleType:3,
  79. FinishIncrease:affected,
  80. }
  81. _, err = pb.Garden.RepairStatisticSet(ctx, &statisticReq)
  82. if err != nil {
  83. db.Rollback()
  84. return nil, err
  85. }
  86. }
  87. db.Commit()
  88. householdGardensDelFromRedis(p.Uid)
  89. return reply, nil
  90. }