|
@@ -3,6 +3,9 @@ package service
|
|
|
import (
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+ "go.uber.org/zap"
|
|
|
+ "gorm.io/gorm"
|
|
|
"log"
|
|
|
"sfbase/global"
|
|
|
"sfbase/utils"
|
|
@@ -12,14 +15,9 @@ import (
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"sync"
|
|
|
- "time"
|
|
|
-
|
|
|
- "github.com/gin-gonic/gin"
|
|
|
- "go.uber.org/zap"
|
|
|
- "gorm.io/gorm"
|
|
|
)
|
|
|
|
|
|
-func CreateUserProduct(appId string, productArr []map[string]interface{}, buyType int) (status int, haveProductId string, errArr []error) {
|
|
|
+func CreateUserProduct(appId string, productArr []map[string]interface{}) (status int, haveProductId string, errArr []error) {
|
|
|
//取出用户锁
|
|
|
lock.MainLock.Lock()
|
|
|
userLock := lock.UserLockMap[appId]
|
|
@@ -32,29 +30,17 @@ func CreateUserProduct(appId string, productArr []map[string]interface{}, buyTyp
|
|
|
for _, val := range productArr {
|
|
|
productId := utils.IntAll(val["productId"])
|
|
|
costModel := utils.IntAll(val["costModel"])
|
|
|
- leftNum := utils.IntAll(val["leftNum"])
|
|
|
- tradeMoney := utils.IntAll(val["tradeMoney"]) * 100
|
|
|
- userProduct := &model.UserProduct{}
|
|
|
+ userProduct := &model.UserProductModel{}
|
|
|
userProduct.AppID = appId
|
|
|
- userProduct.StartAt, _ = time.ParseInLocation("2006-01-02 15:04:05", utils.ObjToString(val["startTime"]), time.Local)
|
|
|
- userProduct.EndAt, _ = time.ParseInLocation("2006-01-02 15:04:05", utils.ObjToString(val["endTime"]), time.Local)
|
|
|
- userProduct.LeftNum = leftNum
|
|
|
userProduct.CostModel = costModel
|
|
|
+ userProduct.LeftNum = 0
|
|
|
userProduct.InterfaceStatus = utils.IntAll(val["interfaceStatus"])
|
|
|
userProduct.CallTimesLimitDay = utils.IntAll(val["callTimesLimitDay"])
|
|
|
userProduct.DataNumLimitOneTimes = utils.IntAll(val["dataNumOneTimes"])
|
|
|
userProduct.ProductID = productId
|
|
|
+ //查询产品是否购买过
|
|
|
userProductInfo := model.UserProduct{}
|
|
|
- product := model.Product{}
|
|
|
- //查询产品信息,获取购买时候产品单价、试用次数
|
|
|
- err := db.GetSFISDB().Where("id = ?", productId).Find(&product).Error
|
|
|
- if err != nil {
|
|
|
- errArr = append(errArr, err)
|
|
|
- global.Logger.Error("CreateUserProduct查询product表出错:", zap.Any("err:", err))
|
|
|
- return 0, haveProductId, errArr
|
|
|
- }
|
|
|
- historyUnitPrice := product.UnitPrice
|
|
|
- err = db.GetSFISDB().Where("product_id = ? and app_id = ?", productId, appId).Find(&userProductInfo).Error
|
|
|
+ err := db.GetSFISDB().Where("product_id = ? and app_id = ?", productId, appId).Find(&userProductInfo).Error
|
|
|
if err != nil {
|
|
|
errArr = append(errArr, err)
|
|
|
global.Logger.Error("CreateUserProduct查询user_product表出错:", zap.Any("err:", err))
|
|
@@ -62,70 +48,10 @@ func CreateUserProduct(appId string, productArr []map[string]interface{}, buyTyp
|
|
|
}
|
|
|
//用户第一次购买产品
|
|
|
if userProductInfo.ID == 0 {
|
|
|
- errs = db.GetSFISDB().Transaction(func(tx *gorm.DB) error {
|
|
|
- rechargeAmount := 0
|
|
|
- remark := ""
|
|
|
- //第一次购买产品赠送试用量
|
|
|
- testNum := product.TestNum
|
|
|
- //扣费类型为扣余额,user_buy_record中tradeMoney金额为0
|
|
|
- if costModel == 0 {
|
|
|
- rechargeAmount = tradeMoney
|
|
|
- leftNum += testNum
|
|
|
- } else if costModel == 1 {
|
|
|
- leftNum = 0
|
|
|
- //扣余额,把赠送量转化成金额
|
|
|
- freeMoney := testNum * product.UnitPrice
|
|
|
- log.Println(freeMoney, "freeMoney")
|
|
|
- remark = "充值金额为:" + strconv.Itoa(tradeMoney) + ",赠送金额为:" + strconv.Itoa(freeMoney)
|
|
|
- tradeMoney += freeMoney
|
|
|
- }
|
|
|
- userProduct.LeftNum = leftNum
|
|
|
- //生用户产品
|
|
|
- err := tx.Create(userProduct).Error
|
|
|
- if err != nil {
|
|
|
- log.Printf("appID:[%s],productId:[%d] execute insert user_product error:[%v]", appId, productId, err)
|
|
|
- tx.Rollback()
|
|
|
- return err
|
|
|
- }
|
|
|
- userProductId := userProduct.ID
|
|
|
- //生成购买产品记录
|
|
|
- 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, userProductId, 0, leftNum, rechargeAmount, buyType, historyUnitPrice).Error
|
|
|
- if err != nil {
|
|
|
- log.Printf("appID:[%s],productId[%d],trade_money:[%d] execute insert into user_buy_record error:[%v]", appId, productId, tradeMoney, err)
|
|
|
- tx.Rollback()
|
|
|
- return err
|
|
|
- }
|
|
|
- //扣费类型是扣余额,充值用户余额
|
|
|
- if costModel == 1 {
|
|
|
- userAccount := model.UserAccount{}
|
|
|
- err = tx.First(&userAccount, model.UserAccount{AppID: appId}).Error
|
|
|
- if err != nil {
|
|
|
- log.Printf("appID:[%s],productId[%d],trade_money:[%d] execute find user_account error:[%v]", appId, productId, tradeMoney, err)
|
|
|
- tx.Rollback()
|
|
|
- return err
|
|
|
- }
|
|
|
- moneyBefore := userAccount.Money
|
|
|
- moneyAfter := userAccount.Money + tradeMoney
|
|
|
- //充值
|
|
|
- err := tx.Exec("update user_account set money = ? WHERE `app_id` = ?", moneyAfter, appId).Error
|
|
|
- if err != nil {
|
|
|
- log.Printf("appID:[%s],money:[%d] execute update 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,remark) values (?,?,?,?,?)", appId, moneyBefore, moneyAfter, tradeMoney, remark).Error
|
|
|
- if err != nil {
|
|
|
- log.Printf("appID:[%s],trade_money:[%d] execute insert into user_money_record error:[%v]", appId, tradeMoney, err)
|
|
|
- tx.Rollback()
|
|
|
- return err
|
|
|
- }
|
|
|
- }
|
|
|
- return nil
|
|
|
- })
|
|
|
- if errs != nil {
|
|
|
+ err := db.GetSFISDB().Create(userProduct).Error
|
|
|
+ if err != nil {
|
|
|
errArr = append(errArr, errs)
|
|
|
- global.Logger.Info("用户已购买产品失败:", zap.Any("appId:", appId), zap.Any("productId:", productId))
|
|
|
+ global.Logger.Info("用户购买产品失败:", zap.Any("appId:", appId), zap.Any("productId:", productId))
|
|
|
continue
|
|
|
}
|
|
|
} else {
|