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