123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package service
- import (
- "sfbase/global"
- "sfis/db"
- "sfis/lock"
- "sfis/model"
- "sfis/model/response"
- "github.com/gin-gonic/gin"
- "log"
- "sfis/utils"
- "time"
- "go.uber.org/zap"
- "gorm.io/gorm"
- )
- func MoneyRecharge(appid string, money int, context *gin.Context) {
- //取出用户锁
- lock.MainLock.Lock()
- userLock := lock.UserLockMap[appid]
- lock.MainLock.Unlock()
- userLock.Lock()
- defer userLock.Unlock()
- //查用户当前余额
- userAccount := &model.UserAccount{}
- db.GetSFISDB().First(userAccount, &model.UserAccount{AppID: appid})
- errs := db.GetSFISDB().Transaction(func(tx *gorm.DB) error {
- moneyBefore := userAccount.Money
- moneyAfter := userAccount.Money + money
- //充值
- err := tx.Exec("update user_account set money = ? WHERE `app_id` = ?", moneyAfter, appid).Error
- if err != nil {
- log.Printf("appID:[%s],money:[%d] execute cost user_account error:[%v]", appid, moneyAfter, err)
- tx.Rollback()
- return err
- }
- //生充值记录
- err = tx.Exec("insert into user_money_record (app_id,`before`,`after`,trade_money) values (?,?,?,?)", appid, moneyBefore, moneyAfter, money).Error
- if err != nil {
- log.Printf("appID:[%s],trade_money:[%d] execute insert into user_money_record error:[%v]", appid, money, err)
- tx.Rollback()
- return err
- }
- return nil
- })
- if errs == nil {
- response.Ok(context)
- } else {
- global.Logger.Error("数据库操作失败", zap.Any("error:", errs))
- response.FailWithMessage("充值失败", context)
- }
- }
- func ProductRecharge(appid string, productId, rechargeNum int, endTime string, context *gin.Context) {
- //取出用户锁
- lock.MainLock.Lock()
- userLock := lock.UserLockMap[appid]
- lock.MainLock.Unlock()
- userLock.Lock()
- defer userLock.Unlock()
- endTimes := ""
- if endTime != "" {
- end := endTime + " 23:59:59"
- loc, _ := time.LoadLocation("Local")
- if ends, err := time.ParseInLocation("2006-01-02 15:04:05", end, loc); err == nil {
- endTimes = ends.Local().Format("2006-01-02 15:04:05")
- }
- }
- //查用户当前剩余量
- userProduct := &model.UserProduct{}
- db.GetSFISDB().First(userProduct, &model.UserProduct{AppID: appid, ProductID: productId})
- product := &model.Product{}
- if products, ok := utils.ProductCaches.Map.Load(productId); ok {
- product = products.(*model.Product)
- }
- errs := db.GetSFISDB().Transaction(func(tx *gorm.DB) error {
- before := userProduct.LeftNum
- after := userProduct.LeftNum + rechargeNum
- //充值
- var err error
- if endTimes != "" {
- err = tx.Exec("update user_product set left_num = ?,end_at = ? WHERE `app_id` = ? and product_id = ?", after, endTimes, appid, productId).Error
- if err != nil {
- log.Printf("appID:[%s],left_num:[%d],endtime:[%s] execute cost user_product error:[%v]", appid, after, endTimes, err)
- tx.Rollback()
- return err
- }
- } else {
- err = tx.Exec("update user_product set left_num = ? WHERE `app_id` = ? and product_id = ?", after, appid, productId).Error
- if err != nil {
- log.Printf("appID:[%s],left_num:[%d] execute cost user_product error:[%v]", appid, after, err)
- tx.Rollback()
- return err
- }
- }
- //生购买记录
- err = tx.Exec("insert into user_buy_record (app_id,product_id,user_product_id,`before`,`after`,trade_money,buy_type,history_unit_price) values (?,?,?,?,?,?,?,?)", appid, productId, userProduct.ID, before, after, product.UnitPrice*rechargeNum, 1, product.UnitPrice).Error
- if err != nil {
- log.Printf("appID:[%s],product_id:[%d],user_product_id:[%d],after:[%d],trade_money:[%d] execute insert into user_buy_record error:[%v]", appid, productId, userProduct.ID, after, product.UnitPrice*rechargeNum, err)
- tx.Rollback()
- return err
- }
- return nil
- })
- if errs == nil {
- response.Ok(context)
- } else {
- global.Logger.Error("数据库操作失败", zap.Any("error:", errs))
- response.FailWithMessage("充值失败", context)
- }
- }
|