chore: 初始开源发布
iotlcm-upstream-proxy:渠道缓存与转发网关。 - 内联原私有依赖:yunze-public → internal/pub/(18 文件), yunze-pb/pb → internal/pb/(9 文件);Consul 注册改用官方 gogf/gf/contrib/registry/consul/v2(API 兼容) - Go module:yz-svr → git.5iot.com/iotlcm-public/iotlcm-upstream-proxy - 凭据与内部标识脱敏:MySQL/Redis/Consul 凭据、私网与生产 IP、内网域名 - 移除内部资产:CI 触发器 - Dockerfile 改用公共基础镜像,移除写死的 registry 凭据 - 补 Apache-2.0 LICENSE,重写 README,LF 行尾 + .gitattributes
This commit is contained in:
@@ -0,0 +1,177 @@
|
||||
package simKeyFill
|
||||
|
||||
import (
|
||||
"context"
|
||||
"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"
|
||||
"strings"
|
||||
|
||||
"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) {
|
||||
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