Skip to content

Commit

Permalink
Merge pull request #31 from weiwiegeng/main
Browse files Browse the repository at this point in the history
feat: 1. 清理社区版脏数据
  • Loading branch information
xinjiayu authored Mar 11, 2024
2 parents 70715f6 + 279d174 commit c5bcc5b
Show file tree
Hide file tree
Showing 8 changed files with 1,628 additions and 2,405 deletions.
9 changes: 9 additions & 0 deletions internal/consts/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ const (
DatabaseTypeMysql = "mysql" // mysql
DatabaseTypePostgresql = "pgsql" // postgresql
)

const (
TdEngineDbNameKey = "tsd.tdengine.dbName"
TdEngineTypeKey = "tsd.tdengine.type"
TdEngineDsnKey = "tsd.tdengine.dsn"
TdEngineMaxIdleConnsKey = "tsd.tdengine.maxIdleConns"
TdEngineMaxOpenConnsKey = "tsd.tdengine.maxOpenConns"
TdEngineDbName = "sagoo_iot"
)
4 changes: 2 additions & 2 deletions internal/logic/product/dev_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ func (s *sDevInit) InitDeviceForTd(ctx context.Context) (err error) {
for _, d := range list {

// 检测设备表是否创建TD表的标识
if d.MetadataTable == 1 {
/*if d.MetadataTable == 1 {
continue
}
}*/

pd, err := service.DevProduct().Detail(ctx, d.ProductKey)
if err != nil {
Expand Down
94 changes: 48 additions & 46 deletions internal/logic/system/sys_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
Expand Down Expand Up @@ -259,16 +260,55 @@ func (s *sSysMenu) Del(ctx context.Context, menuId int64) (err error) {
if menu == nil {
return gerror.New("ID错误")
}
errorString := checkMenuJoin(ctx, menuId)
if errorString != "" {
return gerror.New(errorString)
//查询是否有子节点
num, _ := dao.SysMenu.Ctx(ctx).Where(g.Map{
dao.SysMenu.Columns().ParentId: menuId,
dao.SysMenu.Columns().IsDeleted: 0,
}).Count()
if num > 0 {
return gerror.New("请先删除子节点!")
}

//获取当前登录用户ID
loginUserId := service.Context().GetUserId(ctx)
_, err = dao.SysMenu.Ctx(ctx).Data(g.Map{
dao.SysMenu.Columns().DeletedBy: uint(loginUserId),
dao.SysMenu.Columns().DeletedAt: gtime.Now(),
dao.SysMenu.Columns().IsDeleted: 1,
}).Where(dao.SysMenu.Columns().Id, menuId).Update()

err = dao.SysMenu.Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
//删除菜单
_, err = dao.SysMenu.Ctx(ctx).Data(g.Map{
dao.SysMenu.Columns().DeletedBy: uint(loginUserId),
dao.SysMenu.Columns().DeletedAt: gtime.Now(),
dao.SysMenu.Columns().IsDeleted: 1,
}).Where(dao.SysMenu.Columns().Id, menuId).Update()

//删除关联Api
_, err = dao.SysMenuApi.Ctx(ctx).Data(g.Map{
dao.SysMenuApi.Columns().IsDeleted: 1,
dao.SysMenuApi.Columns().DeletedBy: uint(loginUserId),
dao.SysMenuApi.Columns().DeletedAt: gtime.Now()}).Where(dao.SysMenuApi.Columns().MenuId, menuId).Update()
if err != nil {
return
}

//删除关联按钮
_, err = dao.SysMenuButton.Ctx(ctx).Data(g.Map{
dao.SysMenuButton.Columns().IsDeleted: 1,
dao.SysMenuButton.Columns().DeletedBy: uint(loginUserId),
dao.SysMenuButton.Columns().DeletedAt: gtime.Now()}).Where(dao.SysMenuButton.Columns().MenuId, menuId).Update()
if err != nil {
return
}

//删除关联列表
_, err = dao.SysMenuColumn.Ctx(ctx).Data(g.Map{
dao.SysMenuColumn.Columns().IsDeleted: 1,
dao.SysMenuColumn.Columns().DeletedBy: uint(loginUserId),
dao.SysMenuColumn.Columns().DeletedAt: gtime.Now()}).Where(dao.SysMenuColumn.Columns().MenuId, menuId).Update()
if err != nil {
return
}

return
})
//获取所有的菜单
_, err = s.GetAll(ctx)
if err != nil {
Expand All @@ -277,44 +317,6 @@ func (s *sSysMenu) Del(ctx context.Context, menuId int64) (err error) {
return
}

// 查询关联数据是否存在
func checkMenuJoin(ctx context.Context, menuId int64) string {
num := 0
//查询是否有子节点
num, _ = dao.SysMenu.Ctx(ctx).Where(g.Map{
dao.SysMenu.Columns().ParentId: menuId,
dao.SysMenu.Columns().IsDeleted: 0,
}).Count()
if num > 0 {
return "请先删除子节点!"
}
//查询关联Api
num, _ = dao.SysMenuApi.Ctx(ctx).Where(g.Map{
dao.SysMenuApi.Columns().MenuId: menuId,
dao.SysMenuApi.Columns().IsDeleted: 0,
}).Count()
if num > 0 {
return "存在菜单API关联!"
}
//查询关联列表
num, _ = dao.SysMenuButton.Ctx(ctx).Where(g.Map{
dao.SysMenuButton.Columns().MenuId: menuId,
dao.SysMenuButton.Columns().IsDeleted: 0,
}).Count()
if num > 0 {
return "存在菜单按钮关联!"
}
//查询关联按钮
num, _ = dao.SysMenuColumn.Ctx(ctx).Where(g.Map{
dao.SysMenuColumn.Columns().MenuId: menuId,
dao.SysMenuColumn.Columns().IsDeleted: 0,
}).Count()
if num > 0 {
return "存在菜单列表关联!"
}
return ""
}

// GetData 执行获取数据操作
func (s *sSysMenu) GetData(ctx context.Context, title string, status int) (data []*model.SysMenuOut, err error) {
m := dao.SysMenu.Ctx(ctx)
Expand Down
16 changes: 9 additions & 7 deletions internal/logic/tdengine/td_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tdengine
import (
"context"
"database/sql"
"sagooiot/internal/consts"
"sagooiot/internal/service"
"sync"
"time"
Expand All @@ -26,13 +27,13 @@ func tdEngineNew() *sTdEngine {
return _instance
}

var dbName = "sagoo_iot"
var dbName = consts.TdEngineDbName

// 初始化函数
func init() {
service.RegisterTdEngine(tdEngineNew())
// 简化配置获取过程
dbName = g.Cfg().MustGet(context.Background(), "tsd.tdengine.dbName", "sagoo_iot").String()
dbName = g.Cfg().MustGet(context.Background(), consts.TdEngineDbNameKey, consts.TdEngineDbName).String()
}

type connections struct {
Expand Down Expand Up @@ -67,8 +68,8 @@ func (s *sTdEngine) GetConn(ctx context.Context, dbName string) (*sql.DB, error)

// 初始化连接映射
func initConnectionMap(ctx context.Context) *connections {
driver := g.Cfg().MustGet(ctx, "tsd.tdengine.type")
dsn := g.Cfg().MustGet(ctx, "tsd.tdengine.dsn")
driver := g.Cfg().MustGet(ctx, consts.TdEngineTypeKey)
dsn := g.Cfg().MustGet(ctx, consts.TdEngineDsnKey)
return &connections{
tdEngineType: driver.String(),
tdDsn: dsn.String(),
Expand All @@ -92,16 +93,17 @@ func createNewConnection(ctx context.Context, dbName string) (*sql.DB, error) {
}

// 配置连接池设置
connection.SetMaxIdleConns(g.Cfg().MustGet(ctx, "tdengine.maxIdleConns").Int())
connection.SetMaxOpenConns(g.Cfg().MustGet(ctx, "tdengine.maxOpenConns").Int())
// 配置连接池设置
connection.SetMaxIdleConns(g.Cfg().MustGet(ctx, consts.TdEngineMaxIdleConnsKey).Int())
connection.SetMaxOpenConns(g.Cfg().MustGet(ctx, consts.TdEngineMaxOpenConnsKey).Int())

connectionMap.ConnectionMap[dbName] = connection
return connection, nil
}

// Time REST连接时区处理
func (s *sTdEngine) Time(v *g.Var) (rs *g.Var) {
driver := g.Cfg().MustGet(context.TODO(), "tdengine.type")
driver := g.Cfg().MustGet(context.TODO(), consts.TdEngineTypeKey)
if driver.String() == "taosRestful" {
if t, err := time.Parse("2006-01-02 15:04:05 +0000 UTC", v.String()); err == nil {
rs = gvar.New(t.Local().Format("2006-01-02 15:04:05"))
Expand Down
4 changes: 2 additions & 2 deletions internal/logic/tdengine/tsl_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (s *sTSLTable) CheckStable(ctx context.Context, stable string) (b bool, err
}

var name string
if err = taos.QueryRow("SELECT stable_name FROM information_schema.ins_stables WHERE stable_name = '?' LIMIT 1", stable).Scan(&name); err != nil {
if err = taos.QueryRow("SELECT stable_name FROM information_schema.ins_stables WHERE db_name = '?' AND stable_name = '?' LIMIT 1", dbName, stable).Scan(&name); err != nil {
return
}
if name == "" {
Expand All @@ -265,7 +265,7 @@ func (s *sTSLTable) CheckTable(ctx context.Context, table string) (b bool, err e
}

var name string
if err = taos.QueryRowContext(ctx, "SELECT table_name FROM information_schema.ins_tables WHERE table_name = '?' LIMIT 1", table).Scan(&name); err != nil {
if err = taos.QueryRowContext(ctx, "SELECT table_name FROM information_schema.ins_tables WHERE db_name = '?' AND table_name = '?' LIMIT 1", dbName, table).Scan(&name); err != nil {
return
}
if name == "" {
Expand Down
Binary file modified manifest/docker-compose/iot-open/sagooiot
Binary file not shown.
Loading

0 comments on commit c5bcc5b

Please sign in to comment.