238 lines
7.7 KiB
Go
238 lines
7.7 KiB
Go
// Package gen
|
|
// @Link https://github.com/xiujiecn/xiu-admin
|
|
// @Copyright Copyright (c) 2026 LiXiujie
|
|
// @Author Lxj <xxx@example.com>
|
|
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
|
|
// @AutoGenerate Version
|
|
package iot
|
|
|
|
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/contexts"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/exportcenter"
|
|
"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"
|
|
"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/gtime"
|
|
)
|
|
|
|
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) {
|
|
_, err = exportcenter.Submit(ctx, &exportcenter.Request{
|
|
BizKey: "tSimChannelTemplate",
|
|
BizName: "通道模版",
|
|
PermCode: "cpm:iot:tSimChannelTemplate:export",
|
|
QueryParam: in,
|
|
Tags: func() ([]string, error) {
|
|
return convert.GetEntityDescTags(model.TSimChannelTemplateListModel{})
|
|
},
|
|
Fetch: func(ctx context.Context, page, pageSize int) (rows []interface{}, total int, err error) {
|
|
query := *in
|
|
query.Page = page
|
|
query.PageSize = pageSize
|
|
list, count, err := s.List(ctx, &query)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
rows = make([]interface{}, 0, len(list))
|
|
for _, item := range list {
|
|
rows = append(rows, item)
|
|
}
|
|
return rows, count, nil
|
|
},
|
|
})
|
|
return err
|
|
}
|
|
|
|
// 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
|
|
}
|