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) }