wellet.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. "github.com/gin-gonic/gin"
  10. "github.com/jinzhu/copier"
  11. "github.com/shopspring/decimal"
  12. "gorm.io/gorm"
  13. )
  14. // 充值记录
  15. func TopUpRecord(c *gin.Context) {
  16. type Param struct {
  17. Name string `json:"name"`
  18. common.Pagination
  19. }
  20. var param Param
  21. param.Pagination = common.NewPagination()
  22. if err := c.ShouldBindJSON(&param); err != nil {
  23. c.Error(errors.New("缺少参数:" + err.Error()))
  24. return
  25. }
  26. // 先吧用户查询出来
  27. var userMap = make(map[uint]model.Sys_user)
  28. var user []model.Sys_user
  29. if err := global.App.DB.Select("id", "name", "username").Where("name LIKE ? AND user_type = 2", "%"+param.Name+"%").Find(&user).Error; err != nil {
  30. c.Error(err)
  31. return
  32. }
  33. var userIds []uint
  34. for _, v := range user {
  35. userMap[v.Id] = v
  36. userIds = append(userIds, v.Id)
  37. }
  38. // 查询总记录数
  39. var total int64
  40. if err := global.App.DB.Model(&model.Cfo_recharge_record{}).Where("user_id in (?)", userIds).Count(&total).Error; err != nil {
  41. c.Error(err)
  42. return
  43. }
  44. // 使用分页查询
  45. var dbList []model.Cfo_recharge_record
  46. if err := global.App.DB.Where("user_id in (?)", userIds).
  47. Limit(param.PageSize).
  48. Offset((param.Current - 1) * param.PageSize).
  49. Find(&dbList).Error; err != nil {
  50. c.Error(err)
  51. return
  52. }
  53. type recordsData struct {
  54. model.Cfo_recharge_record
  55. Name string `json:"name"`
  56. UserName string `json:"username"`
  57. }
  58. var recordsList []recordsData
  59. copier.Copy(&recordsList, &dbList)
  60. // 序列化
  61. records := make([]interface{}, len(recordsList))
  62. for i, v := range recordsList {
  63. if user, ok := userMap[v.UserId]; ok {
  64. v.Name = user.Name
  65. v.UserName = user.Username
  66. }
  67. records[i] = v
  68. }
  69. data := common.BuildPagination(records, param.Current, param.PageSize, total)
  70. c.Set("res_data", data)
  71. }
  72. // 充值
  73. func TopUp(c *gin.Context) {
  74. type Param struct {
  75. UserId uint `json:"userId" validate:"required"` // 客户ID
  76. Amount float64 `json:"amount" validate:"required"` // 充值金额
  77. Status string `json:"status" validate:"required"` // 状态
  78. Remarks string `json:"remarks" validate:"required"` // 备注
  79. CertificateImg string `json:"certificateImg" validate:"required"` // 凭证
  80. }
  81. var param Param
  82. if err := c.ShouldBindJSON(&param); err != nil {
  83. c.Error(errors.New("缺少参数:" + err.Error()))
  84. return
  85. }
  86. err := utils.ValidateStruct(param)
  87. if err != nil {
  88. c.Error(err)
  89. return
  90. }
  91. // 事物
  92. tx := global.App.DB.Begin()
  93. // 充值
  94. db := model.Cfo_wallet{
  95. UserId: param.UserId,
  96. Amount: decimal.NewFromFloat(param.Amount),
  97. }
  98. if err := tx.
  99. Model(&model.Cfo_wallet{}).
  100. Where("user_id = ?", param.UserId).
  101. FirstOrCreate(&db).
  102. Update("amount", gorm.Expr("amount + ?", param.Amount)).
  103. Error; err != nil {
  104. tx.Rollback()
  105. c.Error(err)
  106. return
  107. }
  108. dbList := model.Cfo_recharge_record{
  109. UserId: param.UserId,
  110. RechargeAmount: decimal.NewFromFloat(param.Amount),
  111. AvailableAmount: db.Amount.Add(decimal.NewFromFloat(param.Amount)),
  112. Status: param.Status,
  113. Remarks: param.Remarks,
  114. CertificateImg: param.CertificateImg,
  115. }
  116. if err := tx.Create(&dbList).Error; err != nil {
  117. tx.Rollback()
  118. c.Error(err)
  119. return
  120. }
  121. if err := tx.Commit().Error; err != nil {
  122. c.Error(err)
  123. return
  124. }
  125. c.Set("res_data", "充值成功!")
  126. }
  127. // 客户余额看版
  128. func WalletCard(c *gin.Context) {
  129. // 获取token
  130. userInfoInterface, _ := c.Get("userInfo")
  131. userInfo, _ := userInfoInterface.(model.Sys_user)
  132. type card struct {
  133. // 可用余额
  134. AvailableAmount decimal.Decimal `json:"availableAmount"` // 可用余额
  135. // 总支出金额
  136. RechargeAmount decimal.Decimal `json:"rechargeAmount"` // 总支出金额
  137. // 总消耗流量
  138. DataUsage int64 `json:"dataUsage"` // 总消耗流量
  139. // 欠费金额
  140. OweAmount float64 `json:"oweAmount"`
  141. }
  142. var walletItem model.Cfo_wallet
  143. global.App.DB.Model(&model.Cfo_wallet{}).Select("amount").Where("user_id = ?", userInfo.Id).First(&walletItem)
  144. // 总支出金额
  145. var rechargeList []model.Cfo_recharge_record
  146. global.App.DB.Model(&model.Cfo_recharge_record{}).Select("status", "recharge_amount").Where("user_id = ?", userInfo.Id).Find(&rechargeList)
  147. rechargeAmount := decimal.NewFromInt(0)
  148. for _, v := range rechargeList {
  149. if v.Status == "1" {
  150. rechargeAmount = rechargeAmount.Add(v.RechargeAmount)
  151. }
  152. }
  153. // 总消耗流量
  154. // var trafficList []model.Sim_card
  155. var DataUsageSum = int64(0)
  156. var DataUsageTotal []string
  157. global.App.DB.Model(&model.Sim_card{}).Select("data_usage_total").Where("user_id = ?", userInfo.Id).Find(&DataUsageTotal)
  158. for _, v := range DataUsageTotal {
  159. dataUsage, _ := strconv.Atoi(v)
  160. DataUsageSum += int64(dataUsage)
  161. }
  162. records := card{
  163. AvailableAmount: walletItem.Amount,
  164. RechargeAmount: rechargeAmount,
  165. DataUsage: DataUsageSum, // 总消耗流量
  166. OweAmount: 0,
  167. }
  168. c.Set("res_data", records)
  169. }
  170. // 获取消费记录