household_del_house.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package house
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/database"
  7. "git.getensh.com/common/gopkgs/logger"
  8. "go.uber.org/zap"
  9. "google.golang.org/grpc/status"
  10. "gorm.io/gorm"
  11. "property-household/errors"
  12. dbmodel "property-household/model"
  13. pb_v1 "property-household/pb/v1"
  14. )
  15. func checkHouseholdDelHouseParam(req *pb_v1.HouseholdDelHouseRequest) error {
  16. if req.GardenId == 0 || req.HouseId == 0 || req.HouseholdUid == 0 {
  17. return errors.ParamsError
  18. }
  19. return nil
  20. }
  21. //
  22. func HouseholdDelHouse(ctx context.Context, req *pb_v1.HouseholdDelHouseRequest) (reply *pb_v1.HouseholdDelHouseReply, err error) {
  23. reply = &pb_v1.HouseholdDelHouseReply{}
  24. // 捕获各个task中的异常并返回给调用者
  25. defer func() {
  26. if r := recover(); r != nil {
  27. err = fmt.Errorf("%+v", r)
  28. e := &status.Status{}
  29. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  30. logger.Error("err",
  31. zap.String("system_err", err.Error()),
  32. zap.Stack("stacktrace"))
  33. }
  34. }
  35. }()
  36. err = checkHouseholdDelHouseParam(req)
  37. // 获取原始数据
  38. household := dbmodel.THouseApproved{}
  39. where := map[string]interface{}{
  40. "uid": req.HouseholdUid,
  41. "house_id": req.HouseId,
  42. "garden_id": req.GardenId,
  43. }
  44. err = household.Find(database.DB(), where)
  45. if err != nil && err != gorm.ErrRecordNotFound {
  46. return nil, errors.DataBaseError
  47. }
  48. if household.ID == 0 {
  49. return nil, errors.ErrRecordNotFound
  50. }
  51. db := database.DB().Begin()
  52. err = household.Delete(db, where)
  53. if err != nil {
  54. db.Rollback()
  55. return nil, errors.DataBaseError
  56. }
  57. rent := dbmodel.THouseRent{}
  58. where = map[string]interface{}{
  59. "household_uid": req.HouseholdUid,
  60. "house_id": req.HouseId,
  61. "garden_id": req.GardenId,
  62. }
  63. err = rent.Delete(db, where)
  64. if err != nil {
  65. db.Rollback()
  66. return nil, errors.DataBaseError
  67. }
  68. if len(req.RentIds) > 0 {
  69. appoint := dbmodel.THouseRentAppointment{}
  70. where = map[string]interface{}{
  71. "rent_id in": req.RentIds,
  72. }
  73. err = appoint.Delete(db, where)
  74. if err != nil {
  75. db.Rollback()
  76. return nil, errors.DataBaseError
  77. }
  78. }
  79. db.Commit()
  80. return reply, nil
  81. }