WH01243 4 years ago
parent
commit
1febffa509
4 changed files with 226 additions and 65 deletions
  1. 8 7
      entity/integral.go
  2. 44 0
      rpc/test/consume_test.go
  3. 1 1
      rpc/test/integral.yaml
  4. 173 57
      service/integralService.go

+ 8 - 7
entity/integral.go

@@ -15,14 +15,14 @@ const (
 type Balance struct {
 	Id          int64  `xorm:"pk autoincr id" form:"id" json:"id"`
 	UserId      string `xorm:"userId" form:"userId" json:"userId"`                //用户标识
-	CountPoints int    `xorm:"countPoints" form:"countPoints" json:"countPoints"` //合计
+	CountPoints int64  `xorm:"countPoints" form:"countPoints" json:"countPoints"` //合计
 	AppId       int    `xorm:"appId" form:"appId" json:"appId"`                   //剑鱼标识
 }
 
 //积分余额
 type BalanceUpdate struct {
 	UserId      string `xorm:"userId" form:"userId" json:"userId"`                //用户标识
-	CountPoints int    `xorm:"countPoints" form:"countPoints" json:"countPoints"` //合计
+	CountPoints int64  `xorm:"countPoints" form:"countPoints" json:"countPoints"` //合计
 	AppId       int    `xorm:"appId" form:"appId" json:"appId"`                   //剑鱼标识
 	Change      bool   `xorm:"change" form:"change" json:"change"`                //修改方式true新增 false消耗
 }
@@ -34,7 +34,7 @@ type Flow struct {
 	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"`                            //积分
+	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"`                            //剑鱼标识
@@ -43,9 +43,9 @@ type Flow struct {
 //积分结存
 type Solde struct {
 	Id              int64  `xorm:"pk autoincr id" form:"id" json:"id"`
-	PerManEntPoints int    `xorm:"perManEntPoints" form:"perManEntPoints" json:"perManEntPoints"` //永久积分
+	PerManEntPoints int64  `xorm:"perManEntPoints" form:"perManEntPoints" json:"perManEntPoints"` //永久积分
 	UserId          string `xorm:"userId" form:"userId" json:"userId"`                            //用户标识
-	TimePoints      int    `xorm:"timePoints" form:"timePoints" json:"timePoints"`                //时效积分
+	TimePoints      int64  `xorm:"timePoints" form:"timePoints" json:"timePoints"`                //时效积分
 	EndDate         string `xorm:"endDate" form:"endDate" json:"endDate"`                         //有效期止
 	AppId           int    `xorm:"appId" form:"appId" json:"appId"`                               //剑鱼标识
 }
@@ -56,10 +56,11 @@ type FlowJSON struct {
 	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"`                            //积分
+	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
+	OperationType  bool   `xorm:"operationType" form:"operationType" json:"operationType"`    //永久:true、时效:false
 }
 
 //到期积分查询
@@ -75,7 +76,7 @@ type SoldeUpdate struct {
 	Id         int64  `xorm:"pk autoincr id" form:"id" json:"id"`
 	PointsType bool   `xorm:"pointsType" form:"pointsType" json:"pointsType"` //积分类型true永久 false失效
 	UserId     string `xorm:"userId" form:"userId" json:"userId"`             //用户标识
-	Points     int    `xorm:"points" form:"points" json:"points"`             //积分
+	Points     int64  `xorm:"points" form:"points" json:"points"`             //积分
 	EndDate    string `xorm:"endDate" form:"endDate" json:"endDate"`          //有效期止
 	AppId      int    `xorm:"appId" form:"appId" json:"appId"`                //剑鱼标识
 }

+ 44 - 0
rpc/test/consume_test.go

@@ -2,14 +2,32 @@ package test
 
 import (
 	"context"
+	"fmt"
+	_ "github.com/go-sql-driver/mysql"
+	"github.com/go-xorm/xorm"
+	"github.com/tal-tech/go-zero/core/conf"
 	"github.com/tal-tech/go-zero/zrpc"
 	"log"
+	"points_service/entity"
 	"points_service/rpc/integral"
 	"points_service/rpc/integralclient"
+	"points_service/rpc/internal/config"
+	"points_service/rpc/internal/server"
+	"points_service/rpc/internal/svc"
 	"testing"
 	"time"
 )
 
+//创建orm引擎
+func init() {
+	var  err  error
+	entity.Engine, err = xorm.NewEngine("mysql", "admin:123456@tcp(192.168.3.14:3306)/jyintegral?charset=utf8")
+	entity.Engine.ShowSQL(true)
+	if err != nil {
+		log.Fatal("数据库连接失败:", err)
+	}
+	fmt.Println(c.DataSource+"链接成功")
+}
 func  Test_Consume(t *testing.T)  {
 	ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
 	FileSystem :=integralclient.NewIntegral(zrpc.MustNewClient(c.FileSystemConf))
@@ -17,4 +35,30 @@ func  Test_Consume(t *testing.T)  {
 	res, err := FileSystem.IntegralConsume(ctx, req)
 	log.Println("err ", err)
 	log.Println("req ", res)
+}
+
+func  Test_Add(t *testing.T)  {
+	ctx, _ := context.WithTimeout(context.Background(), 500*time.Second)
+	FileSystem :=integralclient.NewIntegral(zrpc.MustNewClient(c.FileSystemConf))
+	req := &integral.AddReq{UserId: "xzh", CountPoints: 123}
+	res, err := FileSystem.IntegralHarvest(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}
+	fmt.Println(srv.IntegralHarvest(context.Background(), req))
+}
+func  Test_Delete(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: 10}
+	fmt.Println(srv.IntegralConsume(context.Background(), req))
 }

+ 1 - 1
rpc/test/integral.yaml

@@ -10,5 +10,5 @@ Cache:
 FileSystemConf:
   Etcd:
     Hosts:
-      - 127.0.0.1:2379
+      - 127.0.0.1:2380
     Key: integral.rpc

+ 173 - 57
service/integralService.go

@@ -10,36 +10,100 @@ import (
 type IntegralService struct{}
 
 //新增积分流水
-func (service *IntegralService) IntegralAddService(userId, businessType, endDate string, pointType, businessTypeId, point, appId int) (int, string) {
+func (service *IntegralService) IntegralAddService(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.UserId = data.UserId
+	flow.BusinessTypeId = data.BusinessTypeId
+	flow.BusinessType = data.BusinessType
+	flow.PointType=data.PointType
+	flow.Point = data.Point
 	flow.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, "积分余额不足"
+	flow.EndDate = data.EndDate
+	flow.AppId = data.AppId
+	var err error
+	var numb = int64(0)
+	numb, err = orm.Table("integral_flow").Insert(flow)
+	if err != nil && numb == 0 {
+		log.Print("新增流水失败")
+		return entity.ErrorCode, "新增流水失败"
 	}
-	if balance.CountPoints < point {
-		return entity.ErrorCode, "积分余额不足"
+	//结存修改
+	solde := entity.Solde{}
+	solde.AppId = data.AppId
+	solde.UserId = data.UserId
+	solde.EndDate = data.EndDate
+	if (data.OperationType) {
+		//永久积分
+		//先查看是否有EndDate的积分
+		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
+			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{
+			solde.PerManEntPoints = data.Point
+			numb, err = orm.Table("integral_solde").Insert(&solde)
+			if err != nil && numb == 0 {
+				log.Print("新增永久积分失败")
+				return entity.ErrorCode, "新增永久积分失败"
+			}
+		}
+
+	} else {
+		//先查看是否有EndDate的积分
+		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
+			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
+			numb, err = orm.Table("integral_solde").Insert(&solde)
+			if err != nil && numb == 0 {
+				log.Print("新增时效积分失败")
+				return entity.ErrorCode, "新增时效积分失败"
+			}
+		}
+	}
+	//余额修改
+	balanceList := []*entity.Balance{}
+	//查看是否存在本人的余额
+	err = entity.Engine.Table("integral_balance").Where("appId=? and  userId=?  ", data.AppId, data.UserId).Find(&balanceList)
+	if len(balanceList) == 0 {
+		balance := entity.Balance{}
+		balance.UserId = data.UserId
+		balance.AppId = data.AppId
+		balance.CountPoints =  data.Point
+		numb, err = entity.Engine.Table("integral_balance").Insert(&balance)
+		if err != nil && numb == 0 {
+			log.Print("新增余额失败")
+			return entity.ErrorCode, "新增余额失败"
+		}
+	}else{
+		//修改余额
+		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, "余额扣除失败"
+		}
 	}
-	return entity.SuccessCode, "消耗积分成功"
+
+	return entity.SuccessCode, "余额扣除失败"
 }
 
 //消耗积分流水
-func(service *IntegralService) IntegralConsumeService(data entity.FlowJSON) (int,string) {
+func (service *IntegralService) IntegralConsumeService(data entity.FlowJSON) (int, string) {
+
 	orm := entity.Engine
 	flow := entity.Flow{}
 	flow.UserId = data.UserId
@@ -51,37 +115,88 @@ func(service *IntegralService) IntegralConsumeService(data entity.FlowJSON) (int
 	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,"积分余额不足"
+	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)
+	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 && 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)
+	if len(soldelist) == 0 {
+		return entity.ErrorCode, "没有结存可以消耗"
+	}
+	var point = data.Point
+	for _, solde := range soldelist {
+		if point == 0 {
+			break
 		}
-		if balance.CountPoints <data.Point {
-			return entity.ErrorCode,"积分余额不足"
+		if (solde.TimePoints == 0) {
+			//消耗永久积分
+			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)
+				if err != nil && af == 0 {
+					log.Print("消耗永久积分失败")
+					return entity.ErrorCode, "消耗永久积分失败"
+				}
+			}
+		}else{
+			//消耗时效积分
+			if (solde.TimePoints > point) {
+
+				solde.TimePoints = solde.TimePoints - point
+				//够消耗
+				point = 0
+				af, err = entity.Engine.Table("integral_solde").ID(solde.Id).Cols("timePoints").Update(solde)
+				if err != nil && af == 0 {
+					log.Print("消耗时效积分失败")
+					return entity.ErrorCode, "消耗时效积分失败"
+				}
+				break
+			}
+			//不够消耗
+			point = point - solde.TimePoints
+			solde.TimePoints = 0
+			af, err = entity.Engine.Table("integral_solde").ID(solde.Id).Cols("timePoints").Update(solde)
+			if err != nil && af == 0 {
+				log.Print("消耗时效积分失败")
+				return entity.ErrorCode, "消耗时效积分失败"
+			}
 		}
-	}
 
-	af, err := orm.Table("integral_flow").Insert(&flow)
-	if err != nil {
-		log.Print("积分记录失败-积分增减类型:",data.Sort)
-		return entity.ErrorCode,"积分记录失败"
 	}
-	if af >0 {
-		log.Println("积分记录成功-积分增减类型:",data.Sort)
-		return entity.SuccessCode,"积分记录成功"
+	//余额消耗
+	balance.CountPoints = balance.CountPoints - data.Point
+	af, err = entity.Engine.Table("integral_balance").ID(balance.Id).Cols("countPoints").Update(balance)
+	if err != nil && af == 0 {
+		log.Print("余额扣除失败")
+		return entity.ErrorCode, "余额扣除失败"
 	}
-	return entity.SuccessCode,"积分记录成功"
+
+	return entity.SuccessCode, "积分记录成功"
 }
 
 //到期积分查询
-func(service *IntegralService) IntegralExpireCheckService(data entity.ExpireJSON) []entity.Flow {
+func (service *IntegralService) IntegralExpireCheckService(data entity.ExpireJSON) []entity.Flow {
 	orm := entity.Engine
 	var flow []entity.Flow
 	var err error
@@ -100,7 +215,8 @@ func(service *IntegralService) IntegralExpireCheckService(data entity.ExpireJSON
 	}
 	return flow
 }
-//结存新增
+
+/*//结存新增
 func (service *IntegralService) IntegralSoldeService(model entity.SoldeUpdate) (bool, string) {
 	solde := entity.Solde{}
 	solde.AppId = model.AppId
@@ -119,13 +235,13 @@ func (service *IntegralService) IntegralSoldeService(model entity.SoldeUpdate) (
 		}
 		return true, "新增永久积分成功"
 	}
-	//效积分
+	//效积分
 	//先查看是否有EndDate的积分
 	soldelist := []entity.Solde{}
 	err = entity.Engine.Table("integral_solde").Where("appId=? and  userId=? and endDate=? ", model.AppId, model.UserId, model.EndDate).Find(&soldelist)
 	if len(soldelist) > 0 {
 		soldelist[0].TimePoints += model.Points
-		numb, err = entity.Engine.Table("integral_solde").ID(soldelist[0].Id).Cols("timePoints").Update(&soldelist[0])
+		numb, err = entity.Engine.Table("integral_solde").ID(soldelist[0].Id).Cols("timePoints").Update(soldelist[0])
 		if err != nil && numb == 0 {
 			log.Print("修改时效积分失败")
 			return false, "修改时效积分失败"
@@ -160,7 +276,7 @@ func (service *IntegralService) IntegralSoldeReduceService(model entity.SoldeUpd
 					//够消耗
 					point = 0
 					solde.PerManEntPoints = solde.PerManEntPoints - point
-					numb, err = entity.Engine.Table("integral_solde").ID(solde.Id).Cols("perManEntPoints").Update(&solde)
+					numb, err = entity.Engine.Table("integral_solde").ID(solde.Id).Cols("perManEntPoints").Update(solde)
 					if err != nil && numb == 0 {
 						log.Print("消耗永久积分失败")
 						return false, "消耗永久积分失败"
@@ -172,7 +288,7 @@ func (service *IntegralService) IntegralSoldeReduceService(model entity.SoldeUpd
 				//够消耗
 				point = 0
 				solde.TimePoints = solde.TimePoints - point
-				numb, err = entity.Engine.Table("integral_solde").ID(solde.Id).Cols("timePoints").Update(&solde)
+				numb, err = entity.Engine.Table("integral_solde").ID(solde.Id).Cols("timePoints").Update(solde)
 				if err != nil && numb == 0 {
 					log.Print("消耗时效积分失败")
 					return false, "消耗时效积分失败"
@@ -182,7 +298,7 @@ func (service *IntegralService) IntegralSoldeReduceService(model entity.SoldeUpd
 			//不够消耗
 			point = point - solde.TimePoints
 			solde.TimePoints = 0
-			numb, err = entity.Engine.Table("integral_solde").ID(solde.Id).Cols("timePoints").Update(&solde)
+			numb, err = entity.Engine.Table("integral_solde").ID(solde.Id).Cols("timePoints").Update(solde)
 			if err != nil && numb == 0 {
 				log.Print("消耗时效积分失败")
 				return false, "消耗时效积分失败"
@@ -226,26 +342,26 @@ func (service *IntegralService) IntegralBalanceService(model entity.BalanceUpdat
 		return false, "积分余额不足"
 	}
 	balanceList[0].CountPoints = balanceList[0].CountPoints - model.CountPoints
-	numb, err = entity.Engine.Table("integral_balance").ID(balanceList[0].Id).Cols("countPoints").Update(&balanceList[0])
+	numb, err = entity.Engine.Table("integral_balance").ID(balanceList[0].Id).Cols("countPoints").Update(balanceList[0])
 	if err != nil && numb == 0 {
 		log.Print("余额扣除失败")
 		return false, "余额扣除失败"
 	}
 	return true, "余额扣除失败"
 }
-
+*/
 //积分守护
-func(service *IntegralService) IntegralGuardService(data entity.ExpireJSON) []entity.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)+""
+		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).
+		Where("userId = ? AND appId = ? AND endDate < ? "+point+"", data.UserId, data.AppId, data.EndDate).
 		GroupBy("pointType").
 		Find(&flow)
 	if err != nil {
@@ -253,4 +369,4 @@ func(service *IntegralService) IntegralGuardService(data entity.ExpireJSON) []en
 		return nil
 	}
 	return flow
-}
+}