package service import ( "app.yhyue.com/moapp/jyMarketing/entity" "app.yhyue.com/moapp/jyMarketing/rpc/activity" "fmt" _ "github.com/garyburd/redigo/redis" "log" "time" ) type ActivityService struct{} //奖券获取 func (service *ActivityService) LotteryReceive(data *activity.LotteryOperation) (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 { 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.Panicln("查询奖品信息:", err) orm.Rollback() return entity.ErrorCode, "奖品信息查询失败" } if !bool { log.Panicln("查询奖品信息:", err) orm.Rollback() return entity.ErrorCode, "奖品信息查询失败" } //2、先判断奖券是否可以重复领取,在判断之前是否领取过 if prizeData.IsLimitNumber == 1 { //查询之前是否领取过 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.Panicln("查询奖品信息:", err) orm.Rollback() return entity.ErrorCode, "查询领取次数失败" } if len(userLotteryList) >= prizeData.LimitNumber { orm.Rollback() return entity.ErrorCode, "你领取奖券数量已达上限" } } //3、在判断奖券是否够用 //4、领取奖券 userLettry := entity.UserPrize{} userLettry.AppId = data.AppId userLettry.UserId = data.UserId userLettry.PrizeType = 0 userLettry.CreateTime = time.Now() 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) //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.Panicln("领取奖券失败:", err) orm.Rollback() return entity.ErrorCode, "领取奖券失败" } //5、修改奖券余额数量 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.Panicln("修改奖券库存失败:", 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、先查看是否有这张奖券 userLotteryList := []*entity.UserPrize{} err = orm.Table("user_prize"). Where("userId=? and appId=? and endDate>=? and prizeType=0 and lotteryId=? ", data.UserId, data.AppId, nowStr, lotteryId). Asc("createTime").Find(&userLotteryList) if err != nil { log.Println("查询奖券库存失败:", err) orm.Rollback() return entity.ErrorCode, "查询奖券库存失败" } if (len(userLotteryList) == 0) { log.Println(data.UserId, "用户没有ID为", lotteryId, "的奖券") orm.Rollback() return entity.ErrorCode, "该用户没有此此奖券" } //2、奖券状态改为已使用 userLotteryList[0].UseDate = time.Now() userLotteryList[0].PrizeType = 1 numb, err := orm.Table("user_prize").ID(userLotteryList[0].Id).Cols("prizeType", "useDate").Update(userLotteryList[0]) if err != nil || numb == 0 { log.Println("修改用户奖券失败:", err) orm.Rollback() return entity.ErrorCode, "修改用户奖券失败" } //3、修改卷的使用量 lottery := entity.Lottery{} lottery.Id = lotteryId _, err = orm.Exec("UPDATE lottery SET `useNumber` = useNumber+1 WHERE `id` = ?", lotteryId) 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) { orm := entity.Engine.NewSession() defer orm.Close() err := orm.Begin() //1、先查找活动下的奖品Id lotteryJsonList := []entity.LotteryJson{} err = orm.Table("activity").Alias("a"). Select(" p.limitNumber,UseProductList,( select count(up.id) FROM user_prize up where up.lotteryId = l.id AND up.userId ='"+in.UserId+"' ) AS count,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) orm.Rollback() return entity.ErrorCode, "用户单位下的奖券查询失败", lotteryJsonList } orm.Commit() return entity.SuccessCode, "活动下的奖券", lotteryJsonList } //用户下的奖券 func (service *ActivityService) UserLottery(data *activity.Request, model int) (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) if model == 1 { err = orm.Table("user_prize").Alias("up").Select("up.*,a.useProductList"). Join("left", "activity a ", "a.prizeId=up.prizeId").Where("up.userId=? and up.appId=? and up.endDate>=?", data.UserId, data.AppId, nowStr).And("up.prizeType=0").Desc("up.createTime").Find(&userLettryList) } else { /* count,err = orm.Table("user_prize").Alias("up").Select("up.*,a.useProductList"). Join("left", "activity a ", "a.prizeId=up.prizeId").Where("up.userId=? and up.appId=? and up.endDate>=?", data.UserId, data.AppId, nowStr).Limit(int(data.PageSize), (int(data.Page - 1))*int(data.PageSize)).FindAndCount(&userLettryList) */ } if err != nil { log.Println("用户单位下的奖券查询失败:", err) orm.Rollback() return entity.ErrorCode, "用户单位下的奖券查询失败", userLettryList, count } 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