123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- package simApi
- import (
- "errors"
- "fmt"
- "go-nc/configs/global"
- "go-nc/internal/utils"
- "go-nc/model"
- "go-nc/model/common"
- "go-nc/pkg/sim"
- "strings"
- "github.com/gin-gonic/gin"
- "github.com/jinzhu/copier"
- "github.com/tidwall/gjson"
- )
- func cardInfoAll(c *gin.Context) {
- // 查询卡信息
- // // 查询卡信息
- type Param struct {
- Iccid string `json:"iccid"`
- common.Pagination
- }
- var param Param
- param.Pagination = common.NewPagination()
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- // 先查询总记录数
- var total int64
- if err := global.App.DB.Model(&model.Sim_card{}).Where("iccid LIKE ?", "%"+param.Iccid+"%").Count(&total).Error; err != nil {
- c.Error(err)
- return
- }
- // 使用分页查询
- var cardList []model.Sim_card
- if err := global.App.DB.Limit(param.PageSize).
- Offset((param.Current - 1) * param.PageSize).
- Find(&cardList).Error; err != nil {
- c.Error(err)
- return
- }
- type RecordData struct {
- model.Sim_card
- DataPackage []model.Sim_package `json:"dataPackage"`
- UserName string `json:"userName"`
- TariffName string `json:"tariffName"`
- PoolName string `json:"poolName"`
- }
- var recordsData []RecordData
- copier.Copy(&recordsData, &cardList)
- // 序列化
- records := make([]interface{}, len(recordsData))
- for i, v := range recordsData {
- userName := ""
- global.App.DB.Model(&model.Sys_user{}).Select("name").Where("id = ?", v.UserId).Find(&userName)
- v.UserName = userName
- tariffName := ""
- global.App.DB.Model(&model.Sim_traffic{}).Select("label").Where("id = ?", v.TariffId).Find(&tariffName)
- v.TariffName = tariffName
- poolName := ""
- global.App.DB.Model(&model.Sim_pool{}).Select("label").Where("id = ?", v.PoolId).Find(&poolName)
- v.PoolName = poolName
- simPackage := []model.Sim_package{}
- global.App.DB.Model(&model.Sim_package{}).Where("iccid = ?", v.Iccid).Find(&simPackage)
- for i := range simPackage {
- simPackage[i].ProductName = ""
- simPackage[i].ValidDays = 0
- }
- v.DataPackage = simPackage
- records[i] = v
- }
- // 返回数据
- data := common.Pagination{
- Records: records,
- Current: param.Current,
- PageSize: param.PageSize,
- Total: total,
- }
- c.Set("res_data", data)
- }
- // 修改卡信息
- func CardInfoUpdate(c *gin.Context) {
- type Param struct {
- ICCID string `json:"iccid" validate:"required"` // 卡ID
- UserId uint `json:"userId" validate:"required"` // 用户ID
- TariffId uint `json:"tariffId" validate:"required"` // 资费ID
- Source string `json:"source" validate:"required"` // 来源
- }
- 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 err := global.App.DB.Model(&model.Sim_card{}).Where("iccid = ? AND user_id = ?", param.ICCID, param.UserId).
- Update("tariff_id", param.TariffId).
- Update("source", param.Source).Error; err != nil {
- c.Error(err)
- return
- }
- // 记录操作
- global.App.DB.Create(&model.Log_card_operation{
- UserId: param.UserId,
- Iccid: param.ICCID,
- Operation: "修改卡信息",
- Source: param.Source,
- Remark: fmt.Sprintf("ICCID: %s, UserId: %d, TariffId: %d, Source: %s", param.ICCID, param.UserId, param.TariffId, param.Source),
- })
- c.Set("res_data", "修改成功")
- }
- // 平台和客户卡信息
- func CardInfoList(c *gin.Context) {
- // 拿到token, 解析token
- userInfoInterface, _ := c.Get("userInfo")
- userInfo, _ := userInfoInterface.(model.Sys_user)
- if userInfo.UserType == "1" {
- cardInfoAll(c)
- return
- }
- }
- // 暂停 SIM 卡服务
- func StopSim(c *gin.Context) {
- type Param struct {
- ICCID string `json:"iccid" validate:"required"` // 卡ID
- Source string `json:"source" validate:"required"` // 来源
- }
- 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
- }
- data, err := sim.StopSim(param.Source, param.ICCID)
- if err != nil {
- c.Error(err)
- return
- }
- sim.GetCardInfo("grace", param.ICCID, nil)
- userInfoInterface, _ := c.Get("userInfo")
- userInfo, _ := userInfoInterface.(model.Sys_user)
- // 记录操作
- global.App.DB.Create(&model.Log_card_operation{
- UserId: userInfo.Id,
- Iccid: param.ICCID,
- Operation: "暂停卡服务",
- Source: param.Source,
- Remark: fmt.Sprintf("ICCID: %s, UserId: %d, Source: %s", param.ICCID, userInfo.Id, param.Source),
- })
- c.Set("res_data", data)
- }
- // 恢复 SIM 卡服务
- func RuneSim(c *gin.Context) {
- type Param struct {
- ICCID string `json:"iccid" validate:"required"` // 卡ID
- Source string `json:"source" validate:"required"` // 来源
- }
- 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
- }
- data, err := sim.RuneSim(param.Source, param.ICCID)
- if err != nil {
- c.Error(err)
- return
- }
- sim.GetCardInfo("grace", param.ICCID, nil)
- userInfoInterface, _ := c.Get("userInfo")
- userInfo, _ := userInfoInterface.(model.Sys_user)
- // 记录操作
- global.App.DB.Create(&model.Log_card_operation{
- UserId: userInfo.Id,
- Iccid: param.ICCID,
- Operation: "恢复卡服务",
- Source: param.Source,
- Remark: fmt.Sprintf("ICCID: %s, UserId: %d, Source: %s", param.ICCID, userInfo.Id, param.Source),
- })
- c.Set("res_data", data)
- }
- // 关闭 SIM 卡
- func CloseSim(c *gin.Context) {
- type Param struct {
- ICCID []string `json:"iccids" validate:"required"` // 卡ID
- Source string `json:"source" validate:"required"` // 来源
- }
- 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
- }
- data, err := sim.CloseSim(param.Source, param.ICCID)
- if err != nil {
- c.Error(err)
- return
- }
- userInfoInterface, _ := c.Get("userInfo")
- userInfo, _ := userInfoInterface.(model.Sys_user)
- // 记录操作
- global.App.DB.Create(&model.Log_card_operation{
- UserId: userInfo.Id,
- Iccid: strings.Join(param.ICCID, ","),
- Operation: "关闭卡服务",
- Source: param.Source,
- Remark: fmt.Sprintf("ICCID: %s, UserId: %d, Source: %s", param.ICCID, userInfo.Id, param.Source),
- })
- c.Set("res_data", data)
- }
- // CDR 使用查询
- func SimCDR(c *gin.Context) {
- type Param struct {
- ICCID string `json:"iccid" validate:"required"` // 卡ID
- Source string `json:"source" validate:"required"` // 来源
- StartDate string `json:"startDate" validate:"required"` // 开始日期:如 2024-09-29
- EndDate string `json:"endDate" validate:"required"` // 结束日期:如 2024-09-29
- }
- 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
- }
- data, err := sim.GetSimCdr(param.Source, param.ICCID, param.StartDate, param.EndDate)
- if err != nil {
- c.Error(err)
- return
- }
- responses := gjson.GetBytes(data.Bytes(), "items").Raw
- c.Set("res_data", responses)
- }
|