sync: 同步 v1.0.4 并脱敏配置与镜像
This commit is contained in:
@@ -2,24 +2,22 @@ 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/exportcenter"
|
||||
"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/util/gconv"
|
||||
|
||||
"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 (
|
||||
@@ -34,6 +32,16 @@ var cancellationExportHeaders = []interface{}{
|
||||
"入库批次", "出库批次", "划拨批次", "操作人", "备注",
|
||||
}
|
||||
|
||||
// cancellationExportFields 与 cancellationExportHeaders 一一对应(json tag),
|
||||
// 供「按页面显示列导出」在保留 ToRow 格式化的前提下按字段取列。
|
||||
var cancellationExportFields = []string{
|
||||
"cancelBatchNo", "cancelledAt", "iccid", "msisdn", "imsi", "imei",
|
||||
"chanKey", "chanName", "deptId", "deptName",
|
||||
"currentPackageCode", "packageName", "bossCardStatus",
|
||||
// 字段名必须与列表页列的 field 一致(操作人页面用 createdBy,显示的是 createdBySumma 摘要)
|
||||
"inboundBatchNo", "outboundBatchNo", "allocationBatchNo", "createdBy", "remark",
|
||||
}
|
||||
|
||||
type sIotTSimCardCancellation struct{}
|
||||
|
||||
func NewIotTSimCardCancellation() *sIotTSimCardCancellation {
|
||||
@@ -72,6 +80,7 @@ func (s *sIotTSimCardCancellation) List(ctx context.Context, in *model.TSimCardC
|
||||
return list, total, nil
|
||||
}
|
||||
|
||||
// Export 导出销户卡信息(异步:提交任务后到下载中心取文件)
|
||||
func (s *sIotTSimCardCancellation) Export(ctx context.Context, filter *model.TSimCardCancellationFilter) error {
|
||||
if filter == nil {
|
||||
filter = &model.TSimCardCancellationFilter{}
|
||||
@@ -80,77 +89,50 @@ func (s *sIotTSimCardCancellation) Export(ctx context.Context, filter *model.TSi
|
||||
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 数据失败")
|
||||
counted := false
|
||||
totalRows := 0
|
||||
_, err := exportcenter.Submit(ctx, &exportcenter.Request{
|
||||
BizKey: "tSimCardCancellation",
|
||||
BizName: "销户卡信息",
|
||||
PermCode: "cpm:iot:tSimCardCancellation:export",
|
||||
QueryParam: filter,
|
||||
Tags: func() ([]string, error) {
|
||||
headers := make([]string, 0, len(cancellationExportHeaders))
|
||||
for _, v := range cancellationExportHeaders {
|
||||
headers = append(headers, gconv.String(v))
|
||||
}
|
||||
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
|
||||
return headers, nil
|
||||
},
|
||||
Fetch: func(ctx context.Context, page, pageSize int) (rows []interface{}, total int, err error) {
|
||||
if !counted {
|
||||
totalRows, err = s.filteredModel(ctx, filter).Count()
|
||||
if err != nil {
|
||||
return nil, 0, gerror.Wrap(err, "统计销户卡导出数量失败")
|
||||
}
|
||||
counted = true
|
||||
}
|
||||
var list []*model.TSimCardCancellationListModel
|
||||
query := s.selectModel(ctx, filter).
|
||||
OrderDesc(dao.TSimCardCancellation.Table()+"."+dao.TSimCardCancellation.Columns().CancelledAt).
|
||||
OrderDesc(dao.TSimCardCancellation.Table()+"."+dao.TSimCardCancellation.Columns().Id).
|
||||
Limit((page-1)*pageSize, pageSize)
|
||||
if err = query.Scan(&list); err != nil {
|
||||
return nil, 0, gerror.Wrap(err, "分批查询销户卡导出数据失败")
|
||||
}
|
||||
rows = make([]interface{}, 0, len(list))
|
||||
for _, row := range list {
|
||||
rows = append(rows, row)
|
||||
}
|
||||
return rows, totalRows, nil
|
||||
},
|
||||
ToRow: func(v interface{}) []interface{} {
|
||||
row, _ := v.(*model.TSimCardCancellationListModel)
|
||||
return cancellationExportRow(row)
|
||||
},
|
||||
// ToRow 返回值与字段名的对应关系,供「按页面显示列导出」按字段取列。
|
||||
RowFields: cancellationExportFields,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *sIotTSimCardCancellation) filteredModel(ctx context.Context, filter *model.TSimCardCancellationFilter) *gdb.Model {
|
||||
@@ -195,13 +177,13 @@ func (s *sIotTSimCardCancellation) selectModel(ctx context.Context, filter *mode
|
||||
query := s.filteredModel(ctx, filter)
|
||||
query = query.FieldsPrefix(cardTable, model.TSimCardCancellationListModel{})
|
||||
query = query.Fields(
|
||||
channelTable+"."+dao.TSimChannels.Columns().ChanName+" AS chanName",
|
||||
channelDisplayNameSQL(ctx, channelTable)+" 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(channelTable, channelTable+"."+dao.TSimChannels.Columns().ChanKey+"="+cardTable+"."+dao.TSimCardCancellation.Columns().ChanKey+" AND "+channelTable+"."+dao.TSimChannels.Columns().TenantId+"="+cardTable+"."+dao.TSimCardCancellation.Columns().TenantId)
|
||||
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)
|
||||
query = query.LeftJoin(packageTable, packageTable+"."+dao.TSimFlowPackageInfo.Columns().PackageCode+"="+cardTable+"."+dao.TSimCardCancellation.Columns().CurrentPackageCode+" AND "+packageTable+"."+dao.TSimFlowPackageInfo.Columns().TenantId+"="+cardTable+"."+dao.TSimCardCancellation.Columns().TenantId)
|
||||
return query.Hook(hook.MemberSummary)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user