update.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package park_space
  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-garden/errors"
  14. "property-garden/impl/v1/charge_utils"
  15. dbmodel "property-garden/model"
  16. pb_v1 "property-garden/pb/v1"
  17. "property-garden/utils"
  18. "strings"
  19. "time"
  20. )
  21. const (
  22. BindTypeNormal = 1
  23. BindTypeSell = 2
  24. BindTypeRent = 3
  25. )
  26. func checkParkSpaceUpdateParam(req *pb_v1.ParkSpaceUpdateRequest) error {
  27. switch {
  28. case req.ParkId < 1:
  29. return status.Error(10003, "停车场不能为空")
  30. case req.SpaceNumber == "":
  31. return status.Error(10003, "车位编号不能为空")
  32. case req.SpaceType < 1:
  33. return status.Error(10003, "车位类型不能为空")
  34. case req.Id < 1:
  35. return status.Error(10003, "id 不能为空")
  36. case req.GardenId < 1:
  37. return status.Error(10003, "小区不能为空")
  38. case req.SpaceArea == 0:
  39. return status.Error(10003, "车位面积不能为空")
  40. }
  41. return nil
  42. }
  43. func updateChargeBindArea(spaceId int64, area float64, db *gorm.DB, dbname string) error {
  44. p := dbmodel.NewChargeBind(dbname)
  45. where := map[string]interface{}{
  46. "obj_id":spaceId,
  47. "obj_type":charge_utils.ObjTypeSpace,
  48. "charge_basis":charge_utils.ChargeBasisSpaceArea,
  49. }
  50. value := map[string]interface{}{
  51. "obj_area":area,
  52. }
  53. err := p.Update(db, where, value)
  54. if err != nil {
  55. return errors.DataBaseError
  56. }
  57. return nil
  58. }
  59. //
  60. func ParkSpaceUpdate(ctx context.Context, req *pb_v1.ParkSpaceUpdateRequest) (reply *pb_v1.ParkSpaceUpdateReply, err error) {
  61. reply = &pb_v1.ParkSpaceUpdateReply{}
  62. // 捕获各个task中的异常并返回给调用者
  63. defer func() {
  64. if r := recover(); r != nil {
  65. err = fmt.Errorf("%+v", r)
  66. e := &status.Status{}
  67. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  68. logger.Error("err",
  69. zap.String("system_err", err.Error()),
  70. zap.Stack("stacktrace"))
  71. }
  72. }
  73. }()
  74. // 参数检查
  75. err = checkParkSpaceUpdateParam(req)
  76. if err != nil {
  77. return nil, err
  78. }
  79. dbname := utils.GetGardenDbName(req.GardenId)
  80. park := dbmodel.NewPark(dbname)
  81. where := map[string]interface{}{
  82. "id":req.ParkId,
  83. //"garden_id":req.GardenId,
  84. }
  85. err = park.Find(database.DB(), where)
  86. if err != nil && err != gorm.ErrRecordNotFound{
  87. return nil, errors.DataBaseError
  88. }
  89. if park.ID == 0 {
  90. return nil, errors.ErrRecordNotFound
  91. }
  92. now := time.Now()
  93. p := dbmodel.NewParkSpace(dbname)
  94. where = map[string]interface{}{
  95. "id":req.Id,
  96. //"garden_id":req.GardenId,
  97. }
  98. values := map[string]interface{}{
  99. "space_number":req.SpaceNumber,
  100. "space_type":req.SpaceType,
  101. "space_area":req.SpaceArea,
  102. "park_id":req.ParkId,
  103. "comment":req.Comment,
  104. "house_id":req.HouseId,
  105. "updated_at":now,
  106. }
  107. old := dbmodel.NewParkSpace(dbname)
  108. err = old.Find(database.DB(), where)
  109. if err != nil && err != gorm.ErrRecordNotFound{
  110. return nil, errors.DataBaseError
  111. }
  112. if old.ID == 0 {
  113. return nil, errors.ErrRecordNotFound
  114. }
  115. if req.HouseId > 0 {
  116. values["status"] = SpaceStatusSell
  117. values["bind_status"] = SpaceBindStatusBind
  118. } else if old.VehicleId == 0 {
  119. values["status"] = SpaceStatusEmpty
  120. values["bind_status"] = SpaceBindStatusEmpty
  121. }
  122. if old.ParkId != req.ParkId || old.SpaceNumber != req.SpaceNumber ||
  123. (old.HouseId > 0 && req.HouseId != old.HouseId){
  124. isBind, err := charge_utils.IsObjBindCharge(req.Id, charge_utils.ObjTypeSpace, dbname)
  125. if err != nil {
  126. return nil, err
  127. }
  128. if isBind {
  129. return nil, status.Error(10003, "该车位已绑定费用,请先解除绑定")
  130. }
  131. }
  132. reply.Origin = &pb_v1.ParkSpaceUpdateRequest{
  133. Id:req.Id,
  134. ParkId:old.ParkId,
  135. Comment:old.Comment,
  136. GardenId:req.GardenId,
  137. SpaceArea:old.SpaceArea,
  138. SpaceType:old.SpaceType,
  139. SpaceNumber:old.SpaceNumber,
  140. }
  141. db := database.DB().Begin()
  142. defer func() {
  143. if err != nil {
  144. db.Rollback()
  145. }
  146. }()
  147. err = p.Update(db, where, values)
  148. if err != nil {
  149. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  150. return nil, status.Error(10003, "该车位已存在")
  151. }
  152. return nil, errors.DataBaseError
  153. }
  154. if req.SpaceArea != old.SpaceArea {
  155. err = updateChargeBindArea(req.Id, req.SpaceArea, db, dbname)
  156. if err != nil {
  157. return nil, err
  158. }
  159. }
  160. if req.HouseId > 0 && req.HouseId != old.HouseId && old.VehicleId > 0 {
  161. // 检查新房屋id与车辆所属用户的房屋是否匹配
  162. veh := dbmodel.NewVehicle(dbname)
  163. filter := map[string]interface{}{
  164. "id":old.VehicleId,
  165. }
  166. err = veh.Find(database.DB(), filter)
  167. if err != nil && err != gorm.ErrRecordNotFound {
  168. return nil, errors.DataBaseError
  169. }
  170. if veh.ID > 0 {
  171. if veh.BindType == BindTypeRent {
  172. return nil, status.Error(10003, fmt.Sprintf("该车位已出租给车辆%s,无法绑定房屋", veh.PlateNo))
  173. }
  174. household := dbmodel.NewHouseApprovedGarden(dbname)
  175. filter = map[string]interface{}{
  176. "uid":veh.HouseholdUid,
  177. "house_id":req.HouseId,
  178. }
  179. count, err := household.Count(db, where, nil)
  180. if err != nil {
  181. return nil, errors.DataBaseError
  182. }
  183. if count == 0 {
  184. return nil, status.Error(10003, "该车位以出售方式绑定车辆, 车辆所属业主未申请该房屋")
  185. }
  186. }
  187. }
  188. db.Commit()
  189. return reply, nil
  190. }