159 lines
5.4 KiB
Go
159 lines
5.4 KiB
Go
|
package database
|
|||
|
|
|||
|
import (
|
|||
|
"gorm.io/gorm"
|
|||
|
)
|
|||
|
|
|||
|
const (
|
|||
|
DEFAULTPWD = "123456"
|
|||
|
DEFAULTUSR = "admin"
|
|||
|
OFFLINE_DB = "database.ydb"
|
|||
|
)
|
|||
|
|
|||
|
var sourceDBMap = map[string]SourceObj{}
|
|||
|
|
|||
|
/*服务器端MySQL的连接实例*/
|
|||
|
var ormIncestance *gorm.DB
|
|||
|
|
|||
|
/*获取服务端的数据库连接实例*/
|
|||
|
func GetORMDBInstance() *gorm.DB {
|
|||
|
return ormIncestance
|
|||
|
}
|
|||
|
func SetORMDBInstance(db *gorm.DB) {
|
|||
|
ormIncestance = db
|
|||
|
sourceDBMap = make(map[string]SourceObj)
|
|||
|
InitTable()
|
|||
|
}
|
|||
|
|
|||
|
type DataSource struct {
|
|||
|
Url string //数据请求地址
|
|||
|
Type string //数据类型
|
|||
|
}
|
|||
|
|
|||
|
type SourceInfo struct {
|
|||
|
MaxLevel int `json:"maximumLevel"`
|
|||
|
MinLevel int `json:"minimumLevel"`
|
|||
|
West string `json:"west"`
|
|||
|
South string `json:"south"`
|
|||
|
East string `json:"east"`
|
|||
|
North string `json:"north"`
|
|||
|
ProFile string `json:"pro_file"`
|
|||
|
TilingScheme int `json:"tiling_scheme"` //是否需要加投影
|
|||
|
}
|
|||
|
type SourceObj struct {
|
|||
|
DataSource
|
|||
|
DB *gorm.DB //链接对象
|
|||
|
Gzip bool //是否压缩
|
|||
|
ContentType string //响应头
|
|||
|
Info SourceInfo //资源信息
|
|||
|
}
|
|||
|
|
|||
|
// 保存资源连接对象
|
|||
|
func SetSourceDB(sourceId string, obj SourceObj) {
|
|||
|
sourceDBMap[sourceId] = obj
|
|||
|
}
|
|||
|
|
|||
|
// 获取资源连接对象
|
|||
|
func GetSourceDB(sourceId string) SourceObj {
|
|||
|
return sourceDBMap[sourceId]
|
|||
|
}
|
|||
|
|
|||
|
type SOURCE struct {
|
|||
|
//gorm.Model
|
|||
|
//ProjectID string `json:"project_id" gorm:"type:varchar(128);comment:'项目id'" dc:"项目id"`
|
|||
|
//SourceName string `json:"source_name" gorm:"type:varchar(128);comment:'资源名称'" dc:"资源名称"`
|
|||
|
SourceID string `json:"source_id" gorm:"type:varchar(128);comment:'资源id'" dc:"资源id"`
|
|||
|
//SourceType string `json:"source_type" gorm:"type:varchar(128);comment:'资源类型'" dc:"资源类型"`
|
|||
|
//IsShow int `json:"is_show" gorm:"type:int(11);comment:'初次渲染的时候是否显示'" dc:"初次渲染的时候是否显示"`
|
|||
|
//Detail string `json:"detail" gorm:"comment:'资源的一些自带参数,序列话的字符串'" dc:"资源的一些自带参数,序列话的字符串"`
|
|||
|
//PID string `json:"p_id" gorm:"type:varchar(128);comment:'父id'" dc:"父id"`
|
|||
|
SourcePath string `json:"source_path" gorm:"type:varchar(512);comments:'资源路径'" dc:"资源路径"`
|
|||
|
//LayerIndex int `json:"layer_index" gorm:"type:int(11);comment:'二维图层在地球上的顺序序号'" dc:"二维图层在地球上的顺序序号"`
|
|||
|
//TreeIndex int `json:"tree_index" gorm:"type:int(11);comment:'结构树上的顺序序号'" dc:"结构树上的顺序序号"`
|
|||
|
//RichText string `json:"rich_text" comments:"富文本内容" dc:"富文本内容"`
|
|||
|
}
|
|||
|
|
|||
|
func (SOURCE) TableName() string {
|
|||
|
return "zmkg_gis_sources"
|
|||
|
}
|
|||
|
|
|||
|
type USER struct {
|
|||
|
gorm.Model
|
|||
|
Username string `json:"username"`
|
|||
|
Password string `json:"password"`
|
|||
|
}
|
|||
|
|
|||
|
func InitTable() {
|
|||
|
createMarkerTable()
|
|||
|
createUserTable()
|
|||
|
createGfbInfoTable()
|
|||
|
}
|
|||
|
func createMarkerTable() {
|
|||
|
if !GetORMDBInstance().Migrator().HasTable(&SOURCE{}) {
|
|||
|
GetORMDBInstance().Migrator().AutoMigrate(&SOURCE{})
|
|||
|
}
|
|||
|
}
|
|||
|
func createUserTable() {
|
|||
|
if !GetORMDBInstance().Migrator().HasTable(&USER{}) {
|
|||
|
err := GetORMDBInstance().Migrator().AutoMigrate(&USER{})
|
|||
|
if err != nil {
|
|||
|
return
|
|||
|
}
|
|||
|
user := USER{Username: DEFAULTUSR, Password: DEFAULTPWD}
|
|||
|
users := []USER{user}
|
|||
|
GetORMDBInstance().Create(&users)
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/*光伏板导入*/
|
|||
|
type GFB struct {
|
|||
|
ProjectID string `json:"project_id" gorm:"type:varchar(128);comment:'项目id'" dc:"项目id"`
|
|||
|
PhotovoltaicNumber string `json:"photovoltaic_number"`
|
|||
|
PhotovoltaicNumberGis string `json:"photovoltaic_number_gis"`
|
|||
|
}
|
|||
|
|
|||
|
func (GFB) TableName() string {
|
|||
|
return "bus_scheduled_weekly_photovoltaic"
|
|||
|
}
|
|||
|
|
|||
|
type FzInfo struct {
|
|||
|
gorm.Model
|
|||
|
ProjectID string `json:"project_id" gorm:"type:varchar(128);comment:'项目id'" dc:"项目id"`
|
|||
|
PhotovoltaicNumber string `json:"photovoltaic_number"gorm:"type:varchar(128);comment:'光伏板编号(candela里面的实例名称)'" dc:"光伏板编号" `
|
|||
|
ZhuangBianHao string `json:"zhuang_bian_hao"gorm:"type:varchar(255);comment:'桩编号'" dc:"桩编号" `
|
|||
|
X string `json:"x"gorm:"type:varchar(255);comment:'X'" dc:"X" `
|
|||
|
Y string `json:"y"gorm:"type:varchar(255);comment:'Y'" dc:"Y" `
|
|||
|
ZTop string `json:"z_top"gorm:"type:varchar(255);comment:'顶部高程'" dc:"顶部高程" `
|
|||
|
ZBottom string `json:"z_bottom"gorm:"type:varchar(255);comment:'底部高程'" dc:"底部高程" `
|
|||
|
FZName string `json:"fz_name"gorm:"type:varchar(255);comment:'方阵名称'" dc:"方阵名称" `
|
|||
|
//ZhuangJianJu string `json:"zhuang_jian_ju"gorm:"type:varchar(255);comment:'桩间距'" dc:"桩间距" `
|
|||
|
//ColorNum int `json:"color_num"gorm:"comment:'颜色编号" dc:"颜色编号" `
|
|||
|
//Positions string `json:"positions"gorm:"comment:'光伏板桩号坐标'" dc:"光伏板桩号坐标" `
|
|||
|
//Params string `json:"params"gorm:"comment:'参数'" dc:"参数" `
|
|||
|
}
|
|||
|
|
|||
|
func (FzInfo) TableName() string {
|
|||
|
return "zmkg_fz_infos"
|
|||
|
}
|
|||
|
|
|||
|
func createGfbInfoTable() {
|
|||
|
if !GetORMDBInstance().Migrator().HasTable(&FzInfo{}) {
|
|||
|
GetORMDBInstance().Migrator().AutoMigrate(&FzInfo{})
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
type Project struct {
|
|||
|
ID int64 `json:"id"`
|
|||
|
}
|
|||
|
|
|||
|
func (Project) TableName() string {
|
|||
|
return "sys_project"
|
|||
|
}
|
|||
|
func CheckProjectIsExists(project_id int64) bool {
|
|||
|
RowsAffected := GetORMDBInstance().Model(&Project{}).Where(&Project{project_id}).Find(&Project{}).RowsAffected
|
|||
|
if RowsAffected > 0 {
|
|||
|
return true
|
|||
|
}
|
|||
|
return false
|
|||
|
}
|