activityService.go 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jyMarketing/entity"
  4. "app.yhyue.com/moapp/jyMarketing/rpc/activity"
  5. C "app.yhyue.com/moapp/jyMarketing/rpc/init"
  6. "app.yhyue.com/moapp/jyMarketing/util"
  7. "app.yhyue.com/moapp/jybase/common"
  8. "fmt"
  9. _ "github.com/garyburd/redigo/redis"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. type ActivityService struct{}
  16. // LotteryReceive 奖券获取
  17. func (service *ActivityService) LotteryReceive(data *activity.LotteryOperation) (int64, string) {
  18. fool := true
  19. if len(data.LotteryIdArr) == 0 {
  20. return entity.ErrorCode, "没有卷可以领取"
  21. }
  22. logx.Info("传入参数", data)
  23. for _, lotteryId := range data.LotteryIdArr {
  24. orm := entity.Engine.NewSession()
  25. err := orm.Begin()
  26. defer orm.Close()
  27. //判断手里的奖券是否使用了
  28. count, _ := orm.Table("user_prize").Where("prizeId!=0 and userId=? and prizeType!=1 and lotteryId=? ", data.UserId, lotteryId).Count()
  29. if int64(count) > 0 {
  30. continue
  31. }
  32. if fool, _ := util.Exists(util.CODE, "lottery_"+fmt.Sprint(lotteryId)); !fool {
  33. continue
  34. }
  35. logx.Info(fmt.Sprint(lotteryId) + "Id奖券领取")
  36. //每种劵处理
  37. //1、先查询奖券信息
  38. prizeData := entity.PrizeJson{}
  39. fool, err = orm.Table("lottery").
  40. 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").
  41. Join("left", "prize p", "l.prizeId=p.id").
  42. Where("l.id=?", lotteryId).Get(&prizeData)
  43. if err != nil {
  44. logx.Info("查询奖品信息:", err)
  45. orm.Close()
  46. continue
  47. }
  48. if prizeData.IsLimitNumber == 0 {
  49. if !activityPartakeHandle(prizeData.PrizeId, data.UserId, prizeData.LimitNumber, 0, 1) {
  50. logx.Info("查询奖品信息:", err)
  51. logx.Info("查询奖品信息:", data.UserId+"领取达上限")
  52. orm.Close()
  53. continue
  54. }
  55. }
  56. if !fool {
  57. logx.Info("查询奖品信息:", err)
  58. continue
  59. }
  60. //2、查看活动是否开始
  61. discountJson := entity.DiscountJson{}
  62. fool, err := orm.Table("activity").Alias("a").Select("a.userRange,a.userGroupId,a.preheatingTime,a.id,a.limitQuantity,a.limitType,a.beginDate,a.endDate").
  63. Join("left", "discount d", "a.id=d.activityId").
  64. Where("d.lotteryId=?", lotteryId).Get(&discountJson)
  65. if !fool {
  66. logx.Info("查询奖品信息:", err)
  67. orm.Close()
  68. continue
  69. }
  70. //判断是否在跟组内
  71. if !UserGroupingHandle(discountJson.UserRange, data.UserId, discountJson.UserGroupId, discountJson.BeginDate, discountJson.EndDate, discountJson.PreheatingTime) {
  72. continue
  73. }
  74. if !util.ActivityTime(discountJson.BeginDate) {
  75. logx.Info("活动没有开始不可领劵", discountJson)
  76. orm.Close()
  77. continue
  78. }
  79. if util.ActivityTimeOther(discountJson.EndDate) {
  80. logx.Info("活动结束不可领劵", discountJson)
  81. orm.Close()
  82. continue
  83. }
  84. if discountJson.LimitType == 0 {
  85. if !activityPartakeHandle(discountJson.Id, data.UserId, discountJson.LimitQuantity, discountJson.LimitType, 2) {
  86. logx.Info("查询奖品信息:", err)
  87. logx.Info("查询奖品信息:", data.UserId+"领取达上限")
  88. orm.Close()
  89. continue
  90. }
  91. }
  92. //6、修改redis余额
  93. if util.GetInt(util.CODE, "lottery_"+fmt.Sprint(lotteryId)) <= 0 {
  94. logx.Info("奖券余额不足:", err)
  95. orm.Close()
  96. continue
  97. }
  98. if !util.DecrbyLimit(util.CODE, "lottery_"+fmt.Sprint(lotteryId), 1, 0) {
  99. logx.Info("修改redis奖券库存失败:", err)
  100. orm.Close()
  101. continue
  102. }
  103. //7、修改奖券余额数量
  104. lottery := entity.Lottery{}
  105. lottery.Id = lotteryId
  106. _, err = orm.Exec("UPDATE lottery SET `stockNumber` = stockNumber-1, `receiveNumber` = receiveNumber+1 WHERE `id` = ?", lotteryId)
  107. if err != nil {
  108. logx.Info("修改奖券库存失败:", err)
  109. orm.Close()
  110. continue
  111. }
  112. //5、领取奖券
  113. userLottery := entity.UserPrize{}
  114. userLottery.AppId = data.AppId
  115. userLottery.UserId = data.UserId
  116. userLottery.PrizeType = 0
  117. userLottery.CreateTime = time.Now().Local()
  118. userLottery.LotteryId = lotteryId
  119. userLottery.PrizeId = prizeData.PrizeId
  120. userLottery.ValidityDates = prizeData.ValidityDates
  121. userLottery.Name = prizeData.Name
  122. userLottery.Full = int64(prizeData.Full)
  123. userLottery.Reduce = int64(prizeData.Reduce)
  124. userLottery.UserName = data.UserName
  125. userLottery.UserPhone = data.UserPhone
  126. userLottery.ActivityId = int64(discountJson.Id)
  127. userLottery.Discount = float64(prizeData.Discount)
  128. //1、有起止时间1、当天起几天可用2、次日起几天可用
  129. switch prizeData.ValidityTimeType {
  130. case 0:
  131. userLottery.EndDate = prizeData.EndDate
  132. userLottery.BeginDate = prizeData.BeginDate
  133. case 1:
  134. nowStr := time.Now().Format("2006-01-02 15:04:05")
  135. userLottery.BeginDate = nowStr
  136. userLottery.EndDate = service.ObtainAppointTimeString(nowStr, prizeData.ValidityDates)
  137. case 2:
  138. nowStr := time.Now().Format("2006-01-02 15:04:05")
  139. nextDayStr := service.ObtainAppointTimeString(nowStr, 1)
  140. userLottery.BeginDate = nextDayStr
  141. userLottery.EndDate = service.ObtainAppointTimeString(nextDayStr, prizeData.ValidityDates)
  142. }
  143. numb, err := orm.Table("user_prize").Insert(&userLottery)
  144. if err != nil || numb == int64(0) {
  145. orm.Close()
  146. logx.Info("领取奖券失败:", err)
  147. continue
  148. //return entity.ErrorCode, "领取奖券失败"
  149. }
  150. err = orm.Commit()
  151. if err != nil {
  152. return 0, ""
  153. }
  154. }
  155. return entity.SuccessCode, "奖券领取成功"
  156. }
  157. // ActivityLottery 活动下的奖券
  158. func (service *ActivityService) ActivityLottery(in *activity.Request) (int64, string, []entity.LotteryJson, entity.Activity) {
  159. //1、查找活动信息
  160. orm := entity.Engine
  161. activityId := in.ActivityId
  162. activityType := 0
  163. receivingLocation := int64(0)
  164. userRange := int64(0)
  165. userGroupId := ""
  166. var lotteryJsonList []entity.LotteryJson
  167. var err error
  168. activityJson := entity.Activity{}
  169. if in.ActivityId == int64(0) {
  170. var activityList = []entity.Activity{}
  171. err = orm.Table("activity").Alias("a").Select(" DISTINCT a.*").
  172. Join("left", "discount d", "d.activityId = a.id").
  173. Join("left", "product p", "FIND_IN_SET(p.productCode,d.useProductList)").
  174. 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)
  175. var lotteryJsonList []entity.LotteryJson
  176. if len(activityList) == 0 {
  177. return entity.ErrorCode, "当前时段没有活动可以参加", lotteryJsonList, entity.Activity{}
  178. }
  179. activityId = activityList[0].Id
  180. activityType = activityList[0].ActivityType
  181. activityJson = activityList[0]
  182. receivingLocation = activityList[0].ReceivingLocation
  183. userRange = activityList[0].UserRange
  184. userGroupId = activityList[0].UserGroupId
  185. } else {
  186. _, err = orm.Table("activity").Alias("a").
  187. Where("id = ? ", in.ActivityId).Get(&activityJson)
  188. activityType = activityJson.ActivityType
  189. receivingLocation = activityJson.ReceivingLocation
  190. userRange = activityJson.UserRange
  191. userGroupId = activityJson.UserGroupId
  192. }
  193. logx.Info(receivingLocation)
  194. //判断是否返回数据
  195. if receivingLocation != 999 && in.UserId != entity.FilterId {
  196. if receivingLocation != in.ReceivingLocation {
  197. return entity.SuccessCode, "活动下的奖券", lotteryJsonList, entity.Activity{}
  198. }
  199. }
  200. // 判断活动是否为全部用户参加
  201. // 1- 全部 2-分组用户
  202. userIdTest := in.UserId
  203. if userRange == int64(2) && in.UserId != entity.FilterId {
  204. if entity.UserIdMap[in.UserId] != "" {
  205. userIdTest = entity.UserIdMap[in.UserId]
  206. }
  207. //根据用户线索id获取用户所属分组
  208. exist, err2 := IsInGroup(userIdTest, strings.Split(userGroupId, ","))
  209. //判断用户是否在分组中
  210. if err2 != "" {
  211. logx.Info("获取用户分组查询失败:", err2)
  212. return entity.SuccessCode, "活动下的奖券", lotteryJsonList, entity.Activity{}
  213. }
  214. if !exist {
  215. logx.Info("用户不在该分组中:", err)
  216. return entity.SuccessCode, "活动下的奖券", lotteryJsonList, entity.Activity{}
  217. }
  218. }
  219. if activityType < 2 {
  220. //2、先查找活动下的奖品Id
  221. err = orm.Table("activity").Alias("a").
  222. 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 ").
  223. Join("left", "discount d", "d.activityId=a.id and d.state=1").
  224. Join("left", "prize p", "d.prizeId=p.id").
  225. Join("left", "lottery l", " l.id = d.lotteryId ").
  226. Where("a.id = ? and a.appId=? ", activityId, in.AppId).Find(&lotteryJsonList)
  227. if err != nil {
  228. logx.Info("用户下的奖券查询失败:", err)
  229. return entity.ErrorCode, "用户下的奖券查询失败", lotteryJsonList, entity.Activity{}
  230. }
  231. }
  232. var productList []entity.ProductJson
  233. if len(lotteryJsonList) > 0 {
  234. in.LotteryId = lotteryJsonList[0].LotteryId
  235. _, _, productList = ActivityProduct(activityId)
  236. activityJson.UseProductList = productList
  237. }
  238. return entity.SuccessCode, "活动下的奖券", lotteryJsonList, activityJson
  239. }
  240. //用户下的奖券(不包含待使用的)
  241. func (service *ActivityService) UserLottery(data *activity.Request) (int64, string, []entity.UserPrizeJson, int64) {
  242. orm := entity.Engine
  243. var err error
  244. var userLotteryList []entity.UserPrizeJson
  245. switch data.Model {
  246. case 1, 4:
  247. isDistribution := false
  248. entOrm := entity.EntEngine
  249. powerList := []map[string]interface{}{}
  250. if data.EntUserId != 0 {
  251. err := entOrm.Table("entniche_power").Select("id").Where("ent_user_id=? and status=1", data.EntUserId).Find(&powerList)
  252. if len(powerList) > 0 || err != nil {
  253. isDistribution = true
  254. }
  255. }
  256. //查看可以使用的奖券(没有过期的)
  257. //满减满折查询
  258. if data.UseProduct != "" {
  259. data.ProductCode = data.UseProduct
  260. }
  261. condition := ""
  262. if data.UserLottertId != 0 {
  263. condition = "( (up.prizeType=0 and up.beginDate<=now() ) or up.id=" + fmt.Sprint(data.UserLottertId) + ") "
  264. } else {
  265. condition = " up.prizeType=0 and up.beginDate<=now()"
  266. }
  267. distributionCondition := ""
  268. if isDistribution {
  269. distributionCondition = " and pr.productCode not in (" + C.C.NotAvailableCode + ")"
  270. }
  271. 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").
  272. Join("left", "prize p", "up.prizeId=p.id").
  273. Join("left", "discount d", "up.lotteryId=d.lotteryId and d.state=1").
  274. Join("left", "activity a ", "d.activityId=a.id").
  275. Join("left", "product pr ", "FIND_IN_SET(pr.productCode,d.useProductList)"+distributionCondition).
  276. 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).
  277. And(condition).
  278. Desc("up.createTime").
  279. Find(&userLotteryList)
  280. //其他查询
  281. NoLotteryHandle(data.ProductCode, data.UserId, data.AppId, &userLotteryList, isDistribution)
  282. LotteryWaitHandle(data.ProductCode, data.UserId, data.AppId, &userLotteryList, isDistribution)
  283. case 0:
  284. //查看名下所有奖券
  285. err = orm.Table("user_prize").Alias("up").Select("DISTINCT up.activityId, d.useProductList as userProduct,up.*,p.instructions,p.remark,p.prizeType as activityType").
  286. Join("left", "prize p", "up.prizeId=p.id").
  287. Join("left", "discount d", "up.lotteryId=d.lotteryId and d.state=1").
  288. Where("up.userId=? and up.appId=? and up.prizeId!=0 and p.prizeType<2", data.UserId, data.AppId).Desc("up.createTime").
  289. Find(&userLotteryList)
  290. case 2:
  291. //过期的奖券
  292. err = orm.Table("user_prize").Alias("up").Select("DISTINCT up.activityId, d.useProductList as userProduct,up.*,p.instructions,p.remark,p.prizeType as activityType").
  293. Join("left", "prize p", "up.prizeId=p.id").
  294. Join("left", "discount d", "up.lotteryId=d.lotteryId and d.state=1").
  295. Where("up.userId=? and up.appId=? and up.prizeId!=0 and p.prizeType<2", data.UserId, data.AppId).
  296. And("up.prizeType=2").
  297. Desc("up.createTime").
  298. Find(&userLotteryList)
  299. case 3:
  300. //已用的奖券
  301. err = orm.Table("user_prize").Alias("up").Select("DISTINCT up.activityId, d.useProductList as userProduct,up.*,p.instructions,p.remark,p.prizeType as activityType").
  302. Join("left", "prize p", "up.prizeId=p.id").
  303. Join("left", "discount d", "up.lotteryId=d.lotteryId and d.state=1").
  304. 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).
  305. Desc("up.createTime").Find(&userLotteryList)
  306. oneuserPrice := []entity.UserPrizeJson{}
  307. 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").
  308. Join("left", "discount d", "up.lotteryId=d.id and d.state=1").
  309. Join("left", "activity a", "up.activityId=a.id").
  310. Join("left", "gift g", "g.giftCode=d.giftCode").
  311. 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).
  312. Desc("up.createTime").Find(&userLotteryList)
  313. for _, prizeJson := range oneuserPrice {
  314. userLotteryList = append(userLotteryList, prizeJson)
  315. }
  316. }
  317. if err != nil {
  318. logx.Info("用户下的奖券查询失败:", err)
  319. return entity.ErrorCode, "用户下的奖券查询失败", userLotteryList, 0
  320. }
  321. startIndex := (data.Page - 1) * data.PageSize
  322. stopIndex := data.Page * data.PageSize
  323. if stopIndex > common.Int64All(len(userLotteryList)) {
  324. stopIndex = common.Int64All(len(userLotteryList))
  325. }
  326. var sizeList []entity.UserPrizeJson
  327. if data.Page == 0 {
  328. startIndex = 0
  329. stopIndex = common.Int64All(len(userLotteryList))
  330. }
  331. for i := startIndex; i < stopIndex; i++ {
  332. productList := []entity.ProductJson{}
  333. var code int64
  334. code, _, productList = service.LotteryProduct(userLotteryList[i].UserProduct)
  335. if code == 0 {
  336. return entity.SuccessCode, "奖券对应商品查询失败", userLotteryList, 0
  337. }
  338. userLotteryList[i].UseProductList = productList
  339. sizeList = append(sizeList, userLotteryList[i])
  340. }
  341. return entity.SuccessCode, "用户下的奖券查询成功", sizeList, common.Int64All(len(userLotteryList))
  342. }
  343. //计算几天之后的时间 不需要改
  344. func (service *ActivityService) ObtainAppointTimeString(now string, beApartDay int) string {
  345. t, _ := time.ParseInLocation("2006-01-02 15:04:05", now, time.Local)
  346. stopTime := t.AddDate(0, 0, beApartDay)
  347. stopTimeStr := stopTime.Format("2006-01-02 15:04:05")
  348. return stopTimeStr
  349. }
  350. //查询过期的奖券改为已过期 不需要改
  351. func (service *ActivityService) UpdateLottery(endDate string) (int64, string) {
  352. orm := entity.Engine.NewSession()
  353. defer orm.Close()
  354. err := orm.Begin()
  355. //1、先查找过期的奖券
  356. userPrizeList := []entity.UserPrize{}
  357. err = orm.Table("user_prize").
  358. Where("endDate<? and (prizeType=0 or prizeType=3) and prizeId!=0 ", endDate).Find(&userPrizeList)
  359. if err != nil {
  360. logx.Info("查询过期奖券失败:", err)
  361. orm.Rollback()
  362. return entity.ErrorCode, "查询过期奖券失败"
  363. }
  364. for _, value := range userPrizeList {
  365. value.PrizeType = 2
  366. numb, err := orm.Table("user_prize").ID(value.Id).Cols("prizeType").Update(value)
  367. if err != nil || numb == 0 {
  368. logx.Info("修改奖券使用数量失败:", err)
  369. orm.Rollback()
  370. return entity.ErrorCode, "修改奖券使用数量失败"
  371. }
  372. }
  373. orm.Commit()
  374. return entity.SuccessCode, "过期奖券处理成功"
  375. }
  376. //待使用与未使用状态之间变化
  377. func (service *ActivityService) LotteryStateChange(data *activity.UpdateStateReq, code string) (int64, string) {
  378. orm := entity.Engine.NewSession()
  379. defer orm.Close()
  380. err := orm.Begin()
  381. //买赠卷使用
  382. if data.DiscountId != 0 {
  383. fool := true
  384. discountId := data.DiscountId
  385. if fool, _ = util.Exists(code, "discount_"+fmt.Sprint(discountId)); !fool {
  386. orm.Rollback()
  387. return entity.ErrorCode, "奖卷领取失败"
  388. }
  389. logx.Info(fmt.Sprint(discountId) + "Id奖券领取")
  390. //1、查找活动赠品对应关系信息
  391. discountJson := entity.DiscountJson{}
  392. fool, err := orm.Table("activity").Alias("a").
  393. Select("a.activityId,a.id,a.beginDate,a.endDate,d.giftCode,d.stockNumber,d.useNumber,a.limitQuantity,a.limitType").
  394. Join("left", "discount d", "a.id=d.activityId").
  395. Where("d.id=?", data.DiscountId).Get(&discountJson)
  396. if !fool {
  397. logx.Info("查询奖品信息:", err)
  398. orm.Rollback()
  399. return entity.ErrorCode, "奖品信息查询失败"
  400. }
  401. //查询是否可以领取
  402. if discountJson.LimitType != 0 {
  403. if !activityPartakeHandle(discountJson.Id, data.UserId, discountJson.LimitQuantity, discountJson.LimitType, 2) {
  404. return entity.ErrorCode, "活动参与已达上限"
  405. }
  406. }
  407. if !util.ActivityTime(discountJson.BeginDate) {
  408. logx.Info("活动没有开始不可领劵", discountJson)
  409. orm.Rollback()
  410. return entity.ErrorCode, "活动没有开始不可领劵"
  411. }
  412. if util.ActivityTimeOther(discountJson.EndDate) {
  413. logx.Info("活动结束不可领劵", discountJson)
  414. orm.Rollback()
  415. return entity.ErrorCode, "活动结束不可领劵"
  416. }
  417. //2、修改redis余额
  418. if util.GetInt(code, "discount_"+fmt.Sprint(discountId)) <= 0 {
  419. logx.Info("赠品余额不足:", err)
  420. orm.Rollback()
  421. return entity.ErrorCode, "奖券余额不足"
  422. }
  423. if !util.DecrbyLimit(code, "discount_"+fmt.Sprint(discountId), 1, 0) {
  424. logx.Info("修改redis赠品库存失败:", err)
  425. orm.Rollback()
  426. return entity.ErrorCode, "修改redis奖券库存失败"
  427. }
  428. userLottery := entity.UserPrize{}
  429. userLottery.AppId = data.AppId
  430. userLottery.UserId = data.UserId
  431. userLottery.PrizeType = 3
  432. userLottery.CreateTime = time.Now().Local()
  433. userLottery.LotteryId = data.DiscountId
  434. userLottery.UserName = data.UserName
  435. userLottery.EndDate = discountJson.EndDate
  436. userLottery.BeginDate = discountJson.BeginDate
  437. userLottery.OrderCode = data.OrderCode
  438. userLottery.UseDate = time.Now().Local()
  439. userLottery.UserPhone = data.UserPhone
  440. userLottery.ActivityId = discountJson.Id
  441. userLottery.UserProduct = data.UseProduct
  442. numb, err := orm.Table("user_prize").Insert(&userLottery)
  443. if err != nil || numb == int64(0) {
  444. logx.Info("领取奖券失败:", err)
  445. orm.Rollback()
  446. return entity.ErrorCode, "领取奖券失败"
  447. }
  448. //3、修改奖券余额数量
  449. discount := entity.Discount{}
  450. discount.Id = discountId
  451. _, err = orm.Exec("UPDATE discount SET `stockNumber` = stockNumber-1,`useNumber` = useNumber+1 WHERE `id` = ?", discountId)
  452. if err != nil {
  453. logx.Info("修改奖券库存失败:", err)
  454. orm.Rollback()
  455. return entity.ErrorCode, "修改奖券库存失败"
  456. }
  457. }
  458. //先查询奖券状态
  459. userLotteryIdStr := data.UserLottertId
  460. if userLotteryIdStr != "" {
  461. userLotteryIdArr := strings.Split(userLotteryIdStr, ",")
  462. for _, userLotteryId := range userLotteryIdArr {
  463. var userLottery entity.UserPrizeJson
  464. fool := true
  465. fool, err = orm.Table("user_prize").Alias("u").
  466. Select("u.*,a.activityType,a.id as activityId").
  467. Join("left", "activity a", "u.activityId=a.id").
  468. Where("u.userId=? and u.appId=? and u.id=? ", data.UserId, data.AppId, userLotteryId).
  469. Asc("createTime").Get(&userLottery)
  470. if err != nil {
  471. logx.Info("查询奖券库存失败:", err)
  472. orm.Rollback()
  473. return entity.ErrorCode, "查询奖券库存失败"
  474. }
  475. if !fool {
  476. logx.Info(data.UserId, "用户没有ID为", userLotteryId, "的奖券")
  477. orm.Rollback()
  478. return entity.ErrorCode, "该用户没有此此奖券"
  479. }
  480. //未使用改为待使用
  481. if data.Model == 3 {
  482. if userLottery.PrizeType == 0 {
  483. //奖券状态改为待使用
  484. _, err := orm.Exec("update user_prize set prizeType=? ,orderCode=?,useDate=?,userProduct=?,userName=?,userPhone=? where id =?",
  485. 3, data.OrderCode, time.Now().Local(), data.UseProduct, data.UserName, data.UserPhone, userLottery.Id)
  486. //numb, err := orm.Table("user_prize").ID(userLottery.Id).Cols("prizeType", "orderCode", "useDate", "userProduct", "userName", "userPhone").Update(userLottery)
  487. if err != nil {
  488. logx.Info("修改用户奖券失败:", err)
  489. orm.Rollback()
  490. return entity.ErrorCode, "修改用户奖券失败"
  491. }
  492. //修改卷的使用量
  493. if userLottery.ActivityType == 2 {
  494. _, err = orm.Exec("UPDATE discount SET `useNumber` = useNumber+1 WHERE `id` = ?", userLottery.LotteryId)
  495. } else {
  496. _, err = orm.Exec("UPDATE lottery SET `useNumber` = useNumber+1 WHERE `id` = ?", userLottery.LotteryId)
  497. }
  498. if err != nil {
  499. logx.Info("修改奖券使用数量失败:", err)
  500. orm.Rollback()
  501. return entity.ErrorCode, "修改奖券使用数量失败"
  502. }
  503. } else {
  504. orm.Rollback()
  505. return entity.ErrorCode, "该奖券状态不可修改"
  506. }
  507. } else if data.Model == 0 {
  508. //奖券状态改为未使用
  509. if userLottery.PrizeType == 3 {
  510. //numb, err := orm.Table("user_prize").ID(userLottery.Id).Cols("prizeType", "orderCode").Update(userLottery)
  511. _, err := orm.Exec("update user_prize set prizeType=? ,orderCode=? where id =?", 0, "", userLottery.Id)
  512. if err != nil {
  513. logx.Info("修改用户奖券失败:", err)
  514. orm.Rollback()
  515. return entity.ErrorCode, "修改用户奖券失败"
  516. }
  517. //修改卷的使用量
  518. if userLottery.ActivityType >= 2 {
  519. if util.Incrby(code, "discount_"+fmt.Sprint(userLottery.LotteryId), 1) <= 0 {
  520. logx.Info("修改redis库存失败:", err)
  521. orm.Rollback()
  522. return entity.ErrorCode, "修改redis库存失败"
  523. }
  524. _, err = orm.Exec("UPDATE discount SET `useNumber` = useNumber-1,`stockNumber` = stockNumber+1 WHERE `id` = ?", userLottery.LotteryId)
  525. } else {
  526. _, err = orm.Exec("UPDATE lottery SET `useNumber` = useNumber-1 WHERE `id` = ?", userLottery.LotteryId)
  527. }
  528. if err != nil {
  529. logx.Info("修改奖券使用数量失败:", err)
  530. orm.Rollback()
  531. return entity.ErrorCode, "修改奖券使用数量失败"
  532. }
  533. } else {
  534. orm.Rollback()
  535. return entity.ErrorCode, "该奖券状态不可修改"
  536. }
  537. } else {
  538. //1、奖券是否过期
  539. if util.ActivityTimeOther(userLottery.EndDate) {
  540. logx.Info(data.UserId, "此卷已过期不可使用", userLottery)
  541. orm.Rollback()
  542. return entity.ErrorCode, "此卷已过期不可使用"
  543. }
  544. //2、奖券状态改为已使用
  545. //numb, err := orm.Table("user_prize").ID(userLottery.Id).Cols("prizeType", "useDate", "ActivityId", "UserProduct", "orderCode", "UserName", "UserPhone").Update(userLottery)
  546. _, err := orm.Exec("update user_prize set prizeType=?, useDate=?, ActivityId=?, UserProduct=?, orderCode=?, userName=?, userPhone=? where id =?",
  547. 1, time.Now().Local(), userLottery.ActivityId, data.UseProduct, data.OrderCode, data.UserName, data.UserPhone, userLottery.Id)
  548. if err != nil {
  549. logx.Info("修改用户奖券失败:", err)
  550. orm.Rollback()
  551. return entity.ErrorCode, "修改用户奖券失败"
  552. }
  553. }
  554. }
  555. }
  556. orm.Commit()
  557. return entity.SuccessCode, "使用奖券成功"
  558. }
  559. //查询对应的产品信息
  560. func (service *ActivityService) LotteryProduct(useProduct string) (int64, string, []entity.ProductJson) {
  561. orm := entity.Engine
  562. productList := []entity.ProductJson{}
  563. err := orm.Table("product").Alias("pr").
  564. Select("pr.*").
  565. Where("FIND_IN_SET( pr.productCode,?)", useProduct).
  566. Find(&productList)
  567. if err != nil {
  568. logx.Info("err:", err)
  569. return entity.ErrorCode, "查询对应的产品信息失败", productList
  570. }
  571. return entity.SuccessCode, "查询对应的产品信息成功", productList
  572. }
  573. //奖券的基本信息
  574. func (service *ActivityService) LotteryInfo(data *activity.Request) (int64, string, entity.UserPrizeJson, bool) {
  575. orm := entity.Engine
  576. //var err error
  577. userLotteryList := []entity.UserPrizeJson{}
  578. var err error
  579. isReceive := false
  580. if data.ActivityType < 2 {
  581. isReceive = true
  582. //有卷的查询
  583. 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").
  584. Join("left", "discount d", "d.lotteryId=up.lotteryId and d.state=1 ").
  585. Join("left", "prize p ", "d.prizeId=p.Id").
  586. Join("left", "lottery l ", "l.id=up.lotteryId").
  587. Join("left", "activity a", "a.id=d.activityId").
  588. Where("up.userId=? and up.appId=? and up.prizeType=0 and l.id=? ", data.UserId, data.AppId, data.LotteryId).
  589. Find(&userLotteryList)
  590. } else {
  591. //先判断用户是否可以领取
  592. var activityList = []entity.Activity{}
  593. err = orm.Table("activity").Alias("a").Select(" DISTINCT a.*").
  594. Join("left", "discount d", "d.activityId = a.id").
  595. Where(" a.state=1 and d.id=?", data.LotteryId).Find(&activityList)
  596. if len(activityList) == 0 {
  597. return entity.ErrorCode, "奖券信息查询失败", entity.UserPrizeJson{}, isReceive
  598. }
  599. for _, activityEntity := range activityList {
  600. //查看在分组内
  601. if !UserGroupingHandle(activityEntity.UserRange, data.UserId, activityEntity.UserGroupId, activityEntity.BeginDate, activityEntity.EndDate, activityEntity.PreheatingTime) {
  602. continue
  603. }
  604. //查看活动是否可以参与
  605. if util.ActivityTime(activityEntity.PreheatingTime) {
  606. //查询是否可以领取
  607. if activityEntity.LimitType != 0 {
  608. if !activityPartakeHandle(activityEntity.Id, data.UserId, activityEntity.LimitQuantity, activityEntity.LimitType, 2) {
  609. continue
  610. }
  611. } else {
  612. isReceive = true
  613. }
  614. } else {
  615. continue
  616. }
  617. }
  618. //无卷查询
  619. 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").
  620. Join("left", "discount d", "d.activityId = a.id ").
  621. Join("left", "gift g ", "g.giftCode=d.giftCode").
  622. Join("left", "product p", "FIND_IN_SET(p.ProductCode,d.useProductList )").
  623. Where(" d.id=?", data.LotteryId).
  624. Find(&userLotteryList)
  625. }
  626. if len(userLotteryList) == 0 || err != nil {
  627. logx.Info("err:", err)
  628. return entity.ErrorCode, "奖券信息查询失败", entity.UserPrizeJson{}, isReceive
  629. }
  630. productList := []entity.ProductJson{}
  631. var code int64
  632. code, _, productList = service.LotteryProduct(userLotteryList[0].UserProduct)
  633. if code == 0 {
  634. return entity.ErrorCode, "查询奖券对应的产品信息失败", entity.UserPrizeJson{}, isReceive
  635. }
  636. userLotteryList[0].UseProductList = productList
  637. return entity.SuccessCode, "用户下的奖券查询成功", userLotteryList[0], isReceive
  638. }
  639. //产品是否存在买送奖券
  640. func (service *ActivityService) GiveInfo(in *activity.Request) (int64, string, []entity.ActivityJson, bool) {
  641. activityList := []entity.Activity{}
  642. activityLists := []entity.ActivityJson{}
  643. orm := entity.Engine
  644. isReceive := false
  645. //var err error
  646. err := orm.Table("activity").Alias("a").Select(" DISTINCT a.*").
  647. Join("left", "discount d", "d.activityId = a.id").
  648. Join("left", "product p", "FIND_IN_SET(p.ProductCode,d.useProductList )").
  649. 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)
  650. if len(activityList) == 0 {
  651. logx.Info("用户下的奖券查询失败:", err)
  652. return entity.SuccessCode, "不存在奖券信息", activityLists, isReceive
  653. }
  654. if util.ActivityTime(activityList[0].PreheatingTime) {
  655. //查询是否可以领取
  656. if activityList[0].LimitType != 0 {
  657. if activityPartakeHandle(activityList[0].Id, in.UserId, activityList[0].LimitQuantity, activityList[0].LimitType, 2) {
  658. isReceive = true
  659. }
  660. } else {
  661. isReceive = true
  662. }
  663. }
  664. userRange := activityList[0].UserRange
  665. userGroupId := activityList[0].UserGroupId
  666. // 判断活动是否为全部用户参加
  667. // 1- 全部 2-分组用户
  668. userIdTest := in.UserId
  669. if userRange == int64(2) {
  670. if entity.UserIdMap[in.UserId] != "" {
  671. userIdTest = entity.UserIdMap[in.UserId]
  672. }
  673. //根据用户线索id获取用户所属分组
  674. exist, err2 := IsInGroup(userIdTest, strings.Split(userGroupId, ","))
  675. //判断用户是否在分组中
  676. if err2 != "" {
  677. logx.Info("获取用户分组查询失败:", err2)
  678. return entity.SuccessCode, "获取用户分组出错", []entity.ActivityJson{}, isReceive
  679. }
  680. if !exist {
  681. logx.Info("用户不在该分组中:", err)
  682. return entity.SuccessCode, "获取用户分组出错", []entity.ActivityJson{}, isReceive
  683. }
  684. }
  685. //先查找产品参与的活动
  686. 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").
  687. Join("left", " discount d", "d.activityId = a.id ").
  688. Join("left", "gift g ", "g.giftCode=d.giftCode").
  689. Join("left", "product p", "FIND_IN_SET(p.ProductCode,d.useProductList )").
  690. 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).
  691. Find(&activityLists)
  692. if err != nil {
  693. logx.Info("用户下的奖券查询失败:", err)
  694. return entity.ErrorCode, "用户下的奖券查询失败", activityLists, isReceive
  695. }
  696. return entity.SuccessCode, "产品下存在的买送券", activityLists, isReceive
  697. }
  698. func (service *ActivityService) GiveActivity(in *activity.Request) (int64, string, entity.Activity) {
  699. orm := entity.Engine
  700. activityList := []entity.Activity{}
  701. err := orm.Table("activity").Alias("a").Select(" a.*,sum(stockNumber) as stockNumber").
  702. Join("left", "discount d", "d.activityId = a.id").
  703. Join("left", "product p", "FIND_IN_SET(p.ProductCode,d.useProductList )").
  704. 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)
  705. if len(activityList) == 0 {
  706. logx.Info("用户下的奖券查询失败:", err)
  707. return entity.SuccessCode, "该时段没有限时活动", entity.Activity{}
  708. } else {
  709. //库存查询
  710. return entity.SuccessCode, "", activityList[0]
  711. }
  712. }
  713. //查询赠品与用户绑定关系标识
  714. func (service *ActivityService) FindUserLotteryId(in *activity.UserLotteryRequest) (int64, string, int64, int64) {
  715. userPrize := entity.UserPrizeJson{}
  716. orm := entity.Engine
  717. //var err error
  718. //先查找产品参与的活动
  719. _, err := orm.Table("user_prize").Alias("u").Select("u.id,a.activityType").
  720. Join("left", "activity a", "u.activityId=a.id").
  721. Where("u.userId=? AND u.orderCode = ? ", in.UserID, in.OrderCode).
  722. Get(&userPrize)
  723. logx.Info(err)
  724. return entity.SuccessCode, "赠品用户绑定关系", userPrize.Id, userPrize.ActivityType
  725. }
  726. type rsDataStr struct {
  727. PrizeId int64 `xorm:"prizeId" `
  728. ValidityDates int64 `xorm:"validityDates" `
  729. BeginDate string `xorm:"beginDate" `
  730. EndDate string `xorm:"endDate" `
  731. LotteryId int64 `xorm:"lotteryId" `
  732. Name string `xorm:"name" `
  733. Full int64 `xorm:"full" `
  734. Reduce int64 `xorm:"reduce" `
  735. Discount float64 `xorm:"discount" `
  736. ActivityId int64 `xorm:"activityId" `
  737. ValidityTimeType int64 `xorm:"validityTimeType" `
  738. DisCountId int64 `xorm:"disCountId" `
  739. }
  740. func (service *ActivityService) GetLottery(activityId string) ([]rsDataStr, []entity.ProductJson) {
  741. orm := entity.Engine
  742. var err error
  743. // 2. 根据活动id查询活动下的券及对应信息
  744. var rsList []rsDataStr
  745. 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)
  746. if err != nil {
  747. logx.Info("获取活动下奖券信息失败err:", err)
  748. return nil, nil
  749. }
  750. i64, _ := strconv.ParseInt(activityId, 10, 64)
  751. _, _, urlList := ActivityProduct(i64)
  752. return rsList, urlList
  753. }
  754. // LotteryGrantSave 保存信息
  755. func (service *ActivityService) LotteryGrantSave(userLottery []*entity.UserPrize) (int64, string) {
  756. orm := entity.SaveEngine.NewSession()
  757. defer orm.Close()
  758. err := orm.Begin()
  759. //5、领取奖券
  760. numb, err := orm.Table("user_prize").Insert(userLottery)
  761. if err != nil || numb == int64(0) {
  762. logx.Info("发放奖券失败:", err)
  763. return entity.ErrorCode, "发放奖券失败"
  764. }
  765. // 修改已发放数量
  766. lottery := entity.Lottery{}
  767. lottery.Id = userLottery[0].LotteryId
  768. _, err = orm.Exec("UPDATE lottery SET `receiveNumber` = receiveNumber+? WHERE `id` = ?", numb, userLottery[0].LotteryId)
  769. if err != nil {
  770. logx.Info("修改奖券领取数量失败:", err)
  771. orm.Rollback()
  772. return entity.ErrorCode, "修改奖券领取数量失败"
  773. }
  774. orm.Commit()
  775. return entity.SuccessCode, "奖券领取成功"
  776. }
  777. //查询对应的产品信息
  778. func ActivityProduct(activityId int64) (int64, string, []entity.ProductJson) {
  779. orm := entity.Engine
  780. productNameList := []entity.ProductJson{}
  781. err := orm.Table("discount").Alias("d").Select("DISTINCT p.* ").
  782. Join("left", "product p", "FIND_IN_SET( p.productCode, d.useProductList )").
  783. Where("d.activityId = ? and d.state=1", activityId).
  784. Find(&productNameList)
  785. if err != nil {
  786. logx.Info("err:", err)
  787. return entity.ErrorCode, "查询对应的产品信息失败", productNameList
  788. }
  789. return entity.SuccessCode, "查询对应的产品信息成功", productNameList
  790. }
  791. //查询奖券对应的商品
  792. func LotteryProduct(activityId int64) (int64, string, []entity.ProductJson) {
  793. orm := entity.Engine
  794. productNameList := []entity.ProductJson{}
  795. err := orm.Table("discount").Alias("d").Select("DISTINCT p.* ").
  796. Join("left", "product p", "FIND_IN_SET( p.productCode, d.useProductList )").
  797. Where("d.activityId = ? and d.state=1", activityId).
  798. Find(&productNameList)
  799. if err != nil {
  800. logx.Info("err:", err)
  801. return entity.ErrorCode, "查询对应的产品信息失败", productNameList
  802. }
  803. return entity.SuccessCode, "查询对应的产品信息成功", productNameList
  804. }
  805. func IsInGroup(userId string, groupId []string) (bool, string) {
  806. tmp := []int64{}
  807. var exist bool
  808. orm := entity.TiEngine
  809. 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)
  810. if err != nil {
  811. logx.Info("查询用户是否在分组中失败", err)
  812. return false, "查询用户是否在分组中失败"
  813. }
  814. if len(tmp) > 0 && tmp[0] > 0 {
  815. exist = true
  816. }
  817. return exist, ""
  818. }
  819. //活动参与数量是否达上限
  820. func activityPartakeHandle(id int64, userId string, limitQuantity, limitType, findType int64) bool {
  821. orm := entity.Engine.NewSession()
  822. count := int64(0)
  823. if findType == 1 {
  824. //查询某个奖券领取数量
  825. count, _ = orm.Table("user_prize").Where("prizeId=? and userId=? ", id, userId).Count()
  826. if int64(count) >= limitQuantity {
  827. return false
  828. }
  829. return true
  830. }
  831. //活动是否已达上限
  832. switch limitType {
  833. case 1:
  834. //查询某个活动领取数量
  835. count, _ = orm.Table("user_prize").Where("activityId=? and userId=? and prizeType!=0 ", id, userId).Count()
  836. if int64(count) >= limitQuantity {
  837. return false
  838. }
  839. break
  840. case 2:
  841. //查询整个活动今天领取数量
  842. count, _ = orm.Table("user_prize").Where("activityId=? and to_days(createTime)=to_days(now()) and userId=? and prizeType!=0", id, userId).Count()
  843. if int64(count) >= limitQuantity {
  844. return false
  845. }
  846. break
  847. }
  848. return true
  849. }
  850. //无卷处理
  851. func NoLotteryHandle(productCode, userId, appId string, userPrice *[]entity.UserPrizeJson, isDistribution bool) {
  852. productQuery := ""
  853. if productCode != "" {
  854. productQuery += " and ( p.productCode='" + productCode + "' )"
  855. }
  856. if isDistribution {
  857. productQuery += " and ( p.parentCode not in (" + C.C.NotAvailableCode + "))"
  858. }
  859. orm := entity.Engine.NewSession()
  860. //var err error
  861. //没有商品标识时处理
  862. // 判断活动是否为全部用户参加
  863. activityList := []entity.Activity{}
  864. err := orm.Table("activity").Alias("a").Select(" DISTINCT a.*").
  865. Join("left", "discount d", "d.activityId = a.id").
  866. Join("left", "product p", "FIND_IN_SET(p.ProductCode,d.useProductList )").
  867. 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).
  868. Asc("a.id").Find(&activityList)
  869. if len(activityList) == 0 {
  870. return
  871. }
  872. for _, value := range activityList {
  873. //分组判断处理
  874. if !UserGroupingHandle(value.UserRange, userId, value.UserGroupId, value.BeginDate, value.EndDate, value.PreheatingTime) {
  875. continue
  876. }
  877. //预热已经开始
  878. if util.ActivityTime(value.PreheatingTime) || util.ActivityTime(value.BeginDate) {
  879. //查询是否可以领取
  880. if value.LimitType != 0 {
  881. if !activityPartakeHandle(value.Id, userId, value.LimitQuantity, value.LimitType, 2) {
  882. continue
  883. }
  884. }
  885. }
  886. oneuserPrice := []entity.UserPrizeJson{}
  887. 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").
  888. Join("left", " discount d", "d.activityId = a.id ").
  889. Join("left", "gift g ", "g.giftCode=d.giftCode").
  890. Join("left", "product p", "FIND_IN_SET(p.ProductCode,d.useProductList )").
  891. Where(" a.id=? "+productQuery+" AND a.activityType >= 2 AND a.appId = ? and a.state=1 and d.state=1 ", value.Id, appId).
  892. Find(&oneuserPrice)
  893. //先查找产品参与的活动
  894. if err != nil {
  895. logx.Info("用户下的奖券查询失败:", err)
  896. continue
  897. }
  898. for _, prizeJson := range oneuserPrice {
  899. *userPrice = append(*userPrice, prizeJson)
  900. }
  901. }
  902. }
  903. func LotteryWaitHandle(productCode, userId, appId string, userPrice *[]entity.UserPrizeJson, isDistribution bool) {
  904. productQuery := " "
  905. if productCode != "" {
  906. productQuery = " and ( p.productCode='" + productCode + "' )"
  907. }
  908. if isDistribution {
  909. productQuery += "and ( p.parentCode not in (" + C.C.NotAvailableCode + "))"
  910. }
  911. orm := entity.Engine.NewSession()
  912. //var err error
  913. //没有商品标识时处理
  914. // 判断活动是否为全部用户参加
  915. activityList := []entity.Activity{}
  916. err := orm.Table("activity").Alias("a").Select(" DISTINCT a.*").
  917. Join("left", "discount d", "d.activityId = a.id").
  918. Join("left", "product p", "FIND_IN_SET(p.ProductCode,d.useProductList )").
  919. 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)
  920. if len(activityList) == 0 {
  921. return
  922. }
  923. for _, value := range activityList {
  924. //分组判断处理
  925. if !UserGroupingHandle(value.UserRange, userId, value.UserGroupId, value.BeginDate, value.EndDate, value.PreheatingTime) {
  926. continue
  927. }
  928. //预热已经开始
  929. if !util.ActivityTime(value.PreheatingTime) || util.ActivityTime(value.BeginDate) {
  930. continue
  931. }
  932. oneuserPrice := []entity.UserPrizeJson{}
  933. 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").
  934. Join("left", " discount d", "d.activityId = a.id ").
  935. Join("left", "lottery l ", "l.id=d.lotteryId").
  936. Join("left", "product p", "FIND_IN_SET(p.ProductCode,d.useProductList )").
  937. Where(" a.id=? "+productQuery+" AND a.activityType < 2 AND a.appId = ? and a.state=1 and d.state=1 ", value.Id, appId).
  938. Find(&oneuserPrice)
  939. //先查找产品参与的活动
  940. if err != nil {
  941. logx.Info("用户下的奖券查询失败:", err)
  942. continue
  943. }
  944. for _, prizeJson := range oneuserPrice {
  945. *userPrice = append(*userPrice, prizeJson)
  946. }
  947. }
  948. }
  949. //有券预热开始活动未开始
  950. //分组计算
  951. func UserGroupingHandle(userRange int64, userId, userGroupId string, startTime, endTime, preheatingTime string) bool {
  952. if userRange == int64(2) {
  953. if entity.UserIdMap[userId] != "" {
  954. userId = entity.UserIdMap[userId]
  955. }
  956. //根据用户线索id获取用户所属分组
  957. exist, err2 := IsInGroup(userId, strings.Split(userGroupId, ","))
  958. //判断用户是否在分组中
  959. if err2 != "" {
  960. logx.Info("用户不在该分组中:", err2)
  961. return false
  962. }
  963. if !exist {
  964. logx.Info("用户不在该分组中:")
  965. return false
  966. }
  967. } else if userRange == 3 {
  968. //新用户判断
  969. stime := ""
  970. etime := endTime
  971. //新用户
  972. if preheatingTime == "" {
  973. stime = startTime
  974. } else {
  975. stime = preheatingTime
  976. }
  977. sd, _ := time.ParseInLocation("2006-01-02 15:04:05", stime, time.Local)
  978. ed, _ := time.ParseInLocation("2006-01-02 15:04:05", etime, time.Local)
  979. data, _ok := entity.Mongo.FindById("user", userId, `{"l_registedate":1}`)
  980. if !_ok || data == nil || len((*data)) == 0 {
  981. return false
  982. }
  983. l_registedate := common.Int64All((*data)["l_registedate"])
  984. if !(l_registedate >= sd.Unix() && l_registedate <= ed.Unix()) {
  985. return false
  986. }
  987. }
  988. return true
  989. }