123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- package platform
- import (
- "errors"
- "fmt"
- "go-nc/configs/global"
- "go-nc/hook"
- "go-nc/internal/utils"
- "go-nc/model"
- "go-nc/model/common"
- "github.com/gin-gonic/gin"
- "github.com/jinzhu/copier"
- )
- // 流量池列表
- func GetTrafficPoolList(c *gin.Context) {
- type Param struct {
- TrafficPoolType string ` json:"trafficPoolType"`
- common.Pagination
- }
- var param Param
- param.Pagination = common.NewPagination()
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- userInfoInterface, _ := c.Get("userInfo")
- userInfo, _ := userInfoInterface.(model.Sys_user)
- var total int64
- var dbList []model.Sim_pool
- type recordsType struct {
- model.Sim_pool
- Iccids []model.Sim_card `json:"iccids"`
- UserId uint `json:"userId"`
- SimTariffName string `json:"simTariffName"`
- Alert model.Alert_traffic_pool `json:"alert"`
- }
- var recordsData []recordsType
- if userInfo.UserType == "1" {
- // 查询总记录数
- if err := global.App.DB.Model(&model.Sim_pool{}).Where("traffic_pool_type LIKE ?", "%"+param.TrafficPoolType+"%").Count(&total).Error; err != nil {
- c.Error(err)
- return
- }
- // 使用分页查询
- if err := global.App.DB.
- Where("traffic_pool_type LIKE ?", "%"+param.TrafficPoolType+"%").
- Limit(param.PageSize).
- Offset((param.Current - 1) * param.PageSize).
- Find(&dbList).Error; err != nil {
- c.Error(err)
- return
- }
- for _, v := range dbList {
- // 查询卡信息
- cardInfo := []model.Sim_card{}
- global.App.DB.Model(&model.Sim_card{}).Where("pool_id = ?", v.Id).Find(&cardInfo)
- // 查询资费名称
- var traffic model.Sim_traffic
- if err := global.App.DB.Select("id", "label", "user_id").Model(&model.Sim_traffic{}).Where("id = ?", v.SimTariffId).First(&traffic).Error; err != nil {
- c.Error(err)
- return
- }
- alert := model.Alert_traffic_pool{}
- global.App.DB.Model(&model.Alert_traffic_pool{}).Where("pool_id = ?", v.Id).First(&alert)
- recordsData = append(recordsData, recordsType{Sim_pool: v, Iccids: cardInfo, SimTariffName: traffic.Label, UserId: traffic.UserId, Alert: alert})
- }
- }
- if userInfo.UserType == "2" {
- // 查询用户资费
- var simTariff []model.Sim_traffic
- if err := global.App.DB.Where("user_id = ?", userInfo.Id).Find(&simTariff).Error; err != nil {
- c.Error(err)
- return
- }
- // 查询资费有哪些流量池
- var ids []string
- for _, v := range simTariff {
- ids = append(ids, v.Id)
- }
- // 查询总记录数
- if err := global.App.DB.Model(&dbList).Where("sim_tariff_id in ?", ids).Where("traffic_pool_type LIKE ?", "%"+param.TrafficPoolType+"%").Count(&total).Error; err != nil {
- c.Error(err)
- return
- }
- if err := global.App.DB.Where("sim_tariff_id in ?", ids).Where("traffic_pool_type LIKE ?", "%"+param.TrafficPoolType+"%").
- Limit(param.PageSize).
- Offset((param.Current - 1) * param.PageSize).
- Find(&dbList).Error; err != nil {
- c.Error(err)
- return
- }
- for _, v := range dbList {
- // 查询卡信息
- cardInfo := []model.Sim_card{}
- global.App.DB.Model(&model.Sim_card{}).Where("pool_id = ?", v.Id).Find(&cardInfo)
- // 查询资费名称
- var traffic model.Sim_traffic
- if err := global.App.DB.Select("id", "label", "user_id").Model(&model.Sim_traffic{}).Where("id = ?", v.SimTariffId).First(&traffic).Error; err != nil {
- c.Error(err)
- return
- }
- alert := model.Alert_traffic_pool{}
- global.App.DB.Model(&model.Alert_traffic_pool{}).Where("pool_id = ?", v.Id).First(&alert)
- recordsData = append(recordsData, recordsType{Sim_pool: v, Iccids: cardInfo, SimTariffName: traffic.Label, UserId: traffic.UserId, Alert: alert})
- }
- }
- // 序列化
- records := make([]interface{}, len(recordsData))
- for i, user := range recordsData {
- records[i] = user
- }
- data := common.BuildPagination(records, param.Current, param.PageSize, total)
- c.Set("res_data", data)
- }
- // 添加流量池
- func AddTrafficPool(c *gin.Context) {
- type Param struct {
- Label string `json:"label" validate:"required"` // 流量包名称
- TrafficPoolType string `json:"trafficPoolType" validate:"required"` // 流量池类型: 1: 前流量池 2: 后流量池
- Source string `json:"source" validate:"required"` // 来源
- SimTariffId string `json:"simTariffId" validate:"required"` // 资费ID
- ExpireTime hook.LocalTime `json:"expireTime" validate:"required"` // 过期时间
- Size int `json:"size"`
- SizeType string `json:"sizeType"`
- }
- var param Param
- 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
- }
- if param.TrafficPoolType == "1" {
- // 查询资费信息
- var tarifc model.Sim_traffic
- if err := global.App.DB.Select("sim_data_plan_id").Where("id = ?", param.SimTariffId).First(&tarifc).Error; err != nil {
- c.Error(err)
- return
- }
- }
- if param.TrafficPoolType == "2" {
- // 判断字符串 Size 是不是空的
- if param.Size == 0 {
- c.Error(errors.New("缺少参数:size"))
- return
- }
- if param.SizeType == "" {
- c.Error(errors.New("缺少参数:sizeType"))
- return
- }
- }
- // 创建流量池
- trafficPool := model.Sim_pool{}
- copier.Copy(&trafficPool, ¶m)
- if err := global.App.DB.Create(&trafficPool).Error; err != nil {
- c.Error(err)
- return
- }
- c.Set("res_data", trafficPool)
- }
- // 删除流量池
- func DeleteTrafficPool(c *gin.Context) {
- id := c.Query("id")
- if id == "" {
- c.Error(errors.New("缺少参数:id"))
- return
- }
- tx := global.App.DB.Begin()
- // 查询绑定了多少张卡
- cardSum := int64(0)
- if err := tx.Model(&model.Sim_card{}).Where("pool_id = ?", id).Count(&cardSum).Error; err != nil {
- tx.Rollback()
- c.Error(err)
- return
- }
- if cardSum > 0 {
- c.Error(fmt.Errorf("该流量池下有%d卡,不可删除!", cardSum))
- return
- }
- if err := tx.Where("id = ?", id).Unscoped().Delete(&model.Sim_pool{}).Error; err != nil {
- tx.Rollback()
- c.Error(err)
- return
- }
- // 清楚卡的流量池
- if err := tx.Model(&model.Sim_card{}).Where("traffic_pool_id = ?", id).Update("pool_id", "").Error; err != nil {
- tx.Rollback()
- c.Error(err)
- return
- }
- // 删除报警
- if err := tx.Where("pool_id = ?", id).Unscoped().Delete(&model.Alert_traffic_pool{}).Error; err != nil {
- tx.Rollback()
- c.Error(err)
- return
- }
- if err := tx.Commit().Error; err != nil {
- tx.Rollback()
- c.Error(err)
- return
- }
- c.Set("res_data", "删除成功!")
- }
- // 编辑流量池
- func UpdateTrafficPool(c *gin.Context) {
- type Param struct {
- Id string `json:"id" validate:"required"`
- Label string `json:"label" validate:"required"` // 流量包名称
- TrafficPoolType string `json:"trafficPoolType" validate:"required"` // 流量池类型: 1: 前流量池 2: 后流量池
- Source string `json:"source" validate:"required"` // 来源
- SimTariffId string `json:"simTariffId" validate:"required"` // 资费ID
- ExpireTime hook.LocalTime `json:"expireTime" validate:"required"` // 过期时间
- Iccids []string `json:"iccids"`
- Size int `json:"size"`
- SizeType string `json:"sizeType"`
- }
- var param Param
- 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
- }
- if param.TrafficPoolType == "1" {
- // 查询资费信息
- var tarifc model.Sim_traffic
- if err := global.App.DB.Select("sim_data_plan_id").Where("id = ?", param.SimTariffId).First(&tarifc).Error; err != nil {
- c.Error(err)
- return
- }
- }
- if param.TrafficPoolType == "2" {
- // 判断字符串 Size 是不是空的
- if param.Size == 0 {
- c.Error(errors.New("缺少参数:size"))
- return
- }
- if param.SizeType == "" {
- c.Error(errors.New("缺少参数:sizeType"))
- return
- }
- }
- // 创建流量池
- tx := global.App.DB.Begin()
- trafficPool := model.Sim_pool{}
- copier.Copy(&trafficPool, ¶m)
- if err := tx.Updates(&trafficPool).Error; err != nil {
- tx.Rollback()
- c.Error(err)
- return
- }
- // 提交
- if err := tx.Commit().Error; err != nil {
- tx.Rollback()
- c.Error(err)
- return
- }
- c.Set("res_data", trafficPool)
- }
- // 查询客户下的流量池
- func GetCustomerTrafficPool(c *gin.Context) {
- // 拿到token, 解析token
- userInfoInterface, _ := c.Get("userInfo")
- userInfo, _ := userInfoInterface.(model.Sys_user)
- // 查询资费
- var trafficList []model.Sim_traffic
- if err := global.App.DB.Where("user_id = ?", userInfo.Id).Find(&trafficList).Error; err != nil {
- c.Error(err)
- return
- }
- var ids []string
- for _, v := range trafficList {
- ids = append(ids, v.Id)
- }
- // 查询流量池
- var trafficPoolList []model.Sim_pool
- if err := global.App.DB.Where("sim_tariff_id in (?)", ids).Find(&trafficPoolList).Error; err != nil {
- c.Error(err)
- return
- }
- c.Set("res_data", trafficPoolList)
- }
|