// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package house import ( "context" "encoding/json" "fmt" "gorm.io/gorm" "property-garden/errors" "property-garden/impl/v1/statistic" dbmodel "property-garden/model" pb_v1 "property-garden/pb/v1" "property-garden/utils" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" ) func checkHouseDelParam(req *pb_v1.HouseDelRequest) error { switch { case req.GardenId < 1: return status.Error(10003, "小区不能为空") case req.Id < 1: return status.Error(10003, "id不能为空") } return nil } // func HouseDel(ctx context.Context, req *pb_v1.HouseDelRequest) (reply *pb_v1.HouseDelReply, err error) { reply = &pb_v1.HouseDelReply{} // 捕获各个task中的异常并返回给调用者 defer func() { if r := recover(); r != nil { err = fmt.Errorf("%+v", r) e := &status.Status{} if er := json.Unmarshal([]byte(err.Error()), e); er != nil { logger.Error("err", zap.String("system_err", err.Error()), zap.Stack("stacktrace")) } } }() err = checkHouseDelParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) oldHouse := dbmodel.NewHouse(dbname) where := map[string]interface{}{ "id": req.Id, //"garden_id":req.GardenId, } err = oldHouse.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if oldHouse.ID == 0 { return nil, errors.ErrRecordNotFound } if oldHouse.Status > HouseStatusWait { return nil, status.Error(10003, "已入住房屋, 不能删改房屋编号,单元号,楼栋号") } reply.Origin = &pb_v1.HouseUpdateRequest{ Id: req.Id, GardenId: req.GardenId, UnitId: oldHouse.UnitId, HouseNumber: oldHouse.HouseNumber, RoomCount: oldHouse.RoomCount, HallCount: oldHouse.HallCount, Layer: oldHouse.Layer, HouseType: oldHouse.HouseType, HouseUsedArea: oldHouse.HouseUsedArea, HouseArea: oldHouse.HouseArea, } house := dbmodel.NewHouse(dbname) where = map[string]interface{}{ "id": req.Id, } db := database.DB().Begin() defer func() { if err != nil { db.Rollback() } }() isBind, err := IsHouseBindCharge(req.GardenId, []int64{req.Id}, db) if err != nil { return nil, err } if isBind { return nil, status.Error(10003, "请先解除房屋绑定费用") } err = house.Delete(db, where) if err != nil { return nil, errors.DataBaseError } mreq := pb_v1.ObjStatisticSetRequest{ ObjType: statistic.ObjTypeHouse, TotalIncrease: int64(-1), GardenId: req.GardenId, } err = statistic.ObjStatisticSetWithDb(&mreq, db) if err != nil { return nil, err } if err = RefuseHouseApply(req.GardenId, oldHouse.BuildingId, oldHouse.UnitId, oldHouse.ID); err != nil { return nil, err } if err = houseCountChange(req.GardenId, int64(-1)); err != nil { return nil, err } db.Commit() return reply, nil }