package house 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-household/errors" dbmodel "property-household/model" pb_v1 "property-household/pb/v1" ) func checkHouseholdDelHouseParam(req *pb_v1.HouseholdDelHouseRequest) error { if req.GardenId == 0 || req.HouseId == 0 || req.HouseholdUid == 0 { return errors.ParamsError } return nil } // func HouseholdDelHouse(ctx context.Context, req *pb_v1.HouseholdDelHouseRequest) (reply *pb_v1.HouseholdDelHouseReply, err error) { reply = &pb_v1.HouseholdDelHouseReply{} // 捕获各个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 = checkHouseholdDelHouseParam(req) // 获取原始数据 household := dbmodel.THouseApproved{} where := map[string]interface{}{ "uid": req.HouseholdUid, "house_id": req.HouseId, "garden_id": req.GardenId, } err = household.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if household.ID == 0 { return nil, errors.ErrRecordNotFound } db := database.DB().Begin() err = household.Delete(db, where) if err != nil { db.Rollback() return nil, errors.DataBaseError } rent := dbmodel.THouseRent{} where = map[string]interface{}{ "household_uid": req.HouseholdUid, "house_id": req.HouseId, "garden_id": req.GardenId, } err = rent.Delete(db, where) if err != nil { db.Rollback() return nil, errors.DataBaseError } if len(req.RentIds) > 0 { appoint := dbmodel.THouseRentAppointment{} where = map[string]interface{}{ "rent_id in": req.RentIds, } err = appoint.Delete(db, where) if err != nil { db.Rollback() return nil, errors.DataBaseError } } db.Commit() return reply, nil }