|
@@ -0,0 +1,114 @@
|
|
|
|
+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)
|
|
|
|
+ }
|
|
|
|
+}
|