WH01243 4 سال پیش
والد
کامیت
c682770c5e

+ 23 - 1
entity/integral.go

@@ -11,6 +11,12 @@ const (
 	ErrorCode   = 0
 )
 
+//流水类型1加-1减
+const (
+	AddCode    = 1
+	ReduceCode = -1
+)
+
 //积分余额
 type Balance struct {
 	Id          int64  `xorm:"pk autoincr id" form:"id" json:"id"`
@@ -38,6 +44,22 @@ type Flow struct {
 	CreateTime     string `xorm:"createTime" form:"createTime" json:"createTime"`             //创建时间
 	EndDate        string `xorm:"endDate" form:"endDate" json:"endDate"`                      //截止时间
 	AppId          int    `xorm:"appId" form:"appId" json:"appId"`                            //剑鱼标识
+	Sort           int    `xorm:"sort" form:"sort" json:"sort"`                               //操作类型1加-1减
+}
+
+//积分流水
+type FlowReq struct {
+	Id             int64  `xorm:"pk autoincr id" form:"id" json:"id"`
+	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          int64  `xorm:"point" form:"point" json:"point"`                            //积分
+	CreateTime     string `xorm:"createTime" form:"createTime" json:"createTime"`             //创建时间
+	EndDate        string `xorm:"endDate" form:"endDate" json:"endDate"`                      //截止时间
+	AppId          int    `xorm:"appId" form:"appId" json:"appId"`                            //剑鱼标识
+	Sort           int    `xorm:"sort" form:"sort" json:"sort"`                               //操作类型1加-1减
+	name           string `xorm:"name" form:"name" json:"name"`                               //积分类型名称
 }
 
 //积分结存
@@ -59,7 +81,7 @@ type FlowJSON struct {
 	Point          int64  `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
+	Sort           int    `xorm:"sort" form:"sort" json:"sort"`                               //操作类型1加-1减
 	OperationType  bool   `xorm:"operationType" form:"operationType" json:"operationType"`    //永久:true、时效:false
 }
 

+ 1 - 0
rpc/integral.go

@@ -44,6 +44,7 @@ func init() {
 	conf.MustLoad(*configFile, &c)
 	fmt.Println(c.DataSource)
 	entity.Engine, err = xorm.NewEngine("mysql", c.DataSource)
+	entity.Engine.ShowSQL(true)
 	if err != nil {
 		log.Fatal("数据库连接失败:", err)
 	}

+ 18 - 13
rpc/integral/integral.pb.go

@@ -7,15 +7,15 @@
 package integral
 
 import (
-	context "context"
-	proto "github.com/golang/protobuf/proto"
-	grpc "google.golang.org/grpc"
-	codes "google.golang.org/grpc/codes"
-	status "google.golang.org/grpc/status"
-	protoreflect "google.golang.org/protobuf/reflect/protoreflect"
-	protoimpl "google.golang.org/protobuf/runtime/protoimpl"
-	reflect "reflect"
-	sync "sync"
+	"context"
+	"github.com/golang/protobuf/proto"
+	"google.golang.org/grpc"
+	"google.golang.org/grpc/codes"
+	"google.golang.org/grpc/status"
+	"google.golang.org/protobuf/reflect/protoreflect"
+	"google.golang.org/protobuf/runtime/protoimpl"
+	"reflect"
+	"sync"
 )
 
 const (
@@ -33,7 +33,7 @@ type AddReq struct {
 	state         protoimpl.MessageState
 	sizeCache     protoimpl.SizeCache
 	unknownFields protoimpl.UnknownFields
-
+	msg           protoimpl.MessageInfo
 
 	UserId      string `protobuf:"bytes,1,opt,name=userId,proto3" json:"userId,omitempty"`
 	CountPoints int64  `protobuf:"varint,2,opt,name=countPoints,proto3" json:"countPoints,omitempty"`
@@ -89,8 +89,8 @@ type AddResp struct {
 	state         protoimpl.MessageState
 	sizeCache     protoimpl.SizeCache
 	unknownFields protoimpl.UnknownFields
-
-	Status bool `protobuf:"varint,1,opt,name=status,proto3" json:"status,omitempty"`
+	Msg           string `protobuf:"varint,1,opt,name=msg,proto3" json:"msg,omitempty"`
+	Status        bool   `protobuf:"varint,1,opt,name=status,proto3" json:"status,omitempty"`
 }
 
 func (x *AddResp) Reset() {
@@ -131,7 +131,12 @@ func (x *AddResp) GetStatus() bool {
 	}
 	return false
 }
-
+func (x *AddResp) GetMsg() string {
+	if x != nil {
+		return x.Msg
+	}
+	return x.Msg
+}
 type CheckReq struct {
 	state         protoimpl.MessageState
 	sizeCache     protoimpl.SizeCache

+ 5 - 4
rpc/internal/logic/integralconsumelogic.go

@@ -2,6 +2,7 @@ package logic
 
 import (
 	"context"
+	"fmt"
 	"points_service/entity"
 	"points_service/service"
 
@@ -33,15 +34,15 @@ 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
 	dat := entity.FlowJSON{}
-	dat.UserId = "ABC"
+	dat.UserId = in.UserId
 	dat.PointType = 1
 	dat.BusinessTypeId = 1
 	dat.BusinessType = "1"
-	dat.Point = 1
+	dat.Point = in.CountPoints
 	dat.EndDate = "2021-03-10"
 	dat.AppId = 10000
-	dat.Sort = false
-	integralService.IntegralConsumeService(dat)
+	dat.Sort = entity.ReduceCode
+	fmt.Println(integralService.IntegralConsumeService(dat))
 	return &integral.AddResp{
 		Status: true,
 	},nil

+ 8 - 1
rpc/internal/logic/integraldetailedchecklogic.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,11 @@ func NewIntegralDetailedCheckLogic(ctx context.Context, svcCtx *svc.ServiceConte
 // 积分明细查询
 func (l *IntegralDetailedCheckLogic) IntegralDetailedCheck(in *integral.AddReq) (*integral.AddResp, error) {
 	// todo: add your logic here and delete this line
-
+	dat := entity.ExpireJSON{}
+	dat.UserId = in.UserId
+	dat.EndDate = "2021-03"
+	dat.AppId = 10000
+	data := integralService.IntegralExpireCheckService(dat)
+	fmt.Println(data)
 	return &integral.AddResp{}, nil
 }

+ 25 - 3
rpc/internal/logic/integralharvestlogic.go

@@ -2,7 +2,8 @@ package logic
 
 import (
 	"context"
-
+	"fmt"
+	"points_service/entity"
 	"points_service/rpc/integral"
 	"points_service/rpc/internal/svc"
 
@@ -25,7 +26,28 @@ func NewIntegralHarvestLogic(ctx context.Context, svcCtx *svc.ServiceContext) *I
 
 // 收获积分
 func (l *IntegralHarvestLogic) IntegralHarvest(in *integral.AddReq) (*integral.AddResp, error) {
-	// todo: add your logic here and delete this line
+	dat := entity.FlowJSON{}
+	dat.PointType = 1
+	dat.BusinessTypeId = 1
+	dat.BusinessType = "1"
+	dat.EndDate = "2021-03-10"
+	dat.AppId = 10000
+	dat.OperationType = false
+	dat.UserId=in.UserId
+	dat.Point=in.CountPoints
+	dat.Sort=entity.AddCode
+	code, msg := integralService.IntegralAddService(dat)
+	fmt.Println(code,msg)
+	if (code == 1) {
+		return &integral.AddResp{
+			Status: true,
+			Msg:    msg,
+		}, nil
+	} else {
+		return &integral.AddResp{
+			Status: false,
+			Msg:    msg,
+		}, nil
+	}
 
-	return &integral.AddResp{}, nil
 }

+ 13 - 2
rpc/test/consume_test.go

@@ -46,12 +46,23 @@ func  Test_Add(t *testing.T)  {
 	log.Println("req ", res)
 }
 
+func  Test_serach(t *testing.T)  {
+	ctx, _ := context.WithTimeout(context.Background(), 500*time.Second)
+	FileSystem :=integralclient.NewIntegral(zrpc.MustNewClient(c.FileSystemConf))
+	req := &integral.AddReq{UserId: "xzh10", CountPoints: 123}
+	res, err := FileSystem.IntegralDetailedCheck(ctx, req)
+	log.Println("err ", err)
+	log.Println("req ", res)
+}
+
+
+
 func  Test_Ad(t *testing.T)  {
 	var c config.Config
 	conf.MustLoad(*configFile, &c)
 	ctx := svc.NewServiceContext(c)
 	srv := server.NewIntegralServer(ctx)
-	req := &integral.AddReq{UserId: "xzh", CountPoints: 123}
+	req := &integral.AddReq{UserId: "xzh10", CountPoints: 100}
 	fmt.Println(srv.IntegralHarvest(context.Background(), req))
 }
 func  Test_Delete(t *testing.T)  {
@@ -59,6 +70,6 @@ func  Test_Delete(t *testing.T)  {
 	conf.MustLoad(*configFile, &c)
 	ctx := svc.NewServiceContext(c)
 	srv := server.NewIntegralServer(ctx)
-	req := &integral.AddReq{UserId: "xzh", CountPoints: 10}
+	req := &integral.AddReq{UserId: "xzh10", CountPoints: 10}
 	fmt.Println(srv.IntegralConsume(context.Background(), req))
 }

+ 35 - 17
service/integralService.go

@@ -16,11 +16,12 @@ func (service *IntegralService) IntegralAddService(data entity.FlowJSON) (int, s
 	flow.UserId = data.UserId
 	flow.BusinessTypeId = data.BusinessTypeId
 	flow.BusinessType = data.BusinessType
-	flow.PointType=data.PointType
+	flow.PointType = data.PointType
 	flow.Point = data.Point
 	flow.CreateTime = time.Now().Format("2006-01-02 15:04:05")
 	flow.EndDate = data.EndDate
 	flow.AppId = data.AppId
+	flow.Sort = entity.AddCode;
 	var err error
 	var numb = int64(0)
 	numb, err = orm.Table("integral_flow").Insert(flow)
@@ -39,13 +40,13 @@ func (service *IntegralService) IntegralAddService(data entity.FlowJSON) (int, s
 		soldelist := []entity.Solde{}
 		err = orm.Table("integral_solde").Where("appId=? and  userId=? and endDate=? and (perManEntPoints != 0 AND timePoints = 0) or (perManEntPoints = 0 AND timePoints = 0 ) ", data.AppId, data.UserId, data.EndDate).Find(&soldelist)
 		if len(soldelist) > 0 {
-			soldelist[0].PerManEntPoints +=  data.Point
+			soldelist[0].PerManEntPoints += data.Point
 			numb, err = orm.Table("integral_solde").ID(soldelist[0].Id).Cols("perManEntPoints").Update(soldelist[0])
 			if err != nil && numb == 0 {
 				log.Print("修改时效积分失败")
 				return entity.ErrorCode, "修改时效积分失败"
 			}
-		}else{
+		} else {
 			solde.PerManEntPoints = data.Point
 			numb, err = orm.Table("integral_solde").Insert(&solde)
 			if err != nil && numb == 0 {
@@ -59,14 +60,14 @@ func (service *IntegralService) IntegralAddService(data entity.FlowJSON) (int, s
 		soldelist := []entity.Solde{}
 		err = orm.Table("integral_solde").Where("appId=? and  userId=? and endDate=? and ( perManEntPoints = 0 AND timePoints != 0) or (perManEntPoints = 0 AND timePoints = 0 ) ", data.AppId, data.UserId, data.EndDate).Find(&soldelist)
 		if len(soldelist) > 0 {
-			soldelist[0].TimePoints +=  data.Point
+			soldelist[0].TimePoints += data.Point
 			numb, err = orm.Table("integral_solde").ID(soldelist[0].Id).Cols("timePoints").Update(soldelist[0])
 			if err != nil && numb == 0 {
 				log.Print("修改时效积分失败")
 				return entity.ErrorCode, "修改时效积分失败"
 			}
 		} else {
-			solde.TimePoints =  data.Point
+			solde.TimePoints = data.Point
 			numb, err = orm.Table("integral_solde").Insert(&solde)
 			if err != nil && numb == 0 {
 				log.Print("新增时效积分失败")
@@ -82,28 +83,27 @@ func (service *IntegralService) IntegralAddService(data entity.FlowJSON) (int, s
 		balance := entity.Balance{}
 		balance.UserId = data.UserId
 		balance.AppId = data.AppId
-		balance.CountPoints =  data.Point
+		balance.CountPoints = data.Point
 		numb, err = entity.Engine.Table("integral_balance").Insert(&balance)
 		if err != nil && numb == 0 {
 			log.Print("新增余额失败")
 			return entity.ErrorCode, "新增余额失败"
 		}
-	}else{
+	} else {
 		//修改余额
-		balanceList[0].CountPoints = balanceList[0].CountPoints +  data.Point
+		balanceList[0].CountPoints = balanceList[0].CountPoints + data.Point
 		numb, err = entity.Engine.Table("integral_balance").ID(balanceList[0].Id).Cols("countPoints").Update(balanceList[0])
 		if err != nil && numb == 0 {
-			log.Print("余额扣除失败")
-			return entity.ErrorCode, "余额扣除失败"
+			log.Print("余额新增失败")
+			return entity.ErrorCode, "余额新增失败"
 		}
 	}
 
-	return entity.SuccessCode, "余额扣除失败"
+	return entity.SuccessCode, "余额新增成功"
 }
 
 //消耗积分流水
 func (service *IntegralService) IntegralConsumeService(data entity.FlowJSON) (int, string) {
-
 	orm := entity.Engine
 	flow := entity.Flow{}
 	flow.UserId = data.UserId
@@ -114,6 +114,7 @@ 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
+	flow.Sort = entity.ReduceCode
 	//判断是否为消耗积分:false
 	balance := entity.Balance{}
 	//查询积分余额是否充足
@@ -130,14 +131,14 @@ func (service *IntegralService) IntegralConsumeService(data entity.FlowJSON) (in
 		return entity.ErrorCode, "积分余额不足"
 	}
 	af, err := orm.Table("integral_flow").Insert(&flow)
-	if err != nil && af==0{
+	if err != nil && af == 0 {
 		log.Print("积分记录失败-积分增减类型:", data.Sort)
 		return entity.ErrorCode, "积分记录失败"
 	}
 
 	//结存消耗
 	soldelist := []entity.Solde{}
-	err = entity.Engine.Table("integral_solde").Where("appId=? and  userId=? and endDate> ? or 	( perManEntPoints != 0 AND timePoints = 0 ) OR ( perManEntPoints = 0 AND timePoints != 0 )", data.AppId, data.UserId, time.Now().Format("2006-01-02")).Desc("endDate").Desc("timePoints").Find(&soldelist)
+	err = entity.Engine.Table("integral_solde").Where("appId=? and  userId=? and endDate> ? and( 	( perManEntPoints != 0 AND timePoints = 0 ) OR ( perManEntPoints = 0 AND timePoints != 0 ))", data.AppId, data.UserId, time.Now().Format("2006-01-02")).Desc("endDate").Desc("timePoints").Find(&soldelist)
 	if len(soldelist) == 0 {
 		return entity.ErrorCode, "没有结存可以消耗"
 	}
@@ -150,7 +151,6 @@ func (service *IntegralService) IntegralConsumeService(data entity.FlowJSON) (in
 			//消耗永久积分
 			if (solde.PerManEntPoints >= point) {
 				//够消耗
-
 				solde.PerManEntPoints = solde.PerManEntPoints - point
 				point = 0
 				af, err = entity.Engine.Table("integral_solde").ID(solde.Id).Cols("perManEntPoints").Update(solde)
@@ -159,7 +159,7 @@ func (service *IntegralService) IntegralConsumeService(data entity.FlowJSON) (in
 					return entity.ErrorCode, "消耗永久积分失败"
 				}
 			}
-		}else{
+		} else {
 			//消耗时效积分
 			if (solde.TimePoints > point) {
 
@@ -192,7 +192,7 @@ func (service *IntegralService) IntegralConsumeService(data entity.FlowJSON) (in
 		return entity.ErrorCode, "余额扣除失败"
 	}
 
-	return entity.SuccessCode, "积分记录成功"
+	return entity.SuccessCode, "积分消耗成功"
 }
 
 //到期积分查询
@@ -370,3 +370,21 @@ func (service *IntegralService) IntegralGuardService(data entity.ExpireJSON) []e
 	}
 	return flow
 }
+
+//按月查询积分使用情况
+func (service *IntegralService) IntegralExpireCheck(data entity.ExpireJSON) []entity.FlowReq {
+	orm := entity.Engine
+	var err error
+	flowReq := []entity.FlowReq{}
+	err = orm.Table("integral_flow").Alias("flow").
+		Select("flow.*,pt.name").
+		Join("left", "point_type pt", "flow.pointType=pt.code").
+		Where("userId = ? AND appId = ? AND DATE_FORMAT(createTime,'%Y-%m') ", data.UserId, data.AppId, data.EndDate).
+		GroupBy("pointType").
+		Find(&flowReq)
+	if err != nil {
+		log.Println(err)
+		return nil
+	}
+	return flowReq
+}