123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- 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
- }
|