config.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package sim
  2. import (
  3. "encoding/json"
  4. "go-nc/configs/global"
  5. "go-nc/model"
  6. "go-nc/pkg/sim/grace"
  7. "strconv"
  8. "github.com/levigross/grequests"
  9. "github.com/tidwall/gjson"
  10. "go.uber.org/zap"
  11. )
  12. // 获取流量包
  13. func GetFlowPackage() {
  14. var simDataPlan model.Metadata_package
  15. plan, err := grace.GetSimPackageTariffPlan()
  16. if err != nil {
  17. global.App.Log.Error("获取流量包失败", zap.Error(err))
  18. return
  19. }
  20. planList := gjson.GetBytes(plan.Bytes(), "items").Array()
  21. for _, v := range planList {
  22. productId := v.Get("productId").String()
  23. planItemInfo, err := grace.GetSimPackageTariffPlanDetail(productId)
  24. if err != nil {
  25. global.App.Log.Error("获取流量包详情失败", zap.Error(err))
  26. return
  27. }
  28. vInfo := planItemInfo.String()
  29. simDataPlan = model.Metadata_package{
  30. ProductId: productId,
  31. Source: "grace",
  32. ProductName: v.Get("productName").String(),
  33. DataZoneId: v.Get("dataZoneId").String(),
  34. DataZoneName: v.Get("dataZoneName").String(),
  35. ValidDays: int(gjson.Get(vInfo, "validDays").Int()),
  36. DataTotal: int(gjson.Get(vInfo, "dataTotal").Int()),
  37. VoiceMt: int(gjson.Get(vInfo, "voiceMt").Int()),
  38. VoiceMo: int(gjson.Get(vInfo, "voiceMo").Int()),
  39. SmsTotal: int(gjson.Get(vInfo, "smsTotal").Int()),
  40. DataSpeedDefault: int(gjson.Get(vInfo, "dataSpeedDefault").Int()),
  41. DataQuota: int(gjson.Get(vInfo, "dataQuota").Int()),
  42. ValidDayType: int(gjson.Get(vInfo, "validDayType").Int()),
  43. RateGroupName: gjson.Get(vInfo, "rateGroupName").String(),
  44. ZoneVoiceMtName: gjson.Get(vInfo, "zoneVoiceMtName").String(),
  45. ZoneVoiceMoName: gjson.Get(vInfo, "zoneVoiceMoName").String(),
  46. ZoneDataName: gjson.Get(vInfo, "zoneDataName").String(),
  47. ZoneSmsName: gjson.Get(vInfo, "zoneSmsName").String(),
  48. Operator: json.RawMessage(gjson.Get(vInfo, "operator").Raw),
  49. }
  50. global.App.DB.Where("product_id = ?", productId).Model(&simDataPlan).Updates(&simDataPlan).FirstOrCreate(&simDataPlan)
  51. }
  52. }
  53. type GetCardInfoOptions struct {
  54. UserId uint
  55. TariffId string
  56. PoolId string
  57. }
  58. // 查询卡信息
  59. func GetCardInfo(source, iccid string, opts *GetCardInfoOptions) {
  60. // 未实现
  61. if source == "grace" {
  62. iccidInfo, err := grace.GetSimInfo(iccid)
  63. if err != nil {
  64. global.App.Log.Error("获取卡信息失败", zap.Error(err))
  65. return
  66. }
  67. vInfo := iccidInfo.String()
  68. cardItem := model.Sim_card{
  69. Iccid: iccid,
  70. Source: "grace",
  71. CurrentImsi: gjson.Get(vInfo, "currentImsi").String(),
  72. DataSpeed: gjson.Get(vInfo, "dataSpeed").String(),
  73. BindImsi: gjson.Get(vInfo, "bindImsi").String(),
  74. MoneyBalances: gjson.Get(vInfo, "moneyBalances").String(),
  75. CreateTime: gjson.Get(vInfo, "createTime").String(),
  76. PayType: gjson.Get(vInfo, "payType").String(),
  77. IccidStatus: gjson.Get(vInfo, "accountStatus").String(),
  78. DataUsageTotal: gjson.Get(vInfo, "dataUsageTotal").String(),
  79. VoiceMtTotal: gjson.Get(vInfo, "voiceMtTotal").String(),
  80. VoiceMoTotal: gjson.Get(vInfo, "voiceMoTotal").String(),
  81. VoiceTotal: gjson.Get(vInfo, "voiceTotal").String(),
  82. SmsTotal: gjson.Get(vInfo, "smsTotal").String(),
  83. ValidMonth: gjson.Get(vInfo, "validMonth").String(),
  84. CloseTime: gjson.Get(vInfo, "closeTime").String(),
  85. ActiveTime: gjson.Get(vInfo, "activeTime").String(),
  86. CurrentImsiProvider: gjson.Get(vInfo, "currentImsiProvider").String(),
  87. }
  88. if opts != nil {
  89. cardItem.UserId = opts.UserId
  90. cardItem.TariffId = opts.TariffId
  91. cardItem.PoolId = opts.PoolId
  92. }
  93. for _, v := range gjson.Get(vInfo, "dataPackage").Array() {
  94. DataPackage := model.Sim_package{
  95. Iccid: iccid,
  96. TId: v.Get("tId").String(),
  97. ProductId: v.Get("productId").String(),
  98. OTWProductId: int(v.Get("otwProductId").Int()),
  99. ProductName: v.Get("productName").String(),
  100. Status: v.Get("status").String(),
  101. CreateTime: v.Get("createTime").String(),
  102. ActiveTime: v.Get("activeTime").String(),
  103. ExpiryTime: v.Get("expiryTime").String(),
  104. DataTotal: int(v.Get("dataTotal").Int()),
  105. DataUsage: int(v.Get("dataUsage").Int()),
  106. DataToday: int(v.Get("dataToday").Int()),
  107. ValidDays: int(v.Get("validDays").Int()),
  108. Present: int(v.Get("present").Int()),
  109. }
  110. // fmt.Printf("%+v\n", DataPackage)
  111. global.App.DB.Where("iccid = ?", iccid).Model(&DataPackage).Updates(&DataPackage).FirstOrCreate(&DataPackage)
  112. simDataUsage := model.Sim_data_usage{
  113. Iccid: iccid,
  114. TId: v.Get("tId").String(),
  115. ProductId: v.Get("productId").String(),
  116. DataUsage: int(v.Get("dataUsage").Int()),
  117. }
  118. tariffData := model.Sim_traffic{}
  119. simCard := model.Sim_card{}
  120. if opts != nil {
  121. simDataUsage.UserId = opts.UserId
  122. simDataUsage.TariffId = opts.TariffId
  123. simDataUsage.PoolId = opts.PoolId
  124. global.App.DB.Where("id = ?", opts.TariffId).First(&tariffData)
  125. } else {
  126. global.App.DB.Model(&model.Sim_card{}).Select("tariff_id", "pool_id", "user_id").Where("iccid = ?", iccid).First(&simCard)
  127. global.App.DB.Model(&model.Sim_traffic{}).Where("id = ?", simCard.TariffId).First(&tariffData)
  128. simDataUsage.UserId = simCard.UserId
  129. simDataUsage.TariffId = simCard.TariffId
  130. simDataUsage.PoolId = simCard.PoolId
  131. }
  132. trafficBilling, _ := strconv.ParseFloat(tariffData.TrafficBilling, 64)
  133. TrafficBillingAmount, _ := strconv.ParseFloat(tariffData.TrafficBillingAmount, 64)
  134. simDataUsage.Amount = (float64(simDataUsage.DataUsage) / trafficBilling) * TrafficBillingAmount
  135. // 获取资费ID
  136. // global.App.DB.Where("iccid = ?", iccid).Model(&simDataUsage).Updates(&simDataUsage).FirstOrCreate(&simDataUsage)
  137. global.App.DB.
  138. Model(&simDataUsage).
  139. Where("iccid = ? AND t_id = ?", iccid, simDataUsage.TId).
  140. Updates(&simDataUsage).
  141. FirstOrCreate(&simDataUsage)
  142. }
  143. // fmt.Printf("%+v\n", iccidInfo)
  144. global.App.DB.Where("iccid = ?", iccid).Model(&cardItem).Updates(&cardItem).FirstOrCreate(&cardItem)
  145. }
  146. }
  147. // 卡充值
  148. func RechargeCard(source string, opts grace.RechargeSimPackage) {
  149. if source == "grace" {
  150. grace.RechargeSim(opts) // 订购包
  151. GetCardInfo("grace", opts.Iccid, nil) // 更新卡信息
  152. }
  153. }
  154. // 暂停 SIM 卡服务
  155. func StopSim(source string, iccid string) (*grequests.Response, error) {
  156. if source == "grace" {
  157. return grace.PauseSim(iccid)
  158. }
  159. return nil, nil
  160. }
  161. // 恢复 SIM 卡服务
  162. func RuneSim(source string, iccid string) (*grequests.Response, error) {
  163. if source == "grace" {
  164. return grace.ResumeSim(iccid)
  165. }
  166. return nil, nil
  167. }
  168. // 关闭 SIM 卡
  169. func CloseSim(source string, iccid []string) (*grequests.Response, error) {
  170. if source == "grace" {
  171. return grace.CloseSim(iccid)
  172. }
  173. return nil, nil
  174. }
  175. // 卡流量信息查询
  176. func GetSimCdr(source, iccid, startDate, endDate string) (*grequests.Response, error) {
  177. if source == "grace" {
  178. return grace.GetSimCdrDetail(iccid, startDate, endDate)
  179. }
  180. return nil, nil
  181. }