xuzhiheng 4 years ago
parent
commit
60723e6996
4 changed files with 22 additions and 15 deletions
  1. 2 1
      model/baseModel.go
  2. 5 5
      model/user.go
  3. 6 3
      utils/cost_by_account_balance.go
  4. 9 6
      utils/cost_by_left_num.go

+ 2 - 1
model/baseModel.go

@@ -1,8 +1,9 @@
 package model
 
 import (
-	"gorm.io/gorm"
 	"time"
+
+	"gorm.io/gorm"
 )
 
 type BaseModel struct {

+ 5 - 5
model/user.go

@@ -18,11 +18,11 @@ func (user *User) TableName() string {
 }
 
 type UserAccount struct {
-	ID    int    `json:"id" gorm:"primaryKey"`
-	AppID string `json:"app_id"`
-	Money int    `json:"money"`
-	CreateAt  time.Time      `json:"-" gorm:"autoCreateTime"` //标签autoCreateTime设置如果字段名字不为CreatAt时候自动插入当前时间
-	UpdateAt  time.Time      `json:"-" gorm:"autoUpdateTime"`
+	ID       int       `json:"id" gorm:"primaryKey"`
+	AppID    string    `json:"app_id"`
+	Money    int       `json:"money"`
+	CreateAt time.Time `json:"-" gorm:"autoCreateTime"` //标签autoCreateTime设置如果字段名字不为CreatAt时候自动插入当前时间
+	UpdateAt time.Time `json:"-" gorm:"autoUpdateTime"`
 }
 
 func (p *UserAccount) TableName() string {

+ 6 - 3
utils/cost_by_account_balance.go

@@ -8,6 +8,7 @@ import (
 	"sfbase/utils"
 	"sfis/db"
 	"sfis/model"
+	"time"
 
 	"go.uber.org/zap"
 	"gorm.io/gorm"
@@ -89,22 +90,24 @@ func afterCheck(dataLen, payMoney int, userProduct *model.UserProduct, statusCod
 	errs = db.GetSFISDB().Transaction(func(tx *gorm.DB) error {
 		orderBefore := userAccount.Money
 		orderAfter := userAccount.Money - payMoney
+		nowStr := time.Now().Local().Format("2006-01-02 15:04:05")
+		log.Println("nowStr", nowStr)
 		//扣费
-		err := tx.Exec("update user_account set money = money - ? WHERE `app_id` = ?", fmt.Sprint(payMoney), appID).Error
+		err := tx.Exec("update user_account set money = money - ?,update_at = ? WHERE `app_id` = ?", fmt.Sprint(payMoney), nowStr, appID).Error
 		if err != nil {
 			log.Printf("appID:[%s],money:[%d] execute cost money error:[%v]", appID, payMoney, err)
 			tx.Rollback()
 			return err
 		}
 		//生调用记录
-		err = tx.Exec("insert into user_call_record (app_id,user_product_id,status,ip,param,order_code) values (?,?,?,?,?,?)", appID, userProductID, statusCode, ip, param, orderCode).Error
+		err = tx.Exec("insert into user_call_record (app_id,user_product_id,status,ip,param,order_code,create_at) values (?,?,?,?,?,?,?)", appID, userProductID, statusCode, ip, param, orderCode, nowStr).Error
 		if err != nil {
 			log.Printf("appID:[%s],productID:[%d] execute insert into user_call_record error:[%v]", appID, productID, err)
 			tx.Rollback()
 			return err
 		}
 		//生订单
-		err = tx.Exec("insert into interface_order (order_code,app_id,user_product_id,`before`,`after`,cost_model,trade_num) values (?,?,?,?,?,?,?)", orderCode, appID, userProductID, orderBefore, orderAfter, 1, payMoney).Error
+		err = tx.Exec("insert into interface_order (order_code,app_id,user_product_id,`before`,`after`,cost_model,trade_num,create_at) values (?,?,?,?,?,?,?,?)", orderCode, appID, userProductID, orderBefore, orderAfter, 1, payMoney, nowStr).Error
 		if err != nil {
 			log.Printf("appID:[%s],productID:[%d] execute insert into interface_order error:[%v]", appID, productID, err)
 			tx.Rollback()

+ 9 - 6
utils/cost_by_left_num.go

@@ -7,6 +7,7 @@ import (
 	"sfbase/utils"
 	"sfis/db"
 	"sfis/model"
+	"time"
 
 	"gorm.io/gorm"
 )
@@ -91,22 +92,23 @@ func after(productType int, dataLen int, userProduct *model.UserProduct, statusC
 		errs = db.GetSFISDB().Transaction(func(tx *gorm.DB) error {
 			orderBefore := userProduct.LeftNum
 			orderAfter := userProduct.LeftNum - 1
+			nowStr := time.Now().Local().Format("2006-01-02 15:04:05")
 			//扣费
-			err := tx.Exec("update user_product set left_num = IF(`left_num`<1, 0, `left_num`-1) WHERE `app_id` = ? and product_id=?", appID, productID).Error
+			err := tx.Exec("update user_product set left_num = IF(`left_num`<1, 0, `left_num`-1),update_at = ? WHERE `app_id` = ? and product_id=?", nowStr, appID, productID).Error
 			if err != nil {
 				log.Printf("appID:[%s],productID:[%d] execute cost left_num-1 error:[%v]", appID, productID, err)
 				tx.Rollback()
 				return err
 			}
 			//生调用记录
-			err = tx.Exec("insert into user_call_record (app_id,user_product_id,status,ip,param,order_code) values (?,?,?,?,?,?)", appID, userProductID, statusCode, ip, param, orderCode).Error
+			err = tx.Exec("insert into user_call_record (app_id,user_product_id,status,ip,param,order_code,create_at) values (?,?,?,?,?,?,?)", appID, userProductID, statusCode, ip, param, orderCode, nowStr).Error
 			if err != nil {
 				log.Printf("appID:[%s],productID:[%d] execute insert into user_call_record error:[%v]", appID, productID, err)
 				tx.Rollback()
 				return err
 			}
 			//生订单
-			err = tx.Exec("insert into interface_order (order_code,app_id,user_product_id,`before`,`after`,cost_model,trade_num) values (?,?,?,?,?,?,?)", orderCode, appID, userProductID, orderBefore, orderAfter, 0, 1).Error
+			err = tx.Exec("insert into interface_order (order_code,app_id,user_product_id,`before`,`after`,cost_model,trade_num,create_at) values (?,?,?,?,?,?,?,?)", orderCode, appID, userProductID, orderBefore, orderAfter, 0, 1, nowStr).Error
 			if err != nil {
 				log.Printf("appID:[%s],productID:[%d] execute insert into interface_order error:[%v]", appID, productID, err)
 				tx.Rollback()
@@ -120,22 +122,23 @@ func after(productType int, dataLen int, userProduct *model.UserProduct, statusC
 		errs = db.GetSFISDB().Transaction(func(tx *gorm.DB) error {
 			orderBefore := userProduct.LeftNum
 			orderAfter := userProduct.LeftNum - dataLen
+			nowStr := time.Now().Local().Format("2006-01-02 15:04:05")
 			//扣费
-			err := tx.Exec("update user_product set left_num = IF(`left_num`<1, 0, `left_num`-?) WHERE `app_id` = ? and product_id=?", dataLen, appID, productID).Error
+			err := tx.Exec("update user_product set left_num = IF(`left_num`<1, 0, `left_num`-?),update_at = ? WHERE `app_id` = ? and product_id=?", nowStr, dataLen, appID, productID).Error
 			if err != nil {
 				log.Printf("appID:[%s],productID:[%d] execute cost money error:[%v]", appID, productID, err)
 				tx.Rollback()
 				return err
 			}
 			//生调用记录
-			err = tx.Exec("insert into user_call_record (app_id,user_product_id,status,ip,param,order_code) values (?,?,?,?,?,?)", appID, userProductID, statusCode, ip, param, orderCode).Error
+			err = tx.Exec("insert into user_call_record (app_id,user_product_id,status,ip,param,order_code,create_at) values (?,?,?,?,?,?,?)", appID, userProductID, statusCode, ip, param, orderCode, nowStr).Error
 			if err != nil {
 				log.Printf("appID:[%s],productID:[%d] execute insert into user_call_record error:[%v]", appID, productID, err)
 				tx.Rollback()
 				return err
 			}
 			//生订单
-			err = tx.Exec("insert into interface_order (order_code,app_id,user_product_id,`before`,`after`,cost_model,trade_num) values (?,?,?,?,?,?,?)", orderCode, appID, userProductID, orderBefore, orderAfter, 0, dataLen).Error
+			err = tx.Exec("insert into interface_order (order_code,app_id,user_product_id,`before`,`after`,cost_model,trade_num,create_at) values (?,?,?,?,?,?,?,?)", orderCode, appID, userProductID, orderBefore, orderAfter, 0, dataLen, nowStr).Error
 			if err != nil {
 				log.Printf("appID:[%s],productID:[%d] execute insert into interface_order error:[%v]", appID, productID, err)
 				tx.Rollback()