12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016 |
- package service
- import (
- "app.yhyue.com/moapp/jyMarketing/entity"
- "app.yhyue.com/moapp/jyMarketing/rpc/activity"
- C "app.yhyue.com/moapp/jyMarketing/rpc/init"
- "app.yhyue.com/moapp/jyMarketing/util"
- "app.yhyue.com/moapp/jybase/common"
- "fmt"
- _ "github.com/garyburd/redigo/redis"
- "github.com/zeromicro/go-zero/core/logx"
- "strconv"
- "strings"
- "time"
- )
- type ActivityService struct{}
- // LotteryReceive 奖券获取
- func (service *ActivityService) LotteryReceive(data *activity.LotteryOperation) (int64, string) {
- fool := true
- if len(data.LotteryIdArr) == 0 {
- return entity.ErrorCode, "没有卷可以领取"
- }
- logx.Info("传入参数", data)
- for _, lotteryId := range data.LotteryIdArr {
- orm := entity.Engine.NewSession()
- err := orm.Begin()
- defer orm.Close()
- //判断手里的奖券是否使用了
- count, _ := orm.Table("user_prize").Where("prizeId!=0 and userId=? and prizeType!=1 and lotteryId=? ", data.UserId, lotteryId).Count()
- if int64(count) > 0 {
- continue
- }
- if fool, _ := util.Exists(util.CODE, "lottery_"+fmt.Sprint(lotteryId)); !fool {
- continue
- }
- logx.Info(fmt.Sprint(lotteryId) + "Id奖券领取")
- //每种劵处理
- //1、先查询奖券信息
- prizeData := entity.PrizeJson{}
- fool, err = orm.Table("lottery").
- Alias("l").Select("p.isLimitNumber,p.limitNumber,l.discount,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 {
- logx.Info("查询奖品信息:", err)
- orm.Close()
- continue
- }
- if prizeData.IsLimitNumber == 0 {
- if !activityPartakeHandle(prizeData.PrizeId, data.UserId, prizeData.LimitNumber, 0, 1) {
- logx.Info("查询奖品信息:", err)
- logx.Info("查询奖品信息:", data.UserId+"领取达上限")
- orm.Close()
- continue
- }
- }
- if !fool {
- logx.Info("查询奖品信息:", err)
- continue
- }
- //2、查看活动是否开始
- discountJson := entity.DiscountJson{}
- fool, err := orm.Table("activity").Alias("a").Select("a.userRange,a.userGroupId,a.preheatingTime,a.id,a.limitQuantity,a.limitType,a.beginDate,a.endDate").
- Join("left", "discount d", "a.id=d.activityId").
- Where("d.lotteryId=?", lotteryId).Get(&discountJson)
- if !fool {
- logx.Info("查询奖品信息:", err)
- orm.Close()
- continue
- }
- //判断是否在跟组内
- if !UserGroupingHandle(discountJson.UserRange, data.UserId, discountJson.UserGroupId, discountJson.BeginDate, discountJson.EndDate, discountJson.PreheatingTime) {
- continue
- }
- if !util.ActivityTime(discountJson.BeginDate) {
- logx.Info("活动没有开始不可领劵", discountJson)
- orm.Close()
- continue
- }
- if util.ActivityTimeOther(discountJson.EndDate) {
- logx.Info("活动结束不可领劵", discountJson)
- orm.Close()
- continue
- }
- if discountJson.LimitType == 0 {
- if !activityPartakeHandle(discountJson.Id, data.UserId, discountJson.LimitQuantity, discountJson.LimitType, 2) {
- logx.Info("查询奖品信息:", err)
- logx.Info("查询奖品信息:", data.UserId+"领取达上限")
- orm.Close()
- continue
- }
- }
- //6、修改redis余额
- if util.GetInt(util.CODE, "lottery_"+fmt.Sprint(lotteryId)) <= 0 {
- logx.Info("奖券余额不足:", err)
- orm.Close()
- continue
- }
- if !util.DecrbyLimit(util.CODE, "lottery_"+fmt.Sprint(lotteryId), 1, 0) {
- logx.Info("修改redis奖券库存失败:", err)
- orm.Close()
- continue
- }
- //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 {
- logx.Info("修改奖券库存失败:", err)
- orm.Close()
- continue
- }
- //5、领取奖券
- userLottery := entity.UserPrize{}
- userLottery.AppId = data.AppId
- userLottery.UserId = data.UserId
- userLottery.PrizeType = 0
- userLottery.CreateTime = time.Now().Local()
- userLottery.LotteryId = lotteryId
- userLottery.PrizeId = prizeData.PrizeId
- userLottery.ValidityDates = prizeData.ValidityDates
- userLottery.Name = prizeData.Name
- userLottery.Full = int64(prizeData.Full)
- userLottery.Reduce = int64(prizeData.Reduce)
- userLottery.UserName = data.UserName
- userLottery.UserPhone = data.UserPhone
- userLottery.ActivityId = int64(discountJson.Id)
- userLottery.Discount = float64(prizeData.Discount)
- //1、有起止时间1、当天起几天可用2、次日起几天可用
- switch prizeData.ValidityTimeType {
- case 0:
- userLottery.EndDate = prizeData.EndDate
- userLottery.BeginDate = prizeData.BeginDate
- case 1:
- nowStr := time.Now().Format("2006-01-02 15:04:05")
- userLottery.BeginDate = nowStr
- userLottery.EndDate = service.ObtainAppointTimeString(nowStr, prizeData.ValidityDates)
- case 2:
- nowStr := time.Now().Format("2006-01-02 15:04:05")
- nextDayStr := service.ObtainAppointTimeString(nowStr, 1)
- userLottery.BeginDate = nextDayStr
- userLottery.EndDate = service.ObtainAppointTimeString(nextDayStr, prizeData.ValidityDates)
- }
- numb, err := orm.Table("user_prize").Insert(&userLottery)
- if err != nil || numb == int64(0) {
- orm.Close()
- logx.Info("领取奖券失败:", err)
- continue
- //return entity.ErrorCode, "领取奖券失败"
- }
- err = orm.Commit()
- if err != nil {
- return 0, ""
- }
- }
- return entity.SuccessCode, "奖券领取成功"
- }
- // ActivityLottery 活动下的奖券
- func (service *ActivityService) ActivityLottery(in *activity.Request) (int64, string, []entity.LotteryJson, entity.Activity) {
- //1、查找活动信息
- orm := entity.Engine
- activityId := in.ActivityId
- activityType := 0
- receivingLocation := int64(0)
- userRange := int64(0)
- userGroupId := ""
- var lotteryJsonList []entity.LotteryJson
- var err error
- activityJson := entity.Activity{}
- if in.ActivityId == int64(0) {
- var activityList = []entity.Activity{}
- err = orm.Table("activity").Alias("a").Select(" DISTINCT a.*").
- Join("left", "discount d", "d.activityId = a.id").
- Join("left", "product p", "FIND_IN_SET(p.productCode,d.useProductList)").
- Where("a.activityType < 2 and a.state=1 and if ( (a.preheatingTime IS NULL or a.preheatingTime='') , a.beginDate<now() ,a.preheatingTime<now()) and a.endDate>=now() and p.productCode=?", in.ProductCode).Find(&activityList)
- var lotteryJsonList []entity.LotteryJson
- if len(activityList) == 0 {
- return entity.ErrorCode, "当前时段没有活动可以参加", lotteryJsonList, entity.Activity{}
- }
- activityId = activityList[0].Id
- activityType = activityList[0].ActivityType
- activityJson = activityList[0]
- receivingLocation = activityList[0].ReceivingLocation
- userRange = activityList[0].UserRange
- userGroupId = activityList[0].UserGroupId
- } else {
- _, err = orm.Table("activity").Alias("a").
- Where("id = ? ", in.ActivityId).Get(&activityJson)
- activityType = activityJson.ActivityType
- receivingLocation = activityJson.ReceivingLocation
- userRange = activityJson.UserRange
- userGroupId = activityJson.UserGroupId
- }
- logx.Info(receivingLocation)
- //判断是否返回数据
- if receivingLocation != 999 && in.UserId != entity.FilterId {
- if receivingLocation != in.ReceivingLocation {
- return entity.SuccessCode, "活动下的奖券", lotteryJsonList, entity.Activity{}
- }
- }
- // 判断活动是否为全部用户参加
- // 1- 全部 2-分组用户
- userIdTest := in.UserId
- if userRange == int64(2) && in.UserId != entity.FilterId {
- if entity.UserIdMap[in.UserId] != "" {
- userIdTest = entity.UserIdMap[in.UserId]
- }
- //根据用户线索id获取用户所属分组
- exist, err2 := IsInGroup(userIdTest, strings.Split(userGroupId, ","))
- //判断用户是否在分组中
- if err2 != "" {
- logx.Info("获取用户分组查询失败:", err2)
- return entity.SuccessCode, "活动下的奖券", lotteryJsonList, entity.Activity{}
- }
- if !exist {
- logx.Info("用户不在该分组中:", err)
- return entity.SuccessCode, "活动下的奖券", lotteryJsonList, entity.Activity{}
- }
- }
- if activityType < 2 {
- //2、先查找活动下的奖品Id
- err = orm.Table("activity").Alias("a").
- Select("l.discount,( SELECT count( up.id ) FROM user_prize up WHERE up.lotteryId = l.id and up.prizeId!=0 and up.userId ='"+in.UserId+"' and up.prizeType=0 ) AS usercount,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.prizeId!=0 AND up.userId ='"+in.UserId+"' ) AS count,( select count(up.id) FROM user_prize up where up.lotteryId = l.id and up.prizeId!=0 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", "discount d", "d.activityId=a.id and d.state=1").
- Join("left", "prize p", "d.prizeId=p.id").
- Join("left", "lottery l", " l.id = d.lotteryId ").
- Where("a.id = ? and a.appId=? ", activityId, in.AppId).Find(&lotteryJsonList)
- if err != nil {
- logx.Info("用户下的奖券查询失败:", err)
- return entity.ErrorCode, "用户下的奖券查询失败", lotteryJsonList, entity.Activity{}
- }
- }
- var productList []entity.ProductJson
- if len(lotteryJsonList) > 0 {
- in.LotteryId = lotteryJsonList[0].LotteryId
- _, _, productList = ActivityProduct(activityId)
- activityJson.UseProductList = productList
- }
- return entity.SuccessCode, "活动下的奖券", lotteryJsonList, activityJson
- }
- //用户下的奖券(不包含待使用的)
- func (service *ActivityService) UserLottery(data *activity.Request) (int64, string, []entity.UserPrizeJson, int64) {
- orm := entity.Engine
- var err error
- var userLotteryList []entity.UserPrizeJson
- switch data.Model {
- case 1, 4:
- isDistribution := false
- entOrm := entity.EntEngine
- powerList := []map[string]interface{}{}
- if data.EntUserId != 0 {
- err := entOrm.Table("entniche_power").Select("id").Where("ent_user_id=? and status=1", data.EntUserId).Find(&powerList)
- if len(powerList) > 0 || err != nil {
- isDistribution = true
- }
- }
- //查看可以使用的奖券(没有过期的)
- //满减满折查询
- if data.UseProduct != "" {
- data.ProductCode = data.UseProduct
- }
- condition := ""
- if data.UserLottertId != 0 {
- condition = "( (up.prizeType=0 and up.beginDate<=now() ) or up.id=" + fmt.Sprint(data.UserLottertId) + ") "
- } else {
- condition = " up.prizeType=0 and up.beginDate<=now()"
- }
- distributionCondition := ""
- if isDistribution {
- distributionCondition = " and pr.productCode not in (" + C.C.NotAvailableCode + ")"
- }
- err = orm.Table("user_prize").Alias("up").Select(" DISTINCT a.id as activityId, a.name as activityName,a.activityDesc,a.activityType,a.beginDate as activityBeginDate,a.endDate as activityEndDate , up.activityId, d.useProductList as userProduct,up.*,p.instructions,p.remark,p.prizeType as activityType").
- Join("left", "prize p", "up.prizeId=p.id").
- Join("left", "discount d", "up.lotteryId=d.lotteryId and d.state=1").
- Join("left", "activity a ", "d.activityId=a.id").
- Join("left", "product pr ", "FIND_IN_SET(pr.productCode,d.useProductList)"+distributionCondition).
- Where("up.userId=? and up.appId=? and up.prizeId!=0 and up.endDate>=now() and FIND_IN_SET(?,d.useProductList) and p.prizeType<2 and up.prizeType=0", data.UserId, data.AppId, data.ProductCode).
- And(condition).
- Desc("up.createTime").
- Find(&userLotteryList)
- //其他查询
- NoLotteryHandle(data.ProductCode, data.UserId, data.AppId, &userLotteryList, isDistribution)
- LotteryWaitHandle(data.ProductCode, data.UserId, data.AppId, &userLotteryList, isDistribution)
- case 0:
- //查看名下所有奖券
- err = orm.Table("user_prize").Alias("up").Select("DISTINCT up.activityId, d.useProductList as userProduct,up.*,p.instructions,p.remark,p.prizeType as activityType").
- Join("left", "prize p", "up.prizeId=p.id").
- Join("left", "discount d", "up.lotteryId=d.lotteryId and d.state=1").
- Where("up.userId=? and up.appId=? and up.prizeId!=0 and p.prizeType<2", data.UserId, data.AppId).Desc("up.createTime").
- Find(&userLotteryList)
- case 2:
- //过期的奖券
- err = orm.Table("user_prize").Alias("up").Select("DISTINCT up.activityId, d.useProductList as userProduct,up.*,p.instructions,p.remark,p.prizeType as activityType").
- Join("left", "prize p", "up.prizeId=p.id").
- Join("left", "discount d", "up.lotteryId=d.lotteryId and d.state=1").
- Where("up.userId=? and up.appId=? and up.prizeId!=0 and p.prizeType<2", data.UserId, data.AppId).
- And("up.prizeType=2").
- Desc("up.createTime").
- Find(&userLotteryList)
- case 3:
- //已用的奖券
- err = orm.Table("user_prize").Alias("up").Select("DISTINCT up.activityId, d.useProductList as userProduct,up.*,p.instructions,p.remark,p.prizeType as activityType").
- Join("left", "prize p", "up.prizeId=p.id").
- Join("left", "discount d", "up.lotteryId=d.lotteryId and d.state=1").
- Where("up.userId=? and up.appId=? and up.prizeId!=0 and (up.prizeType=1 or up.prizeType=3) and p.prizeType<2", data.UserId, data.AppId).
- Desc("up.createTime").Find(&userLotteryList)
- oneuserPrice := []entity.UserPrizeJson{}
- err = orm.Table("user_prize").Alias("up").Select("DISTINCT up.activityId,a.activityType,d.useProductList as userProduct,d.promotionalPrice,d.discount,d.reduce,d.giftCode,up.*,g.name,g.time,g.timeType").
- Join("left", "discount d", "up.lotteryId=d.id and d.state=1").
- Join("left", "activity a", "up.activityId=a.id").
- Join("left", "gift g", "g.giftCode=d.giftCode").
- Where("up.userId=? and up.appId=? and up.prizeId=0 and (up.prizeType=1 or up.prizeType=3) and a.activityType>=2", data.UserId, data.AppId).
- Desc("up.createTime").Find(&userLotteryList)
- for _, prizeJson := range oneuserPrice {
- userLotteryList = append(userLotteryList, prizeJson)
- }
- }
- if err != nil {
- logx.Info("用户下的奖券查询失败:", err)
- return entity.ErrorCode, "用户下的奖券查询失败", userLotteryList, 0
- }
- startIndex := (data.Page - 1) * data.PageSize
- stopIndex := data.Page * data.PageSize
- if stopIndex > common.Int64All(len(userLotteryList)) {
- stopIndex = common.Int64All(len(userLotteryList))
- }
- var sizeList []entity.UserPrizeJson
- if data.Page == 0 {
- startIndex = 0
- stopIndex = common.Int64All(len(userLotteryList))
- }
- for i := startIndex; i < stopIndex; i++ {
- productList := []entity.ProductJson{}
- var code int64
- code, _, productList = service.LotteryProduct(userLotteryList[i].UserProduct)
- if code == 0 {
- return entity.SuccessCode, "奖券对应商品查询失败", userLotteryList, 0
- }
- userLotteryList[i].UseProductList = productList
- sizeList = append(sizeList, userLotteryList[i])
- }
- return entity.SuccessCode, "用户下的奖券查询成功", sizeList, common.Int64All(len(userLotteryList))
- }
- //计算几天之后的时间 不需要改
- func (service *ActivityService) ObtainAppointTimeString(now string, beApartDay int) string {
- t, _ := time.ParseInLocation("2006-01-02 15:04:05", now, time.Local)
- stopTime := t.AddDate(0, 0, beApartDay)
- stopTimeStr := stopTime.Format("2006-01-02 15:04:05")
- 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) and prizeId!=0 ", endDate).Find(&userPrizeList)
- if err != nil {
- logx.Info("查询过期奖券失败:", 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 {
- logx.Info("修改奖券使用数量失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "修改奖券使用数量失败"
- }
- }
- orm.Commit()
- return entity.SuccessCode, "过期奖券处理成功"
- }
- //待使用与未使用状态之间变化
- func (service *ActivityService) LotteryStateChange(data *activity.UpdateStateReq, code string) (int64, string) {
- orm := entity.Engine.NewSession()
- defer orm.Close()
- err := orm.Begin()
- //买赠卷使用
- if data.DiscountId != 0 {
- fool := true
- discountId := data.DiscountId
- if fool, _ = util.Exists(code, "discount_"+fmt.Sprint(discountId)); !fool {
- orm.Rollback()
- return entity.ErrorCode, "奖卷领取失败"
- }
- logx.Info(fmt.Sprint(discountId) + "Id奖券领取")
- //1、查找活动赠品对应关系信息
- discountJson := entity.DiscountJson{}
- fool, err := orm.Table("activity").Alias("a").
- Select("a.activityId,a.id,a.beginDate,a.endDate,d.giftCode,d.stockNumber,d.useNumber,a.limitQuantity,a.limitType").
- Join("left", "discount d", "a.id=d.activityId").
- Where("d.id=?", data.DiscountId).Get(&discountJson)
- if !fool {
- logx.Info("查询奖品信息:", err)
- orm.Rollback()
- return entity.ErrorCode, "奖品信息查询失败"
- }
- //查询是否可以领取
- if discountJson.LimitType != 0 {
- if !activityPartakeHandle(discountJson.Id, data.UserId, discountJson.LimitQuantity, discountJson.LimitType, 2) {
- return entity.ErrorCode, "活动参与已达上限"
- }
- }
- if !util.ActivityTime(discountJson.BeginDate) {
- logx.Info("活动没有开始不可领劵", discountJson)
- orm.Rollback()
- return entity.ErrorCode, "活动没有开始不可领劵"
- }
- if util.ActivityTimeOther(discountJson.EndDate) {
- logx.Info("活动结束不可领劵", discountJson)
- orm.Rollback()
- return entity.ErrorCode, "活动结束不可领劵"
- }
- //2、修改redis余额
- if util.GetInt(code, "discount_"+fmt.Sprint(discountId)) <= 0 {
- logx.Info("赠品余额不足:", err)
- orm.Rollback()
- return entity.ErrorCode, "奖券余额不足"
- }
- if !util.DecrbyLimit(code, "discount_"+fmt.Sprint(discountId), 1, 0) {
- logx.Info("修改redis赠品库存失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "修改redis奖券库存失败"
- }
- userLottery := entity.UserPrize{}
- userLottery.AppId = data.AppId
- userLottery.UserId = data.UserId
- userLottery.PrizeType = 3
- userLottery.CreateTime = time.Now().Local()
- userLottery.LotteryId = data.DiscountId
- userLottery.UserName = data.UserName
- userLottery.EndDate = discountJson.EndDate
- userLottery.BeginDate = discountJson.BeginDate
- userLottery.OrderCode = data.OrderCode
- userLottery.UseDate = time.Now().Local()
- userLottery.UserPhone = data.UserPhone
- userLottery.ActivityId = discountJson.Id
- userLottery.UserProduct = data.UseProduct
- numb, err := orm.Table("user_prize").Insert(&userLottery)
- if err != nil || numb == int64(0) {
- logx.Info("领取奖券失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "领取奖券失败"
- }
- //3、修改奖券余额数量
- discount := entity.Discount{}
- discount.Id = discountId
- _, err = orm.Exec("UPDATE discount SET `stockNumber` = stockNumber-1,`useNumber` = useNumber+1 WHERE `id` = ?", discountId)
- if err != nil {
- logx.Info("修改奖券库存失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "修改奖券库存失败"
- }
- }
- //先查询奖券状态
- userLotteryIdStr := data.UserLottertId
- if userLotteryIdStr != "" {
- userLotteryIdArr := strings.Split(userLotteryIdStr, ",")
- for _, userLotteryId := range userLotteryIdArr {
- var userLottery entity.UserPrizeJson
- fool := true
- fool, err = orm.Table("user_prize").Alias("u").
- Select("u.*,a.activityType,a.id as activityId").
- Join("left", "activity a", "u.activityId=a.id").
- Where("u.userId=? and u.appId=? and u.id=? ", data.UserId, data.AppId, userLotteryId).
- Asc("createTime").Get(&userLottery)
- if err != nil {
- logx.Info("查询奖券库存失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "查询奖券库存失败"
- }
- if !fool {
- logx.Info(data.UserId, "用户没有ID为", userLotteryId, "的奖券")
- orm.Rollback()
- return entity.ErrorCode, "该用户没有此此奖券"
- }
- //未使用改为待使用
- if data.Model == 3 {
- if userLottery.PrizeType == 0 {
- //奖券状态改为待使用
- _, err := orm.Exec("update user_prize set prizeType=? ,orderCode=?,useDate=?,userProduct=?,userName=?,userPhone=? where id =?",
- 3, data.OrderCode, time.Now().Local(), data.UseProduct, data.UserName, data.UserPhone, userLottery.Id)
- //numb, err := orm.Table("user_prize").ID(userLottery.Id).Cols("prizeType", "orderCode", "useDate", "userProduct", "userName", "userPhone").Update(userLottery)
- if err != nil {
- logx.Info("修改用户奖券失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "修改用户奖券失败"
- }
- //修改卷的使用量
- if userLottery.ActivityType == 2 {
- _, err = orm.Exec("UPDATE discount SET `useNumber` = useNumber+1 WHERE `id` = ?", userLottery.LotteryId)
- } else {
- _, err = orm.Exec("UPDATE lottery SET `useNumber` = useNumber+1 WHERE `id` = ?", userLottery.LotteryId)
- }
- if err != nil {
- logx.Info("修改奖券使用数量失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "修改奖券使用数量失败"
- }
- } else {
- orm.Rollback()
- return entity.ErrorCode, "该奖券状态不可修改"
- }
- } else if data.Model == 0 {
- //奖券状态改为未使用
- if userLottery.PrizeType == 3 {
- //numb, err := orm.Table("user_prize").ID(userLottery.Id).Cols("prizeType", "orderCode").Update(userLottery)
- _, err := orm.Exec("update user_prize set prizeType=? ,orderCode=? where id =?", 0, "", userLottery.Id)
- if err != nil {
- logx.Info("修改用户奖券失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "修改用户奖券失败"
- }
- //修改卷的使用量
- if userLottery.ActivityType >= 2 {
- if util.Incrby(code, "discount_"+fmt.Sprint(userLottery.LotteryId), 1) <= 0 {
- logx.Info("修改redis库存失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "修改redis库存失败"
- }
- _, err = orm.Exec("UPDATE discount SET `useNumber` = useNumber-1,`stockNumber` = stockNumber+1 WHERE `id` = ?", userLottery.LotteryId)
- } else {
- _, err = orm.Exec("UPDATE lottery SET `useNumber` = useNumber-1 WHERE `id` = ?", userLottery.LotteryId)
- }
- if err != nil {
- logx.Info("修改奖券使用数量失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "修改奖券使用数量失败"
- }
- } else {
- orm.Rollback()
- return entity.ErrorCode, "该奖券状态不可修改"
- }
- } else {
- //1、奖券是否过期
- if util.ActivityTimeOther(userLottery.EndDate) {
- logx.Info(data.UserId, "此卷已过期不可使用", userLottery)
- orm.Rollback()
- return entity.ErrorCode, "此卷已过期不可使用"
- }
- //2、奖券状态改为已使用
- //numb, err := orm.Table("user_prize").ID(userLottery.Id).Cols("prizeType", "useDate", "ActivityId", "UserProduct", "orderCode", "UserName", "UserPhone").Update(userLottery)
- _, err := orm.Exec("update user_prize set prizeType=?, useDate=?, ActivityId=?, UserProduct=?, orderCode=?, userName=?, userPhone=? where id =?",
- 1, time.Now().Local(), userLottery.ActivityId, data.UseProduct, data.OrderCode, data.UserName, data.UserPhone, userLottery.Id)
- if err != nil {
- logx.Info("修改用户奖券失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "修改用户奖券失败"
- }
- }
- }
- }
- orm.Commit()
- return entity.SuccessCode, "使用奖券成功"
- }
- //查询对应的产品信息
- func (service *ActivityService) LotteryProduct(useProduct string) (int64, string, []entity.ProductJson) {
- orm := entity.Engine
- productList := []entity.ProductJson{}
- err := orm.Table("product").Alias("pr").
- Select("pr.*").
- Where("FIND_IN_SET( pr.productCode,?)", useProduct).
- Find(&productList)
- if err != nil {
- logx.Info("err:", err)
- return entity.ErrorCode, "查询对应的产品信息失败", productList
- }
- return entity.SuccessCode, "查询对应的产品信息成功", productList
- }
- //奖券的基本信息
- func (service *ActivityService) LotteryInfo(data *activity.Request) (int64, string, entity.UserPrizeJson, bool) {
- orm := entity.Engine
- //var err error
- userLotteryList := []entity.UserPrizeJson{}
- var err error
- isReceive := false
- if data.ActivityType < 2 {
- isReceive = true
- //有卷的查询
- err = orm.Table("user_prize").Alias("up").Select(" a.name as activityName,d.useProductList as userProduct,l.stockNumber ,up.*,p.instructions,p.remark,p.prizeType as lotteryType").
- Join("left", "discount d", "d.lotteryId=up.lotteryId and d.state=1 ").
- Join("left", "prize p ", "d.prizeId=p.Id").
- Join("left", "lottery l ", "l.id=up.lotteryId").
- Join("left", "activity a", "a.id=d.activityId").
- Where("up.userId=? and up.appId=? and up.prizeType=0 and l.id=? ", data.UserId, data.AppId, data.LotteryId).
- Find(&userLotteryList)
- } else {
- //先判断用户是否可以领取
- var activityList = []entity.Activity{}
- err = orm.Table("activity").Alias("a").Select(" DISTINCT a.*").
- Join("left", "discount d", "d.activityId = a.id").
- Where(" a.state=1 and d.id=?", data.LotteryId).Find(&activityList)
- if len(activityList) == 0 {
- return entity.ErrorCode, "奖券信息查询失败", entity.UserPrizeJson{}, isReceive
- }
- for _, activityEntity := range activityList {
- //查看在分组内
- if !UserGroupingHandle(activityEntity.UserRange, data.UserId, activityEntity.UserGroupId, activityEntity.BeginDate, activityEntity.EndDate, activityEntity.PreheatingTime) {
- continue
- }
- //查看活动是否可以参与
- if util.ActivityTime(activityEntity.PreheatingTime) {
- //查询是否可以领取
- if activityEntity.LimitType != 0 {
- if !activityPartakeHandle(activityEntity.Id, data.UserId, activityEntity.LimitQuantity, activityEntity.LimitType, 2) {
- continue
- }
- } else {
- isReceive = true
- }
- } else {
- continue
- }
- }
- //无卷查询
- err = orm.Table("activity").Alias("a").Select("a.name as activityName,d.giftCode,d.stockNumber,d.promotionalPrice,d.discount,d.reduce,g.name,a.beginDate,a.endDate,d.useProductList as userProduct,a.activityType as lotteryType,g.time,g.timeType").
- Join("left", "discount d", "d.activityId = a.id ").
- Join("left", "gift g ", "g.giftCode=d.giftCode").
- Join("left", "product p", "FIND_IN_SET(p.ProductCode,d.useProductList )").
- Where(" d.id=?", data.LotteryId).
- Find(&userLotteryList)
- }
- if len(userLotteryList) == 0 || err != nil {
- logx.Info("err:", err)
- return entity.ErrorCode, "奖券信息查询失败", entity.UserPrizeJson{}, isReceive
- }
- productList := []entity.ProductJson{}
- var code int64
- code, _, productList = service.LotteryProduct(userLotteryList[0].UserProduct)
- if code == 0 {
- return entity.ErrorCode, "查询奖券对应的产品信息失败", entity.UserPrizeJson{}, isReceive
- }
- userLotteryList[0].UseProductList = productList
- return entity.SuccessCode, "用户下的奖券查询成功", userLotteryList[0], isReceive
- }
- //产品是否存在买送奖券
- func (service *ActivityService) GiveInfo(in *activity.Request) (int64, string, []entity.ActivityJson, bool) {
- activityList := []entity.Activity{}
- activityLists := []entity.ActivityJson{}
- orm := entity.Engine
- isReceive := false
- //var err error
- err := orm.Table("activity").Alias("a").Select(" DISTINCT a.*").
- Join("left", "discount d", "d.activityId = a.id").
- Join("left", "product p", "FIND_IN_SET(p.ProductCode,d.useProductList )").
- Where("a.activityType >= 2 and a.state=1 and if ((a.preheatingTime IS NULL or a.preheatingTime='') , a.beginDate<now() ,a.preheatingTime<now()) and a.endDate>=now() and (p.parentCode=? or p.productCode=? )", in.UseProduct, in.UseProduct).Find(&activityList)
- if len(activityList) == 0 {
- logx.Info("用户下的奖券查询失败:", err)
- return entity.SuccessCode, "不存在奖券信息", activityLists, isReceive
- }
- if util.ActivityTime(activityList[0].PreheatingTime) {
- //查询是否可以领取
- if activityList[0].LimitType != 0 {
- if activityPartakeHandle(activityList[0].Id, in.UserId, activityList[0].LimitQuantity, activityList[0].LimitType, 2) {
- isReceive = true
- }
- } else {
- isReceive = true
- }
- }
- userRange := activityList[0].UserRange
- userGroupId := activityList[0].UserGroupId
- // 判断活动是否为全部用户参加
- // 1- 全部 2-分组用户
- userIdTest := in.UserId
- if userRange == int64(2) {
- if entity.UserIdMap[in.UserId] != "" {
- userIdTest = entity.UserIdMap[in.UserId]
- }
- //根据用户线索id获取用户所属分组
- exist, err2 := IsInGroup(userIdTest, strings.Split(userGroupId, ","))
- //判断用户是否在分组中
- if err2 != "" {
- logx.Info("获取用户分组查询失败:", err2)
- return entity.SuccessCode, "获取用户分组出错", []entity.ActivityJson{}, isReceive
- }
- if !exist {
- logx.Info("用户不在该分组中:", err)
- return entity.SuccessCode, "获取用户分组出错", []entity.ActivityJson{}, isReceive
- }
- }
- //先查找产品参与的活动
- err = orm.Table("activity").Alias("a").Select("DISTINCT p.pcPage,p.wxPage,p.appPage,d.stockNumber,a.preheatingTime, a.activityType,a.beginDate,a.endDate,d.promotionalPrice,d.discount,d.reduce,p.productCode,g.giftCode,g.parentCode,a.id as activityId,a.name as activityName,a.activityDesc,g.name,g.time,g.timeType,d.id as discountId").
- Join("left", " discount d", "d.activityId = a.id ").
- Join("left", "gift g ", "g.giftCode=d.giftCode").
- Join("left", "product p", "FIND_IN_SET(p.ProductCode,d.useProductList )").
- Where("(p.parentCode=? or p.productCode=? ) and a.id=? AND a.activityType >= 2 AND a.appId = ? and a.state=1 and d.state=1", in.UseProduct, in.UseProduct, activityList[0].Id, in.AppId).
- Find(&activityLists)
- if err != nil {
- logx.Info("用户下的奖券查询失败:", err)
- return entity.ErrorCode, "用户下的奖券查询失败", activityLists, isReceive
- }
- return entity.SuccessCode, "产品下存在的买送券", activityLists, isReceive
- }
- func (service *ActivityService) GiveActivity(in *activity.Request) (int64, string, entity.Activity) {
- orm := entity.Engine
- activityList := []entity.Activity{}
- err := orm.Table("activity").Alias("a").Select(" a.*,sum(stockNumber) as stockNumber").
- Join("left", "discount d", "d.activityId = a.id").
- Join("left", "product p", "FIND_IN_SET(p.ProductCode,d.useProductList )").
- Where("a.activityType > 2 and a.state=1 and if ( (a.preheatingTime IS NULL or a.preheatingTime='') , a.beginDate<now() ,a.preheatingTime<now()) and a.endDate>=now() and p.parentCode=? ", in.ProductCode).GroupBy("a.id").Find(&activityList)
- if len(activityList) == 0 {
- logx.Info("用户下的奖券查询失败:", err)
- return entity.SuccessCode, "该时段没有限时活动", entity.Activity{}
- } else {
- //库存查询
- return entity.SuccessCode, "", activityList[0]
- }
- }
- //查询赠品与用户绑定关系标识
- func (service *ActivityService) FindUserLotteryId(in *activity.UserLotteryRequest) (int64, string, int64, int64) {
- userPrize := entity.UserPrizeJson{}
- orm := entity.Engine
- //var err error
- //先查找产品参与的活动
- _, err := orm.Table("user_prize").Alias("u").Select("u.id,a.activityType").
- Join("left", "activity a", "u.activityId=a.id").
- Where("u.userId=? AND u.orderCode = ? ", in.UserID, in.OrderCode).
- Get(&userPrize)
- logx.Info(err)
- return entity.SuccessCode, "赠品用户绑定关系", userPrize.Id, userPrize.ActivityType
- }
- type rsDataStr struct {
- PrizeId int64 `xorm:"prizeId" `
- ValidityDates int64 `xorm:"validityDates" `
- BeginDate string `xorm:"beginDate" `
- EndDate string `xorm:"endDate" `
- LotteryId int64 `xorm:"lotteryId" `
- Name string `xorm:"name" `
- Full int64 `xorm:"full" `
- Reduce int64 `xorm:"reduce" `
- Discount float64 `xorm:"discount" `
- ActivityId int64 `xorm:"activityId" `
- ValidityTimeType int64 `xorm:"validityTimeType" `
- DisCountId int64 `xorm:"disCountId" `
- }
- func (service *ActivityService) GetLottery(activityId string) ([]rsDataStr, []entity.ProductJson) {
- orm := entity.Engine
- var err error
- // 2. 根据活动id查询活动下的券及对应信息
- var rsList []rsDataStr
- err = orm.SQL("SELECT d.lotteryId, l.discount, l.NAME, l.FULL, l.reduce, l.prizeId, p.beginDate, p.endDate, p.validityDates, p.validityTimeType, d.id AS disCountId, d.promotionalPrice, a.preheatingTime, d.reduce as timeLimitReduce, d.discount as timeLimitDiscount, d.useProductList FROM activity a LEFT JOIN discount d ON d.activityId = a.id LEFT JOIN prize p ON d.prizeId = p.id LEFT JOIN lottery l ON d.lotteryId = l.id WHERE d.activityId = ? and d.state=1", activityId).Find(&rsList)
- if err != nil {
- logx.Info("获取活动下奖券信息失败err:", err)
- return nil, nil
- }
- i64, _ := strconv.ParseInt(activityId, 10, 64)
- _, _, urlList := ActivityProduct(i64)
- return rsList, urlList
- }
- // LotteryGrantSave 保存信息
- func (service *ActivityService) LotteryGrantSave(userLottery []*entity.UserPrize) (int64, string) {
- orm := entity.SaveEngine.NewSession()
- defer orm.Close()
- err := orm.Begin()
- //5、领取奖券
- numb, err := orm.Table("user_prize").Insert(userLottery)
- if err != nil || numb == int64(0) {
- logx.Info("发放奖券失败:", err)
- return entity.ErrorCode, "发放奖券失败"
- }
- // 修改已发放数量
- lottery := entity.Lottery{}
- lottery.Id = userLottery[0].LotteryId
- _, err = orm.Exec("UPDATE lottery SET `receiveNumber` = receiveNumber+? WHERE `id` = ?", numb, userLottery[0].LotteryId)
- if err != nil {
- logx.Info("修改奖券领取数量失败:", err)
- orm.Rollback()
- return entity.ErrorCode, "修改奖券领取数量失败"
- }
- orm.Commit()
- return entity.SuccessCode, "奖券领取成功"
- }
- //查询对应的产品信息
- func ActivityProduct(activityId int64) (int64, string, []entity.ProductJson) {
- orm := entity.Engine
- productNameList := []entity.ProductJson{}
- err := orm.Table("discount").Alias("d").Select("DISTINCT p.* ").
- Join("left", "product p", "FIND_IN_SET( p.productCode, d.useProductList )").
- Where("d.activityId = ? and d.state=1", activityId).
- Find(&productNameList)
- if err != nil {
- logx.Info("err:", err)
- return entity.ErrorCode, "查询对应的产品信息失败", productNameList
- }
- return entity.SuccessCode, "查询对应的产品信息成功", productNameList
- }
- //查询奖券对应的商品
- func LotteryProduct(activityId int64) (int64, string, []entity.ProductJson) {
- orm := entity.Engine
- productNameList := []entity.ProductJson{}
- err := orm.Table("discount").Alias("d").Select("DISTINCT p.* ").
- Join("left", "product p", "FIND_IN_SET( p.productCode, d.useProductList )").
- Where("d.activityId = ? and d.state=1", activityId).
- Find(&productNameList)
- if err != nil {
- logx.Info("err:", err)
- return entity.ErrorCode, "查询对应的产品信息失败", productNameList
- }
- return entity.SuccessCode, "查询对应的产品信息成功", productNameList
- }
- func IsInGroup(userId string, groupId []string) (bool, string) {
- tmp := []int64{}
- var exist bool
- orm := entity.TiEngine
- err := orm.Select("count(*)").Table("customers_user").Alias("u").Where("u.userid=?", userId).Join("left", "groupcustomers as g", "g.customerid = u.customerid").In("g.groupid", groupId).Find(&tmp)
- if err != nil {
- logx.Info("查询用户是否在分组中失败", err)
- return false, "查询用户是否在分组中失败"
- }
- if len(tmp) > 0 && tmp[0] > 0 {
- exist = true
- }
- return exist, ""
- }
- //活动参与数量是否达上限
- func activityPartakeHandle(id int64, userId string, limitQuantity, limitType, findType int64) bool {
- orm := entity.Engine.NewSession()
- count := int64(0)
- if findType == 1 {
- //查询某个奖券领取数量
- count, _ = orm.Table("user_prize").Where("prizeId=? and userId=? ", id, userId).Count()
- if int64(count) >= limitQuantity {
- return false
- }
- return true
- }
- //活动是否已达上限
- switch limitType {
- case 1:
- //查询某个活动领取数量
- count, _ = orm.Table("user_prize").Where("activityId=? and userId=? and prizeType!=0 ", id, userId).Count()
- if int64(count) >= limitQuantity {
- return false
- }
- break
- case 2:
- //查询整个活动今天领取数量
- count, _ = orm.Table("user_prize").Where("activityId=? and to_days(createTime)=to_days(now()) and userId=? and prizeType!=0", id, userId).Count()
- if int64(count) >= limitQuantity {
- return false
- }
- break
- }
- return true
- }
- //无卷处理
- func NoLotteryHandle(productCode, userId, appId string, userPrice *[]entity.UserPrizeJson, isDistribution bool) {
- productQuery := ""
- if productCode != "" {
- productQuery += " and ( p.productCode='" + productCode + "' )"
- }
- if isDistribution {
- productQuery += " and ( p.parentCode not in (" + C.C.NotAvailableCode + "))"
- }
- orm := entity.Engine.NewSession()
- //var err error
- //没有商品标识时处理
- // 判断活动是否为全部用户参加
- activityList := []entity.Activity{}
- err := orm.Table("activity").Alias("a").Select(" DISTINCT a.*").
- Join("left", "discount d", "d.activityId = a.id").
- Join("left", "product p", "FIND_IN_SET(p.ProductCode,d.useProductList )").
- Where("a.activityType >= 2 and a.state=1 and if ((a.preheatingTime IS NULL or a.preheatingTime='') , a.beginDate<now() ,a.preheatingTime<now()) and a.endDate>=now() " + productQuery).
- Asc("a.id").Find(&activityList)
- if len(activityList) == 0 {
- return
- }
- for _, value := range activityList {
- //分组判断处理
- if !UserGroupingHandle(value.UserRange, userId, value.UserGroupId, value.BeginDate, value.EndDate, value.PreheatingTime) {
- continue
- }
- //预热已经开始
- if util.ActivityTime(value.PreheatingTime) || util.ActivityTime(value.BeginDate) {
- //查询是否可以领取
- if value.LimitType != 0 {
- if !activityPartakeHandle(value.Id, userId, value.LimitQuantity, value.LimitType, 2) {
- continue
- }
- }
- }
- oneuserPrice := []entity.UserPrizeJson{}
- err = orm.Table("activity").Alias("a").Select("DISTINCT a.id as activityId, a.name as activityName,a.beginDate as activityBeginDate,a.endDate as activityEndDate ,d.stockNumber, a.activityType,a.beginDate ,a.endDate ,d.promotionalPrice,d.discount,d.reduce,d.useProductList as userProduct,a.preheatingTime,g.giftCode ,a.activityDesc,g.name,g.time,g.timeType,d.id as lotteryId").
- Join("left", " discount d", "d.activityId = a.id ").
- Join("left", "gift g ", "g.giftCode=d.giftCode").
- Join("left", "product p", "FIND_IN_SET(p.ProductCode,d.useProductList )").
- Where(" a.id=? "+productQuery+" AND a.activityType >= 2 AND a.appId = ? and a.state=1 and d.state=1 ", value.Id, appId).
- Find(&oneuserPrice)
- //先查找产品参与的活动
- if err != nil {
- logx.Info("用户下的奖券查询失败:", err)
- continue
- }
- for _, prizeJson := range oneuserPrice {
- *userPrice = append(*userPrice, prizeJson)
- }
- }
- }
- func LotteryWaitHandle(productCode, userId, appId string, userPrice *[]entity.UserPrizeJson, isDistribution bool) {
- productQuery := " "
- if productCode != "" {
- productQuery = " and ( p.productCode='" + productCode + "' )"
- }
- if isDistribution {
- productQuery += "and ( p.parentCode not in (" + C.C.NotAvailableCode + "))"
- }
- orm := entity.Engine.NewSession()
- //var err error
- //没有商品标识时处理
- // 判断活动是否为全部用户参加
- activityList := []entity.Activity{}
- err := orm.Table("activity").Alias("a").Select(" DISTINCT a.*").
- Join("left", "discount d", "d.activityId = a.id").
- Join("left", "product p", "FIND_IN_SET(p.ProductCode,d.useProductList )").
- Where("a.activityType < 2 and a.state=1 and a.preheatingTime is not null and a.preheatingTime!='' and a.preheatingTime<now() and a.beginDate>now() " + productQuery).Asc("a.id").Find(&activityList)
- if len(activityList) == 0 {
- return
- }
- for _, value := range activityList {
- //分组判断处理
- if !UserGroupingHandle(value.UserRange, userId, value.UserGroupId, value.BeginDate, value.EndDate, value.PreheatingTime) {
- continue
- }
- //预热已经开始
- if !util.ActivityTime(value.PreheatingTime) || util.ActivityTime(value.BeginDate) {
- continue
- }
- oneuserPrice := []entity.UserPrizeJson{}
- err = orm.Table("activity").Alias("a").Select("DISTINCT a.id as activityId, a.name as activityName,a.beginDate as activityBeginDate,a.endDate as activityEndDate ,d.stockNumber, a.activityType,a.beginDate ,a.endDate,l.full ,d.promotionalPrice,l.discount,l.reduce,d.useProductList as userProduct,a.preheatingTime,a.activityDesc,d.id as lotteryId").
- Join("left", " discount d", "d.activityId = a.id ").
- Join("left", "lottery l ", "l.id=d.lotteryId").
- Join("left", "product p", "FIND_IN_SET(p.ProductCode,d.useProductList )").
- Where(" a.id=? "+productQuery+" AND a.activityType < 2 AND a.appId = ? and a.state=1 and d.state=1 ", value.Id, appId).
- Find(&oneuserPrice)
- //先查找产品参与的活动
- if err != nil {
- logx.Info("用户下的奖券查询失败:", err)
- continue
- }
- for _, prizeJson := range oneuserPrice {
- *userPrice = append(*userPrice, prizeJson)
- }
- }
- }
- //有券预热开始活动未开始
- //分组计算
- func UserGroupingHandle(userRange int64, userId, userGroupId string, startTime, endTime, preheatingTime string) bool {
- if userRange == int64(2) {
- if entity.UserIdMap[userId] != "" {
- userId = entity.UserIdMap[userId]
- }
- //根据用户线索id获取用户所属分组
- exist, err2 := IsInGroup(userId, strings.Split(userGroupId, ","))
- //判断用户是否在分组中
- if err2 != "" {
- logx.Info("用户不在该分组中:", err2)
- return false
- }
- if !exist {
- logx.Info("用户不在该分组中:")
- return false
- }
- } else if userRange == 3 {
- //新用户判断
- stime := ""
- etime := endTime
- //新用户
- if preheatingTime == "" {
- stime = startTime
- } else {
- stime = preheatingTime
- }
- sd, _ := time.ParseInLocation("2006-01-02 15:04:05", stime, time.Local)
- ed, _ := time.ParseInLocation("2006-01-02 15:04:05", etime, time.Local)
- data, _ok := entity.Mongo.FindById("user", userId, `{"l_registedate":1}`)
- if !_ok || data == nil || len((*data)) == 0 {
- return false
- }
- l_registedate := common.Int64All((*data)["l_registedate"])
- if !(l_registedate >= sd.Unix() && l_registedate <= ed.Unix()) {
- return false
- }
- }
- return true
- }
|