package garden 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/model" pb_v1 "property-garden/pb/v1" "property-garden/utils" ) func initTable(db *gorm.DB, dbname string) error { err := model.NewBuilding(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewUnit(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewHouse(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewChargeConf(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewChargeBind(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewChargeBill(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewChargeBillDel(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewChargeBillPayed(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewChargePower(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewChargeUrge(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewChargeOrder(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewPark(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewMsg(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewParkSpace(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewVehicle(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewHouseApprovedGarden(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewPropertyPackage(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewRepairOrder(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewSuggestionOrder(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewRepairClass(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewAnnouncement(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewHouseRentManager(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewHouseRent(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewHouseholdUser(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewEvent(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewEventSign(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewServicePhone(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewServicePhoneClass(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewStatisticObj(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewStatisticRepair(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewVote(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewNeighborClass(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewNeighborArticle(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewNeighborComment(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewNeighborLike(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewBuildingManager(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } return nil } func initView(db *gorm.DB, dbname string) error { err := model.NewBuildingUnit(dbname).CreateView(db, dbname) if err != nil { return errors.DataBaseError } err = model.NewHouseUnitBuilding(dbname).CreateView(db, dbname) if err != nil { return errors.DataBaseError } err = model.NewSpacePark(dbname).CreateView(db, dbname) if err != nil { return errors.DataBaseError } err = model.NewServicePhoneView(dbname).CreateView(db, dbname) if err != nil { return errors.DataBaseError } return nil } func initDb(gardenId int64) (err error) { dbname := utils.GetGardenDbName(gardenId) db := database.DB() // 创建库 createDbSql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_bin';", dbname) err = db.Exec(createDbSql).Error if err != nil { return errors.DataBaseError } defer func() { if err != nil { fmt.Printf("drop database\n") sql := fmt.Sprintf("drop database %s", dbname) db.Exec(sql) } }() // 创建表 err = initTable(db, dbname) if err != nil { return errors.DataBaseError } // 创建视图 err = initView(db, dbname) if err != nil { return errors.DataBaseError } return nil } func GardenInitDb(ctx context.Context, req *pb_v1.GardenInitDbRequest) (reply *pb_v1.GardenInitDbReply, err error) { reply = &pb_v1.GardenInitDbReply{} // 捕获各个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 = initDb(req.GardenId) if err != nil { return nil, err } return reply, nil }