pool.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. package platform
  2. import (
  3. "errors"
  4. "fmt"
  5. "go-nc/configs/global"
  6. "go-nc/hook"
  7. "go-nc/internal/utils"
  8. "go-nc/model"
  9. "go-nc/model/common"
  10. "github.com/gin-gonic/gin"
  11. "github.com/jinzhu/copier"
  12. )
  13. // 流量池列表
  14. func GetTrafficPoolList(c *gin.Context) {
  15. type Param struct {
  16. TrafficPoolType string ` json:"trafficPoolType"`
  17. common.Pagination
  18. }
  19. var param Param
  20. param.Pagination = common.NewPagination()
  21. if err := c.ShouldBindJSON(&param); err != nil {
  22. c.Error(errors.New("缺少参数:" + err.Error()))
  23. return
  24. }
  25. userInfoInterface, _ := c.Get("userInfo")
  26. userInfo, _ := userInfoInterface.(model.Sys_user)
  27. var total int64
  28. var dbList []model.Sim_pool
  29. type recordsType struct {
  30. model.Sim_pool
  31. Iccids []model.Sim_card `json:"iccids"`
  32. UserId uint `json:"userId"`
  33. SimTariffName string `json:"simTariffName"`
  34. Alert model.Alert_traffic_pool `json:"alert"`
  35. }
  36. var recordsData []recordsType
  37. if userInfo.UserType == "1" {
  38. // 查询总记录数
  39. if err := global.App.DB.Model(&model.Sim_pool{}).Where("traffic_pool_type LIKE ?", "%"+param.TrafficPoolType+"%").Count(&total).Error; err != nil {
  40. c.Error(err)
  41. return
  42. }
  43. // 使用分页查询
  44. if err := global.App.DB.
  45. Where("traffic_pool_type LIKE ?", "%"+param.TrafficPoolType+"%").
  46. Limit(param.PageSize).
  47. Offset((param.Current - 1) * param.PageSize).
  48. Find(&dbList).Error; err != nil {
  49. c.Error(err)
  50. return
  51. }
  52. for _, v := range dbList {
  53. // 查询卡信息
  54. cardInfo := []model.Sim_card{}
  55. global.App.DB.Model(&model.Sim_card{}).Where("pool_id = ?", v.Id).Find(&cardInfo)
  56. // 查询资费名称
  57. var traffic model.Sim_traffic
  58. if err := global.App.DB.Select("id", "label", "user_id").Model(&model.Sim_traffic{}).Where("id = ?", v.SimTariffId).First(&traffic).Error; err != nil {
  59. c.Error(err)
  60. return
  61. }
  62. alert := model.Alert_traffic_pool{}
  63. global.App.DB.Model(&model.Alert_traffic_pool{}).Where("pool_id = ?", v.Id).First(&alert)
  64. recordsData = append(recordsData, recordsType{Sim_pool: v, Iccids: cardInfo, SimTariffName: traffic.Label, UserId: traffic.UserId, Alert: alert})
  65. }
  66. }
  67. if userInfo.UserType == "2" {
  68. // 查询用户资费
  69. var simTariff []model.Sim_traffic
  70. if err := global.App.DB.Where("user_id = ?", userInfo.Id).Find(&simTariff).Error; err != nil {
  71. c.Error(err)
  72. return
  73. }
  74. // 查询资费有哪些流量池
  75. var ids []string
  76. for _, v := range simTariff {
  77. ids = append(ids, v.Id)
  78. }
  79. // 查询总记录数
  80. 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 {
  81. c.Error(err)
  82. return
  83. }
  84. if err := global.App.DB.Where("sim_tariff_id in ?", ids).Where("traffic_pool_type LIKE ?", "%"+param.TrafficPoolType+"%").
  85. Limit(param.PageSize).
  86. Offset((param.Current - 1) * param.PageSize).
  87. Find(&dbList).Error; err != nil {
  88. c.Error(err)
  89. return
  90. }
  91. for _, v := range dbList {
  92. // 查询卡信息
  93. cardInfo := []model.Sim_card{}
  94. global.App.DB.Model(&model.Sim_card{}).Where("pool_id = ?", v.Id).Find(&cardInfo)
  95. // 查询资费名称
  96. var traffic model.Sim_traffic
  97. if err := global.App.DB.Select("id", "label", "user_id").Model(&model.Sim_traffic{}).Where("id = ?", v.SimTariffId).First(&traffic).Error; err != nil {
  98. c.Error(err)
  99. return
  100. }
  101. alert := model.Alert_traffic_pool{}
  102. global.App.DB.Model(&model.Alert_traffic_pool{}).Where("pool_id = ?", v.Id).First(&alert)
  103. recordsData = append(recordsData, recordsType{Sim_pool: v, Iccids: cardInfo, SimTariffName: traffic.Label, UserId: traffic.UserId, Alert: alert})
  104. }
  105. }
  106. // 序列化
  107. records := make([]interface{}, len(recordsData))
  108. for i, user := range recordsData {
  109. records[i] = user
  110. }
  111. data := common.BuildPagination(records, param.Current, param.PageSize, total)
  112. c.Set("res_data", data)
  113. }
  114. // 添加流量池
  115. func AddTrafficPool(c *gin.Context) {
  116. type Param struct {
  117. Label string `json:"label" validate:"required"` // 流量包名称
  118. TrafficPoolType string `json:"trafficPoolType" validate:"required"` // 流量池类型: 1: 前流量池 2: 后流量池
  119. Source string `json:"source" validate:"required"` // 来源
  120. SimTariffId string `json:"simTariffId" validate:"required"` // 资费ID
  121. ExpireTime hook.LocalTime `json:"expireTime" validate:"required"` // 过期时间
  122. Size int `json:"size"`
  123. SizeType string `json:"sizeType"`
  124. }
  125. var param Param
  126. if err := c.ShouldBindJSON(&param); err != nil {
  127. c.Error(errors.New("缺少参数:" + err.Error()))
  128. return
  129. }
  130. err := utils.ValidateStruct(param)
  131. if err != nil {
  132. c.Error(err)
  133. return
  134. }
  135. if param.TrafficPoolType == "1" {
  136. // 查询资费信息
  137. var tarifc model.Sim_traffic
  138. if err := global.App.DB.Select("sim_data_plan_id").Where("id = ?", param.SimTariffId).First(&tarifc).Error; err != nil {
  139. c.Error(err)
  140. return
  141. }
  142. }
  143. if param.TrafficPoolType == "2" {
  144. // 判断字符串 Size 是不是空的
  145. if param.Size == 0 {
  146. c.Error(errors.New("缺少参数:size"))
  147. return
  148. }
  149. if param.SizeType == "" {
  150. c.Error(errors.New("缺少参数:sizeType"))
  151. return
  152. }
  153. }
  154. // 创建流量池
  155. trafficPool := model.Sim_pool{}
  156. copier.Copy(&trafficPool, &param)
  157. if err := global.App.DB.Create(&trafficPool).Error; err != nil {
  158. c.Error(err)
  159. return
  160. }
  161. c.Set("res_data", trafficPool)
  162. }
  163. // 删除流量池
  164. func DeleteTrafficPool(c *gin.Context) {
  165. id := c.Query("id")
  166. if id == "" {
  167. c.Error(errors.New("缺少参数:id"))
  168. return
  169. }
  170. tx := global.App.DB.Begin()
  171. // 查询绑定了多少张卡
  172. cardSum := int64(0)
  173. if err := tx.Model(&model.Sim_card{}).Where("pool_id = ?", id).Count(&cardSum).Error; err != nil {
  174. tx.Rollback()
  175. c.Error(err)
  176. return
  177. }
  178. if cardSum > 0 {
  179. c.Error(fmt.Errorf("该流量池下有%d卡,不可删除!", cardSum))
  180. return
  181. }
  182. if err := tx.Where("id = ?", id).Unscoped().Delete(&model.Sim_pool{}).Error; err != nil {
  183. tx.Rollback()
  184. c.Error(err)
  185. return
  186. }
  187. // 清楚卡的流量池
  188. if err := tx.Model(&model.Sim_card{}).Where("traffic_pool_id = ?", id).Update("pool_id", "").Error; err != nil {
  189. tx.Rollback()
  190. c.Error(err)
  191. return
  192. }
  193. // 删除报警
  194. if err := tx.Where("pool_id = ?", id).Unscoped().Delete(&model.Alert_traffic_pool{}).Error; err != nil {
  195. tx.Rollback()
  196. c.Error(err)
  197. return
  198. }
  199. if err := tx.Commit().Error; err != nil {
  200. tx.Rollback()
  201. c.Error(err)
  202. return
  203. }
  204. c.Set("res_data", "删除成功!")
  205. }
  206. // 编辑流量池
  207. func UpdateTrafficPool(c *gin.Context) {
  208. type Param struct {
  209. Id string `json:"id" validate:"required"`
  210. Label string `json:"label" validate:"required"` // 流量包名称
  211. TrafficPoolType string `json:"trafficPoolType" validate:"required"` // 流量池类型: 1: 前流量池 2: 后流量池
  212. Source string `json:"source" validate:"required"` // 来源
  213. SimTariffId string `json:"simTariffId" validate:"required"` // 资费ID
  214. ExpireTime hook.LocalTime `json:"expireTime" validate:"required"` // 过期时间
  215. Iccids []string `json:"iccids"`
  216. Size int `json:"size"`
  217. SizeType string `json:"sizeType"`
  218. }
  219. var param Param
  220. if err := c.ShouldBindJSON(&param); err != nil {
  221. c.Error(errors.New("缺少参数:" + err.Error()))
  222. return
  223. }
  224. err := utils.ValidateStruct(param)
  225. if err != nil {
  226. c.Error(err)
  227. return
  228. }
  229. if param.TrafficPoolType == "1" {
  230. // 查询资费信息
  231. var tarifc model.Sim_traffic
  232. if err := global.App.DB.Select("sim_data_plan_id").Where("id = ?", param.SimTariffId).First(&tarifc).Error; err != nil {
  233. c.Error(err)
  234. return
  235. }
  236. }
  237. if param.TrafficPoolType == "2" {
  238. // 判断字符串 Size 是不是空的
  239. if param.Size == 0 {
  240. c.Error(errors.New("缺少参数:size"))
  241. return
  242. }
  243. if param.SizeType == "" {
  244. c.Error(errors.New("缺少参数:sizeType"))
  245. return
  246. }
  247. }
  248. // 创建流量池
  249. tx := global.App.DB.Begin()
  250. trafficPool := model.Sim_pool{}
  251. copier.Copy(&trafficPool, &param)
  252. if err := tx.Updates(&trafficPool).Error; err != nil {
  253. tx.Rollback()
  254. c.Error(err)
  255. return
  256. }
  257. // 提交
  258. if err := tx.Commit().Error; err != nil {
  259. tx.Rollback()
  260. c.Error(err)
  261. return
  262. }
  263. c.Set("res_data", trafficPool)
  264. }
  265. // 查询客户下的流量池
  266. func GetCustomerTrafficPool(c *gin.Context) {
  267. // 拿到token, 解析token
  268. userInfoInterface, _ := c.Get("userInfo")
  269. userInfo, _ := userInfoInterface.(model.Sys_user)
  270. // 查询资费
  271. var trafficList []model.Sim_traffic
  272. if err := global.App.DB.Where("user_id = ?", userInfo.Id).Find(&trafficList).Error; err != nil {
  273. c.Error(err)
  274. return
  275. }
  276. var ids []string
  277. for _, v := range trafficList {
  278. ids = append(ids, v.Id)
  279. }
  280. // 查询流量池
  281. var trafficPoolList []model.Sim_pool
  282. if err := global.App.DB.Where("sim_tariff_id in (?)", ids).Find(&trafficPoolList).Error; err != nil {
  283. c.Error(err)
  284. return
  285. }
  286. c.Set("res_data", trafficPoolList)
  287. }