123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494 |
- package service
- import (
- "app.yhyue.com/moapp/jyMarketing/entity"
- "app.yhyue.com/moapp/jyMarketing/rpc/activity"
- "app.yhyue.com/moapp/jyMarketing/util"
- "fmt"
- _ "github.com/garyburd/redigo/redis"
- "log"
- "time"
- )
- type ActivityService struct{}
- //奖券获取
- func (service *ActivityService) LotteryReceive(data *activity.LotteryOperation, code string) (int64, string) {
- orm := entity.Engine.NewSession()
- defer orm.Close()
- err := orm.Begin()
- bool := true
- if len(data.LotteryIdArr) == 0 {
- return entity.ErrorCode, "没有卷可以领取"
- }
- for _, lotteryId := range data.LotteryIdArr {
- if fool, _ := util.Exists(code, "lottery_"+fmt.Sprint(lotteryId)); !fool {
- orm.Rollback()
- return entity.ErrorCode, "奖卷领取失败"
- }
- log.Println(fmt.Sprint(lotteryId) + "Id奖券领取")
- //每种劵处理
- //1、先查询奖券信息
- prizeData := entity.PrizeJson{}
- bool, err = orm.Table("lottery").
- Alias("l").Select("l.name,l.full,l.reduce, l.prizeId,p.beginDate,p.endDate,p.isLimitNumber,p.limitNumber,p.validityDates,p.validityTimeType").
- Join("left", "prize p", "l.prizeId=p.id").
- Where("l.id=?", lotteryId).Get(&prizeData)
- if err != nil {
- log.Println("查询奖品信息:", err)
- orm.Rollback()
- return entity.ErrorCode, "奖品信息查询失败"
- }
- if !bool {
- log.Println("查询奖品信息:", err)
- orm.Rollback()
- return entity.ErrorCode, "奖品信息查询失败"
- }
- //2、查看活动是否开始
- activity := entity.Activity{}
- bool, err = orm.Table("activity").Where("prizeId=? and state=1", prizeData.PrizeId).Get(&activity)
- if !bool {
- log.Println("查询奖品信息:", err)
- orm.Rollback()
- return entity.ErrorCode, "奖品信息查询失败"
- }
- if !activityTime(activity.BeginDate) {
- log.Println("活动没有开始不可领劵", activity)
- orm.Rollback()
- return entity.ErrorCode, "活动没有开始不可领劵"
- }
- if activityTime(activity.EndDate) {
- log.Println("活动结束不可领劵", activity)
- orm.Rollback()
- return entity.ErrorCode, "活动结束不可领劵"
- }
- //3、先判断奖券是否可以重复领取,在判断之前是否领取过
- userLotteryList := []entity.UserPrize{}
- err = orm.Table("user_prize").
- Where("userId=? and appId=? and lotteryId=? and to_days(createTime) = to_days(now()) ", data.UserId, data.AppId, lotteryId).Find(&userLotteryList)
- if err != nil {
- log.Println("查询奖品信息:", err)
- orm.Rollback()
- return entity.ErrorCode, "查询领取次数失败"
- }
- if len(userLotteryList) >0 {
- orm.Rollback()
- return entity.ErrorCode, "你今日领取奖券数量已达上限"
- }
- if prizeData.IsLimitNumber == 0 {
- //查询之前是否领取过
- userLotteryList := []entity.UserPrize{}
- err = orm.Table("user_prize").
- Where("userId=? and appId=? and lotteryId=?", data.UserId, data.AppId, lotteryId).Find(&userLotteryList)
- if err != nil {
- log.Println("查询奖品信息:", err)
- orm.Rollback()
- return entity.ErrorCode, "查询领取次数失败"
- }
- if len(userLotteryList) >= prizeData.LimitNumber {
- orm.Rollback()
- return entity.ErrorCode, "你领取奖券数量已达上限"
- }
- }
- //5、领取奖券
- userLettry := entity.UserPrize{}
- userLettry.AppId = data.AppId
- userLettry.UserId = data.UserId
- userLettry.PrizeType = 0
- userLettry.CreateTime = time.Now().Local()
- userLettry.LotteryId = lotteryId
- userLettry.PrizeId = prizeData.PrizeId
- userLettry.ValidityDates = prizeData.ValidityDates
- userLettry.Name = prizeData.Name
- userLettry.Full = int64(prizeData.Full)
- userLettry.Reduce = int64(prizeData.Reduce)
- userLettry.UserName = data.UserName
- //0、有起止时间1、当天起几天可用2、次日起几天可用
- switch prizeData.ValidityTimeType {
- case 0:
- userLettry.EndDate = prizeData.EndDate
- userLettry.BeginDate = prizeData.BeginDate
- case 1:
- nowStr := time.Now().Format("2006-01-02")
- userLettry.BeginDate = nowStr
- userLettry.EndDate = service.ObtainAppointTimeString(nowStr, prizeData.ValidityDates)
- case 2:
- nowStr := time.Now().Format("2006-01-02")
- nextDayStr := service.ObtainAppointTimeString(nowStr, 1)
- userLettry.BeginDate = nextDayStr
- userLettry.EndDate = service.ObtainAppointTimeString(nextDayStr, prizeData.ValidityDates)
- }
- numb, err := orm.Table("user_prize").Insert(&userLettry)
- if err != nil || numb == int64(0) {
- log.Println("领取奖券失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "领取奖券失败"
- }
- //6、修改redis余额
- if util.GetInt(code, "lottery_"+fmt.Sprint(lotteryId)) <= 0 {
- log.Println("奖券余额不足:", err)
- orm.Rollback()
- return entity.ErrorCode, "奖券余额不足"
- }
- if !util.DecrbyLimit(code, "lottery_"+fmt.Sprint(lotteryId), 1, 0) {
- log.Println("修改redis奖券库存失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "修改redis奖券库存失败"
- }
- //7、修改奖券余额数量
- lottery := entity.Lottery{}
- lottery.Id = lotteryId
- _, err = orm.Exec("UPDATE lottery SET `stockNumber` = stockNumber-1, `receiveNumber` = receiveNumber+1 WHERE `id` = ?", lotteryId)
- if err != nil {
- log.Println("修改奖券库存失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "修改奖券库存失败"
- }
- }
- orm.Commit()
- return entity.SuccessCode, "奖券领取成功"
- }
- //奖券使用
- func (service *ActivityService) ActivityUse(data *activity.LotteryOperation) (int64, string) {
- orm := entity.Engine.NewSession()
- defer orm.Close()
- err := orm.Begin()
- //nowStr := time.Now().Format("2006-01-02")
- for _, lotteryId := range data.LotteryIdArr {
- //1、先查看是否有这张奖券
- var userLottery entity.UserPrize
- bool := true
- bool, err = orm.Table("user_prize").
- Where("userId=? and appId=? and id=? ", data.UserId, data.AppId, lotteryId).Get(&userLottery)
- if err != nil {
- log.Println("查询奖券库存失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "查询奖券库存失败"
- }
- if !bool {
- log.Println(data.UserId, "用户没有ID为", lotteryId, "的奖券")
- orm.Rollback()
- return entity.ErrorCode, "该用户没有此此奖券"
- }
- //2、奖券是否过期
- if activityTime(userLottery.EndDate) {
- log.Println(data.UserId, "此卷已过期不可使用", userLottery)
- orm.Rollback()
- return entity.ErrorCode, "此卷已过期不可使用"
- }
- //3、奖券状态改为已使用
- userLottery.UseDate = time.Now().Local()
- userLottery.PrizeType = 1
- numb, err := orm.Table("user_prize").ID(userLottery.Id).Cols("prizeType", "useDate").Update(userLottery)
- if err != nil || numb == 0 {
- log.Println("修改用户奖券失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "修改用户奖券失败"
- }
- }
- orm.Commit()
- return entity.SuccessCode, "使用奖券成功"
- }
- //活动下的奖券
- func (service *ActivityService) ActivityLottery(in *activity.Request) (int64, string, []entity.LotteryJson, entity.Activity) {
- orm := entity.Engine.NewSession()
- //1、查找活动信息
- activityJson := entity.Activity{}
- _, err := orm.Table("activity").Alias("a").
- Where("id = ? ", in.ActivityId).Get(&activityJson)
- //2、先查找活动下的奖品Id
- lotteryJsonList := []entity.LotteryJson{}
- err = orm.Table("activity").Alias("a").
- Select("p.prizeType as lotteryType,l.full,l.reduce, p.beginDate,p.endDate,l.stockNumber,l.receiveNumber,p.limitNumber,( select count(up.id) FROM user_prize up where up.lotteryId = l.id AND up.userId ='"+in.UserId+"' ) AS count,( select count(up.id) FROM user_prize up where up.lotteryId = l.id AND up.userId ='"+in.UserId+"' and to_days(createTime) = to_days(now())) AS daycount,p.isLimitNumber,p.validityTimeType,p.validityDates,p.instructions,p.remark,l.id as lotteryId ,l.name ").
- Join("left", "prize p", " a.prizeId = p.Id").
- Join("left", "lottery l", " l.prizeId = p.id ").
- Where("a.id = ? and a.appId=? ", in.ActivityId, in.AppId).Find(&lotteryJsonList)
- if err != nil {
- log.Println("用户下的奖券查询失败:", err)
- return entity.ErrorCode, "用户下的奖券查询失败", lotteryJsonList, activityJson
- }
- for key, value := range lotteryJsonList {
- productList := []entity.ProductJson{}
- var code int64
- in.LotteryId=value.LotteryId
- code,_,productList=service.LotteryProduct(in)
- if (code==0){
- return entity.ErrorCode, "奖券对应商品查询失败", lotteryJsonList, activityJson
- }
- lotteryJsonList[key].UseProductList=productList
- }
- orm.Commit()
- return entity.SuccessCode, "活动下的奖券", lotteryJsonList, activityJson
- }
- //用户下的奖券(不包含待使用的 )
- func (service *ActivityService) UserLottery(data *activity.Request) (int64, string, []entity.UserPrizeJson, int64) {
- orm := entity.Engine.NewSession()
- defer orm.Close()
- err := orm.Begin()
- userLettryList := []entity.UserPrizeJson{}
- nowStr := time.Now().Format("2006-01-02")
- count := int64(0)
- switch data.Model {
- case 1:
- //查看可以使用的奖券(没有过期的)
- condition := ""
- if data.UserLottertId != 0 {
- condition = "( (up.prizeType=0 and up.beginDate<='" + nowStr + "' ) or up.id=" + fmt.Sprint(data.UserLottertId) + ") "
- } else {
- condition = " up.prizeType=0 and up.beginDate<='" + nowStr + "'"
- }
- count, err = orm.Table("user_prize").Alias("up").Select("up.*,p.instructions,p.remark,p.prizeType as lotteryType").
- Join("left", "activity a ", "a.prizeId=up.prizeId").
- Join("left", "prize p ", "a.prizeId=p.Id").
- Where("up.userId=? and up.appId=? and up.endDate>=?", data.UserId, data.AppId, nowStr).
- And(condition).
- Desc("up.createTime").
- FindAndCount(&userLettryList)
- case 0:
- //查看名下所有奖券
- count, err = orm.Table("user_prize").Alias("up").Select("up.*,p.instructions,p.remark,p.prizeType as lotteryType").
- Join("left", "activity a ", "a.prizeId=up.prizeId").
- Join("left", "prize p ", "a.prizeId=p.Id").
- Where("up.userId=? and up.appId=?", data.UserId, data.AppId).Desc("up.createTime").
- Limit(int(data.PageSize), (int(data.Page - 1))*int(data.PageSize)).
- FindAndCount(&userLettryList)
- case 2:
- //过期的奖券
- count, err = orm.Table("user_prize").Alias("up").Select("up.*,p.instructions,p.remark,p.prizeType as lotteryType").
- Join("left", "activity a ", "a.prizeId=up.prizeId").
- Join("left", "prize p ", "a.prizeId=p.Id").
- Where("up.userId=? and up.appId=? ", data.UserId, data.AppId ).
- And("up.prizeType=2").
- Desc("up.createTime").Limit(int(data.PageSize), (int(data.Page - 1))*int(data.PageSize)).
- FindAndCount(&userLettryList)
- case 3:
- //已用的奖券
- count, err = orm.Table("user_prize").Alias("up").Select("up.*,p.instructions,p.remark,p.prizeType as lotteryType").
- Join("left", "activity a ", "a.prizeId=up.prizeId").
- Join("left", "prize p ", "a.prizeId=p.Id").
- Where("up.userId=? and up.appId=? and (up.prizeType=1 or up.prizeType=3)", data.UserId, data.AppId, ).
- Desc("up.createTime").
- Limit(int(data.PageSize), (int(data.Page - 1))*int(data.PageSize)).FindAndCount(&userLettryList)
- case 4:
- //所有未使用奖券
- count, err = orm.Table("user_prize").Alias("up").Select("up.*,p.instructions,p.remark,p.prizeType as lotteryType").
- Join("left", "activity a ", "a.prizeId=up.prizeId").
- Join("left", "prize p ", "a.prizeId=p.Id").
- Where("up.userId=? and up.appId=? and up.endDate>=?", data.UserId, data.AppId, nowStr).
- And("up.prizeType=0").
- Limit(int(data.PageSize), (int(data.Page - 1))*int(data.PageSize)).
- Desc("up.createTime").
- FindAndCount(&userLettryList)
- }
- if err != nil {
- log.Println("用户下的奖券查询失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "用户下的奖券查询失败", userLettryList, count
- }
- for key, value := range userLettryList {
- productList := []entity.ProductJson{}
- var code int64
- data.LotteryId=value.LotteryId
- code,_,productList=service.LotteryProduct(data)
- if (code==0){
- return entity.SuccessCode, "奖券对应商品查询失败", userLettryList, count
- }
- userLettryList[key].UseProductList=productList
- }
- orm.Commit()
- return entity.SuccessCode, "用户下的奖券查询成功", userLettryList, count
- }
- //计算几天之后的时间
- func (service *ActivityService) ObtainAppointTimeString(now string, beApartDay int) string {
- local, _ := time.LoadLocation("Local")
- t, _ := time.ParseInLocation("2006-01-02", now, local)
- stopTime := t.AddDate(0, 0, beApartDay)
- stopTimeStr := stopTime.Format("2006-01-02")
- return stopTimeStr
- }
- //查询过期的奖券改为已过期
- func (service *ActivityService) UpdateLottery(endDate string) (int64, string) {
- orm := entity.Engine.NewSession()
- defer orm.Close()
- err := orm.Begin()
- //1、先查找过期的奖券
- userPrizeList := []entity.UserPrize{}
- err = orm.Table("user_prize").
- Where("endDate<? and (prizeType=0 or prizeType=3) ", endDate).Find(&userPrizeList)
- if err != nil {
- log.Println("查询过期奖券失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "查询过期奖券失败"
- }
- for _, value := range userPrizeList {
- value.PrizeType = 2
- numb, err := orm.Table("user_prize").ID(value.Id).Cols("prizeType").Update(value)
- if err != nil || numb == 0 {
- log.Println("修改奖券使用数量失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "修改奖券使用数量失败"
- }
- }
- orm.Commit()
- return entity.SuccessCode, "过期奖券处理成功"
- }
- //活动创建及修改时间需大于当前时间
- func activityTime(beginDate interface{}) bool {
- timeData := time.Now().Format("2006-01-02")
- if timeData >= fmt.Sprint(beginDate) {
- return true
- }
- return false
- }
- //待使用与未使用状态之间变化
- func (service *ActivityService) LotteryStateChange(data *activity.UpdateStateReq) (int64, string) {
- orm := entity.Engine.NewSession()
- defer orm.Close()
- err := orm.Begin()
- //先查询奖券状态
- var userLottery entity.UserPrize
- bool := true
- bool, err = orm.Table("user_prize").
- Where("userId=? and appId=? and id=? ", data.UserId, data.AppId, data.UserLottertId).
- Asc("createTime").Get(&userLottery)
- if err != nil {
- log.Println("查询奖券库存失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "查询奖券库存失败"
- }
- if !bool {
- log.Println(data.UserId, "用户没有ID为", data.UserLottertId, "的奖券")
- orm.Rollback()
- return entity.ErrorCode, "该用户没有此此奖券"
- }
- //未使用改为待使用
- if data.Model == 3 {
- if (userLottery.PrizeType == 0) {
- //奖券状态改为待使用
- userLottery.PrizeType = 3
- userLottery.OrderCode = data.OrderCode
- userLottery.UserName = data.UserName
- userLottery.UseDate = time.Now().Local()
- numb, err := orm.Table("user_prize").ID(userLottery.Id).Cols("prizeType", "orderCode", "userName", "useDate").Update(userLottery)
- if err != nil || numb == 0 {
- log.Println("修改用户奖券失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "修改用户奖券失败"
- }
- //修改卷的使用量
- _, err = orm.Exec("UPDATE lottery SET `useNumber` = useNumber+1 WHERE `id` = ?", userLottery.LotteryId)
- if err != nil || numb == 0 {
- log.Println("修改奖券使用数量失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "修改奖券使用数量失败"
- }
- orm.Commit()
- return entity.SuccessCode, "修改用户奖券成功"
- } else if userLottery.PrizeType == 3 {
- return entity.SuccessCode, "修改用户奖券成功"
- }
- return entity.ErrorCode, "该奖券状态不可修改"
- } else if data.Model == 0 {
- if (userLottery.PrizeType == 3) {
- //奖券状态改为未使用
- userLottery.PrizeType = 0
- userLottery.OrderCode = ""
- userLottery.UserName = ""
- numb, err := orm.Table("user_prize").ID(userLottery.Id).Cols("prizeType", "orderCode", "userName").Update(userLottery)
- if err != nil || numb == 0 {
- log.Println("修改用户奖券失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "修改用户奖券失败"
- }
- //修改卷的使用量
- _, err = orm.Exec("UPDATE lottery SET `useNumber` = useNumber-1 WHERE `id` = ?", userLottery.LotteryId)
- if err != nil || numb == 0 {
- log.Println("修改奖券使用数量失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "修改奖券使用数量失败"
- }
- orm.Commit()
- return entity.SuccessCode, "修改用户奖券成功"
- } else if (userLottery.PrizeType == 2) {
- orm.Commit()
- return entity.SuccessCode, "修改用户奖券成功"
- }
- return entity.ErrorCode, "该奖券状态不可修改"
- } else {
- //1、奖券是否过期
- if activityTime(userLottery.EndDate) {
- log.Println(data.UserId, "此卷已过期不可使用", userLottery)
- orm.Rollback()
- return entity.ErrorCode, "此卷已过期不可使用"
- }
- //2、奖券状态改为已使用
- userLottery.UseDate = time.Now().Local()
- userLottery.PrizeType = 1
- numb, err := orm.Table("user_prize").ID(userLottery.Id).Cols("prizeType", "useDate").Update(userLottery)
- if err != nil || numb == 0 {
- log.Println("修改用户奖券失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "修改用户奖券失败"
- }
- orm.Commit()
- return entity.SuccessCode, "使用奖券成功"
- return entity.ErrorCode, "该奖券状态不可修改"
- }
- }
- //查询奖券对应的产品信息
- func (service *ActivityService)LotteryProduct(data *activity.Request) (int64, string, []entity.ProductJson) {
- orm := entity.Engine.NewSession()
- productList := []entity.ProductJson{}
- err := orm.Table("product").Alias("pr").
- Select("pr.*").
- Join("left", "activity a ", "FIND_IN_SET( pr.productCode,a.useProductList)").
- Join("left", "prize p ", "a.prizeId=p.Id").
- Join("left","lottery l" ,"l.prizeId=p.Id").
- Where("l.id=?", data.LotteryId).
- Find(&productList)
- if err != nil {
- log.Println("err:", err)
- return entity.ErrorCode, "查询奖券对应的产品信息失败", productList
- }
- return entity.SuccessCode, "查询奖券对应的产品信息成功", productList
- }
- //奖券的基本信息
- func (service *ActivityService)LotteryInfo(data *activity.Request) (int64, string, entity.UserPrizeJson) {
- orm := entity.Engine.NewSession()
- userLettryList := []entity.UserPrizeJson{}
- //已用的奖券
- err := orm.Table("user_prize").Alias("up").Select("up.*,p.instructions,p.remark,p.prizeType as lotteryType").
- Join("left", "activity a ", "a.prizeId=up.prizeId").
- Join("left", "prize p ", "a.prizeId=p.Id").
- Where("up.userId=? and up.appId=? and up.prizeType=0 and up.lotteryId=?",data.UserId,data.AppId,data.LotteryId).
- Find(&userLettryList)
- if err != nil {
- log.Println("err:", err)
- return entity.ErrorCode, "用户下的奖券查询失败", entity.UserPrizeJson{}
- }
- if len(userLettryList)==0{
- return entity.ErrorCode, "用户下的奖券查询失败", entity.UserPrizeJson{}
- }
- productList := []entity.ProductJson{}
- var code int64
- code,_,productList=service.LotteryProduct(data)
- if (code==0){
- return entity.ErrorCode, "查询奖券对应的产品信息失败", entity.UserPrizeJson{}
- }
- userLettryList[0].UseProductList=productList
- return entity.SuccessCode, "用户下的奖券查询成功", userLettryList[0]
- }
|