|
@@ -1,17 +1,15 @@
|
|
|
package service
|
|
|
|
|
|
import (
|
|
|
+ "fmt"
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+ "gorm.io/gorm"
|
|
|
+ "log"
|
|
|
"sfis/db"
|
|
|
"sfis/lock"
|
|
|
"sfis/model"
|
|
|
-
|
|
|
- "github.com/gin-gonic/gin"
|
|
|
-
|
|
|
- "log"
|
|
|
"sfis/utils"
|
|
|
"time"
|
|
|
-
|
|
|
- "gorm.io/gorm"
|
|
|
)
|
|
|
|
|
|
func MoneyRecharge(appid string, money int, context *gin.Context) error {
|
|
@@ -46,42 +44,39 @@ func MoneyRecharge(appid string, money int, context *gin.Context) error {
|
|
|
return errs
|
|
|
}
|
|
|
|
|
|
-func ProductRecharge(appid string, productId, rechargeNum int, endTime string, context *gin.Context) error {
|
|
|
+func ProductRecharge(appid string, productId, rechargeNum int, startTime string, endTime string, tradeMoney int, context *gin.Context) error {
|
|
|
//取出用户锁
|
|
|
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")
|
|
|
- }
|
|
|
- }
|
|
|
+
|
|
|
//查用户当前剩余量
|
|
|
+ fmt.Println(startTime, endTime)
|
|
|
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)
|
|
|
}
|
|
|
+ // 更新时间
|
|
|
+ nowStr := time.Now().Local().Format("2006-01-02 15:04:05")
|
|
|
+
|
|
|
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 endTime != "" && startTime != "" {
|
|
|
+ err = tx.Exec("update user_product set left_num = ?,start_at=?,end_at = ? ,update_at = ? WHERE `app_id` = ? and product_id = ?", after, startTime, endTime,nowStr, 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)
|
|
|
+ log.Printf("appID:[%s],left_num:[%d],endtime:[%s],starttime:[%s] execute cost user_product error:[%v]", appid, after, endTime, startTime, 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
|
|
|
+ err = tx.Exec("update user_product set left_num = ?,update_at = ? WHERE `app_id` = ? and product_id = ?", after,nowStr, appid, productId).Error
|
|
|
if err != nil {
|
|
|
log.Printf("appID:[%s],left_num:[%d] execute cost user_product error:[%v]", appid, after, err)
|
|
|
tx.Rollback()
|
|
@@ -89,9 +84,9 @@ func ProductRecharge(appid string, productId, rechargeNum int, endTime string, c
|
|
|
}
|
|
|
}
|
|
|
//生购买记录
|
|
|
- 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
|
|
|
+ err = tx.Exec("insert into user_buy_record (app_id,product_id,user_product_id,`before`,`after`,trade_money,buy_type,history_unit_price,create_at) values (?,?,?,?,?,?,?,?,?)", appid, productId, userProduct.ID, before, after, tradeMoney, 1, product.UnitPrice,nowStr).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)
|
|
|
+ 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, tradeMoney, err)
|
|
|
tx.Rollback()
|
|
|
return err
|
|
|
}
|