Files
iotlcm/admin-svr/server/internal/logic/iot/t_sim_card_cancellation.go
T

238 lines
8.6 KiB
Go

package iot
import (
"context"
"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/os/gtime"
)
const (
cancellationExportBatchSize = 2000
cancellationExportMaxRows = 1048575
)
var cancellationExportHeaders = []interface{}{
"销户批次号", "销户时间", "ICCID", "MSISDN/卡号", "IMSI", "IMEI",
"通道编号", "通道名称", "渠道客户ID", "渠道客户名称",
"销户前套餐编号", "销户前套餐名称", "卡状态",
"入库批次", "出库批次", "划拨批次", "操作人", "备注",
}
// 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 {
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
}
// Export 导出销户卡信息(异步:提交任务后到下载中心取文件)
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
}
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))
}
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 {
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(
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+" 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+" AND "+packageTable+"."+dao.TSimFlowPackageInfo.Columns().TenantId+"="+cardTable+"."+dao.TSimCardCancellation.Columns().TenantId)
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)
}