李哲 4 년 전
부모
커밋
ddc99bd900

+ 20 - 0
entity/integral.go

@@ -41,3 +41,23 @@ type Solde struct {
 	CountPoints     int    `xorm:"countPoints" form:"countPoints" json:"countPoints"`             //合计
 	AppId           int    `xorm:"appId" form:"appId" json:"appId"`                               //剑鱼标识
 }
+
+//积分流水
+type FlowJSON struct {
+	UserId         string `xorm:"userId" form:"userId" json:"userId"`                         //用户标识
+	PointType      int    `xorm:"pointType" form:"pointType" json:"pointType"`                //积分类型
+	BusinessTypeId int    `xorm:"businessTypeId" form:"businessTypeId" json:"businessTypeId"` //业务单据标识
+	BusinessType   string `xorm:"businessType" form:"businessType" json:"businessType"`       //业务类型
+	Point          int    `xorm:"point" form:"point" json:"point"`                            //积分
+	EndDate        string `xorm:"endDate" form:"endDate" json:"endDate"`                      //截止时间
+	AppId          int    `xorm:"appId" form:"appId" json:"appId"`                            //剑鱼标识
+	Sort           bool   `xorm:"sort" form:"sort" json:"sort"`                               //获取积分:true、消耗积分:false
+}
+
+//到期积分查询
+type ExpireJSON 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: admin:123456@tcp(192.168.3.14:3306)/jyintegral?charset=utf8
+DataSource: root:root@tcp(127.0.0.1:3306)/jyintegral?charset=utf8
 Cache:
   - Host: localhost:6379
 FileSystemConf:

+ 1 - 0
rpc/integral/integral.pb.go

@@ -34,6 +34,7 @@ type AddReq struct {
 	sizeCache     protoimpl.SizeCache
 	unknownFields protoimpl.UnknownFields
 
+
 	UserId      string `protobuf:"bytes,1,opt,name=userId,proto3" json:"userId,omitempty"`
 	CountPoints int64  `protobuf:"varint,2,opt,name=countPoints,proto3" json:"countPoints,omitempty"`
 }

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

@@ -2,6 +2,7 @@ package logic
 
 import (
 	"context"
+	"points_service/entity"
 	"points_service/service"
 
 	"points_service/rpc/integral"
@@ -31,7 +32,16 @@ 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
-	integralService.IntegralConsumeService("ABC", "1", "2021-03-10 08:00:00", "2021-03-20",1, 1, 1, 10000)
+	dat := entity.FlowJSON{}
+	dat.UserId = "ABC"
+	dat.PointType = 1
+	dat.BusinessTypeId = 1
+	dat.BusinessType = "1"
+	dat.Point = 1
+	dat.EndDate = "2021-03-10"
+	dat.AppId = 10000
+	dat.Sort = false
+	integralService.IntegralConsumeService(dat)
 	return &integral.AddResp{
 		Status: true,
 	},nil

+ 17 - 2
rpc/internal/logic/integralexpirechecklogic.go

@@ -2,6 +2,8 @@ package logic
 
 import (
 	"context"
+	"fmt"
+	"points_service/entity"
 
 	"points_service/rpc/integral"
 	"points_service/rpc/internal/svc"
@@ -26,6 +28,19 @@ func NewIntegralExpireCheckLogic(ctx context.Context, svcCtx *svc.ServiceContext
 // 到期积分查询
 func (l *IntegralExpireCheckLogic) IntegralExpireCheck(in *integral.AddReq) (*integral.AddResp, error) {
 	// todo: add your logic here and delete this line
-
-	return &integral.AddResp{}, nil
+	result := &integral.AddResp{}
+	dat := entity.ExpireJSON{}
+	dat.UserId = "ABC"
+	dat.PointType = 1
+	dat.EndDate = "2021-03-16"
+	dat.AppId = 10000
+	data := integralService.IntegralExpireCheckService(dat)
+	res := make(map[string]interface{})
+	res["data"] = data
+	fmt.Println(res)
+	result.Status = true
+	/*result.Code = -1
+	result.Data = res
+	result.Msg = "查询列表失败"*/
+	return result,nil
 }

+ 20 - 0
rpc/test/expire_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_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)
+}

+ 74 - 27
service/integralService.go

@@ -1,45 +1,92 @@
 package service
 
 import (
+	"fmt"
 	"log"
 	"points_service/entity"
+	"time"
 )
 type IntegralService struct{}
 //消耗积分流水
-func(service *IntegralService) IntegralConsumeService(userId, businessType, createTime, endDate string,pointType, businessTypeId, point, appId int) (int,string) {
+func(service *IntegralService) IntegralConsumeService(data entity.FlowJSON) (int,string) {
 	orm := entity.Engine
 	flow := entity.Flow{}
-	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,"积分余额不足"
+	flow.UserId = data.UserId
+	flow.PointType = data.PointType
+	flow.BusinessTypeId = data.BusinessTypeId
+	flow.BusinessType = data.BusinessType
+	flow.Point = data.Point
+	flow.CreateTime = time.Now().Format("2006-01-02 15:04:05")
+	flow.EndDate = data.EndDate
+	flow.AppId = data.AppId
+	//判断是否为消耗积分:false
+	if !data.Sort {
+		//查询积分余额是否充足
+		balance := entity.Balance{}
+		balance.UserId = data.UserId
+		balance.AppId = data.AppId
+		b,err := orm.Table("integral_balance").Select("countPoints").
+			Where("userId = ? AND appId = ?",data.UserId,data.AppId).
+			Get(&balance)
+		if !b || err != nil {
+			log.Printf("积分余额查询出错,userId:[%s],err:[%v]", data.UserId, err)
+			return entity.ErrorCode,"积分余额不足"
+		}
+		if balance.CountPoints <data.Point {
+			return entity.ErrorCode,"积分余额不足"
+		}
 	}
 
 	af, err := orm.Table("integral_flow").Insert(&flow)
 	if err != nil {
-		log.Print("消耗积分记录失败")
-		return entity.ErrorCode,"消耗积分失败"
+		log.Print("积分记录失败-积分增减类型:",data.Sort)
+		return entity.ErrorCode,"积分记录失败"
 	}
 	if af >0 {
-		log.Println("消耗积分记录成功")
-		return entity.SuccessCode,"消耗积分成功"
+		log.Println("积分记录成功-积分增减类型:",data.Sort)
+		return entity.SuccessCode,"积分记录成功"
+	}
+	return entity.SuccessCode,"积分记录成功"
+}
+
+//到期积分查询
+func(service *IntegralService) IntegralExpireCheckService(data entity.ExpireJSON) []entity.Flow {
+	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)
+	if err != nil {
+		log.Println(err)
+		return nil
 	}
-	return entity.SuccessCode,"消耗积分成功"
+	return flow
 }
+
+//积分守护
+func(service *IntegralService) IntegralGuardService(data entity.ExpireJSON) []entity.Flow {
+	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)
+	if err != nil {
+		log.Println(err)
+		return nil
+	}
+	return flow
+}