house_add.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. "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. "strings"
  15. "time"
  16. "git.getensh.com/common/gopkgs/database"
  17. "git.getensh.com/common/gopkgs/logger"
  18. "go.uber.org/zap"
  19. "google.golang.org/grpc/status"
  20. )
  21. const (
  22. HouseStatusWait = 1
  23. HouseStatusUsed = 2
  24. HouseStatusRented = 3
  25. )
  26. func checkHouseAddParam(req *pb_v1.HouseAddRequest) error {
  27. switch {
  28. case req.HouseNumber == "":
  29. return status.Error(10003, "门牌号不能为空")
  30. case req.Layer < 1:
  31. return status.Error(10003, "楼层不能为空")
  32. case req.GardenId < 1:
  33. return status.Error(10003, "小区不能为空")
  34. case req.RoomCount < 0 || req.HallCount < 0:
  35. return status.Error(10003, "几室几厅不能为空")
  36. case req.HouseType < 1:
  37. return status.Error(10003, "房屋类型不能为空")
  38. case req.UnitId < 1:
  39. return status.Error(10003, "单元id不能为空")
  40. case req.HouseArea < 1:
  41. return status.Error(10003, "房屋面积不能为空")
  42. case req.HouseUsedArea < 1:
  43. return status.Error(10003, "房屋使用面积不能为空")
  44. case strings.Contains(req.HouseNumber, "-"):
  45. return status.Error(10003, "门牌号不能包含-")
  46. }
  47. return nil
  48. }
  49. //
  50. func HouseAdd(ctx context.Context, req *pb_v1.HouseAddRequest) (reply *pb_v1.HouseAddReply, err error) {
  51. reply = &pb_v1.HouseAddReply{}
  52. // 捕获各个task中的异常并返回给调用者
  53. defer func() {
  54. if r := recover(); r != nil {
  55. err = fmt.Errorf("%+v", r)
  56. e := &status.Status{}
  57. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  58. logger.Error("err",
  59. zap.String("system_err", err.Error()),
  60. zap.Stack("stacktrace"))
  61. }
  62. }
  63. }()
  64. err = checkHouseAddParam(req)
  65. if err != nil {
  66. return nil, err
  67. }
  68. dbname := utils.GetGardenDbName(req.GardenId)
  69. unit := dbmodel.NewUnit(dbname)
  70. where := map[string]interface{}{
  71. "id": req.UnitId,
  72. }
  73. err = unit.Find(database.DB(), where)
  74. if err != nil && err != gorm.ErrRecordNotFound {
  75. return nil, errors.DataBaseError
  76. }
  77. if unit.ID == 0 {
  78. return nil, errors.ErrRecordNotFound
  79. }
  80. building := dbmodel.NewBuilding(dbname)
  81. where = map[string]interface{}{
  82. "id": unit.BuildingId,
  83. }
  84. err = building.Find(database.DB(), where)
  85. if err != nil && err != gorm.ErrRecordNotFound {
  86. return nil, errors.DataBaseError
  87. }
  88. if building.ID == 0 {
  89. return nil, errors.ErrRecordNotFound
  90. }
  91. if building.ID != req.BuildingId {
  92. return nil, status.Error(10003, "单元和楼栋不匹配")
  93. }
  94. now := time.Now()
  95. house := dbmodel.THouse{
  96. //GardenId: req.GardenId,
  97. HouseType: req.HouseType,
  98. HouseNumber: req.HouseNumber,
  99. BuildingId: unit.BuildingId,
  100. UnitId: req.UnitId,
  101. HasLift: unit.HasLift,
  102. HouseArea: req.HouseArea,
  103. HouseUsedArea: req.HouseUsedArea,
  104. Layer: req.Layer,
  105. RoomCount: req.RoomCount,
  106. HallCount: req.HallCount,
  107. UpdatedAt: now,
  108. CreatedAt: now,
  109. Status: HouseStatusWait,
  110. }
  111. house.SetTable(dbname)
  112. db := database.DB().Begin()
  113. defer func() {
  114. if err != nil {
  115. db.Rollback()
  116. }
  117. }()
  118. err = house.Insert(db)
  119. if err != nil {
  120. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  121. return nil, errors.ErrRecordExist
  122. }
  123. return nil, errors.DataBaseError
  124. }
  125. mreq := pb_v1.ObjStatisticSetRequest{
  126. ObjType: statistic.ObjTypeHouse,
  127. TotalIncrease: int64(1),
  128. GardenId: req.GardenId,
  129. }
  130. err = statistic.ObjStatisticSetWithDb(&mreq, db)
  131. if err != nil {
  132. return nil, err
  133. }
  134. if err = checkHouseCount(req.GardenId, int64(1)); err != nil {
  135. return nil, err
  136. }
  137. if err = houseCountChange(req.GardenId, int64(1)); err != nil {
  138. return nil, err
  139. }
  140. db.Commit()
  141. reply.Id = house.ID
  142. return reply, nil
  143. }