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 已说明。
240 lines
7.7 KiB
Go
240 lines
7.7 KiB
Go
// Package gen
|
|
// @Link https://github.com/xiujiecn/xiu-admin
|
|
// @Copyright Copyright (c) 2026 LiXiujie
|
|
// @Author Lxj <li@xiujie.cn>
|
|
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
|
|
// @AutoGenerate Version
|
|
package iot
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"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/contexts"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/rabbitmq"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/xgorm/handler"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/xgorm/hook"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/service"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/utility/convert"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/utility/excel"
|
|
"strings"
|
|
"time"
|
|
|
|
"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/gctx"
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
)
|
|
|
|
type sIotTSimChannelTemplate struct{}
|
|
|
|
var publishChannelTemplateRefresh = rabbitmq.Publish
|
|
|
|
func NewIotTSimChannelTemplate() *sIotTSimChannelTemplate {
|
|
return &sIotTSimChannelTemplate{}
|
|
}
|
|
|
|
func init() {
|
|
service.RegisterIotTSimChannelTemplate(NewIotTSimChannelTemplate())
|
|
}
|
|
|
|
// Model 通道模板ORM模型
|
|
func (s *sIotTSimChannelTemplate) Model(ctx context.Context, option ...*handler.Option) *gdb.Model {
|
|
if len(option) == 0 {
|
|
option = append(option, &handler.Option{
|
|
FilterTenant: true,
|
|
FilterAuth: true,
|
|
})
|
|
}
|
|
return handler.Model(dao.TSimChannelTemplate.Ctx(ctx), option...)
|
|
}
|
|
|
|
// List 获取通道模板列表
|
|
func (s *sIotTSimChannelTemplate) List(ctx context.Context, in *model.TSimChannelTemplateListParam) (list []*model.TSimChannelTemplateListModel, totalCount int, err error) {
|
|
mod := s.Model(ctx)
|
|
g.Log().Debugf(ctx, "sIotTSimChannelTemplate.List.in: %+v", in)
|
|
|
|
// 字段过滤
|
|
mod = mod.Fields(model.TSimChannelTemplateListModel{})
|
|
|
|
// 查询ID
|
|
if in.Id > 0 {
|
|
mod = mod.Where(dao.TSimChannelTemplate.Columns().Id, in.Id)
|
|
}
|
|
|
|
// 查询模板状态(0未启用,1启用)
|
|
if in.TempStatus != nil {
|
|
mod = mod.Where(dao.TSimChannelTemplate.Columns().TempStatus, *in.TempStatus)
|
|
}
|
|
|
|
// 查询创建时间
|
|
if len(in.CreatedAt) == 2 {
|
|
in.CreatedAt[1] = in.CreatedAt[1].EndOfDay()
|
|
}
|
|
if len(in.CreatedAt) == 2 {
|
|
mod = mod.WhereBetween(dao.TSimChannelTemplate.Columns().CreatedAt, in.CreatedAt[0], in.CreatedAt[1])
|
|
}
|
|
if in.TempKey != "" {
|
|
mod = mod.Where(dao.TSimChannelTemplate.Columns().TempKey+" LIKE ?", "%"+in.TempKey+"%")
|
|
}
|
|
if in.TempName != "" {
|
|
mod = mod.Where(dao.TSimChannelTemplate.Columns().TempName+" LIKE ?", "%"+in.TempName+"%")
|
|
}
|
|
if in.TempModel != "" {
|
|
mod = mod.Where(dao.TSimChannelTemplate.Columns().TempModel, in.TempModel)
|
|
}
|
|
|
|
// 分页
|
|
mod = mod.Page(in.Page, in.PageSize)
|
|
|
|
// 排序
|
|
mod = mod.OrderDesc(dao.TSimChannelTemplate.Columns().Id)
|
|
|
|
// 操作人摘要信息
|
|
mod = mod.Hook(hook.MemberSummary)
|
|
|
|
// 查询数据
|
|
if err = mod.ScanAndCount(&list, &totalCount, false); err != nil {
|
|
err = gerror.Wrap(err, "获取通道模板列表失败,请稍后重试!")
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// Export 导出通道模板
|
|
func (s *sIotTSimChannelTemplate) Export(ctx context.Context, in *model.TSimChannelTemplateListParam) (err error) {
|
|
list, totalCount, err := s.List(ctx, in)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// 字段的排序是依据tags的字段顺序,如果你不想使用默认的排序方式,可以直接定义 tags = []string{"字段名称", "字段名称2", ...}
|
|
tags, err := convert.GetEntityDescTags(model.TSimChannelTemplateExportModel{})
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
var (
|
|
fileName = "导出通道模板-" + gctx.CtxId(ctx)
|
|
sheetName = fmt.Sprintf("索引条件共%v行,共%v页,当前导出是第%v页,本页共%v行", totalCount, (totalCount+in.PageSize-1)/in.PageSize, in.Page, len(list))
|
|
exports []model.TSimChannelTemplateExportModel
|
|
)
|
|
|
|
if err = gconv.Scan(list, &exports); err != nil {
|
|
return
|
|
}
|
|
|
|
err = excel.ExportByStructs(ctx, tags, exports, fileName, sheetName)
|
|
return
|
|
}
|
|
|
|
// Edit 修改/新增通道模板
|
|
func (s *sIotTSimChannelTemplate) Edit(ctx context.Context, in *model.TSimChannelTemplateEditParam) (err error) {
|
|
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
|
|
|
// 修改
|
|
if in.Id > 0 {
|
|
var existing struct {
|
|
TempKey string `json:"tempKey"`
|
|
}
|
|
if err = handler.Model(tx.Ctx(ctx).Model(dao.TSimChannelTemplate.Table())).
|
|
Fields(dao.TSimChannelTemplate.Columns().TempKey).
|
|
WherePri(in.Id).LockUpdate().Scan(&existing); err != nil {
|
|
return gerror.Wrap(err, "查询原模板标识失败")
|
|
}
|
|
if in.TempKey != nil && strings.TrimSpace(*in.TempKey) != strings.TrimSpace(existing.TempKey) {
|
|
return gerror.New("已有通道模板不允许修改模板标识")
|
|
}
|
|
if in.TempKey != nil {
|
|
normalizedTempKey := strings.TrimSpace(*in.TempKey)
|
|
in.TempKey = &normalizedTempKey
|
|
}
|
|
updateBy := contexts.GetUserId(ctx)
|
|
in.UpdatedBy = &updateBy
|
|
in.UpdatedAt = gtime.Now()
|
|
if _, err = handler.Model(tx.Ctx(ctx).Model(dao.TSimChannelTemplate.Table())).
|
|
Fields(model.TSimChannelTemplateUpdateFields{}).
|
|
WherePri(in.Id).Data(in).Update(); err != nil {
|
|
err = gerror.Wrap(err, "修改通道模板失败,请稍后重试!")
|
|
}
|
|
return
|
|
}
|
|
|
|
// 新增
|
|
tenantId := contexts.GetTenantId(ctx)
|
|
in.TenantId = &tenantId
|
|
deptId := contexts.GetDeptId(ctx)
|
|
in.CreatedDept = &deptId
|
|
createBy := contexts.GetUserId(ctx)
|
|
in.CreatedBy = &createBy
|
|
in.CreatedAt = gtime.Now()
|
|
if _, err = handler.Model(tx.Ctx(ctx).Model(dao.TSimChannelTemplate.Table()), &handler.Option{FilterAuth: false}).
|
|
Fields(model.TSimChannelTemplateInsertFields{}).
|
|
Data(in).Insert(); err != nil {
|
|
err = gerror.Wrap(err, "新增通道模板失败,请稍后重试!")
|
|
}
|
|
return
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tempKey := s.resolveSavedTemplateKey(ctx, in); tempKey != "" {
|
|
publishCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
defer cancel()
|
|
if err = publishChannelTemplateRefresh(publishCtx, consts.QueueChannelRefresh, model.ChannelRefreshMessage{Type: "template", TempKey: tempKey}); err != nil {
|
|
return gerror.Wrap(err, "通道模板已保存,但刷新任务投递失败")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *sIotTSimChannelTemplate) resolveSavedTemplateKey(ctx context.Context, in *model.TSimChannelTemplateEditParam) string {
|
|
if in == nil {
|
|
return ""
|
|
}
|
|
if in.TempKey != nil && strings.TrimSpace(*in.TempKey) != "" {
|
|
return strings.TrimSpace(*in.TempKey)
|
|
}
|
|
if in.Id <= 0 {
|
|
return ""
|
|
}
|
|
var row struct {
|
|
TempKey string `json:"tempKey"`
|
|
}
|
|
if err := s.Model(ctx).
|
|
Fields(dao.TSimChannelTemplate.Columns().TempKey).
|
|
WherePri(in.Id).
|
|
Limit(1).
|
|
Scan(&row); err != nil {
|
|
g.Log().Warningf(ctx, "resolve saved template key failed, id=%d err=%+v", in.Id, err)
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(row.TempKey)
|
|
}
|
|
|
|
// Delete 删除通道模板
|
|
func (s *sIotTSimChannelTemplate) Delete(ctx context.Context, in *model.TSimChannelTemplateDeleteParam) (err error) {
|
|
|
|
if _, err = s.Model(ctx).WherePri(in.Id).Delete(); err != nil {
|
|
err = gerror.Wrap(err, "删除通道模板失败,请稍后重试!")
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// View 获取通道模板指定信息
|
|
func (s *sIotTSimChannelTemplate) View(ctx context.Context, in *model.TSimChannelTemplateViewParam) (res *model.TSimChannelTemplateViewModel, err error) {
|
|
if in == nil || in.Id <= 0 {
|
|
return nil, gerror.New("ID不能为空")
|
|
}
|
|
if err = s.Model(ctx).WherePri(in.Id).Hook(hook.MemberSummary).Scan(&res); err != nil {
|
|
err = gerror.Wrap(err, "获取通道模板信息,请稍后重试!")
|
|
return
|
|
}
|
|
return
|
|
}
|