李哲 4 жил өмнө
parent
commit
886df5b507

+ 8 - 0
entity/integral.go

@@ -2,7 +2,15 @@ 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"`

+ 0 - 1
rpc/integral.go

@@ -47,6 +47,5 @@ func init() {
 	if err != nil {
 		log.Fatal("数据库连接失败:", err)
 	}
-	log.Println("积分流水:",integralService.IntegralConsumeService())
 	fmt.Println(c.DataSource+"链接成功")
 }

+ 9 - 2
rpc/internal/logic/integralconsumelogic.go

@@ -2,6 +2,7 @@ package logic
 
 import (
 	"context"
+	"points_service/service"
 
 	"points_service/rpc/integral"
 	"points_service/rpc/internal/svc"
@@ -15,6 +16,10 @@ type IntegralConsumeLogic struct {
 	logx.Logger
 }
 
+var (
+	integralService = &service.IntegralService{}
+)
+
 func NewIntegralConsumeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *IntegralConsumeLogic {
 	return &IntegralConsumeLogic{
 		ctx:    ctx,
@@ -26,6 +31,8 @@ func NewIntegralConsumeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *I
 // 消耗积分
 func (l *IntegralConsumeLogic) IntegralConsume(in *integral.AddReq) (*integral.AddResp, error) {
 	// todo: add your logic here and delete this line
-
-	return &integral.AddResp{}, nil
+	integralService.IntegralConsumeService("ABC", "1", "2021-03-10 08:00:00", "2021-03-20",1, 1, 1, 10000)
+	return &integral.AddResp{
+		Status: true,
+	},nil
 }

+ 20 - 0
rpc/test/consume_test.go

@@ -0,0 +1,20 @@
+package test
+
+import (
+	"context"
+	"github.com/tal-tech/go-zero/zrpc"
+	"log"
+	"points_service/rpc/integral"
+	"points_service/rpc/integralclient"
+	"testing"
+	"time"
+)
+
+func  Test_Consume(t *testing.T)  {
+	ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
+	FileSystem :=integralclient.NewIntegral(zrpc.MustNewClient(c.FileSystemConf))
+	req := &integral.AddReq{UserId: "xzh", CountPoints: 123}
+	res, err := FileSystem.IntegralConsume(ctx, req)
+	log.Println("err ", err)
+	log.Println("req ", res)
+}

+ 28 - 14
service/integralService.go

@@ -3,29 +3,43 @@ package service
 import (
 	"log"
 	"points_service/entity"
-	"time"
 )
 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
+	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 balance.CountPoints < point {
+		return entity.ErrorCode,"积分余额不足"
+	}
 
-	af, err := entity.Engine.Table("integral_flow").Insert(&flow)
+	af, err := orm.Table("integral_flow").Insert(&flow)
 	if err != nil {
 		log.Print("消耗积分记录失败")
-		return true
+		return entity.ErrorCode,"消耗积分失败"
 	}
 	if af >0 {
 		log.Println("消耗积分记录成功")
-		return true
+		return entity.SuccessCode,"消耗积分成功"
 	}
-	return true
+	return entity.SuccessCode,"消耗积分成功"
 }