chore: 并入 iotlcm-upstream-proxy 历史到 upstream-proxy/(原提交 44209ec)
This commit is contained in:
@@ -0,0 +1,193 @@
|
||||
package simKeyFill
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-upstream-proxy/internal/dao"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-upstream-proxy/internal/model"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-upstream-proxy/internal/model/entity"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-upstream-proxy/internal/service"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
type sSimKeyFill struct {
|
||||
}
|
||||
|
||||
func NewSimKeyFill() *sSimKeyFill {
|
||||
return &sSimKeyFill{}
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterSimKeyFill(NewSimKeyFill())
|
||||
}
|
||||
|
||||
func (s *sSimKeyFill) SimKeyFill(ctx context.Context, input *model.SimBaseReqJson) (output model.SimBaseResJson, err error) {
|
||||
var operation struct {
|
||||
OperationID string `json:"operationId"`
|
||||
}
|
||||
request := g.RequestFromCtx(ctx)
|
||||
if request != nil && (request.URL.Path == "/sim/change/sim-apn" || request.URL.Path == "/sim/query/sim-apn-capability") && json.Unmarshal(input.ReqData, &operation) == nil && operation.OperationID != "" {
|
||||
if len(input.SimKeys) == 0 || len(input.SimKeys) > 100 {
|
||||
return nil, gerror.New("异步子任务卡数必须在1至100之间")
|
||||
}
|
||||
for _, key := range input.SimKeys {
|
||||
if key == nil || key.ChanKey == "" || key.ICCID == "" {
|
||||
return nil, gerror.New("异步子任务必须保留chanKey和ICCID快照")
|
||||
}
|
||||
}
|
||||
return service.UpstreamAPI().SimBase(ctx, input)
|
||||
}
|
||||
simKeys, err := s.batchGetSimKey(ctx, input.SimKeys)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(simKeys) == 0 {
|
||||
g.Log().Debug(ctx, "SimKeyFill.batchGetSimKey, simKeys is empty, input %+v", input)
|
||||
return nil, gerror.New("simKeys is empty")
|
||||
}
|
||||
input.SimKeys = simKeys
|
||||
output, err = service.UpstreamAPI().SimBase(ctx, input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sSimKeyFill) batchGetSimKey(ctx context.Context, inputs []*model.SimKey) ([]*model.SimKey, error) {
|
||||
queryICCIDKey := "sim:code:iccid:"
|
||||
queryMSISDNKey := "sim:code:msisdn:"
|
||||
queryIMSIKey := "sim:code:imsi:"
|
||||
if len(inputs) == 0 {
|
||||
g.Log().Debugf(ctx, "SimKeyFill.batchGetSimKey, The parameter is empty, inputs %+v", inputs)
|
||||
return nil, gerror.New("参数不能为空")
|
||||
}
|
||||
|
||||
var redisKeys []string
|
||||
var dbKeys []string
|
||||
simKeys := []*model.SimKey{}
|
||||
|
||||
// 设置Redis查询Key
|
||||
for _, in := range inputs {
|
||||
switch {
|
||||
case in.ICCID != "":
|
||||
key := queryICCIDKey + in.ICCID
|
||||
redisKeys = append(redisKeys, key)
|
||||
case in.MSISDN != "":
|
||||
key := queryMSISDNKey + in.MSISDN
|
||||
redisKeys = append(redisKeys, key)
|
||||
case in.IMSI != "":
|
||||
key := queryIMSIKey + in.IMSI
|
||||
redisKeys = append(redisKeys, key)
|
||||
default:
|
||||
g.Log().Debugf(ctx, "SimKeyFill.batchGetSimKey, The parameter is empty, inputs %+v", inputs)
|
||||
return nil, gerror.New("iccid/msisdn/imsi至少一个不能为空")
|
||||
}
|
||||
}
|
||||
|
||||
// 查询Redis数据
|
||||
g.Log().Info(ctx, "SimKeyFill.batchGetSimKey, query redis")
|
||||
vals, err := g.Redis().MGet(ctx, redisKeys...)
|
||||
if err != nil {
|
||||
g.Log().Debug(ctx, "SimKeyFill.batchGetSimKey, query redis error %+v, redisKeys %+v, inputs %+v", err, redisKeys, inputs)
|
||||
} else {
|
||||
// 解析Redis数据
|
||||
for _, key := range redisKeys {
|
||||
simKey := new(model.SimKey)
|
||||
err := vals[key].Struct(&simKey)
|
||||
if err != nil {
|
||||
g.Log().Debug(ctx, "SimKeyFill.batchGetSimKey, unmarshalValue error %+v, key %s, inputs %+v", err, key, inputs)
|
||||
dbKeys = append(dbKeys, key)
|
||||
continue
|
||||
}
|
||||
simKeys = append(simKeys, simKey)
|
||||
}
|
||||
}
|
||||
|
||||
g.Log().Info(ctx, "SimKeyFill.batchGetSimKey, query db")
|
||||
if len(dbKeys) > 0 {
|
||||
// 解析数据库查询Key
|
||||
var dbIccid []string
|
||||
var dbMsisdn []string
|
||||
var dbImsi []string
|
||||
for _, key := range dbKeys {
|
||||
keySplit := strings.Split(key, ":")
|
||||
if len(keySplit) != 4 {
|
||||
g.Log().Debug(ctx, "SimKeyFill.batchGetSimKey, Error in splicing data, key %s, keySplit %v", key, keySplit)
|
||||
continue
|
||||
}
|
||||
switch keySplit[2] {
|
||||
case "iccid":
|
||||
dbIccid = append(dbIccid, keySplit[3])
|
||||
case "msisdn":
|
||||
dbMsisdn = append(dbMsisdn, keySplit[3])
|
||||
case "imsi":
|
||||
dbImsi = append(dbImsi, keySplit[3])
|
||||
}
|
||||
}
|
||||
|
||||
mod := dao.TSimCardInfo.Ctx(ctx)
|
||||
|
||||
if len(dbIccid) > 0 {
|
||||
mod = mod.WhereOrIn(dao.TSimCardInfo.Columns().Iccid, dbIccid)
|
||||
}
|
||||
if len(dbMsisdn) > 0 {
|
||||
mod = mod.WhereOrIn(dao.TSimCardInfo.Columns().Msisdn, dbMsisdn)
|
||||
}
|
||||
if len(dbImsi) > 0 {
|
||||
mod = mod.WhereOrIn(dao.TSimCardInfo.Columns().Imsi, dbImsi)
|
||||
}
|
||||
|
||||
// 查询数据库数据
|
||||
var simInfos []*entity.TSimCardInfo
|
||||
err = mod.Scan(&simInfos)
|
||||
if err != nil {
|
||||
g.Log().Error(ctx, "SimKeyFill.batchGetSimKey, query db error %+v, dbKeys %+v, inputs %+v", err, dbKeys, inputs)
|
||||
return nil, gerror.New("查询数据库数据失败")
|
||||
}
|
||||
if len(simInfos) == 0 {
|
||||
g.Log().Debug(ctx, "SimKeyFill.batchGetSimKey, simInfos is empty, dbKeys %+v, inputs %+v", dbKeys, inputs)
|
||||
return nil, gerror.New("卡信息不存在")
|
||||
}
|
||||
|
||||
// 整理卡信息
|
||||
simInfoMap := make(map[string]*model.SimKey)
|
||||
for _, SimInfo := range simInfos {
|
||||
simInfoMap[queryICCIDKey+SimInfo.Iccid] = &model.SimKey{
|
||||
ICCID: SimInfo.Iccid,
|
||||
MSISDN: SimInfo.Msisdn,
|
||||
IMSI: SimInfo.Imsi,
|
||||
}
|
||||
simInfoMap[queryMSISDNKey+SimInfo.Msisdn] = &model.SimKey{
|
||||
MSISDN: SimInfo.Msisdn,
|
||||
IMSI: SimInfo.Imsi,
|
||||
ICCID: SimInfo.Iccid,
|
||||
}
|
||||
simInfoMap[queryIMSIKey+SimInfo.Imsi] = &model.SimKey{
|
||||
IMSI: SimInfo.Imsi,
|
||||
ICCID: SimInfo.Iccid,
|
||||
MSISDN: SimInfo.Msisdn,
|
||||
}
|
||||
}
|
||||
|
||||
// 新建一个线程,进行Redis数据存储
|
||||
go s.SetRedis(ctx, simInfoMap)
|
||||
// 解析数据库数据
|
||||
for _, info := range dbKeys {
|
||||
simKeys = append(simKeys, simInfoMap[info])
|
||||
}
|
||||
}
|
||||
return simKeys, nil
|
||||
}
|
||||
|
||||
func (s *sSimKeyFill) SetRedis(ctx context.Context, simInfoMap map[string]*model.SimKey) {
|
||||
for key, simKey := range simInfoMap {
|
||||
_, err := g.Redis().Set(ctx, key, *simKey)
|
||||
if err != nil {
|
||||
g.Log().Debugf(ctx, "SimKeyFill.batchGetSimKey, Set error %+v, key %s, simKey %+v", err, key, simKey)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user