household_del_house.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package household
  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-garden/errors"
  12. "property-garden/impl/v1/charge_utils"
  13. "property-garden/impl/v1/oss_utils"
  14. "property-garden/impl/v1/statistic"
  15. dbmodel "property-garden/model"
  16. "property-garden/pb"
  17. pb_v1 "property-garden/pb/v1"
  18. "property-garden/utils"
  19. "strings"
  20. "time"
  21. )
  22. func checkGardenHouseholdDelHouseParam(req *pb_v1.GardenHouseholdDelHouseRequest) error {
  23. if req.GardenId == 0 || req.HouseId == 0 || req.HouseholdUid == 0 {
  24. return errors.ParamsError
  25. }
  26. return nil
  27. }
  28. func delVehicle(req *pb_v1.GardenHouseholdDelHouseRequest, dbname string, db *gorm.DB) ([]int64, error) {
  29. vehicleIds := []int64{}
  30. vehicle := dbmodel.NewVehicle(dbname)
  31. where := map[string]interface{}{
  32. "household_uid": req.HouseholdUid,
  33. }
  34. vlist, err := vehicle.List(db, where, nil, -1, -1)
  35. if err != nil {
  36. return nil, errors.DataBaseError
  37. }
  38. for _, v := range vlist {
  39. vehicleIds = append(vehicleIds, v.ID)
  40. }
  41. err = vehicle.Delete(db, where)
  42. if err != nil {
  43. return nil, errors.DataBaseError
  44. }
  45. chargeBind := dbmodel.NewChargeBind(dbname)
  46. where = map[string]interface{}{
  47. "obj_type": charge_utils.ObjTypeVehicle,
  48. "obj_id in": vehicleIds,
  49. }
  50. err = chargeBind.Delete(db, where)
  51. if err != nil {
  52. return nil, errors.DataBaseError
  53. }
  54. mreq := pb_v1.ObjStatisticSetRequest{
  55. ObjType: statistic.ObjTypeVehicle,
  56. TotalIncrease: int64(0 - len(vehicleIds)),
  57. GardenId: req.GardenId,
  58. }
  59. err = statistic.ObjStatisticSetWithDb(&mreq, db)
  60. if err != nil {
  61. return nil, err
  62. }
  63. return vehicleIds, nil
  64. }
  65. func changeSpaceByHouseId(req *pb_v1.GardenHouseholdDelHouseRequest, dbname string, db *gorm.DB) error {
  66. p := dbmodel.NewParkSpace(dbname)
  67. where := map[string]interface{}{
  68. "house_id": req.HouseId,
  69. }
  70. spaceIds := []int64{}
  71. list, err := p.List(db, where, nil, -1, -1)
  72. if err != nil {
  73. return errors.DataBaseError
  74. }
  75. for _, v := range list {
  76. spaceIds = append(spaceIds, v.ID)
  77. }
  78. values := map[string]interface{}{
  79. "vehicle_id": 0,
  80. "status": 1,
  81. "bind_status": 1,
  82. "updated_at": time.Now(),
  83. }
  84. err = p.Update(db, where, values)
  85. if err != nil {
  86. return errors.DataBaseError
  87. }
  88. chargeBind := dbmodel.NewChargeBind(dbname)
  89. where = map[string]interface{}{
  90. "obj_type": charge_utils.ObjTypeSpace,
  91. "obj_id in": spaceIds,
  92. }
  93. err = chargeBind.Delete(db, where)
  94. if err != nil {
  95. return errors.DataBaseError
  96. }
  97. return nil
  98. }
  99. func changeSpaceByVehicleId(vehicleIds []int64, dbname string, db *gorm.DB) error {
  100. p := dbmodel.NewParkSpace(dbname)
  101. where := map[string]interface{}{
  102. "vehicle_id in": vehicleIds,
  103. }
  104. values := map[string]interface{}{
  105. "vehicle_id": 0,
  106. "updated_at": time.Now(),
  107. }
  108. err := p.Update(db, where, values)
  109. if err != nil {
  110. return errors.DataBaseError
  111. }
  112. return nil
  113. }
  114. func delRentInfo(req *pb_v1.GardenHouseholdDelHouseRequest, dbname string, db *gorm.DB) ([]int64, error) {
  115. rent := dbmodel.NewHouseRent(dbname)
  116. where := map[string]interface{}{
  117. "household_uid": req.HouseholdUid,
  118. "house_id": req.HouseId,
  119. }
  120. rentIds := []int64{}
  121. rlist, err := rent.List(db, where, nil, -1, -1)
  122. if err != nil {
  123. return nil, errors.DataBaseError
  124. }
  125. for _, v := range rlist {
  126. rentIds = append(rentIds, v.ID)
  127. }
  128. if err := rent.Delete(db, where); err != nil {
  129. return nil, errors.DataBaseError
  130. }
  131. // 删除租房经纪人信息
  132. manager := dbmodel.NewHouseRentManager(dbname)
  133. where = map[string]interface{}{
  134. "rent_id in": rentIds,
  135. }
  136. if err := manager.Delete(db, where); err != nil {
  137. return nil, errors.DataBaseError
  138. }
  139. return rentIds, nil
  140. }
  141. //
  142. func GardenHouseholdDelHouse(ctx context.Context, req *pb_v1.GardenHouseholdDelHouseRequest) (reply *pb_v1.GardenHouseholdDelHouseReply, err error) {
  143. reply = &pb_v1.GardenHouseholdDelHouseReply{}
  144. // 捕获各个task中的异常并返回给调用者
  145. defer func() {
  146. if r := recover(); r != nil {
  147. err = fmt.Errorf("%+v", r)
  148. e := &status.Status{}
  149. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  150. logger.Error("err",
  151. zap.String("system_err", err.Error()),
  152. zap.Stack("stacktrace"))
  153. }
  154. }
  155. }()
  156. err = checkGardenHouseholdDelHouseParam(req)
  157. dbname := utils.GetGardenDbName(req.GardenId)
  158. // 获取原始数据
  159. household := dbmodel.NewHouseApprovedGarden(dbname)
  160. where := map[string]interface{}{
  161. "uid": req.HouseholdUid,
  162. "house_id": req.HouseId,
  163. }
  164. householdList, err := household.List(database.DB(), where, nil, -1, -1)
  165. if err != nil && err != gorm.ErrRecordNotFound {
  166. return nil, errors.DataBaseError
  167. }
  168. if len(householdList) == 0 {
  169. return nil, errors.ErrRecordNotFound
  170. }
  171. // 检查账单
  172. bill := dbmodel.NewChargeBill(dbname)
  173. where = map[string]interface{}{
  174. "house_id": req.HouseId,
  175. }
  176. billCount, err := bill.Count(database.DB(), where, nil)
  177. if err != nil {
  178. return nil, errors.DataBaseError
  179. }
  180. if billCount > 0 {
  181. return nil, status.Error(10003, "该房屋有账单未支付,无法退出")
  182. }
  183. where = map[string]interface{}{
  184. "uid": req.HouseholdUid,
  185. }
  186. householdCount, err := household.Count(database.DB(), where, nil)
  187. if err != nil {
  188. return nil, errors.DataBaseError
  189. }
  190. where = map[string]interface{}{
  191. "house_id": req.HouseId,
  192. }
  193. houseCount, err := household.Count(database.DB(), where, nil)
  194. if err != nil {
  195. return nil, errors.DataBaseError
  196. }
  197. db := database.DB().Begin()
  198. defer func() {
  199. if err != nil {
  200. db.Rollback()
  201. }
  202. }()
  203. vehicleIds := []int64{}
  204. // 若该住户在该小区没有其他房屋,删除车,并
  205. if householdCount == 1 {
  206. vehicleIds, err = delVehicle(req, dbname, db)
  207. if err != nil {
  208. return nil, err
  209. }
  210. }
  211. // 若该房屋在该小区没有其他住户,更新该房屋车位信息
  212. if houseCount == 1 {
  213. err = changeSpaceByHouseId(req, dbname, db)
  214. } else if len(vehicleIds) > 0 {
  215. // 若该房屋在该小区有其他住户,更新车辆对应的车位
  216. err = changeSpaceByVehicleId(vehicleIds, dbname, db)
  217. }
  218. if err != nil {
  219. return nil, err
  220. }
  221. // 删除租房信息
  222. rentIds, err := delRentInfo(req, dbname, db)
  223. if err != nil {
  224. return nil, err
  225. }
  226. where = map[string]interface{}{
  227. "uid": req.HouseholdUid,
  228. "house_id": req.HouseId,
  229. }
  230. // 解除住户房屋绑定关系
  231. err = household.Delete(db, where)
  232. if err != nil {
  233. return nil, errors.DataBaseError
  234. }
  235. mreq := pb_v1.HouseholdDelHouseRequest{
  236. GardenId: req.GardenId,
  237. HouseId: req.HouseId,
  238. HouseholdUid: req.HouseholdUid,
  239. RentIds: rentIds,
  240. }
  241. // 删除住户库里的相关信息
  242. _, err = pb.Household.HouseholdDelHouse(ctx, &mreq)
  243. if err != nil {
  244. return nil, err
  245. }
  246. db.Commit()
  247. outList := []string{}
  248. for _, v := range householdList {
  249. array := strings.Split(v.Appendix, ";")
  250. for _, out := range array {
  251. if out != "" {
  252. outList = append(outList, out)
  253. }
  254. }
  255. }
  256. oss_utils.OssObjAdd(nil, outList)
  257. return reply, nil
  258. }