提交 0dd6348a authored 作者: Jack Jie's avatar Jack Jie

Initial commit

上级
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/pg2es-test.iml" filepath="$PROJECT_DIR$/.idea/pg2es-test.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
mysql:
host: "192.168.60.10"
user: "root"
password: "gtmap"
dbname: "gtc_db"
port: "3306"
timezone: "Asia/Shanghai"
charset: "UTF8"
postgres:
host: "192.168.2.54"
user: "system"
password: "gtis"
dbname: "test"
port: "54321"
sslmode: "disable"
timezone: "Asia/Shanghai"
charset: "UTF8"
schemaname: "public"
elasticsearch:
url: "http://192.168.2.194:9200"
index: "scp-platform-"
query:
start_time: "2024-06-21 00:00:00"
end_time: "2024-12-01 00:00:00"
module mysql2es-test
go 1.22
require (
github.com/google/uuid v1.6.0
github.com/olivere/elastic/v7 v7.0.32
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/mysql v1.5.7
gorm.io/driver/postgres v1.5.7
gorm.io/gorm v1.25.11
)
require (
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.4.3 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/text v0.18.0 // indirect
)
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.4.3 h1:cxFyXhxlvAifxnkKKdlxv8XqUf59tDlYjnV5YYfsJJY=
github.com/jackc/pgx/v5 v5.4.3/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/olivere/elastic/v7 v7.0.32 h1:R7CXvbu8Eq+WlsLgxmKVKPox0oOwAE/2T9Si5BnvK6E=
github.com/olivere/elastic/v7 v7.0.32/go.mod h1:c7PVmLe3Fxq77PIfY/bZmxY/TAamBhCzZ8xDOE09a9k=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.5.7 h1:MndhOPYOfEp2rHKgkZIhJ16eVUIRf2HmzgoPmh7FCWo=
gorm.io/driver/mysql v1.5.7/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
gorm.io/driver/postgres v1.5.7 h1:8ptbNJTDbEmhdr62uReG5BGkdQyeasu/FZHxI0IMGnM=
gorm.io/driver/postgres v1.5.7/go.mod h1:3e019WlBaYI5o5LIdNV+LyxCMNtLOQETBXL2h4chKpA=
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/gorm v1.25.11 h1:/Wfyg1B/je1hnDx3sMkX+gAlxrlZpn6X0BXRlwXlvHg=
gorm.io/gorm v1.25.11/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
差异被折叠。
package main
import (
"gorm.io/gorm"
)
type Service struct {
Id string `gorm:"primaryKey;type:char(32)" json:"id"`
ServiceName string `gorm:"type:varchar(255);comment:服务名称" json:"service_name"`
Url string `gorm:"type:varchar(255);comment:服务对外地址" json:"url"`
DelFlag int8 `gorm:"comment:是否删除 0:正常 1:已删除" json:"del_flag"`
}
type RelationshipsThemeService struct {
Id string `gorm:"primaryKey;type:char(32)" json:"id"`
ServiceId string `gorm:"type:char(32);comment:服务 id" json:"service_id"`
ThemeId string `gorm:"type:char(32);comment:服务主题 id" json:"theme_id"`
DelFlag int8 `gorm:"comment:是否删除 0:正常 1:已删除" json:"del_flag"`
}
type ServiceTheme struct {
Id string `gorm:"primaryKey;type:char(32)" json:"id"`
ThemeCode string `gorm:"type:varchar(255);comment:服务主题编码" json:"theme_code"`
ThemeName string `gorm:"type:varchar(45);comment:服务主题名称" json:"theme_name"`
DelFlag int8 `gorm:"comment:是否删除 0:正常 1:已删除" json:"del_flag"`
}
type ServiceResult struct {
Id string `json:"id"`
ServiceName string `json:"service_name"`
Url string `json:"url"`
ThemeCode string `json:"theme_code"`
ThemeName string `json:"theme_name"`
}
func GetServiceThemeInfo(schemaName string, db *gorm.DB) (map[string]ServiceResult, error) {
var results []ServiceResult
serviceMap := make(map[string]ServiceResult)
// 动态生成表名
serviceInfoTable := schemaName + ".service_info"
relationshipsTable := schemaName + ".relationships_theme_service"
themeTable := schemaName + ".theme_service"
// 查询所有符合条件的 service_info, relationships_theme_service, 和 service_theme 表
err := db.Table(serviceInfoTable).
Select("service_info.id, service_info.service_name, service_info.url, theme_service.theme_code, theme_service.theme_name").
Joins("JOIN " + relationshipsTable + " ON service_info.id = " + relationshipsTable + ".service_id").
Joins("JOIN " + themeTable + " ON " + relationshipsTable + ".theme_id = " + themeTable + ".id").
Where("service_info.del_flag = 0 AND " + relationshipsTable + ".del_flag = 0 AND " + themeTable + ".del_flag = 0").
Scan(&results).Error
if err != nil {
return nil, err
}
// 将结果按 service_id 存入 map
for _, result := range results {
serviceMap[result.Id] = result
}
return serviceMap, nil
}
// ServiceAccess 服务接入信息
type ServiceAccess struct {
Id string `gorm:"primaryKey;type:char(32)" json:"id"` // 主键id
BuilderId string `gorm:"type:char(32);comment:建设单位id 需求方id" json:"builder_id"` // 建设单位id 需求方id
AccessUuid string `gorm:"type:varchar(70);comment:接入唯一码" json:"access_uuid"` // 接入唯一码
DelFlag int8 `gorm:"comment:是否删除 0:正常 1:已删除" json:"del_flag"` // 是否删除 0:正常 1:已删除
}
// AccessBuilder 建设单位信息
type AccessBuilder struct {
Id string `gorm:"primaryKey;type:char(32)" json:"id"` // 主键id
SystemName string `gorm:"type:varchar(45);comment:系统名称" json:"system_name"` // 系统名称
DelFlag int8 `gorm:"comment:是否删除 0:正常 1:已删除" json:"del_flag"` // 是否删除 0:正常 1:已删除
}
// GetServiceAccessInfo 查询服务接入信息和建设单位信息
func GetServiceAccessInfo(db *gorm.DB) (map[string]string, error) {
var results []struct {
AccessUuid string
SystemName string
}
// 执行查询,关联 service_access 和 access_builder 表,并且保证各个表的 del_flag = 0
err := db.Table("service_access").
Select("service_access.access_uuid, access_builder.system_name").
Joins("JOIN access_builder ON service_access.builder_id = access_builder.id").
Where("service_access.del_flag = 0 AND access_builder.del_flag = 0").
Scan(&results).Error
if err != nil {
return nil, err
}
// 将查询结果存入 map[string]string
resultMap := make(map[string]string)
for _, result := range results {
resultMap[result.AccessUuid] = result.SystemName
}
return resultMap, nil
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论