323 lines
9.1 KiB
Go
323 lines
9.1 KiB
Go
package oldApi
|
|
|
|
import (
|
|
"context"
|
|
"git.5iot.com/iotlcm-public/iotlcm-openapi/internal/dao"
|
|
"git.5iot.com/iotlcm-public/iotlcm-openapi/internal/library/xgorm/handler"
|
|
"git.5iot.com/iotlcm-public/iotlcm-openapi/internal/model"
|
|
"git.5iot.com/iotlcm-public/iotlcm-openapi/internal/model/entity"
|
|
"git.5iot.com/iotlcm-public/iotlcm-openapi/internal/service"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
)
|
|
|
|
type sOldApiQuery struct {
|
|
}
|
|
|
|
func NewOldApiQuery() *sOldApiQuery {
|
|
return &sOldApiQuery{}
|
|
}
|
|
|
|
func init() {
|
|
service.RegisterOldApiQuery(NewOldApiQuery())
|
|
}
|
|
|
|
func (s *sOldApiQuery) StatusConversion(simCardInfo entity.TSimCardInfo) int {
|
|
switch simCardInfo.BossCardStatus {
|
|
case 0:
|
|
return 8
|
|
case 1:
|
|
return 3
|
|
case 2:
|
|
return 4
|
|
case 4:
|
|
return 5
|
|
case 6:
|
|
return 2
|
|
case 7:
|
|
return 1
|
|
case 8:
|
|
return 6
|
|
case 9:
|
|
return 7
|
|
default:
|
|
return 8
|
|
}
|
|
}
|
|
|
|
func oldApiDate(times ...*gtime.Time) string {
|
|
for _, item := range times {
|
|
if item == nil || item.IsZero() {
|
|
continue
|
|
}
|
|
return item.Format("Y-m-d")
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func oldApiEndOfDay(t *gtime.Time) *gtime.Time {
|
|
if t == nil || t.IsZero() {
|
|
return nil
|
|
}
|
|
return t.EndOfDay()
|
|
}
|
|
|
|
func oldApiEndOfDayString(t *gtime.Time) string {
|
|
if t == nil || t.IsZero() {
|
|
return ""
|
|
}
|
|
return t.EndOfDay().Format("Y-m-d H:i:s")
|
|
}
|
|
|
|
func oldApiScopedCardByIccid(ctx context.Context, iccid string) (card *entity.TSimCardInfo, err error) {
|
|
err = service.IotTSimCardInfo().
|
|
Model(ctx).
|
|
Where(dao.TSimCardInfo.Columns().Iccid, iccid).
|
|
Limit(1).
|
|
Scan(&card)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if card == nil {
|
|
return nil, gerror.New("sim卡不存在")
|
|
}
|
|
return card, nil
|
|
}
|
|
|
|
func (s *sOldApiQuery) QuerySimCard(ctx context.Context, input *model.SimCardInfoInput) (output *model.SimCardInfoOutput, err error) {
|
|
input.Param.Type = strings.ToLower(strings.TrimSpace(input.Param.Type))
|
|
input.Param.Cardno = strings.TrimSpace(input.Param.Cardno)
|
|
if input.Param.Type == "" {
|
|
return nil, gerror.New("数据类型不能为空")
|
|
}
|
|
if input.Param.Cardno == "" {
|
|
return nil, gerror.New("值不能为空")
|
|
}
|
|
|
|
field := ""
|
|
if input.Param.Type == "iccid" {
|
|
field = dao.TSimCardInfo.Columns().Iccid
|
|
} else if input.Param.Type == "imsi" {
|
|
field = dao.TSimCardInfo.Columns().Imsi
|
|
} else if input.Param.Type == "msisdn" {
|
|
field = dao.TSimCardInfo.Columns().Msisdn
|
|
} else {
|
|
return nil, gerror.New("数据类型错误")
|
|
}
|
|
|
|
var out []model.TSimCardInfoListModel
|
|
sysDept := dao.SysDept.Table()
|
|
cardInfoTable := dao.TSimCardInfo.Table()
|
|
flowPackage := dao.TSimFlowPackageInfo.Table()
|
|
err = service.IotTSimCardInfo().
|
|
Model(ctx, &handler.Option{FilterAuth: false, FilterTenant: true}).
|
|
Where(field, input.Param.Cardno).
|
|
LeftJoin(sysDept, sysDept+"."+dao.SysDept.Columns().DeptId+"="+cardInfoTable+"."+dao.TSimCardInfo.Columns().DeptId).
|
|
LeftJoin(flowPackage, flowPackage+"."+dao.TSimFlowPackageInfo.Columns().PackageCode+"="+cardInfoTable+"."+dao.TSimCardInfo.Columns().CurrentPackageCode).
|
|
Fields(cardInfoTable + ".*," + sysDept + "." + dao.SysDept.Columns().DeptName + " as DeptName," + flowPackage + "." + dao.TSimFlowPackageInfo.Columns().PackageName + " as PackageName").
|
|
Limit(1).
|
|
Scan(&out)
|
|
if err != nil {
|
|
return nil, gerror.New("查询sim卡信息失败")
|
|
}
|
|
if len(out) == 0 {
|
|
return nil, gerror.New("sim卡不存在")
|
|
}
|
|
|
|
output = &model.SimCardInfoOutput{
|
|
Vid: strconv.FormatInt(out[0].Id, 10),
|
|
Msisdn: out[0].Msisdn,
|
|
Iccid: out[0].Iccid,
|
|
Imei: out[0].Imei,
|
|
Imsi: out[0].Imsi,
|
|
DeptName: out[0].DeptName,
|
|
StatusId: s.StatusConversion(entity.TSimCardInfo{
|
|
BossCardStatus: out[0].BossCardStatus,
|
|
}),
|
|
PackageId: out[0].CurrentPackageCode,
|
|
PackageName: out[0].PackageName,
|
|
Used: out[0].UsedFlowMb,
|
|
Remaining: out[0].PackageTotalFlowMb - out[0].UsedFlowMb,
|
|
Type: model.SimCardNameMap[out[0].CardType],
|
|
PacketArr: nil,
|
|
Remark: out[0].Remark,
|
|
Grouping: "",
|
|
DeliverDate: "",
|
|
ActivateDate: oldApiDate(out[0].BossActivateAt, out[0].BossOpenAt, out[0].FlowPkgBeginAt),
|
|
ExpireTime: oldApiEndOfDay(out[0].FlowPkgEndAt),
|
|
}
|
|
return output, nil
|
|
}
|
|
|
|
func (s *sOldApiQuery) QuerySimDataUsage(ctx context.Context, input *model.SimDataUsageOldInput) (output *model.SimDataUsageOldOutput, err error) {
|
|
input.Param.Iccid = strings.TrimSpace(input.Param.Iccid)
|
|
if input.Param.Iccid == "" {
|
|
return nil, gerror.New("iccid不能为空")
|
|
}
|
|
|
|
card, err := oldApiScopedCardByIccid(ctx, input.Param.Iccid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
output = &model.SimDataUsageOldOutput{
|
|
UseUnit: "MB",
|
|
UseAmount: card.UsedFlowMb,
|
|
}
|
|
return output, nil
|
|
}
|
|
|
|
func (s *sOldApiQuery) QuerySimDataUsageMonthly(ctx context.Context, input *model.SimDataUsageMonthlyOldInput) (output *model.SimDataUsageMonthlyOldOutput, err error) {
|
|
input.Param.Iccid = strings.TrimSpace(input.Param.Iccid)
|
|
input.Param.DateTime = strings.TrimSpace(input.Param.DateTime)
|
|
if input.Param.Iccid == "" {
|
|
return nil, gerror.New("iccid不能为空")
|
|
}
|
|
if input.Param.DateTime == "" {
|
|
return nil, gerror.New("日期时间不能为空")
|
|
}
|
|
if _, err = oldApiScopedCardByIccid(ctx, input.Param.Iccid); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
simDataUsageMonthlyInput := &model.SimDataUsageMonthlyInput{
|
|
SimKeys: []*model.SimKey{
|
|
{
|
|
ICCID: input.Param.Iccid,
|
|
},
|
|
},
|
|
ReqData: model.SimDataUsageMonthlyInputItem{
|
|
QueryDate: input.Param.DateTime,
|
|
},
|
|
}
|
|
out, err := service.UpstreamChan().QuerySimDatausageMonthly(ctx, simDataUsageMonthlyInput)
|
|
if err != nil {
|
|
return nil, gerror.New("查询sim卡历史用量失败")
|
|
}
|
|
if len(out) == 0 {
|
|
return &model.SimDataUsageMonthlyOldOutput{
|
|
UseUnit: "MB",
|
|
UseAmount: 0,
|
|
}, nil
|
|
}
|
|
|
|
output = &model.SimDataUsageMonthlyOldOutput{
|
|
UseUnit: "MB",
|
|
UseAmount: out[0].ResData.DataAmount,
|
|
}
|
|
return output, nil
|
|
}
|
|
|
|
func (s *sOldApiQuery) QuerySimStatus(ctx context.Context, input *model.SimStatusOldInput) (output *model.SimStatusOldOutput, err error) {
|
|
input.Param.Iccid = strings.TrimSpace(input.Param.Iccid)
|
|
if input.Param.Iccid == "" {
|
|
return nil, gerror.New("iccid不能为空")
|
|
}
|
|
|
|
card, err := oldApiScopedCardByIccid(ctx, input.Param.Iccid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
status := s.StatusConversion(*card)
|
|
output = &model.SimStatusOldOutput{
|
|
StatusMessage: model.SimCardStatusMap[status],
|
|
StatusCode: status,
|
|
}
|
|
return output, nil
|
|
}
|
|
|
|
func (s *sOldApiQuery) QuerySimSession(ctx context.Context, input *model.SimStatusOnlineOldInput) (output *model.SimStatusOnlineOldOutput, err error) {
|
|
input.Param.Iccid = strings.TrimSpace(input.Param.Iccid)
|
|
if input.Param.Iccid == "" {
|
|
return nil, gerror.New("iccid不能为空")
|
|
}
|
|
if _, err = oldApiScopedCardByIccid(ctx, input.Param.Iccid); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
simSessionInput := &model.SimSessionInput{
|
|
SimKeys: []*model.SimKey{
|
|
{
|
|
ICCID: input.Param.Iccid,
|
|
},
|
|
},
|
|
}
|
|
outSession, err := service.UpstreamChan().QuerySimSession(ctx, simSessionInput)
|
|
if err != nil {
|
|
return nil, gerror.New("查询sim卡会话失败")
|
|
}
|
|
if len(outSession) == 0 {
|
|
return &model.SimStatusOnlineOldOutput{
|
|
TypeName: "",
|
|
Imei: "",
|
|
Data: model.SimStatusOnlineOldData{},
|
|
StatusMessage: "",
|
|
StatusCode: 0,
|
|
}, nil
|
|
}
|
|
|
|
simStatusOnlineOldData := model.SimStatusOnlineOldData{
|
|
ApnId: outSession[0].ResData.ApnId,
|
|
Rat: outSession[0].ResData.Rat,
|
|
Ipv6: outSession[0].ResData.Ipv6,
|
|
Ip: outSession[0].ResData.Ipv4,
|
|
Ipv6Prefix: outSession[0].ResData.Ipv6Prefix,
|
|
Status: outSession[0].ResData.Status,
|
|
CreateDate: outSession[0].ResData.CreateTime,
|
|
}
|
|
output = &model.SimStatusOnlineOldOutput{
|
|
TypeName: "",
|
|
Imei: "",
|
|
Data: simStatusOnlineOldData,
|
|
StatusMessage: "",
|
|
StatusCode: 0,
|
|
}
|
|
return output, nil
|
|
}
|
|
|
|
func (s *sOldApiQuery) QueryCardInfoList(ctx context.Context, input *model.QueryCardInfoListInput) (output []model.QueryCardInfoListOutput, err error) {
|
|
iccids := make([]string, 0, len(input.Param.Iccid))
|
|
for _, iccid := range input.Param.Iccid {
|
|
iccid = strings.TrimSpace(iccid)
|
|
if iccid == "" {
|
|
continue
|
|
}
|
|
iccids = append(iccids, iccid)
|
|
}
|
|
if len(iccids) == 0 {
|
|
return nil, gerror.New("iccid不能为空")
|
|
}
|
|
if len(iccids) > 100 {
|
|
return nil, gerror.New("iccid数量不能超过100个")
|
|
}
|
|
|
|
entity := []entity.TSimCardInfo{}
|
|
if err := service.IotTSimCardInfo().
|
|
Model(ctx).
|
|
WhereIn(dao.TSimCardInfo.Columns().Iccid, iccids).
|
|
Scan(&entity); err != nil {
|
|
return nil, gerror.New("查询sim卡信息失败")
|
|
}
|
|
if len(entity) == 0 {
|
|
return nil, gerror.New("sim卡不存在")
|
|
}
|
|
|
|
output = make([]model.QueryCardInfoListOutput, 0, len(entity))
|
|
for _, item := range entity {
|
|
output = append(output, model.QueryCardInfoListOutput{
|
|
Iccid: item.Iccid,
|
|
ActivateDate: oldApiDate(item.BossActivateAt, item.BossOpenAt, item.FlowPkgBeginAt),
|
|
DueExpireTime: oldApiEndOfDayString(item.FlowPkgEndAt),
|
|
Imei: item.Imei,
|
|
Imsi: item.Imsi,
|
|
Msisdn: item.Msisdn,
|
|
Type: model.SimCardNameMap[item.CardType],
|
|
Remarks: item.Remark,
|
|
Status: s.StatusConversion(item),
|
|
})
|
|
}
|
|
return output, nil
|
|
}
|