|
@@ -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
|
|
|
+}
|