59eb34976a
iotlcm-admin-svr:管理后台服务。 - Go module:iotlcm → git.5iot.com/iotlcm-public/iotlcm-admin-svr(729 文件) - 凭据与地址脱敏:JWT signingKey(含代码里的 MustGet 默认值)、 MySQL DSN、Redis 口令、告警邮件发件人邮箱全部改为占位; 私网 IP → 文档地址段;库名 xiujie_admin → iotlcm - 真实手机号/邮箱 → 合法 11 位编造号 / example.com - 移除内部资产:CI 触发器、过时 DDL(sql/db_iotlcm.sql)、 私有依赖 go get 行、明文 registry 登录口令 - 启动 banner 统一 IOTLCM 方块字 + 社区惯例署名 - Dockerfile 改用公共基础镜像 - 拼错的 RADEME.md → README.md,README 重写 - 需要真实数据库的用例改为可用 IOTLCM_TEST_WITH_DB=1 显式启用 - 补 Apache-2.0 LICENSE,LF 行尾 + .gitattributes 数据库 schema 不随本仓发布(生产 DDL 已过时),README 已说明。
212 lines
6.6 KiB
Go
212 lines
6.6 KiB
Go
package memorydb
|
|
|
|
import (
|
|
"context"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/consts"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/dao"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/event"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model/entity"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/service"
|
|
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/utility/queue"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
)
|
|
|
|
type sMemDBSysDept struct {
|
|
}
|
|
|
|
var instanceMemDBSysDept *sMemDBSysDept
|
|
|
|
func NewMemDBSysDept() *sMemDBSysDept {
|
|
if instanceMemDBSysDept != nil {
|
|
return instanceMemDBSysDept
|
|
}
|
|
instanceMemDBSysDept = &sMemDBSysDept{}
|
|
initLoadMemoryDB := g.Cfg().MustGet(context.TODO(), "server.initLoadMemoryDB").Bool()
|
|
if initLoadMemoryDB {
|
|
instanceMemDBSysDept.Init(context.TODO())
|
|
}
|
|
return instanceMemDBSysDept
|
|
}
|
|
|
|
func init() {
|
|
s := NewMemDBSysDept()
|
|
service.RegisterMemDBSysDept(s)
|
|
RegisterMemDB(s.TableName(context.TODO()), s)
|
|
}
|
|
|
|
func (s *sMemDBSysDept) Init(ctx context.Context) error {
|
|
err := s.CreateTable(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = s.EventHandler(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *sMemDBSysDept) EventHandler(ctx context.Context) error {
|
|
eventList := []string{
|
|
consts.EventKeyDBSysDeptCreate,
|
|
consts.EventKeyDBSysDeptUpdate,
|
|
consts.EventKeyDBSysDeptDelete,
|
|
}
|
|
event.EventsInstance().Register(eventList, func(ctx context.Context, eventKey string, args ...interface{}) {
|
|
if len(args) < 1 {
|
|
g.Log().Errorf(ctx, "sMemDBSysDept.EventHandler event args error. eventKey: %s, args: %+v", eventKey, args)
|
|
return
|
|
}
|
|
pkList, ok := args[0].([]int64)
|
|
if !ok {
|
|
pk, ok := args[0].(int64)
|
|
if !ok {
|
|
g.Log().Errorf(ctx, "sMemDBSysDept.EventHandler event args error. eventKey: %s, args: %+v", eventKey, args)
|
|
return
|
|
}
|
|
pkList = []int64{pk}
|
|
}
|
|
if len(pkList) == 0 {
|
|
g.Log().Errorf(ctx, "sMemDBSysDept.EventHandler event args error. eventKey: %s, args: %+v", eventKey, args)
|
|
return
|
|
}
|
|
changeType := "edit"
|
|
if eventKey == consts.EventKeyDBSysDeptDelete {
|
|
changeType = "del"
|
|
}
|
|
if err := service.MemoryDB().EventHandler(ctx, &model.BroadcastDbChgCache{
|
|
TableName: s.TableName(ctx), PK: pkList, Type: changeType,
|
|
}); err != nil {
|
|
g.Log().Errorf(ctx, "refresh memory dept failed: %v", err)
|
|
return
|
|
}
|
|
|
|
dbType := "edit"
|
|
if eventKey == consts.EventKeyDBSysDeptCreate {
|
|
dbType = "add"
|
|
} else if eventKey == consts.EventKeyDBSysDeptDelete {
|
|
dbType = "del"
|
|
} else {
|
|
dbType = "edit"
|
|
}
|
|
queue.Push(ctx, consts.BroadcastDbChg, &model.BroadcastDbChgCache{
|
|
TableName: s.TableName(ctx),
|
|
PK: pkList,
|
|
Type: dbType,
|
|
Data: nil,
|
|
})
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func (s *sMemDBSysDept) TableName(ctx context.Context) string {
|
|
return dao.SysDept.Table()
|
|
}
|
|
|
|
func (s *sMemDBSysDept) CreateTable(ctx context.Context) error {
|
|
_, err := db.Exec(ctx, "DROP TABLE IF EXISTS "+s.TableName(ctx))
|
|
if err != nil {
|
|
g.Log().Errorf(ctx, "sMemDBSysDept.CreateTable delete table err: %v", err)
|
|
return err
|
|
}
|
|
|
|
total, err := db.Ctx(ctx).Model("sqlite_master").Where("type", "table").Where("name", dao.SysDept.Table()).Count()
|
|
if err != nil {
|
|
g.Log().Errorf(ctx, "sMemDBSysDept.CreateTable count err: %v", err)
|
|
return err
|
|
}
|
|
if total == 0 {
|
|
g.Log().Debug(ctx, "sMemDBSysDept.List create table ", s.TableName(ctx))
|
|
res, err := db.Exec(ctx, `
|
|
CREATE TABLE "sys_dept" (
|
|
"dept_id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
|
"tenant_id" TEXT DEFAULT '000000' CHECK(length("tenant_id") <= 20),
|
|
"parent_id" INTEGER DEFAULT 0,
|
|
"ancestors" TEXT DEFAULT '' CHECK(length("ancestors") <= 500),
|
|
"dept_key" TEXT DEFAULT '' CHECK(length("dept_key") == 15 OR "dept_key" == '' OR "dept_key" IS NULL),
|
|
"dept_name" TEXT DEFAULT '' CHECK(length("dept_name") <= 30),
|
|
"dept_type" INTEGER DEFAULT 0,
|
|
"dept_category" TEXT CHECK(length("dept_category") <= 100),
|
|
"credit_code" TEXT CHECK(length("credit_code") == 18 OR "credit_code" == '' OR "credit_code" IS NULL),
|
|
"area_code" TEXT CHECK(length("area_code") == 12 OR "area_code" == '' OR "area_code" IS NULL),
|
|
"address" TEXT CHECK(length("address") <= 200),
|
|
"order_num" INTEGER DEFAULT 0,
|
|
"leader" INTEGER,
|
|
"phone" TEXT CHECK(length("phone") <= 11),
|
|
"email" TEXT CHECK(length("email") <= 50),
|
|
"status" TEXT DEFAULT '0' ,
|
|
"created_dept" INTEGER,
|
|
"created_by" INTEGER NOT NULL,
|
|
"created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"updated_by" INTEGER,
|
|
"updated_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"deleted_by" INTEGER,
|
|
"deleted_at" DATETIME
|
|
);
|
|
`)
|
|
if err != nil {
|
|
g.Log().Errorf(ctx, "sMemDBSysDept.CreateTable create table err: %v", err)
|
|
return err
|
|
}
|
|
g.Log().Debug(ctx, "sMemDBSysDept.CreateTable create table success", res)
|
|
// 加载数据
|
|
return s.LoadData(ctx, 0)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *sMemDBSysDept) LoadData(ctx context.Context, pk int64) error {
|
|
count := 0
|
|
if pk == 0 {
|
|
maxDeptId := int64(0)
|
|
for {
|
|
page := 1
|
|
pageSize := 100
|
|
dataList := make([]*entity.SysDept, 0)
|
|
if err := dao.SysDept.Ctx(sourceContext(ctx)).Page(page, pageSize).WhereGT(dao.SysDept.Columns().DeptId, maxDeptId).
|
|
OrderAsc(dao.SysDept.Columns().DeptId).Scan(&dataList); err != nil {
|
|
return err
|
|
}
|
|
if len(dataList) == 0 {
|
|
break
|
|
}
|
|
count += len(dataList)
|
|
_, err := db.Ctx(ctx).Model(dao.SysDept.Table()).Data(dataList).OnConflict(dao.SysDept.Columns().DeptId).Save()
|
|
if err != nil {
|
|
g.Log().Errorf(ctx, "sMemDBSysDept.Load data insert err: %v", err)
|
|
return err
|
|
}
|
|
maxDeptId = dataList[len(dataList)-1].DeptId
|
|
if len(dataList) < pageSize {
|
|
break
|
|
}
|
|
}
|
|
} else {
|
|
dataList := make([]*entity.SysDept, 0)
|
|
if err := dao.SysDept.Ctx(sourceContext(ctx)).Where(dao.SysDept.Columns().DeptId, pk).Scan(&dataList); err != nil {
|
|
return err
|
|
}
|
|
_, err := db.Ctx(ctx).Model(dao.SysDept.Table()).Data(dataList).OnConflict(dao.SysDept.Columns().DeptId).Save()
|
|
if err != nil {
|
|
g.Log().Errorf(ctx, "sMemDBSysDept.Load data insert err: %v", err)
|
|
return err
|
|
}
|
|
}
|
|
|
|
g.Log().Debugf(ctx, "sMemDBSysDept.LoadData load data success, total: %d, pk: %d", count, pk)
|
|
return nil
|
|
}
|
|
|
|
func (s *sMemDBSysDept) DeleteData(ctx context.Context, ids []int64) error {
|
|
_, err := db.Ctx(ctx).Model(dao.SysDept.Table()).WhereIn(dao.SysDept.Columns().DeptId, ids).Unscoped().Delete()
|
|
if err != nil {
|
|
g.Log().Errorf(ctx, "sMemDBSysDept.DeleteData err: %v", err)
|
|
return err
|
|
}
|
|
g.Log().Debugf(ctx, "sMemDBSysDept.DeleteData delete data success, total: %d, ids: %+v", len(ids), ids)
|
|
return nil
|
|
}
|