Files
iotlcm/server/internal/logic/common/t_notify_log.go
T
MiFaZhan 59eb34976a chore: 初始开源发布
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 已说明。
2026-10-23 14:48:30 +08:00

265 lines
7.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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 common
import (
"context"
"fmt"
"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/websocket"
"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/model/entity"
"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"
"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 sCommonTNotifyLog struct{}
func NewCommonTNotifyLog() *sCommonTNotifyLog {
return &sCommonTNotifyLog{}
}
func init() {
service.RegisterCommonTNotifyLog(NewCommonTNotifyLog())
}
// Model 通知日志ORM模型
func (s *sCommonTNotifyLog) 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.TNotifyLog.Ctx(ctx), option...)
}
// List 获取通知日志列表
func (s *sCommonTNotifyLog) List(ctx context.Context, in *model.TNotifyLogListParam) (list []*model.TNotifyLogListModel, totalCount int, err error) {
mod := dao.TNotifyLog.Ctx(ctx)
// 字段过滤
mod = mod.Fields(model.TNotifyLogListModel{})
if title := strings.TrimSpace(in.NotifyTitle); title != "" {
mod = mod.WhereLike(dao.TNotifyLog.Columns().NotifyTitle, "%"+title+"%")
}
// 查询发送用户类型
if in.SendUserType != nil && *in.SendUserType > 0 {
mod = mod.Where(dao.TNotifyLog.Columns().SendUserType, *in.SendUserType)
}
// 查询发送时间范围
if in.SendTimeStart != nil {
mod = mod.WhereGTE(dao.TNotifyLog.Columns().SendTime, in.SendTimeStart)
}
if in.SendTimeEnd != nil {
mod = mod.WhereLTE(dao.TNotifyLog.Columns().SendTime, in.SendTimeEnd.EndOfDay())
}
// 兼容旧的单日发送时间查询
if in.SendTime != nil {
mod = mod.Where(fmt.Sprintf("DATE(%s) = ?", dao.TNotifyLog.Columns().SendTime), in.SendTime.Format("Y-m-d"))
}
userId := contexts.GetUserId(ctx)
var sysUser *entity.SysUser
dao.SysUser.Ctx(ctx).WherePri(userId).Scan(&sysUser)
userType := sysUser.UserType
// 1. 查找顶级部门(ParentId = 0)
var topDept entity.SysDept
err = dao.SysDept.Ctx(ctx).
Where(dao.SysDept.Columns().ParentId, 0).
Scan(&topDept)
if err != nil {
return
}
// 获取当前用户部门ID
deptId := contexts.GetDeptId(ctx)
// 只有非顶级用户才根据用户类型过滤
if deptId != topDept.DeptId {
if userType == "sys_user" {
mod = mod.Where(dao.TNotifyLog.Columns().SendUserType, 1)
} else if userType == "app_user" {
mod = mod.Where(dao.TNotifyLog.Columns().SendUserType, 2)
}
}
// 分页
mod = mod.Page(in.Page, in.PageSize)
// 排序
mod = mod.OrderDesc(dao.TNotifyLog.Columns().NotifyId)
// 操作人摘要信息
mod = mod.Hook(hook.MemberSummary)
// 查询数据
if err = mod.ScanAndCount(&list, &totalCount, false); err != nil {
err = gerror.Wrap(err, "获取通知日志列表失败,请稍后重试!")
return
}
return
}
// Export 导出通知日志
func (s *sCommonTNotifyLog) Export(ctx context.Context, in *model.TNotifyLogListParam) (err error) {
list, totalCount, err := s.List(ctx, in)
if err != nil {
return
}
// 字段的排序是依据tags的字段顺序,如果你不想使用默认的排序方式,可以直接定义 tags = []string{"字段名称", "字段名称2", ...}
tags, err := convert.GetEntityDescTags(model.TNotifyLogExportModel{})
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.TNotifyLogExportModel
)
if err = gconv.Scan(list, &exports); err != nil {
return
}
err = excel.ExportByStructs(ctx, tags, exports, fileName, sheetName)
return
}
// Edit 修改/新增通知日志
func (s *sCommonTNotifyLog) Edit(ctx context.Context, in *model.TNotifyLogEditParam) (err error) {
var (
pushPayload g.Map
pushUserIds []uint64
)
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
// 修改
if in.NotifyId > 0 {
tenantId := contexts.GetTenantId(ctx)
in.TenantId = &tenantId
updatedBy := contexts.GetUserId(ctx)
in.UpdatedBy = &updatedBy
in.UpdatedAt = gtime.Now()
if _, err = s.Model(ctx).
Fields(model.TNotifyLogUpdateFields{}).
WherePri(in.NotifyId).Data(in).Update(); err != nil {
err = gerror.Wrap(err, "修改通知日志失败,请稍后重试!")
}
return
}
// 新增
tenantId := contexts.GetTenantId(ctx)
in.TenantId = &tenantId
createdDept := contexts.GetDeptId(ctx)
in.CreatedDept = &createdDept
createdBy := contexts.GetUserId(ctx)
in.CreatedBy = &createdBy
in.CreatedAt = gtime.Now()
in.SendTime = gtime.Now()
result, err := s.Model(ctx, &handler.Option{FilterAuth: false}).
Fields(model.TNotifyLogInsertFields{}).
Data(in).Insert()
if err != nil {
err = gerror.Wrap(err, "新增通知日志失败,请稍后重试!")
return
}
notifyId, err := result.LastInsertId()
if err != nil {
err = gerror.Wrap(err, "获取通知ID失败")
return
}
pushPayload = g.Map{
"notifyId": notifyId,
"notifyTitle": gconv.String(in.NotifyTitle),
"notifyContent": gconv.String(in.NotifyContent),
"sendUserType": gconv.Int(in.SendUserType),
"sendTime": in.SendTime,
}
var userType string
if in.SendUserType != nil {
switch *in.SendUserType {
case 1:
userType = "sys_user"
case 2:
userType = "app_user"
}
}
if userType != "" {
var users []*entity.SysUser
if err = tx.Ctx(ctx).Model(dao.SysUser.Table()).
Fields(dao.SysUser.Columns().UserId).
Where(dao.SysUser.Columns().TenantId, tenantId).
Where(dao.SysUser.Columns().UserType, userType).
Where(dao.SysUser.Columns().Status, "0").
Scan(&users); err != nil {
err = gerror.Wrap(err, "获取通知推送用户失败")
return
}
for _, user := range users {
if user.UserId > 0 {
pushUserIds = append(pushUserIds, uint64(user.UserId))
}
}
}
return
})
if err != nil {
return
}
for _, userId := range pushUserIds {
g.Log().Info(ctx, "推送通知到用户 %v", userId)
websocket.SendToUser(userId, &websocket.WResponse{
Event: "notify:new",
Data: pushPayload,
})
}
return
}
// Delete 删除通知日志
func (s *sCommonTNotifyLog) Delete(ctx context.Context, in *model.TNotifyLogDeleteParam) (err error) {
if _, err = s.Model(ctx).WherePri(in.NotifyId).Unscoped().Delete(); err != nil {
err = gerror.Wrap(err, "删除通知日志失败,请稍后重试!")
return
}
return
}
// View 获取通知日志指定信息
func (s *sCommonTNotifyLog) View(ctx context.Context, in *model.TNotifyLogViewParam) (res *model.TNotifyLogViewModel, err error) {
if err = dao.TNotifyLog.Ctx(ctx).WherePri(in.NotifyId).Hook(hook.MemberSummary).Scan(&res); err != nil {
err = gerror.Wrap(err, "获取通知日志信息,请稍后重试!")
return
}
return
}