package service import ( "fmt" "log" "points_service/entity" "time" ) type IntegralService struct{} //消耗积分流水 func(service *IntegralService) IntegralConsumeService(data entity.FlowJSON) (int,string) { orm := entity.Engine flow := entity.Flow{} 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 0 { 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 flow } //结存新增 func (service *IntegralService) IntegralSoldeService(model entity.SoldeUpdate) (bool,string){ solde := entity.Solde{} solde.AppId = model.AppId solde.UserId = model.UserId solde.EndDate = model.EndDate var err error var numb = int64(0) //新增积分 if (model.PointsType) { //永久积分 solde.PerManEntPoints = model.Points numb, err = entity.Engine.Table("integral_solde").Insert(&solde) if err != nil && numb == 0 { log.Print("新增永久积分失败") return false,"新增永久积分失败" } 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]) if err != nil && numb == 0 { log.Print("修改时效积分失败") return false,"修改时效积分失败" } return true,"修改时效积分成功" } else { solde.TimePoints = model.Points numb, err = entity.Engine.Table("integral_solde").Insert(&solde) if err != nil && numb == 0 { log.Print("新增时效积分失败") return false,"新增时效积分失败" } return true,"新增时效积分成功" } } //结存扣除 func (service *IntegralService) IntegralSoldeReduceService(model entity.SoldeUpdate)(bool,string) { var err error var numb = int64(0) soldelist := []entity.Solde{} err = entity.Engine.Table("integral_solde").Where("appId=? and userId=? and endDate> ? ( perManEntPoints != 0 AND timePoints = 0 ) OR ( perManEntPoints = 0 AND timePoints != 0 )", model.AppId, model.UserId,time.Now().Format("2006/01/02/")).Desc("endDate,timePoints").Find(&soldelist) if len(soldelist) > 0 { var point = model.Points for _, solde := range soldelist { if point == 0 { return true,"积分消耗成功" } if (solde.TimePoints == 0) { //消耗永久积分 if (solde.PerManEntPoints >= point) { //够消耗 point = 0 solde.PerManEntPoints = solde.PerManEntPoints - point numb, err = entity.Engine.Table("integral_solde").ID(solde.Id).Cols("perManEntPoints").Update(&solde) if err != nil && numb == 0 { log.Print("消耗永久积分失败") return false,"消耗永久积分失败" } } } //消耗时效积分 if (solde.TimePoints > point) { //够消耗 point = 0 solde.TimePoints = solde.TimePoints - point numb, err = entity.Engine.Table("integral_solde").ID(solde.Id).Cols("timePoints").Update(&solde) if err != nil && numb == 0 { log.Print("消耗时效积分失败") return false,"消耗时效积分失败" } return true,"消耗时效积分成功" } //不够消耗 point = point - solde.TimePoints solde.TimePoints = 0 numb, err = entity.Engine.Table("integral_solde").ID(solde.Id).Cols("timePoints").Update(&solde) if err != nil && numb == 0 { log.Print("消耗时效积分失败") return false,"消耗时效积分失败" } } } return false,"没有积分可以扣除" } //调整余额 func (service *IntegralService) IntegralBalanceService(model entity.BalanceUpdate) (bool,string) { var err error var numb = int64(0) fmt.Println(numb) balanceList := []*entity.Balance{} //查看是否存在本人的余额 err = entity.Engine.Table("integral_balance").Where("appId=? and userId=? ", model.AppId, model.UserId).Find(&balanceList) if model.Change { //新增 if len(balanceList) == 0 { balance := entity.Balance{} balance.UserId = model.UserId balance.AppId = model.AppId balance.CountPoints = model.CountPoints numb, err = entity.Engine.Table("integral_balance").Insert(&balance) if err != nil && numb == 0 { log.Print("新增余额失败") return false,"新增余额失败" } } //修改余额 balanceList[0].CountPoints = balanceList[0].CountPoints + model.CountPoints } //消耗 if len(balanceList) == 0 { log.Println("没有积分可以扣除") return false,"没有积分可以扣除" } if (balanceList[0].CountPoints < model.CountPoints) { log.Println("积分余额不足") 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]) if err != nil && numb == 0 { log.Print("余额扣除失败") return false,"余额扣除失败" } return true,"余额扣除失败" } //积分守护 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 }