123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- package household
- import (
- "context"
- "encoding/json"
- "fmt"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- "gorm.io/gorm"
- "property-garden/errors"
- "property-garden/impl/v1/charge_utils"
- "property-garden/impl/v1/oss_utils"
- "property-garden/impl/v1/statistic"
- dbmodel "property-garden/model"
- "property-garden/pb"
- pb_v1 "property-garden/pb/v1"
- "property-garden/utils"
- "strings"
- "time"
- )
- func checkGardenHouseholdDelHouseParam(req *pb_v1.GardenHouseholdDelHouseRequest) error {
- if req.GardenId == 0 || req.HouseId == 0 || req.HouseholdUid == 0 {
- return errors.ParamsError
- }
- return nil
- }
- func delVehicle(req *pb_v1.GardenHouseholdDelHouseRequest, dbname string, db *gorm.DB) ([]int64, error) {
- vehicleIds := []int64{}
- vehicle := dbmodel.NewVehicle(dbname)
- where := map[string]interface{}{
- "household_uid": req.HouseholdUid,
- }
- vlist, err := vehicle.List(db, where, nil, -1, -1)
- if err != nil {
- return nil, errors.DataBaseError
- }
- for _, v := range vlist {
- vehicleIds = append(vehicleIds, v.ID)
- }
- err = vehicle.Delete(db, where)
- if err != nil {
- return nil, errors.DataBaseError
- }
- chargeBind := dbmodel.NewChargeBind(dbname)
- where = map[string]interface{}{
- "obj_type": charge_utils.ObjTypeVehicle,
- "obj_id in": vehicleIds,
- }
- err = chargeBind.Delete(db, where)
- if err != nil {
- return nil, errors.DataBaseError
- }
- mreq := pb_v1.ObjStatisticSetRequest{
- ObjType: statistic.ObjTypeVehicle,
- TotalIncrease: int64(0 - len(vehicleIds)),
- GardenId: req.GardenId,
- }
- err = statistic.ObjStatisticSetWithDb(&mreq, db)
- if err != nil {
- return nil, err
- }
- return vehicleIds, nil
- }
- func changeSpaceByHouseId(req *pb_v1.GardenHouseholdDelHouseRequest, dbname string, db *gorm.DB) error {
- p := dbmodel.NewParkSpace(dbname)
- where := map[string]interface{}{
- "house_id": req.HouseId,
- }
- spaceIds := []int64{}
- list, err := p.List(db, where, nil, -1, -1)
- if err != nil {
- return errors.DataBaseError
- }
- for _, v := range list {
- spaceIds = append(spaceIds, v.ID)
- }
- values := map[string]interface{}{
- "vehicle_id": 0,
- "status": 1,
- "bind_status": 1,
- "updated_at": time.Now(),
- }
- err = p.Update(db, where, values)
- if err != nil {
- return errors.DataBaseError
- }
- chargeBind := dbmodel.NewChargeBind(dbname)
- where = map[string]interface{}{
- "obj_type": charge_utils.ObjTypeSpace,
- "obj_id in": spaceIds,
- }
- err = chargeBind.Delete(db, where)
- if err != nil {
- return errors.DataBaseError
- }
- return nil
- }
- func changeSpaceByVehicleId(vehicleIds []int64, dbname string, db *gorm.DB) error {
- p := dbmodel.NewParkSpace(dbname)
- where := map[string]interface{}{
- "vehicle_id in": vehicleIds,
- }
- values := map[string]interface{}{
- "vehicle_id": 0,
- "updated_at": time.Now(),
- }
- err := p.Update(db, where, values)
- if err != nil {
- return errors.DataBaseError
- }
- return nil
- }
- func delRentInfo(req *pb_v1.GardenHouseholdDelHouseRequest, dbname string, db *gorm.DB) ([]int64, error) {
- rent := dbmodel.NewHouseRent(dbname)
- where := map[string]interface{}{
- "household_uid": req.HouseholdUid,
- "house_id": req.HouseId,
- }
- rentIds := []int64{}
- rlist, err := rent.List(db, where, nil, -1, -1)
- if err != nil {
- return nil, errors.DataBaseError
- }
- for _, v := range rlist {
- rentIds = append(rentIds, v.ID)
- }
- if err := rent.Delete(db, where); err != nil {
- return nil, errors.DataBaseError
- }
- // 删除租房经纪人信息
- manager := dbmodel.NewHouseRentManager(dbname)
- where = map[string]interface{}{
- "rent_id in": rentIds,
- }
- if err := manager.Delete(db, where); err != nil {
- return nil, errors.DataBaseError
- }
- return rentIds, nil
- }
- //
- func GardenHouseholdDelHouse(ctx context.Context, req *pb_v1.GardenHouseholdDelHouseRequest) (reply *pb_v1.GardenHouseholdDelHouseReply, err error) {
- reply = &pb_v1.GardenHouseholdDelHouseReply{}
- // 捕获各个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 = checkGardenHouseholdDelHouseParam(req)
- dbname := utils.GetGardenDbName(req.GardenId)
- // 获取原始数据
- household := dbmodel.NewHouseApprovedGarden(dbname)
- where := map[string]interface{}{
- "uid": req.HouseholdUid,
- "house_id": req.HouseId,
- }
- householdList, err := household.List(database.DB(), where, nil, -1, -1)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataBaseError
- }
- if len(householdList) == 0 {
- return nil, errors.ErrRecordNotFound
- }
- // 检查账单
- bill := dbmodel.NewChargeBill(dbname)
- where = map[string]interface{}{
- "house_id": req.HouseId,
- }
- billCount, err := bill.Count(database.DB(), where, nil)
- if err != nil {
- return nil, errors.DataBaseError
- }
- if billCount > 0 {
- return nil, status.Error(10003, "该房屋有账单未支付,无法退出")
- }
- where = map[string]interface{}{
- "uid": req.HouseholdUid,
- }
- householdCount, err := household.Count(database.DB(), where, nil)
- if err != nil {
- return nil, errors.DataBaseError
- }
- where = map[string]interface{}{
- "house_id": req.HouseId,
- }
- houseCount, err := household.Count(database.DB(), where, nil)
- if err != nil {
- return nil, errors.DataBaseError
- }
- db := database.DB().Begin()
- defer func() {
- if err != nil {
- db.Rollback()
- }
- }()
- vehicleIds := []int64{}
- // 若该住户在该小区没有其他房屋,删除车,并
- if householdCount == 1 {
- vehicleIds, err = delVehicle(req, dbname, db)
- if err != nil {
- return nil, err
- }
- }
- // 若该房屋在该小区没有其他住户,更新该房屋车位信息
- if houseCount == 1 {
- err = changeSpaceByHouseId(req, dbname, db)
- } else if len(vehicleIds) > 0 {
- // 若该房屋在该小区有其他住户,更新车辆对应的车位
- err = changeSpaceByVehicleId(vehicleIds, dbname, db)
- }
- if err != nil {
- return nil, err
- }
- // 删除租房信息
- rentIds, err := delRentInfo(req, dbname, db)
- if err != nil {
- return nil, err
- }
- where = map[string]interface{}{
- "uid": req.HouseholdUid,
- "house_id": req.HouseId,
- }
- // 解除住户房屋绑定关系
- err = household.Delete(db, where)
- if err != nil {
- return nil, errors.DataBaseError
- }
- mreq := pb_v1.HouseholdDelHouseRequest{
- GardenId: req.GardenId,
- HouseId: req.HouseId,
- HouseholdUid: req.HouseholdUid,
- RentIds: rentIds,
- }
- // 删除住户库里的相关信息
- _, err = pb.Household.HouseholdDelHouse(ctx, &mreq)
- if err != nil {
- return nil, err
- }
- db.Commit()
- outList := []string{}
- for _, v := range householdList {
- array := strings.Split(v.Appendix, ";")
- for _, out := range array {
- if out != "" {
- outList = append(outList, out)
- }
- }
- }
- oss_utils.OssObjAdd(nil, outList)
- return reply, nil
- }
|