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 已说明。
This commit is contained in:
@@ -0,0 +1,255 @@
|
||||
package iot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"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/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"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
excelize "github.com/xuri/excelize/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
cancellationExportBatchSize = 2000
|
||||
cancellationExportMaxRows = 1048575
|
||||
)
|
||||
|
||||
var cancellationExportHeaders = []interface{}{
|
||||
"销户批次号", "销户时间", "ICCID", "MSISDN/卡号", "IMSI", "IMEI",
|
||||
"通道编号", "通道名称", "渠道客户ID", "渠道客户名称",
|
||||
"销户前套餐编号", "销户前套餐名称", "卡状态",
|
||||
"入库批次", "出库批次", "划拨批次", "操作人", "备注",
|
||||
}
|
||||
|
||||
type sIotTSimCardCancellation struct{}
|
||||
|
||||
func NewIotTSimCardCancellation() *sIotTSimCardCancellation {
|
||||
return &sIotTSimCardCancellation{}
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterIotTSimCardCancellation(NewIotTSimCardCancellation())
|
||||
}
|
||||
|
||||
func (s *sIotTSimCardCancellation) 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.TSimCardCancellation.Ctx(ctx), option...)
|
||||
}
|
||||
|
||||
func (s *sIotTSimCardCancellation) List(ctx context.Context, in *model.TSimCardCancellationListParam) (list []*model.TSimCardCancellationListModel, total int, err error) {
|
||||
if in == nil {
|
||||
return nil, 0, gerror.New("销户卡查询参数不能为空")
|
||||
}
|
||||
if err = in.TSimCardCancellationFilter.Filter(ctx); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
query := s.selectModel(ctx, &in.TSimCardCancellationFilter).
|
||||
Page(in.Page, in.PageSize).
|
||||
OrderDesc(dao.TSimCardCancellation.Table() + "." + dao.TSimCardCancellation.Columns().CancelledAt).
|
||||
OrderDesc(dao.TSimCardCancellation.Table() + "." + dao.TSimCardCancellation.Columns().Id)
|
||||
if err = query.ScanAndCount(&list, &total, false); err != nil {
|
||||
return nil, 0, gerror.Wrap(err, "查询销户卡信息失败")
|
||||
}
|
||||
return list, total, nil
|
||||
}
|
||||
|
||||
func (s *sIotTSimCardCancellation) Export(ctx context.Context, filter *model.TSimCardCancellationFilter) error {
|
||||
if filter == nil {
|
||||
filter = &model.TSimCardCancellationFilter{}
|
||||
}
|
||||
if err := filter.Filter(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
total, err := s.filteredModel(ctx, filter).Count()
|
||||
if err != nil {
|
||||
return gerror.Wrap(err, "统计销户卡导出数量失败")
|
||||
}
|
||||
if total == 0 {
|
||||
return gerror.New("暂无可导出数据")
|
||||
}
|
||||
if total > cancellationExportMaxRows {
|
||||
return gerror.Newf("导出数据超过 Excel 上限 %d 条,请缩小筛选范围", cancellationExportMaxRows)
|
||||
}
|
||||
|
||||
file := excelize.NewFile()
|
||||
defer file.Close()
|
||||
const sheetName = "销户卡信息"
|
||||
file.SetSheetName(file.GetSheetName(0), sheetName)
|
||||
stream, err := file.NewStreamWriter(sheetName)
|
||||
if err != nil {
|
||||
return gerror.Wrap(err, "创建销户卡 Excel 流失败")
|
||||
}
|
||||
if err = stream.SetColWidth(1, len(cancellationExportHeaders), 22); err != nil {
|
||||
return gerror.Wrap(err, "设置销户卡 Excel 列宽失败")
|
||||
}
|
||||
if err = stream.SetColWidth(3, 6, 28); err != nil {
|
||||
return gerror.Wrap(err, "设置销户卡号 Excel 列宽失败")
|
||||
}
|
||||
headerStyle, err := file.NewStyle(&excelize.Style{Font: &excelize.Font{Bold: true}})
|
||||
if err != nil {
|
||||
return gerror.Wrap(err, "创建销户卡 Excel 表头样式失败")
|
||||
}
|
||||
if err = stream.SetRow("A1", cancellationExportHeaders, excelize.RowOpts{StyleID: headerStyle}); err != nil {
|
||||
return gerror.Wrap(err, "写入销户卡 Excel 表头失败")
|
||||
}
|
||||
|
||||
rowNumber := 2
|
||||
for offset := 0; offset < total; offset += cancellationExportBatchSize {
|
||||
var rows []*model.TSimCardCancellationListModel
|
||||
query := s.selectModel(ctx, filter).
|
||||
OrderDesc(dao.TSimCardCancellation.Table()+"."+dao.TSimCardCancellation.Columns().CancelledAt).
|
||||
OrderDesc(dao.TSimCardCancellation.Table()+"."+dao.TSimCardCancellation.Columns().Id).
|
||||
Limit(offset, cancellationExportBatchSize)
|
||||
if err = query.Scan(&rows); err != nil {
|
||||
return gerror.Wrap(err, "分批查询销户卡导出数据失败")
|
||||
}
|
||||
for _, row := range rows {
|
||||
cell, _ := excelize.CoordinatesToCellName(1, rowNumber)
|
||||
if err = stream.SetRow(cell, cancellationExportRow(row)); err != nil {
|
||||
return gerror.Wrap(err, "写入销户卡 Excel 数据失败")
|
||||
}
|
||||
rowNumber++
|
||||
}
|
||||
}
|
||||
if err = stream.Flush(); err != nil {
|
||||
return gerror.Wrap(err, "刷新销户卡 Excel 数据失败")
|
||||
}
|
||||
|
||||
request := ghttp.RequestFromCtx(ctx)
|
||||
if request == nil {
|
||||
return gerror.New("销户卡导出缺少 HTTP 请求上下文")
|
||||
}
|
||||
fileName := "销户卡信息-" + gtime.Now().Format("YmdHis") + ".xlsx"
|
||||
writer := request.Response.Writer
|
||||
writer.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
||||
writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", url.QueryEscape(fileName)))
|
||||
writer.Header().Set("Content-Transfer-Encoding", "binary")
|
||||
writer.Header().Set("Access-Control-Expose-Headers", "Content-Disposition")
|
||||
if err = file.Write(writer); err != nil {
|
||||
return gerror.Wrap(err, "输出销户卡 Excel 失败")
|
||||
}
|
||||
request.Response.Status = http.StatusOK
|
||||
writer.Flush()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *sIotTSimCardCancellation) filteredModel(ctx context.Context, filter *model.TSimCardCancellationFilter) *gdb.Model {
|
||||
query := s.Model(ctx)
|
||||
columns := dao.TSimCardCancellation.Columns()
|
||||
table := dao.TSimCardCancellation.Table()
|
||||
|
||||
if value := strings.TrimSpace(filter.CardValue); value != "" {
|
||||
query = query.Where(
|
||||
"("+table+"."+columns.Iccid+" = ? OR "+table+"."+columns.Msisdn+" = ? OR "+table+"."+columns.Imsi+" = ?)",
|
||||
value, value, value,
|
||||
)
|
||||
}
|
||||
if value := strings.TrimSpace(filter.CancelBatchNo); value != "" {
|
||||
query = query.Where(table+"."+columns.CancelBatchNo, value)
|
||||
}
|
||||
if filter.DeptId > 0 {
|
||||
query = query.Where(table+"."+columns.DeptId, filter.DeptId)
|
||||
}
|
||||
if value := strings.TrimSpace(filter.ChanKey); value != "" {
|
||||
query = query.Where(table+"."+columns.ChanKey, value)
|
||||
}
|
||||
if filter.CancelledStartAt != nil {
|
||||
query = query.WhereGTE(table+"."+columns.CancelledAt, filter.CancelledStartAt)
|
||||
}
|
||||
if filter.CancelledEndAt != nil {
|
||||
query = query.WhereLTE(table+"."+columns.CancelledAt, filter.CancelledEndAt)
|
||||
}
|
||||
if value := strings.TrimSpace(filter.Remark); value != "" {
|
||||
query = query.WhereLike(table+"."+columns.Remark, "%"+value+"%")
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
func (s *sIotTSimCardCancellation) selectModel(ctx context.Context, filter *model.TSimCardCancellationFilter) *gdb.Model {
|
||||
var (
|
||||
cardTable = dao.TSimCardCancellation.Table()
|
||||
channelTable = dao.TSimChannels.Table()
|
||||
deptTable = dao.SysDept.Table()
|
||||
packageTable = dao.TSimFlowPackageInfo.Table()
|
||||
)
|
||||
query := s.filteredModel(ctx, filter)
|
||||
query = query.FieldsPrefix(cardTable, model.TSimCardCancellationListModel{})
|
||||
query = query.Fields(
|
||||
channelTable+"."+dao.TSimChannels.Columns().ChanName+" AS chanName",
|
||||
deptTable+"."+dao.SysDept.Columns().DeptName+" AS deptName",
|
||||
packageTable+"."+dao.TSimFlowPackageInfo.Columns().PackageName+" AS packageName",
|
||||
)
|
||||
query = query.LeftJoin(channelTable, channelTable+"."+dao.TSimChannels.Columns().ChanKey+"="+cardTable+"."+dao.TSimCardCancellation.Columns().ChanKey)
|
||||
query = query.LeftJoin(deptTable, deptTable+"."+dao.SysDept.Columns().DeptId+"="+cardTable+"."+dao.TSimCardCancellation.Columns().DeptId)
|
||||
query = query.LeftJoin(packageTable, packageTable+"."+dao.TSimFlowPackageInfo.Columns().PackageCode+"="+cardTable+"."+dao.TSimCardCancellation.Columns().CurrentPackageCode)
|
||||
return query.Hook(hook.MemberSummary)
|
||||
}
|
||||
|
||||
func cancellationExportRow(row *model.TSimCardCancellationListModel) []interface{} {
|
||||
if row == nil {
|
||||
return make([]interface{}, len(cancellationExportHeaders))
|
||||
}
|
||||
statusName := consts.V3ApiSimBossCardStatusNameMap[row.BossCardStatus]
|
||||
if statusName == "" {
|
||||
statusName = strconv.Itoa(row.BossCardStatus)
|
||||
}
|
||||
return []interface{}{
|
||||
row.CancelBatchNo,
|
||||
formatCancellationTime(row.CancelledAt),
|
||||
row.Iccid,
|
||||
row.Msisdn,
|
||||
row.Imsi,
|
||||
row.Imei,
|
||||
row.ChanKey,
|
||||
row.ChanName,
|
||||
row.DeptId,
|
||||
row.DeptName,
|
||||
row.CurrentPackageCode,
|
||||
row.PackageName,
|
||||
statusName,
|
||||
row.InboundBatchNo,
|
||||
row.OutboundBatchNo,
|
||||
row.AllocationBatchNo,
|
||||
cancellationOperatorName(row),
|
||||
row.Remark,
|
||||
}
|
||||
}
|
||||
|
||||
func formatCancellationTime(value *gtime.Time) string {
|
||||
if value == nil {
|
||||
return ""
|
||||
}
|
||||
return value.Format("Y-m-d H:i:s")
|
||||
}
|
||||
|
||||
func cancellationOperatorName(row *model.TSimCardCancellationListModel) string {
|
||||
if row.CreatedBySumma != nil {
|
||||
if value := strings.TrimSpace(row.CreatedBySumma.NickName); value != "" {
|
||||
return value
|
||||
}
|
||||
if value := strings.TrimSpace(row.CreatedBySumma.UserName); value != "" {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return strconv.FormatInt(row.CreatedBy, 10)
|
||||
}
|
||||
Reference in New Issue
Block a user