package service import ( "fmt" "log" "points_service/entity" "time" ) type IntegralService struct{} //新增积分流水 func (service *IntegralService) IntegralAddService(userId, businessType, endDate string, pointType, businessTypeId, point, appId int) (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 = 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, "积分余额不足" } return entity.SuccessCode, "消耗积分成功" } //消耗积分流水 func (service *IntegralService) IntegralConsumeService(userId, businessType, createTime, endDate string, pointType, businessTypeId, point, appId int) (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, "积分余额不足" } af, err := entity.Engine.Table("integral_flow").Insert(&flow) if err != nil && af>0 { log.Print("消耗积分记录失败") return entity.ErrorCode,"消耗积分记录失败" } return entity.SuccessCode, "消耗积分成功" } //结存新增 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, "余额扣除失败" }