sim.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package simApi
  2. import (
  3. "errors"
  4. "fmt"
  5. "go-nc/configs/global"
  6. "go-nc/internal/utils"
  7. "go-nc/model"
  8. "go-nc/model/common"
  9. "go-nc/pkg/sim"
  10. "strings"
  11. "github.com/gin-gonic/gin"
  12. "github.com/jinzhu/copier"
  13. "github.com/tidwall/gjson"
  14. )
  15. func cardInfoAll(c *gin.Context) {
  16. // 查询卡信息
  17. // // 查询卡信息
  18. type Param struct {
  19. Iccid string `json:"iccid"`
  20. common.Pagination
  21. }
  22. var param Param
  23. param.Pagination = common.NewPagination()
  24. if err := c.ShouldBindJSON(&param); err != nil {
  25. c.Error(errors.New("缺少参数:" + err.Error()))
  26. return
  27. }
  28. // 先查询总记录数
  29. var total int64
  30. if err := global.App.DB.Model(&model.Sim_card{}).Where("iccid LIKE ?", "%"+param.Iccid+"%").Count(&total).Error; err != nil {
  31. c.Error(err)
  32. return
  33. }
  34. // 使用分页查询
  35. var cardList []model.Sim_card
  36. if err := global.App.DB.Limit(param.PageSize).
  37. Offset((param.Current - 1) * param.PageSize).
  38. Find(&cardList).Error; err != nil {
  39. c.Error(err)
  40. return
  41. }
  42. type RecordData struct {
  43. model.Sim_card
  44. DataPackage []model.Sim_package `json:"dataPackage"`
  45. UserName string `json:"userName"`
  46. TariffName string `json:"tariffName"`
  47. PoolName string `json:"poolName"`
  48. }
  49. var recordsData []RecordData
  50. copier.Copy(&recordsData, &cardList)
  51. // 序列化
  52. records := make([]interface{}, len(recordsData))
  53. for i, v := range recordsData {
  54. userName := ""
  55. global.App.DB.Model(&model.Sys_user{}).Select("name").Where("id = ?", v.UserId).Find(&userName)
  56. v.UserName = userName
  57. tariffName := ""
  58. global.App.DB.Model(&model.Sim_traffic{}).Select("label").Where("id = ?", v.TariffId).Find(&tariffName)
  59. v.TariffName = tariffName
  60. poolName := ""
  61. global.App.DB.Model(&model.Sim_pool{}).Select("label").Where("id = ?", v.PoolId).Find(&poolName)
  62. v.PoolName = poolName
  63. simPackage := []model.Sim_package{}
  64. global.App.DB.Model(&model.Sim_package{}).Where("iccid = ?", v.Iccid).Find(&simPackage)
  65. for i := range simPackage {
  66. simPackage[i].ProductName = ""
  67. simPackage[i].ValidDays = 0
  68. }
  69. v.DataPackage = simPackage
  70. records[i] = v
  71. }
  72. // 返回数据
  73. data := common.Pagination{
  74. Records: records,
  75. Current: param.Current,
  76. PageSize: param.PageSize,
  77. Total: total,
  78. }
  79. c.Set("res_data", data)
  80. }
  81. // 修改卡信息
  82. func CardInfoUpdate(c *gin.Context) {
  83. type Param struct {
  84. ICCID string `json:"iccid" validate:"required"` // 卡ID
  85. UserId uint `json:"userId" validate:"required"` // 用户ID
  86. TariffId uint `json:"tariffId" validate:"required"` // 资费ID
  87. Source string `json:"source" validate:"required"` // 来源
  88. }
  89. var param Param
  90. if err := c.ShouldBindJSON(&param); err != nil {
  91. c.Error(errors.New("缺少参数:" + err.Error()))
  92. return
  93. }
  94. err := utils.ValidateStruct(param)
  95. if err != nil {
  96. c.Error(err)
  97. return
  98. }
  99. // 创建
  100. if err := global.App.DB.Model(&model.Sim_card{}).Where("iccid = ? AND user_id = ?", param.ICCID, param.UserId).
  101. Update("tariff_id", param.TariffId).
  102. Update("source", param.Source).Error; err != nil {
  103. c.Error(err)
  104. return
  105. }
  106. // 记录操作
  107. global.App.DB.Create(&model.Log_card_operation{
  108. UserId: param.UserId,
  109. Iccid: param.ICCID,
  110. Operation: "修改卡信息",
  111. Source: param.Source,
  112. Remark: fmt.Sprintf("ICCID: %s, UserId: %d, TariffId: %d, Source: %s", param.ICCID, param.UserId, param.TariffId, param.Source),
  113. })
  114. c.Set("res_data", "修改成功")
  115. }
  116. // 平台和客户卡信息
  117. func CardInfoList(c *gin.Context) {
  118. // 拿到token, 解析token
  119. userInfoInterface, _ := c.Get("userInfo")
  120. userInfo, _ := userInfoInterface.(model.Sys_user)
  121. if userInfo.UserType == "1" {
  122. cardInfoAll(c)
  123. return
  124. }
  125. }
  126. // 暂停 SIM 卡服务
  127. func StopSim(c *gin.Context) {
  128. type Param struct {
  129. ICCID string `json:"iccid" validate:"required"` // 卡ID
  130. Source string `json:"source" validate:"required"` // 来源
  131. }
  132. var param Param
  133. if err := c.ShouldBindJSON(&param); err != nil {
  134. c.Error(errors.New("缺少参数:" + err.Error()))
  135. return
  136. }
  137. err := utils.ValidateStruct(param)
  138. if err != nil {
  139. c.Error(err)
  140. return
  141. }
  142. data, err := sim.StopSim(param.Source, param.ICCID)
  143. if err != nil {
  144. c.Error(err)
  145. return
  146. }
  147. sim.GetCardInfo("grace", param.ICCID, nil)
  148. userInfoInterface, _ := c.Get("userInfo")
  149. userInfo, _ := userInfoInterface.(model.Sys_user)
  150. // 记录操作
  151. global.App.DB.Create(&model.Log_card_operation{
  152. UserId: userInfo.Id,
  153. Iccid: param.ICCID,
  154. Operation: "暂停卡服务",
  155. Source: param.Source,
  156. Remark: fmt.Sprintf("ICCID: %s, UserId: %d, Source: %s", param.ICCID, userInfo.Id, param.Source),
  157. })
  158. c.Set("res_data", data)
  159. }
  160. // 恢复 SIM 卡服务
  161. func RuneSim(c *gin.Context) {
  162. type Param struct {
  163. ICCID string `json:"iccid" validate:"required"` // 卡ID
  164. Source string `json:"source" validate:"required"` // 来源
  165. }
  166. var param Param
  167. if err := c.ShouldBindJSON(&param); err != nil {
  168. c.Error(errors.New("缺少参数:" + err.Error()))
  169. return
  170. }
  171. err := utils.ValidateStruct(param)
  172. if err != nil {
  173. c.Error(err)
  174. return
  175. }
  176. data, err := sim.RuneSim(param.Source, param.ICCID)
  177. if err != nil {
  178. c.Error(err)
  179. return
  180. }
  181. sim.GetCardInfo("grace", param.ICCID, nil)
  182. userInfoInterface, _ := c.Get("userInfo")
  183. userInfo, _ := userInfoInterface.(model.Sys_user)
  184. // 记录操作
  185. global.App.DB.Create(&model.Log_card_operation{
  186. UserId: userInfo.Id,
  187. Iccid: param.ICCID,
  188. Operation: "恢复卡服务",
  189. Source: param.Source,
  190. Remark: fmt.Sprintf("ICCID: %s, UserId: %d, Source: %s", param.ICCID, userInfo.Id, param.Source),
  191. })
  192. c.Set("res_data", data)
  193. }
  194. // 关闭 SIM 卡
  195. func CloseSim(c *gin.Context) {
  196. type Param struct {
  197. ICCID []string `json:"iccids" validate:"required"` // 卡ID
  198. Source string `json:"source" validate:"required"` // 来源
  199. }
  200. var param Param
  201. if err := c.ShouldBindJSON(&param); err != nil {
  202. c.Error(errors.New("缺少参数:" + err.Error()))
  203. return
  204. }
  205. err := utils.ValidateStruct(param)
  206. if err != nil {
  207. c.Error(err)
  208. return
  209. }
  210. data, err := sim.CloseSim(param.Source, param.ICCID)
  211. if err != nil {
  212. c.Error(err)
  213. return
  214. }
  215. userInfoInterface, _ := c.Get("userInfo")
  216. userInfo, _ := userInfoInterface.(model.Sys_user)
  217. // 记录操作
  218. global.App.DB.Create(&model.Log_card_operation{
  219. UserId: userInfo.Id,
  220. Iccid: strings.Join(param.ICCID, ","),
  221. Operation: "关闭卡服务",
  222. Source: param.Source,
  223. Remark: fmt.Sprintf("ICCID: %s, UserId: %d, Source: %s", param.ICCID, userInfo.Id, param.Source),
  224. })
  225. c.Set("res_data", data)
  226. }
  227. // CDR 使用查询
  228. func SimCDR(c *gin.Context) {
  229. type Param struct {
  230. ICCID string `json:"iccid" validate:"required"` // 卡ID
  231. Source string `json:"source" validate:"required"` // 来源
  232. StartDate string `json:"startDate" validate:"required"` // 开始日期:如 2024-09-29
  233. EndDate string `json:"endDate" validate:"required"` // 结束日期:如 2024-09-29
  234. }
  235. var param Param
  236. if err := c.ShouldBindJSON(&param); err != nil {
  237. c.Error(errors.New("缺少参数:" + err.Error()))
  238. return
  239. }
  240. err := utils.ValidateStruct(param)
  241. if err != nil {
  242. c.Error(err)
  243. return
  244. }
  245. data, err := sim.GetSimCdr(param.Source, param.ICCID, param.StartDate, param.EndDate)
  246. if err != nil {
  247. c.Error(err)
  248. return
  249. }
  250. responses := gjson.GetBytes(data.Bytes(), "items").Raw
  251. c.Set("res_data", responses)
  252. }