sim.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package platform
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "go-nc/configs/global"
  6. "go-nc/internal/utils"
  7. "go-nc/model"
  8. "go-nc/model/common"
  9. "strconv"
  10. "github.com/gin-gonic/gin"
  11. "github.com/jinzhu/copier"
  12. )
  13. // 查看流量消耗明细
  14. func SimTraffic(c *gin.Context) {
  15. type Param struct {
  16. Name string `json:"name"`
  17. common.Pagination
  18. }
  19. var param Param
  20. param.Pagination = common.NewPagination()
  21. if err := c.ShouldBindJSON(&param); err != nil {
  22. c.Error(errors.New("缺少参数:" + err.Error()))
  23. return
  24. }
  25. err := utils.ValidateStruct(param)
  26. if err != nil {
  27. c.Error(err)
  28. return
  29. }
  30. // 总数
  31. var total int64
  32. var userList []model.Sys_user
  33. if err := global.App.DB.Model(&model.Sys_user{}).
  34. Where("user_type = 2").
  35. Where("name LIKE ?", "%"+param.Name+"%").
  36. Count(&total).Error; err != nil {
  37. c.Error(err)
  38. return
  39. }
  40. // 分页查询
  41. if err := global.App.DB.
  42. Model(&model.Sys_user{}).
  43. Where("user_type = 2").
  44. Where("name LIKE ?", "%"+param.Name+"%").
  45. Limit(param.PageSize).
  46. Offset((param.Current - 1) * param.PageSize).
  47. Find(&userList).Error; err != nil {
  48. c.Error(err)
  49. return
  50. }
  51. // 昨天日期
  52. // statisticalTime := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  53. type records struct {
  54. UserId uint `json:"userId"` // 用户ID -
  55. Username string `json:"username"` // 用户名 -
  56. Source string `json:"source"` // 来源-
  57. StatisticalTime string `json:"statisticalTime"` // 统计时间
  58. CardPackageName string `json:"cardPackageName"` // 卡套餐名称 -
  59. DataUsage float64 `json:"dataUsage"` // 流量用量 -
  60. BillingCycle string `json:"billingCycle"` // 计费周期 -
  61. }
  62. var recordsList []records
  63. // for _, v := range userList {
  64. // // 资费
  65. // var tariffData model.Sim_traffic
  66. // global.App.DB.Select("id", "label", "billing_cycle", "source").Where("user_id = ?", v.Id).Find(&tariffData)
  67. // if tariffData.Id == "" {
  68. // recordsList = append(recordsList, records{
  69. // UserId: v.Id,
  70. // Username: v.Name,
  71. // Source: "",
  72. // StatisticalTime: statisticalTime,
  73. // CardPackageName: "",
  74. // DataUsage: 0,
  75. // BillingCycle: tariffData.BillingCycle,
  76. // })
  77. // } else {
  78. // // 卡信息
  79. // var simMapList []model.Iot_sim_map
  80. // global.App.DB.
  81. // Model(&model.Iot_sim_map{}).
  82. // Select("map_source").
  83. // Where("tariff_id = ?", tariffData.Id).
  84. // Find(&simMapList)
  85. // // 卡原数据
  86. // var mIds []string
  87. // for _, v := range simMapList {
  88. // mIds = append(mIds, v.MapSource)
  89. // }
  90. // var simList []model.Metadata_group_sim
  91. // global.App.DB.Select("data_package").Where("id in (?)", mIds).Find(&simList)
  92. // if len(simList) > 0 {
  93. // // for simList中的for DataPackage是数组 统计出 dataUsage 总数
  94. // var dataUsageSum float64
  95. // for _, v := range simList {
  96. // var dataPackages []interface{}
  97. // json.Unmarshal([]byte(v.DataPackage), &dataPackages)
  98. // for _, dataPackage := range dataPackages {
  99. // dataPackageMap, _ := dataPackage.(map[string]interface{})
  100. // dataUsage, _ := dataPackageMap["dataUsage"].(float64)
  101. // dataUsageSum += dataUsage
  102. // }
  103. // }
  104. // recordsList = append(recordsList, records{
  105. // UserId: v.Id,
  106. // Username: v.Name,
  107. // Source: tariffData.Source,
  108. // StatisticalTime: statisticalTime,
  109. // CardPackageName: tariffData.Label,
  110. // DataUsage: dataUsageSum,
  111. // BillingCycle: tariffData.BillingCycle,
  112. // })
  113. // } else {
  114. // recordsList = append(recordsList, records{
  115. // UserId: v.Id,
  116. // Username: v.Name,
  117. // Source: tariffData.Source,
  118. // StatisticalTime: statisticalTime,
  119. // CardPackageName: tariffData.Label,
  120. // DataUsage: 0,
  121. // BillingCycle: tariffData.BillingCycle,
  122. // })
  123. // }
  124. // }
  125. // }
  126. // 序列化
  127. recordsListSlice := make([]interface{}, len(recordsList))
  128. for i, user := range recordsList {
  129. recordsListSlice[i] = user
  130. }
  131. data := common.BuildPagination(recordsListSlice, param.Current, param.PageSize, total)
  132. c.Set("res_data", data)
  133. }
  134. // 查看用户下的卡详情
  135. func SimCardInfo(c *gin.Context) {
  136. userId := c.Query("id")
  137. if userId == "" {
  138. c.Error(errors.New("缺少参数:userId"))
  139. return
  140. }
  141. id, _ := strconv.Atoi(userId)
  142. var traffic model.Sim_traffic
  143. if err := global.App.DB.Where("user_id = ?", id).First(&traffic).Error; err != nil {
  144. c.Error(err)
  145. return
  146. }
  147. var user model.Sys_user
  148. if err := global.App.DB.Select("id", "name").Where("id = ?", id).First(&user).Error; err != nil {
  149. c.Error(err)
  150. return
  151. }
  152. var simMap []model.Iot_sim_map
  153. if err := global.App.DB.Where("tariff_id = ?", traffic.Id).Find(&simMap).Error; err != nil {
  154. c.Error(err)
  155. return
  156. }
  157. ids := make([]string, len(simMap))
  158. for i, v := range simMap {
  159. ids[i] = v.MapSource
  160. }
  161. var simList []model.Sim_card
  162. if err := global.App.DB.Select("id", "data_package").Where("id in (?)", ids).Find(&simList).Error; err != nil {
  163. c.Error(err)
  164. return
  165. }
  166. type userSImInfo struct {
  167. model.Iot_sim_map
  168. UserId uint `json:"userId"`
  169. UserName string `json:"userName"`
  170. DataPackage json.RawMessage `json:"dataPackage"`
  171. }
  172. var simMapList []userSImInfo
  173. copier.Copy(&simMapList, &simMap)
  174. // for i, v := range simMapList {
  175. // simMapList[i].UserId = user.Id
  176. // simMapList[i].UserName = user.Name
  177. // for _, sim := range simList {
  178. // if v.MapSource == sim.Id {
  179. // simMapList[i].DataPackage = sim.DataPackage
  180. // }
  181. // }
  182. // }
  183. c.Set("res_data", simMapList)
  184. }