tariff.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. package platform
  2. import (
  3. "errors"
  4. "go-nc/configs/global"
  5. "go-nc/internal/utils"
  6. "go-nc/model"
  7. "go-nc/model/common"
  8. "strconv"
  9. "time"
  10. "github.com/gin-gonic/gin"
  11. "github.com/jinzhu/copier"
  12. )
  13. // 获取资费列表: 分页查询
  14. func GetTariffList(c *gin.Context) {
  15. type Param struct {
  16. common.Pagination
  17. }
  18. var param Param
  19. param.Pagination = common.NewPagination()
  20. if err := c.ShouldBindJSON(&param); err != nil {
  21. c.Error(errors.New("缺少参数:" + err.Error()))
  22. return
  23. }
  24. //获取token
  25. userInfoInterface, _ := c.Get("userInfo")
  26. userInfo, _ := userInfoInterface.(model.Sys_user)
  27. var total int64
  28. var list []model.Sim_traffic
  29. if userInfo.UserType == "1" {
  30. // 查询总记录数
  31. if err := global.App.DB.Model(&model.Sim_traffic{}).Count(&total).Error; err != nil {
  32. c.Error(err)
  33. return
  34. }
  35. // 分页查询
  36. if err := global.App.DB.
  37. Limit(param.PageSize).
  38. Offset((param.Current - 1) * param.PageSize).
  39. Find(&list).Error; err != nil {
  40. c.Error(err)
  41. return
  42. }
  43. }
  44. if userInfo.UserType == "2" {
  45. // 查询总记录数
  46. if err := global.App.DB.Where("user_id = ?", userInfo.Id).Model(&model.Sim_traffic{}).Count(&total).Error; err != nil {
  47. c.Error(err)
  48. return
  49. }
  50. // 分页查询
  51. if err := global.App.DB.Where("user_id = ?", userInfo.Id).
  52. Limit(param.PageSize).
  53. Offset((param.Current - 1) * param.PageSize).
  54. Find(&list).Error; err != nil {
  55. c.Error(err)
  56. return
  57. }
  58. }
  59. type RecordsData struct {
  60. model.Sim_traffic
  61. UserName string `json:"userName"`
  62. CardSum int64 `json:"cardSum"`
  63. }
  64. var recordsData []RecordsData
  65. copier.Copy(&recordsData, &list)
  66. for i, v := range recordsData {
  67. // 客户名称
  68. var userItem model.Sys_user
  69. if err := global.App.DB.Select("name").Where("id = ?", v.UserId).First(&userItem).Error; err != nil {
  70. c.Error(err)
  71. return
  72. }
  73. recordsData[i].UserName = userItem.Name
  74. // 卡数量
  75. cardTotal := int64(0)
  76. global.App.DB.Where("traffic_id = ?", v.Id).Count(&cardTotal)
  77. recordsData[i].CardSum = cardTotal
  78. }
  79. // 序列化
  80. records := make([]interface{}, len(recordsData))
  81. for i, user := range recordsData {
  82. records[i] = user
  83. }
  84. // 返回数据
  85. data := common.BuildPagination(records, param.Current, param.PageSize, total)
  86. c.Set("res_data", data)
  87. }
  88. // 添加资费计划
  89. func AddTariff(c *gin.Context) {
  90. type Param struct {
  91. Label string `json:"label" validate:"required" ` // 资费名称
  92. SimDataPlanId string `json:"simDataPlanId" validate:"required" ` // 流量包ID
  93. UserId uint `json:"userId" validate:"required" ` // 用户ID
  94. Source string `json:"source" validate:"required" ` // 来源
  95. BillingCycle string `json:"billingCycle" validate:"required" ` // 计费周期
  96. BillingMethod string `json:"billingMethod" validate:"required" ` // 计费方式
  97. EndDate string `json:"endDate" validate:"required"` // 结算周期
  98. Pricing int64 `json:"pricing"` // 价格
  99. Currency string `json:"currency" validate:"required"` // 币种
  100. TrafficBilling string `json:"trafficBilling"` // 流量资费计费
  101. TrafficBillingType string `json:"trafficBillingType"` // 流量资费计费类型
  102. TrafficBillingAmount string `json:"trafficBillingAmount"` // 流量资费计费金额
  103. MRCAmount string `json:"mrcAmount"` // MRC金额
  104. NetworkAccessFee string `json:"networkAccessFee"` // 网络接入费
  105. }
  106. var param Param
  107. if err := c.ShouldBindJSON(&param); err != nil {
  108. c.Error(errors.New("缺少参数:" + err.Error()))
  109. return
  110. }
  111. err := utils.ValidateStruct(param)
  112. if err != nil {
  113. c.Error(err)
  114. return
  115. }
  116. // 添加数据
  117. var data model.Sim_traffic
  118. copier.Copy(&data, &param)
  119. if err := global.App.DB.Create(&data).Error; err != nil {
  120. c.Error(err)
  121. return
  122. }
  123. c.Set("res_data", data)
  124. }
  125. // 更新资费计划
  126. func UpdateTariff(c *gin.Context) {
  127. type Param struct {
  128. Id string `json:"id" validate:"required"`
  129. Label string `json:"label" validate:"required" ` // 资费名称
  130. SimDataPlanId string `json:"simDataPlanId" validate:"required" ` // 流量包ID
  131. UserId uint `json:"userId" validate:"required" ` // 用户ID
  132. Source string `json:"source" validate:"required" ` // 来源
  133. BillingCycle string `json:"billingCycle" validate:"required" ` // 计费周期
  134. BillingMethod string `json:"billingMethod" validate:"required" ` // 计费方式
  135. EndDate string `json:"endDate" validate:"required"` // 结算周期
  136. Pricing int64 `json:"pricing" ` // 价格
  137. Currency string `json:"currency" validate:"required"` // 币种
  138. TrafficBilling string `json:"trafficBilling"` // 流量资费计费
  139. TrafficBillingType string `json:"trafficBillingType"` // 流量资费计费类型
  140. TrafficBillingAmount string `json:"trafficBillingAmount"` // 流量资费计费金额
  141. MRCAmount string `json:"mrcAmount"` // MRC金额
  142. NetworkAccessFee string `json:"networkAccessFee"` // 网络接入费
  143. }
  144. var param Param
  145. if err := c.ShouldBindJSON(&param); err != nil {
  146. c.Error(errors.New("缺少参数:" + err.Error()))
  147. return
  148. }
  149. err := utils.ValidateStruct(param)
  150. if err != nil {
  151. c.Error(err)
  152. return
  153. }
  154. // 根据id更新数据
  155. var data model.Sim_traffic
  156. copier.Copy(&data, &param)
  157. if data.BillingMethod == "2" {
  158. data.TrafficBilling = ""
  159. data.TrafficBillingType = ""
  160. data.TrafficBillingAmount = ""
  161. }
  162. if err := global.App.DB.Updates(&data).Update("traffic_billing", data.TrafficBilling).Update("traffic_billing_type", data.TrafficBillingType).Update("traffic_billing_amount", data.TrafficBillingAmount).Error; err != nil {
  163. c.Error(err)
  164. return
  165. }
  166. c.Set("res_data", data)
  167. }
  168. // 删除资费计划
  169. func DeleteTariff(c *gin.Context) {
  170. id := c.Query("id")
  171. if id == "" {
  172. c.Error(errors.New("缺少参数:id"))
  173. return
  174. }
  175. if err := global.App.DB.Delete(&model.Sim_traffic{}, id).Error; err != nil {
  176. c.Error(err)
  177. return
  178. }
  179. c.Set("res_data", "删除成功!")
  180. }
  181. // 获取资费下的卡
  182. func GetTariffCard(c *gin.Context) {
  183. id := c.Query("id")
  184. if id == "" {
  185. c.Error(errors.New("缺少参数:id"))
  186. return
  187. }
  188. var data []model.Sim_card
  189. if err := global.App.DB.Where("tariff_id = ?", id).Find(&data).Error; err != nil {
  190. c.Error(err)
  191. return
  192. }
  193. type records struct {
  194. Iccid string `json:"iccid"`
  195. ServiceUsageMode string `json:"serviceUsageMode"`
  196. ExpireTime string `json:"expireTime"`
  197. Status string `json:"status"`
  198. }
  199. var recordsData []records
  200. copier.Copy(&recordsData, &data)
  201. c.Set("res_data", recordsData)
  202. }
  203. // id 换取资费信息
  204. func GetTariffById(c *gin.Context) {
  205. id := c.Query("id")
  206. if id == "" {
  207. c.Error(errors.New("缺少参数:id"))
  208. return
  209. }
  210. var data model.Sim_traffic
  211. if err := global.App.DB.Where("id = ?", id).First(&data).Error; err != nil {
  212. c.Error(err)
  213. return
  214. }
  215. c.Set("res_data", data)
  216. }
  217. // 资费商品列表
  218. func GetTariffProductList(c *gin.Context) {
  219. tariffId := c.Query("tariffId")
  220. if tariffId == "" {
  221. c.Error(errors.New("缺少参数:id"))
  222. return
  223. }
  224. var tariff model.Sim_traffic
  225. if err := global.App.DB.Where("id = ?", tariffId).First(&tariff).Error; err != nil {
  226. c.Error(err)
  227. return
  228. }
  229. var data []model.Sim_traffic_product
  230. if err := global.App.DB.Where("traffic_id = ?", tariffId).Find(&data).Error; err != nil {
  231. c.Error(err)
  232. return
  233. }
  234. type records struct {
  235. model.Sim_traffic_product
  236. TrafficBilling string `json:"trafficBilling"` // 流量资费计费
  237. TrafficBillingType string `json:"trafficBillingType"`
  238. }
  239. var recordsData []records
  240. copier.Copy(&recordsData, &data)
  241. for i, v := range recordsData {
  242. billing, _ := strconv.ParseFloat(tariff.TrafficBilling, 64)
  243. recordsData[i].TrafficBilling = strconv.FormatFloat(billing*float64(v.Period), 'f', 0, 64)
  244. recordsData[i].TrafficBillingType = tariff.TrafficBillingType
  245. }
  246. c.Set("res_data", recordsData)
  247. }
  248. // 添加资费商品
  249. func AddTariffProduct(c *gin.Context) {
  250. type Param struct {
  251. TrafficId string `json:"trafficId" validate:"required"` // 资费ID
  252. Price string `json:"price" validate:"required"` // 价格
  253. Currency string `json:"currency" validate:"required"` // 币种
  254. Period int `json:"period" validate:"required"` // 期限
  255. Label string `json:"label" validate:"required"`
  256. }
  257. var param Param
  258. if err := c.ShouldBindJSON(&param); err != nil {
  259. c.Error(errors.New("缺少参数:" + err.Error()))
  260. return
  261. }
  262. err := utils.ValidateStruct(param)
  263. if err != nil {
  264. c.Error(err)
  265. return
  266. }
  267. // 添加数据
  268. var data model.Sim_traffic_product
  269. copier.Copy(&data, &param)
  270. if err := global.App.DB.Create(&data).Error; err != nil {
  271. c.Error(err)
  272. return
  273. }
  274. c.Set("res_data", data)
  275. }
  276. // 修改资费商品
  277. func UpdateTariffProduct(c *gin.Context) {
  278. type Param struct {
  279. Id string `json:"id" validate:"required"`
  280. Price string `json:"price" validate:"required"` // 价格
  281. Currency string `json:"currency" validate:"required"` // 币种
  282. Period int `json:"period" validate:"required"` // 期限
  283. Label string `json:"label" validate:"required"` // 资费名称
  284. }
  285. var param Param
  286. if err := c.ShouldBindJSON(&param); err != nil {
  287. c.Error(errors.New("缺少参数:" + err.Error()))
  288. return
  289. }
  290. err := utils.ValidateStruct(param)
  291. if err != nil {
  292. c.Error(err)
  293. return
  294. }
  295. // 根据id更新数据
  296. var data model.Sim_traffic_product
  297. copier.Copy(&data, &param)
  298. if err := global.App.DB.Model(&data).
  299. Updates(data).FirstOrCreate(&data).Error; err != nil {
  300. c.Error(err)
  301. return
  302. }
  303. c.Set("res_data", data)
  304. }
  305. // 删除资费商品
  306. func DeleteTariffProduct(c *gin.Context) {
  307. id := c.Query("id")
  308. if id == "" {
  309. c.Error(errors.New("缺少参数:id"))
  310. return
  311. }
  312. if err := global.App.DB.Where("id = ?", id).Delete(&model.Sim_traffic_product{}).Error; err != nil {
  313. c.Error(err)
  314. return
  315. }
  316. c.Set("res_data", "删除成功!")
  317. }
  318. // 资费续有效期
  319. func RenewTariff(c *gin.Context) {
  320. type Param struct {
  321. Id string `json:"id" validate:"required"`
  322. EndDate string `json:"endDate" validate:"required"`
  323. }
  324. var param Param
  325. if err := c.ShouldBindJSON(&param); err != nil {
  326. c.Error(errors.New("缺少参数:" + err.Error()))
  327. return
  328. }
  329. err := utils.ValidateStruct(param)
  330. if err != nil {
  331. c.Error(err)
  332. return
  333. }
  334. userInfoInterface, _ := c.Get("userInfo")
  335. userInfo, _ := userInfoInterface.(model.Sys_user)
  336. if userInfo.UserType != "1" {
  337. c.Error(errors.New("无操作权限!"))
  338. return
  339. }
  340. // 格式化日期
  341. date, err := time.Parse("2006-01-02", param.EndDate)
  342. if err != nil {
  343. c.Error(err)
  344. return
  345. }
  346. endDate := time.Date(date.Year(), date.Month(), date.Day(), 23, 59, 59, 0, date.Location())
  347. // 资费信息
  348. var traffic model.Sim_traffic
  349. if err := global.App.DB.Where("id = ?", param.Id).First(&traffic).Error; err != nil {
  350. c.Error(err)
  351. return
  352. }
  353. // 生成续费记录
  354. record := model.Cmi_traffic_order{
  355. TrafficId: traffic.Id,
  356. UserId: traffic.UserId,
  357. Source: traffic.Source,
  358. BeforeEndDate: traffic.EndDate,
  359. EndDate: endDate,
  360. Quantity: 0,
  361. Amount: 0,
  362. }
  363. if err := global.App.DB.Model(&model.Cmi_traffic_order{}).Create(&record).Error; err != nil {
  364. c.Error(err)
  365. return
  366. }
  367. if err := global.App.DB.Model(model.Sim_traffic{}).Where("id = ?", param.Id).
  368. Update("end_date", param.EndDate).
  369. Error; err != nil {
  370. c.Error(err)
  371. return
  372. }
  373. c.Set("res_data", "success")
  374. }
  375. // 自费续费订单信息
  376. func GetTrafficOrderList(c *gin.Context) {
  377. type Param struct {
  378. common.Pagination
  379. }
  380. var param Param
  381. param.Pagination = common.NewPagination()
  382. if err := c.ShouldBindJSON(&param); err != nil {
  383. c.Error(errors.New("缺少参数:" + err.Error()))
  384. return
  385. }
  386. //获取token
  387. userInfoInterface, _ := c.Get("userInfo")
  388. userInfo, _ := userInfoInterface.(model.Sys_user)
  389. var total int64
  390. var list []model.Cmi_traffic_order
  391. if userInfo.UserType == "1" {
  392. // 查询总记录数
  393. if err := global.App.DB.Model(&model.Cmi_traffic_order{}).Count(&total).Error; err != nil {
  394. c.Error(err)
  395. return
  396. }
  397. // 分页查询
  398. if err := global.App.DB.
  399. Limit(param.PageSize).
  400. Offset((param.Current - 1) * param.PageSize).
  401. Find(&list).Error; err != nil {
  402. c.Error(err)
  403. return
  404. }
  405. }
  406. if userInfo.UserType == "2" {
  407. // 查询总记录数
  408. if err := global.App.DB.Model(&model.Cmi_traffic_order{}).Where("user_id = ?", userInfo.Id).Count(&total).Error; err != nil {
  409. c.Error(err)
  410. return
  411. }
  412. // 分页查询
  413. if err := global.App.DB.
  414. Limit(param.PageSize).
  415. Offset((param.Current-1)*param.PageSize).
  416. Where("user_id = ?", userInfo.Id).
  417. Find(&list).Error; err != nil {
  418. c.Error(err)
  419. return
  420. }
  421. }
  422. // 资费信息
  423. type RecordData struct {
  424. model.Cmi_traffic_order
  425. TrafficName string `json:"trafficName"`
  426. UserName string `json:"userName"`
  427. }
  428. var recordsData []RecordData
  429. for _, v := range list {
  430. var traffic model.Sim_traffic
  431. if err := global.App.DB.Select("label").Where("id = ?", v.TrafficId).First(&traffic).Error; err != nil {
  432. c.Error(err)
  433. return
  434. }
  435. var user model.Sys_user
  436. if err := global.App.DB.Select("name").Where("id = ?", v.UserId).First(&user).Error; err != nil {
  437. c.Error(err)
  438. return
  439. }
  440. recordsData = append(recordsData, RecordData{
  441. Cmi_traffic_order: v,
  442. TrafficName: traffic.Label,
  443. UserName: user.Name,
  444. })
  445. }
  446. // 序列化
  447. records := make([]interface{}, len(recordsData))
  448. for i, user := range recordsData {
  449. records[i] = user
  450. }
  451. // 返回数据
  452. data := common.Pagination{
  453. Records: records,
  454. Current: param.Current,
  455. PageSize: param.PageSize,
  456. Total: total,
  457. }
  458. c.Set("res_data", data)
  459. }