WH01243 4 年之前
父節點
當前提交
27f115622c
共有 2 個文件被更改,包括 31 次插入18 次删除
  1. 6 0
      entity/integral.go
  2. 25 18
      service/integralService.go

+ 6 - 0
entity/integral.go

@@ -2,8 +2,14 @@ package entity
 
 import "github.com/go-xorm/xorm"
 
+//定义orm引擎
 var Engine *xorm.Engine
 
+//定义返回状态
+const (
+	SuccessCode = 1
+	ErrorCode   = 0
+)
 //积分余额
 type Balance struct {
 	Id          int64  `xorm:"pk autoincr id" form:"id" json:"id"`

+ 25 - 18
service/integralService.go

@@ -9,27 +9,34 @@ import (
 
 type IntegralService struct{}
 
-//新增、消耗积分流水
-func (service *IntegralService) IntegralConsumeService() bool {
+
+//消耗积分流水
+func(service *IntegralService) IntegralConsumeService(userId, businessType, createTime, endDate string,pointType, businessTypeId, point, appId int) (int,string) {
+	orm := entity.Engine
 	flow := entity.Flow{}
-	flow.UserId = "222"
-	flow.PointType = 1
-	flow.BusinessTypeId = 1
-	flow.BusinessType = "1"
-	flow.Point = 2
-	flow.CreateTime = time.Now().Format("2006-01-02 15:04:05")
-	flow.EndDate = time.Now().Format("2006-01-02")
-	flow.AppId = 10000
-	af, err := entity.Engine.Table("integral_flow").Insert(&flow)
-	if err != nil {
-		log.Print("消耗积分记录失败")
-		return true
+	flow.UserId = userId
+	flow.PointType = pointType
+	flow.BusinessTypeId = businessTypeId
+	flow.BusinessType = businessType
+	flow.Point = point
+	flow.CreateTime = createTime //time.Now().Format("2006-01-02 15:04:05")
+	flow.EndDate = endDate
+	flow.AppId = appId
+	//查询积分余额是否充足
+	balance := entity.Balance{}
+	balance.UserId = userId
+	balance.AppId = appId
+	b,err := orm.Table("integral_balance").Select("countPoints").
+		Where("userId = ? AND appId = ?",userId,appId).
+		Get(&balance)
+	if !b || err != nil {
+		log.Printf("积分余额查询出错,userId:[%s],err:[%v]", userId, err)
+		return entity.ErrorCode,"积分余额不足"
 	}
-	if af > 0 {
-		log.Println("消耗积分记录成功")
-		return true
+	if balance.CountPoints < point {
+		return entity.ErrorCode,"积分余额不足"
 	}
-	return true
+	return entity.SuccessCode,"消耗积分成功"
 }
 
 //结存新增