123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- package platform
- import (
- "errors"
- "go-nc/configs/global"
- "go-nc/internal/utils"
- "go-nc/model"
- "go-nc/model/common"
- "strconv"
- "github.com/gin-gonic/gin"
- "github.com/jinzhu/copier"
- "github.com/shopspring/decimal"
- "gorm.io/gorm"
- )
- // 充值记录
- func TopUpRecord(c *gin.Context) {
- type Param struct {
- Name string `json:"name"`
- common.Pagination
- }
- var param Param
- param.Pagination = common.NewPagination()
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- // 先吧用户查询出来
- var userMap = make(map[uint]model.Sys_user)
- var user []model.Sys_user
- if err := global.App.DB.Select("id", "name", "username").Where("name LIKE ? AND user_type = 2", "%"+param.Name+"%").Find(&user).Error; err != nil {
- c.Error(err)
- return
- }
- var userIds []uint
- for _, v := range user {
- userMap[v.Id] = v
- userIds = append(userIds, v.Id)
- }
- // 查询总记录数
- var total int64
- if err := global.App.DB.Model(&model.Cfo_recharge_record{}).Where("user_id in (?)", userIds).Count(&total).Error; err != nil {
- c.Error(err)
- return
- }
- // 使用分页查询
- var dbList []model.Cfo_recharge_record
- if err := global.App.DB.Where("user_id in (?)", userIds).
- Limit(param.PageSize).
- Offset((param.Current - 1) * param.PageSize).
- Find(&dbList).Error; err != nil {
- c.Error(err)
- return
- }
- type recordsData struct {
- model.Cfo_recharge_record
- Name string `json:"name"`
- UserName string `json:"username"`
- }
- var recordsList []recordsData
- copier.Copy(&recordsList, &dbList)
- // 序列化
- records := make([]interface{}, len(recordsList))
- for i, v := range recordsList {
- if user, ok := userMap[v.UserId]; ok {
- v.Name = user.Name
- v.UserName = user.Username
- }
- records[i] = v
- }
- data := common.BuildPagination(records, param.Current, param.PageSize, total)
- c.Set("res_data", data)
- }
- // 充值
- func TopUp(c *gin.Context) {
- type Param struct {
- UserId uint `json:"userId" validate:"required"` // 客户ID
- Amount float64 `json:"amount" validate:"required"` // 充值金额
- Status string `json:"status" validate:"required"` // 状态
- Remarks string `json:"remarks" validate:"required"` // 备注
- CertificateImg string `json:"certificateImg" 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
- }
- // 事物
- tx := global.App.DB.Begin()
- // 充值
- db := model.Cfo_wallet{
- UserId: param.UserId,
- Amount: decimal.NewFromFloat(param.Amount),
- }
- if err := tx.
- Model(&model.Cfo_wallet{}).
- Where("user_id = ?", param.UserId).
- FirstOrCreate(&db).
- Update("amount", gorm.Expr("amount + ?", param.Amount)).
- Error; err != nil {
- tx.Rollback()
- c.Error(err)
- return
- }
- dbList := model.Cfo_recharge_record{
- UserId: param.UserId,
- RechargeAmount: decimal.NewFromFloat(param.Amount),
- AvailableAmount: db.Amount.Add(decimal.NewFromFloat(param.Amount)),
- Status: param.Status,
- Remarks: param.Remarks,
- CertificateImg: param.CertificateImg,
- }
- if err := tx.Create(&dbList).Error; err != nil {
- tx.Rollback()
- c.Error(err)
- return
- }
- if err := tx.Commit().Error; err != nil {
- c.Error(err)
- return
- }
- c.Set("res_data", "充值成功!")
- }
- // 客户余额看版
- func WalletCard(c *gin.Context) {
- // 获取token
- userInfoInterface, _ := c.Get("userInfo")
- userInfo, _ := userInfoInterface.(model.Sys_user)
- type card struct {
- // 可用余额
- AvailableAmount decimal.Decimal `json:"availableAmount"` // 可用余额
- // 总支出金额
- RechargeAmount decimal.Decimal `json:"rechargeAmount"` // 总支出金额
- // 总消耗流量
- DataUsage int64 `json:"dataUsage"` // 总消耗流量
- // 欠费金额
- OweAmount float64 `json:"oweAmount"`
- }
- var walletItem model.Cfo_wallet
- global.App.DB.Model(&model.Cfo_wallet{}).Select("amount").Where("user_id = ?", userInfo.Id).First(&walletItem)
- // 总支出金额
- var rechargeList []model.Cfo_recharge_record
- global.App.DB.Model(&model.Cfo_recharge_record{}).Select("status", "recharge_amount").Where("user_id = ?", userInfo.Id).Find(&rechargeList)
- rechargeAmount := decimal.NewFromInt(0)
- for _, v := range rechargeList {
- if v.Status == "1" {
- rechargeAmount = rechargeAmount.Add(v.RechargeAmount)
- }
- }
- // 总消耗流量
- // var trafficList []model.Sim_card
- var DataUsageSum = int64(0)
- var DataUsageTotal []string
- global.App.DB.Model(&model.Sim_card{}).Select("data_usage_total").Where("user_id = ?", userInfo.Id).Find(&DataUsageTotal)
- for _, v := range DataUsageTotal {
- dataUsage, _ := strconv.Atoi(v)
- DataUsageSum += int64(dataUsage)
- }
- records := card{
- AvailableAmount: walletItem.Amount,
- RechargeAmount: rechargeAmount,
- DataUsage: DataUsageSum, // 总消耗流量
- OweAmount: 0,
- }
- c.Set("res_data", records)
- }
- // 获取消费记录
|