WH01243 4 жил өмнө
parent
commit
37fc19a2db

+ 8 - 0
entity/integral.go

@@ -102,3 +102,11 @@ type SoldeUpdate struct {
 	EndDate    string `xorm:"endDate" form:"endDate" json:"endDate"`          //有效期止
 	AppId      int    `xorm:"appId" form:"appId" json:"appId"`                //剑鱼标识
 }
+
+//积分守护
+type GuardJSON struct {
+	UserId    string `xorm:"userId" form:"userId" json:"userId"`          //用户标识
+	PointType int    `xorm:"pointType" form:"pointType" json:"pointType"` //积分类型
+	EndDate   string `xorm:"endDate" form:"endDate" json:"endDate"`       //截止时间
+	AppId     int    `xorm:"appId" form:"appId" json:"appId"`             //剑鱼标识
+}

+ 1 - 1
rpc/etc/integral.yaml

@@ -4,7 +4,7 @@ Etcd:
   Hosts:
     - 127.0.0.1:2379
   Key: integral.rpc
-DataSource: root:root@tcp(127.0.0.1:3306)/jyintegral?charset=utf8
+DataSource: admin:123456@tcp(192.168.3.14:3306)/jyintegral?charset=utf8
 Cache:
   - Host: localhost:6379
 FileSystemConf:

+ 4 - 2
rpc/internal/logic/integralbalancechecklogic.go

@@ -2,7 +2,7 @@ package logic
 
 import (
 	"context"
-
+	"fmt"
 	"points_service/rpc/integral"
 	"points_service/rpc/internal/svc"
 
@@ -26,6 +26,8 @@ func NewIntegralBalanceCheckLogic(ctx context.Context, svcCtx *svc.ServiceContex
 // 积分余额查询
 func (l *IntegralBalanceCheckLogic) IntegralBalanceCheck(in *integral.AddReq) (*integral.AddResp, error) {
 	// todo: add your logic here and delete this line
-
+	var  bc int64
+	flag,bc := integralService.IntegralBalanceCheckService("ABC","10000")
+	fmt.Println(flag,bc)
 	return &integral.AddResp{}, nil
 }

+ 1 - 0
rpc/internal/logic/integralconsumelogic.go

@@ -43,6 +43,7 @@ func (l *IntegralConsumeLogic) IntegralConsume(in *integral.AddReq) (*integral.A
 	dat.AppId = 10000
 	dat.Sort = entity.ReduceCode
 	fmt.Println(integralService.IntegralConsumeService(dat))
+	//integralService.IntegralConsumeService(dat)
 	return &integral.AddResp{
 		Status: true,
 	},nil

+ 1 - 1
rpc/internal/logic/integralguardlogic.go

@@ -26,6 +26,6 @@ func NewIntegralGuardLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Int
 // 积分守护
 func (l *IntegralGuardLogic) IntegralGuard(in *integral.AddReq) (*integral.AddResp, error) {
 	// todo: add your logic here and delete this line
-
+	integralService.IntegralGuardService("2021-03-10 23:59:59")
 	return &integral.AddResp{}, nil
 }

+ 27 - 0
rpc/test/consume_test.go

@@ -72,4 +72,31 @@ func  Test_Delete(t *testing.T)  {
 	srv := server.NewIntegralServer(ctx)
 	req := &integral.AddReq{UserId: "xzh10", CountPoints: 10}
 	fmt.Println(srv.IntegralConsume(context.Background(), req))
+}
+
+func  Test_Expire(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.IntegralExpireCheck(ctx, req)
+	log.Println("err ", err)
+	log.Println("req ", res)
+}
+
+func  Test_Guard(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.IntegralGuard(ctx, req)
+	log.Println("err ", err)
+	log.Println("req ", res)
+}
+
+func  Test_BalanceCheck(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.IntegralBalanceCheck(ctx, req)
+	log.Println("err ", err)
+	log.Println("req ", res)
 }

+ 0 - 20
rpc/test/expire_test.go

@@ -1,20 +0,0 @@
-package test
-
-import (
-	"context"
-	"github.com/tal-tech/go-zero/zrpc"
-	"log"
-	"points_service/rpc/integral"
-	"points_service/rpc/integralclient"
-	"testing"
-	"time"
-)
-
-func  Test_Expire(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.IntegralExpireCheck(ctx, req)
-	log.Println("err ", err)
-	log.Println("req ", res)
-}

+ 96 - 21
service/integralService.go

@@ -98,8 +98,12 @@ func (service *IntegralService) IntegralAddService(data entity.FlowJSON) (int, s
 			return entity.ErrorCode, "余额新增失败"
 		}
 	}
+<<<<<<< HEAD
 
 	return entity.SuccessCode, "余额新增成功"
+=======
+	return entity.SuccessCode, "余额扣除失败"
+>>>>>>> fe8bc988f5e435ac07c7685f68745db1a4ebc0f4
 }
 
 //消耗积分流水
@@ -114,12 +118,13 @@ func (service *IntegralService) IntegralConsumeService(data entity.FlowJSON) (in
 	flow.CreateTime = time.Now().Format("2006-01-02 15:04:05")
 	flow.EndDate = data.EndDate
 	flow.AppId = data.AppId
+<<<<<<< HEAD
 	flow.Sort = entity.ReduceCode
 	//判断是否为消耗积分:false
+=======
+>>>>>>> fe8bc988f5e435ac07c7685f68745db1a4ebc0f4
 	balance := entity.Balance{}
 	//查询积分余额是否充足
-	balance.UserId = data.UserId
-	balance.AppId = data.AppId
 	b, err := orm.Table("integral_balance").Select("countPoints,id").
 		Where("userId = ? AND appId = ?", data.UserId, data.AppId).
 		Get(&balance)
@@ -131,8 +136,13 @@ func (service *IntegralService) IntegralConsumeService(data entity.FlowJSON) (in
 		return entity.ErrorCode, "积分余额不足"
 	}
 	af, err := orm.Table("integral_flow").Insert(&flow)
+<<<<<<< HEAD
 	if err != nil && af == 0 {
 		log.Print("积分记录失败-积分增减类型:", data.Sort)
+=======
+	if err != nil && af==0{
+		log.Print("积分记录失败")
+>>>>>>> fe8bc988f5e435ac07c7685f68745db1a4ebc0f4
 		return entity.ErrorCode, "积分记录失败"
 	}
 
@@ -191,8 +201,12 @@ func (service *IntegralService) IntegralConsumeService(data entity.FlowJSON) (in
 		log.Print("余额扣除失败")
 		return entity.ErrorCode, "余额扣除失败"
 	}
+<<<<<<< HEAD
 
 	return entity.SuccessCode, "积分消耗成功"
+=======
+	return entity.SuccessCode, "积分记录成功"
+>>>>>>> fe8bc988f5e435ac07c7685f68745db1a4ebc0f4
 }
 
 //到期积分查询
@@ -225,7 +239,7 @@ func (service *IntegralService) IntegralSoldeService(model entity.SoldeUpdate) (
 	var err error
 	var numb = int64(0)
 	//新增积分
-	if (model.PointsType) {
+	if model.PointsType {
 		//永久积分
 		solde.PerManEntPoints = model.Points
 		numb, err = entity.Engine.Table("integral_solde").Insert(&solde)
@@ -270,9 +284,9 @@ func (service *IntegralService) IntegralSoldeReduceService(model entity.SoldeUpd
 			if point == 0 {
 				return true, "积分消耗成功"
 			}
-			if (solde.TimePoints == 0) {
+			if solde.TimePoints == 0 {
 				//消耗永久积分
-				if (solde.PerManEntPoints >= point) {
+				if solde.PerManEntPoints >= point {
 					//够消耗
 					point = 0
 					solde.PerManEntPoints = solde.PerManEntPoints - point
@@ -284,7 +298,7 @@ func (service *IntegralService) IntegralSoldeReduceService(model entity.SoldeUpd
 				}
 			}
 			//消耗时效积分
-			if (solde.TimePoints > point) {
+			if solde.TimePoints > point {
 				//够消耗
 				point = 0
 				solde.TimePoints = solde.TimePoints - point
@@ -337,7 +351,7 @@ func (service *IntegralService) IntegralBalanceService(model entity.BalanceUpdat
 		log.Println("没有积分可以扣除")
 		return false, "没有积分可以扣除"
 	}
-	if (balanceList[0].CountPoints < model.CountPoints) {
+	if balanceList[0].CountPoints < model.CountPoints {
 		log.Println("积分余额不足")
 		return false, "积分余额不足"
 	}
@@ -351,24 +365,85 @@ func (service *IntegralService) IntegralBalanceService(model entity.BalanceUpdat
 }
 */
 //积分守护
-func (service *IntegralService) IntegralGuardService(data entity.ExpireJSON) []entity.Flow {
+func (service *IntegralService) IntegralGuardService(endDate string) bool {
 	orm := entity.Engine
-	var flow []entity.Flow
 	var err error
-	point := ""
-	if data.PointType != 0 {
-		point = "AND pointType = " + fmt.Sprint(data.PointType) + ""
-	}
-	err = orm.Table("integral_flow").
-		Select("id,pointType,SUM(point) AS point").
-		Where("userId = ? AND appId = ? AND endDate < ? "+point+"", data.UserId, data.AppId, data.EndDate).
-		GroupBy("pointType").
-		Find(&flow)
+	soldelist := []entity.Solde{}
+	err = orm.Table("integral_solde").
+		Where("timePoints != 0 AND endDate < ? ", endDate).
+		Desc("endDate").Find(&soldelist)
 	if err != nil {
-		log.Println(err)
-		return nil
+		log.Println("积分结存查询失败")
 	}
-	return flow
+	for _, solde := range soldelist {
+		//查询积分余额是否充足
+		balance := entity.Balance{}
+		b, err := orm.Table("integral_balance").Select("countPoints,id").
+			Where("userId = ? AND appId = ?", solde.UserId, solde.AppId).
+			Get(&balance)
+		if !b || err != nil {
+			log.Printf("积分余额查询出错,userId:[%s],err:[%v]", solde.UserId, err)
+			return false
+		}
+		if balance.CountPoints < solde.TimePoints {
+			return false
+		}
+
+		//积分余额-时效过期积分=当前总积分余额
+		balance.CountPoints = balance.CountPoints - solde.TimePoints
+		orm.Table("integral_balance")
+		numb, err := orm.Table("integral_balance").
+			ID(balance.Id).
+			Cols("countPoints").
+			Update(balance)
+		if err != nil && numb == 0 {
+			log.Print("余额扣除失败")
+			return false
+		}
+
+		//积分过期流水记录
+		flow := entity.Flow{}
+		flow.UserId = solde.UserId
+		flow.PointType = 9
+		flow.BusinessTypeId = 0
+		flow.BusinessType = "0"
+		flow.Point = solde.TimePoints
+		flow.CreateTime = time.Now().Format("2006-01-02 15:04:05")
+		flow.EndDate = solde.EndDate
+		flow.AppId = solde.AppId
+		af, err := orm.Table("integral_flow").Insert(&flow)
+		if err != nil && af==0{
+			log.Print("积分记录失败")
+			return false
+		}
+
+		//清空过期积分的时效积分
+		soldeUpdate := &entity.Solde{}
+		soldeUpdate.TimePoints = 0
+		af, err = orm.Table("integral_solde").
+			Cols("timePoints").
+			ID(solde.Id).
+			Update(soldeUpdate)
+		if af == 0 && err != nil {
+			log.Println("过期时效积分清空失败", solde.Id)
+			return false
+		}
+	}
+	return true
+}
+
+//积分余额查询
+func (service *IntegralService) IntegralBalanceCheckService(userId,appId string) (bool,int64) {
+	orm := entity.Engine
+	balance := entity.Balance{}
+	b,err := orm.Table("integral_balance").
+		Where("userId = ? AND appId = ?", userId,appId).
+		Get(&balance)
+	if !b || err != nil {
+		log.Println("积分余额查询失败")
+		return false,0
+	}
+	return true,balance.CountPoints
 }
 
 //按月查询积分使用情况