123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- package platform
- import (
- "encoding/json"
- "errors"
- "go-nc/configs/global"
- "go-nc/internal/utils"
- "go-nc/model"
- "go-nc/model/common"
- "strconv"
- "github.com/gin-gonic/gin"
- "github.com/jinzhu/copier"
- )
- // 查看流量消耗明细
- func SimTraffic(c *gin.Context) {
- type Param struct {
- Name string `json:"name"`
- common.Pagination
- }
- var param Param
- param.Pagination = common.NewPagination()
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- err := utils.ValidateStruct(param)
- if err != nil {
- c.Error(err)
- return
- }
- // 总数
- var total int64
- var userList []model.Sys_user
- if err := global.App.DB.Model(&model.Sys_user{}).
- Where("user_type = 2").
- Where("name LIKE ?", "%"+param.Name+"%").
- Count(&total).Error; err != nil {
- c.Error(err)
- return
- }
- // 分页查询
- if err := global.App.DB.
- Model(&model.Sys_user{}).
- Where("user_type = 2").
- Where("name LIKE ?", "%"+param.Name+"%").
- Limit(param.PageSize).
- Offset((param.Current - 1) * param.PageSize).
- Find(&userList).Error; err != nil {
- c.Error(err)
- return
- }
- // 昨天日期
- // statisticalTime := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
- type records struct {
- UserId uint `json:"userId"` // 用户ID -
- Username string `json:"username"` // 用户名 -
- Source string `json:"source"` // 来源-
- StatisticalTime string `json:"statisticalTime"` // 统计时间
- CardPackageName string `json:"cardPackageName"` // 卡套餐名称 -
- DataUsage float64 `json:"dataUsage"` // 流量用量 -
- BillingCycle string `json:"billingCycle"` // 计费周期 -
- }
- var recordsList []records
- // for _, v := range userList {
- // // 资费
- // var tariffData model.Sim_traffic
- // global.App.DB.Select("id", "label", "billing_cycle", "source").Where("user_id = ?", v.Id).Find(&tariffData)
- // if tariffData.Id == "" {
- // recordsList = append(recordsList, records{
- // UserId: v.Id,
- // Username: v.Name,
- // Source: "",
- // StatisticalTime: statisticalTime,
- // CardPackageName: "",
- // DataUsage: 0,
- // BillingCycle: tariffData.BillingCycle,
- // })
- // } else {
- // // 卡信息
- // var simMapList []model.Iot_sim_map
- // global.App.DB.
- // Model(&model.Iot_sim_map{}).
- // Select("map_source").
- // Where("tariff_id = ?", tariffData.Id).
- // Find(&simMapList)
- // // 卡原数据
- // var mIds []string
- // for _, v := range simMapList {
- // mIds = append(mIds, v.MapSource)
- // }
- // var simList []model.Metadata_group_sim
- // global.App.DB.Select("data_package").Where("id in (?)", mIds).Find(&simList)
- // if len(simList) > 0 {
- // // for simList中的for DataPackage是数组 统计出 dataUsage 总数
- // var dataUsageSum float64
- // for _, v := range simList {
- // var dataPackages []interface{}
- // json.Unmarshal([]byte(v.DataPackage), &dataPackages)
- // for _, dataPackage := range dataPackages {
- // dataPackageMap, _ := dataPackage.(map[string]interface{})
- // dataUsage, _ := dataPackageMap["dataUsage"].(float64)
- // dataUsageSum += dataUsage
- // }
- // }
- // recordsList = append(recordsList, records{
- // UserId: v.Id,
- // Username: v.Name,
- // Source: tariffData.Source,
- // StatisticalTime: statisticalTime,
- // CardPackageName: tariffData.Label,
- // DataUsage: dataUsageSum,
- // BillingCycle: tariffData.BillingCycle,
- // })
- // } else {
- // recordsList = append(recordsList, records{
- // UserId: v.Id,
- // Username: v.Name,
- // Source: tariffData.Source,
- // StatisticalTime: statisticalTime,
- // CardPackageName: tariffData.Label,
- // DataUsage: 0,
- // BillingCycle: tariffData.BillingCycle,
- // })
- // }
- // }
- // }
- // 序列化
- recordsListSlice := make([]interface{}, len(recordsList))
- for i, user := range recordsList {
- recordsListSlice[i] = user
- }
- data := common.BuildPagination(recordsListSlice, param.Current, param.PageSize, total)
- c.Set("res_data", data)
- }
- // 查看用户下的卡详情
- func SimCardInfo(c *gin.Context) {
- userId := c.Query("id")
- if userId == "" {
- c.Error(errors.New("缺少参数:userId"))
- return
- }
- id, _ := strconv.Atoi(userId)
- var traffic model.Sim_traffic
- if err := global.App.DB.Where("user_id = ?", id).First(&traffic).Error; err != nil {
- c.Error(err)
- return
- }
- var user model.Sys_user
- if err := global.App.DB.Select("id", "name").Where("id = ?", id).First(&user).Error; err != nil {
- c.Error(err)
- return
- }
- var simMap []model.Iot_sim_map
- if err := global.App.DB.Where("tariff_id = ?", traffic.Id).Find(&simMap).Error; err != nil {
- c.Error(err)
- return
- }
- ids := make([]string, len(simMap))
- for i, v := range simMap {
- ids[i] = v.MapSource
- }
- var simList []model.Sim_card
- if err := global.App.DB.Select("id", "data_package").Where("id in (?)", ids).Find(&simList).Error; err != nil {
- c.Error(err)
- return
- }
- type userSImInfo struct {
- model.Iot_sim_map
- UserId uint `json:"userId"`
- UserName string `json:"userName"`
- DataPackage json.RawMessage `json:"dataPackage"`
- }
- var simMapList []userSImInfo
- copier.Copy(&simMapList, &simMap)
- // for i, v := range simMapList {
- // simMapList[i].UserId = user.Id
- // simMapList[i].UserName = user.Name
- // for _, sim := range simList {
- // if v.MapSource == sim.Id {
- // simMapList[i].DataPackage = sim.DataPackage
- // }
- // }
- // }
- c.Set("res_data", simMapList)
- }
|