Files
iotlcm/server/internal/logic/iot/t_sim_upstream_package.go
T
2026-10-29 14:36:49 +08:00

177 lines
5.6 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/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/xgorm"
"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"
"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 sIotTUpstreamPackage struct{}
func NewIotTUpstreamPackage() *sIotTUpstreamPackage {
return &sIotTUpstreamPackage{}
}
func init() {
service.RegisterIotTUpstreamPackage(NewIotTUpstreamPackage())
}
// Model 上游套餐表ORM模型
func (s *sIotTUpstreamPackage) 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.TUpstreamPackage.Ctx(ctx), option...)
}
// List 获取上游套餐表列表
func (s *sIotTUpstreamPackage) List(ctx context.Context, in *model.TUpstreamPackageListParam) (list []*model.TUpstreamPackageListModel, totalCount int, err error) {
mod := s.Model(ctx)
// 字段过滤
mod = mod.Fields(model.TUpstreamPackageListModel{})
// 查询上游套餐编码
if in.UpPkgCode != "" {
mod = mod.WhereLike(dao.TUpstreamPackage.Columns().UpPkgCode, in.UpPkgCode)
}
// 查询上游套餐名称
if in.UpPkgName != "" {
mod = mod.WhereLike(dao.TUpstreamPackage.Columns().UpPkgName, in.UpPkgName)
}
// 查询运营商:1移动,2联通,3电信
if in.Operator > 0 {
mod = mod.Where(dao.TUpstreamPackage.Columns().Operator, in.Operator)
}
// 查询创建时间
if len(in.CreatedAt) == 2 {
in.CreatedAt[1] = in.CreatedAt[1].EndOfDay()
}
if len(in.CreatedAt) == 2 {
mod = mod.WhereBetween(dao.TUpstreamPackage.Columns().CreatedAt, in.CreatedAt[0], in.CreatedAt[1])
}
// 分页
mod = mod.Page(in.Page, in.PageSize)
// 排序
mod = mod.OrderDesc(dao.TUpstreamPackage.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 *sIotTUpstreamPackage) Export(ctx context.Context, in *model.TUpstreamPackageListParam) (err error) {
_, err = exportcenter.Submit(ctx, &exportcenter.Request{
BizKey: "tSimUpstreamPackage",
BizName: "上游套餐",
PermCode: "cpm:iot:tSimUpstreamPackage:export",
QueryParam: in,
Tags: func() ([]string, error) {
return convert.GetEntityDescTags(model.TUpstreamPackageListModel{})
},
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 *sIotTUpstreamPackage) Edit(ctx context.Context, in *model.TUpstreamPackageEditParam) (err error) {
// 验证'UpPkgCode'唯一
if err = xgorm.IsUnique(ctx, &dao.TUpstreamPackage, g.Map{dao.TUpstreamPackage.Columns().UpPkgCode: in.UpPkgCode}, "上游套餐编码已存在", in.Id); err != nil {
return
}
return g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
// 修改
if in.Id > 0 {
in.UpdatedAt = gtime.Now()
updatedBy := contexts.GetUserId(ctx)
in.UpdatedBy = &updatedBy
if _, err = s.Model(ctx).
Fields(model.TUpstreamPackageUpdateFields{}).
WherePri(in.Id).Data(in).Update(); err != nil {
err = gerror.Wrap(err, "修改上游套餐表失败,请稍后重试!")
}
return
}
// 新增
createdDept := contexts.GetDeptId(ctx)
in.CreatedDept = &createdDept
in.CreatedAt = gtime.Now()
createdBy := contexts.GetUserId(ctx)
in.CreatedBy = &createdBy
if _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).
Fields(model.TUpstreamPackageInsertFields{}).
Data(in).Insert(); err != nil {
err = gerror.Wrap(err, "新增上游套餐表失败,请稍后重试!")
}
return
})
}
// Delete 删除上游套餐表
func (s *sIotTUpstreamPackage) Delete(ctx context.Context, in *model.TUpstreamPackageDeleteParam) (err error) {
if _, err = s.Model(ctx).WherePri(in.Id).Unscoped().Delete(); err != nil {
err = gerror.Wrap(err, "删除上游套餐表失败,请稍后重试!")
return
}
return
}
// View 获取上游套餐表指定信息
func (s *sIotTUpstreamPackage) View(ctx context.Context, in *model.TUpstreamPackageViewParam) (res *model.TUpstreamPackageViewModel, err error) {
if err = s.Model(ctx).WherePri(in.Id).Hook(hook.MemberSummary).Scan(&res); err != nil {
err = gerror.Wrap(err, "获取上游套餐表信息,请稍后重试!")
return
}
return
}