package sim import ( "encoding/json" "go-nc/configs/global" "go-nc/model" "go-nc/pkg/sim/grace" "strconv" "github.com/levigross/grequests" "github.com/tidwall/gjson" "go.uber.org/zap" ) // 获取流量包 func GetFlowPackage() { var simDataPlan model.Metadata_package plan, err := grace.GetSimPackageTariffPlan() if err != nil { global.App.Log.Error("获取流量包失败", zap.Error(err)) return } planList := gjson.GetBytes(plan.Bytes(), "items").Array() for _, v := range planList { productId := v.Get("productId").String() planItemInfo, err := grace.GetSimPackageTariffPlanDetail(productId) if err != nil { global.App.Log.Error("获取流量包详情失败", zap.Error(err)) return } vInfo := planItemInfo.String() simDataPlan = model.Metadata_package{ ProductId: productId, Source: "grace", ProductName: v.Get("productName").String(), DataZoneId: v.Get("dataZoneId").String(), DataZoneName: v.Get("dataZoneName").String(), ValidDays: int(gjson.Get(vInfo, "validDays").Int()), DataTotal: int(gjson.Get(vInfo, "dataTotal").Int()), VoiceMt: int(gjson.Get(vInfo, "voiceMt").Int()), VoiceMo: int(gjson.Get(vInfo, "voiceMo").Int()), SmsTotal: int(gjson.Get(vInfo, "smsTotal").Int()), DataSpeedDefault: int(gjson.Get(vInfo, "dataSpeedDefault").Int()), DataQuota: int(gjson.Get(vInfo, "dataQuota").Int()), ValidDayType: int(gjson.Get(vInfo, "validDayType").Int()), RateGroupName: gjson.Get(vInfo, "rateGroupName").String(), ZoneVoiceMtName: gjson.Get(vInfo, "zoneVoiceMtName").String(), ZoneVoiceMoName: gjson.Get(vInfo, "zoneVoiceMoName").String(), ZoneDataName: gjson.Get(vInfo, "zoneDataName").String(), ZoneSmsName: gjson.Get(vInfo, "zoneSmsName").String(), Operator: json.RawMessage(gjson.Get(vInfo, "operator").Raw), } global.App.DB.Where("product_id = ?", productId).Model(&simDataPlan).Updates(&simDataPlan).FirstOrCreate(&simDataPlan) } } type GetCardInfoOptions struct { UserId uint TariffId string PoolId string } // 查询卡信息 func GetCardInfo(source, iccid string, opts *GetCardInfoOptions) { // 未实现 if source == "grace" { iccidInfo, err := grace.GetSimInfo(iccid) if err != nil { global.App.Log.Error("获取卡信息失败", zap.Error(err)) return } vInfo := iccidInfo.String() cardItem := model.Sim_card{ Iccid: iccid, Source: "grace", CurrentImsi: gjson.Get(vInfo, "currentImsi").String(), DataSpeed: gjson.Get(vInfo, "dataSpeed").String(), BindImsi: gjson.Get(vInfo, "bindImsi").String(), MoneyBalances: gjson.Get(vInfo, "moneyBalances").String(), CreateTime: gjson.Get(vInfo, "createTime").String(), PayType: gjson.Get(vInfo, "payType").String(), IccidStatus: gjson.Get(vInfo, "accountStatus").String(), DataUsageTotal: gjson.Get(vInfo, "dataUsageTotal").String(), VoiceMtTotal: gjson.Get(vInfo, "voiceMtTotal").String(), VoiceMoTotal: gjson.Get(vInfo, "voiceMoTotal").String(), VoiceTotal: gjson.Get(vInfo, "voiceTotal").String(), SmsTotal: gjson.Get(vInfo, "smsTotal").String(), ValidMonth: gjson.Get(vInfo, "validMonth").String(), CloseTime: gjson.Get(vInfo, "closeTime").String(), ActiveTime: gjson.Get(vInfo, "activeTime").String(), CurrentImsiProvider: gjson.Get(vInfo, "currentImsiProvider").String(), } if opts != nil { cardItem.UserId = opts.UserId cardItem.TariffId = opts.TariffId cardItem.PoolId = opts.PoolId } for _, v := range gjson.Get(vInfo, "dataPackage").Array() { DataPackage := model.Sim_package{ Iccid: iccid, TId: v.Get("tId").String(), ProductId: v.Get("productId").String(), OTWProductId: int(v.Get("otwProductId").Int()), ProductName: v.Get("productName").String(), Status: v.Get("status").String(), CreateTime: v.Get("createTime").String(), ActiveTime: v.Get("activeTime").String(), ExpiryTime: v.Get("expiryTime").String(), DataTotal: int(v.Get("dataTotal").Int()), DataUsage: int(v.Get("dataUsage").Int()), DataToday: int(v.Get("dataToday").Int()), ValidDays: int(v.Get("validDays").Int()), Present: int(v.Get("present").Int()), } // fmt.Printf("%+v\n", DataPackage) global.App.DB.Where("iccid = ?", iccid).Model(&DataPackage).Updates(&DataPackage).FirstOrCreate(&DataPackage) simDataUsage := model.Sim_data_usage{ Iccid: iccid, TId: v.Get("tId").String(), ProductId: v.Get("productId").String(), DataUsage: int(v.Get("dataUsage").Int()), } tariffData := model.Sim_traffic{} simCard := model.Sim_card{} if opts != nil { simDataUsage.UserId = opts.UserId simDataUsage.TariffId = opts.TariffId simDataUsage.PoolId = opts.PoolId global.App.DB.Where("id = ?", opts.TariffId).First(&tariffData) } else { global.App.DB.Model(&model.Sim_card{}).Select("tariff_id", "pool_id", "user_id").Where("iccid = ?", iccid).First(&simCard) global.App.DB.Model(&model.Sim_traffic{}).Where("id = ?", simCard.TariffId).First(&tariffData) simDataUsage.UserId = simCard.UserId simDataUsage.TariffId = simCard.TariffId simDataUsage.PoolId = simCard.PoolId } trafficBilling, _ := strconv.ParseFloat(tariffData.TrafficBilling, 64) TrafficBillingAmount, _ := strconv.ParseFloat(tariffData.TrafficBillingAmount, 64) simDataUsage.Amount = (float64(simDataUsage.DataUsage) / trafficBilling) * TrafficBillingAmount // 获取资费ID // global.App.DB.Where("iccid = ?", iccid).Model(&simDataUsage).Updates(&simDataUsage).FirstOrCreate(&simDataUsage) global.App.DB. Model(&simDataUsage). Where("iccid = ? AND t_id = ?", iccid, simDataUsage.TId). Updates(&simDataUsage). FirstOrCreate(&simDataUsage) } // fmt.Printf("%+v\n", iccidInfo) global.App.DB.Where("iccid = ?", iccid).Model(&cardItem).Updates(&cardItem).FirstOrCreate(&cardItem) } } // 卡充值 func RechargeCard(source string, opts grace.RechargeSimPackage) { if source == "grace" { grace.RechargeSim(opts) // 订购包 GetCardInfo("grace", opts.Iccid, nil) // 更新卡信息 } } // 暂停 SIM 卡服务 func StopSim(source string, iccid string) (*grequests.Response, error) { if source == "grace" { return grace.PauseSim(iccid) } return nil, nil } // 恢复 SIM 卡服务 func RuneSim(source string, iccid string) (*grequests.Response, error) { if source == "grace" { return grace.ResumeSim(iccid) } return nil, nil } // 关闭 SIM 卡 func CloseSim(source string, iccid []string) (*grequests.Response, error) { if source == "grace" { return grace.CloseSim(iccid) } return nil, nil } // 卡流量信息查询 func GetSimCdr(source, iccid, startDate, endDate string) (*grequests.Response, error) { if source == "grace" { return grace.GetSimCdrDetail(iccid, startDate, endDate) } return nil, nil }