// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package db import ( "context" "cp-organization-management/errors" "cp-organization-management/model" pb_v1 "cp-organization-management/pb/v1" "cp-organization-management/utils" "encoding/json" "fmt" "github.com/jinzhu/gorm" "github.com/jaryhe/gopkgs/database" "github.com/jaryhe/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" ) func initTable(db *gorm.DB, dbname string) error { err := model.NewDevice(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewRbacGroup(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewRbacUser(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewRbacNode(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewStaff(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewStaffType(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewStation(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewUserZone(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewTLog(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } err = model.NewZone(dbname).CreateTable(db) if err != nil { return errors.DataBaseError } zoneIndexSql := fmt.Sprintf("ALTER TABLE `%s`.`t_zone` ADD UNIQUE INDEX `code_index`(`zone_code`) USING BTREE", dbname) err = db.Exec(zoneIndexSql).Error if err != nil { return errors.DataBaseError } p := model.NewZone(dbname) p.ZoneCode = "001" p.ParentZoneCode = "000" p.ZoneName = "全部区域" err = p.Insert(database.DB()) if err != nil { return errors.DataBaseError } return nil } func initDb(ctx context.Context, organizationCode string) error { dbname := utils.GetDbName(organizationCode) // create database createDbSql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s DEFAULT CHARSET utf8 COLLATE utf8_general_ci;", dbname) db := database.DB() err := db.Exec(createDbSql).Error if err != nil { return errors.DataBaseError } err = initTable(db, dbname) if err != nil { return errors.DataBaseError } createViewSql := fmt.Sprintf("CREATE VIEW `%s`.`view_user` AS select t1.*, t2.zone_code, t3.zone_name, t3.level as zone_level from %s.t_rbac_user as t1 left join %s.t_user_zone as t2 on t1.id=t2.user_id left join %s.t_zone as t3 on t2.zone_code=t3.zone_code;", dbname, dbname, dbname, dbname) err = db.Exec(createViewSql).Error if err != nil { return errors.DataBaseError } return nil } // 初始化机构schema func InitOrganizationDb(ctx context.Context, req *pb_v1.InitOrganizationDbRequest) (reply *pb_v1.InitOrganizationDbReply, err error) { reply = &pb_v1.InitOrganizationDbReply{} // 捕获各个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")) } } }() if req.OrganizationCode == "" { return reply, status.Error(10003, "参数错误,机构编码为空") } err = initDb(ctx, req.OrganizationCode) return reply, err }