ActivityService.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jyMarketing/entity"
  4. "app.yhyue.com/moapp/jyMarketing/rpc/activity"
  5. "app.yhyue.com/moapp/jyMarketing/util"
  6. "fmt"
  7. _ "github.com/garyburd/redigo/redis"
  8. "log"
  9. "time"
  10. )
  11. type ActivityService struct{}
  12. //奖券获取
  13. func (service *ActivityService) LotteryReceive(data *activity.LotteryOperation, code string) (int64, string) {
  14. orm := entity.Engine.NewSession()
  15. defer orm.Close()
  16. err := orm.Begin()
  17. bool := true
  18. if len(data.LotteryIdArr) == 0 {
  19. return entity.ErrorCode, "没有卷可以领取"
  20. }
  21. for _, lotteryId := range data.LotteryIdArr {
  22. if fool, _ := util.Exists(code, "lottery_"+fmt.Sprint(lotteryId)); !fool {
  23. orm.Rollback()
  24. return entity.ErrorCode, "奖卷领取失败"
  25. }
  26. log.Println(fmt.Sprint(lotteryId) + "Id奖券领取")
  27. //每种劵处理
  28. //1、先查询奖券信息
  29. prizeData := entity.PrizeJson{}
  30. bool, err = orm.Table("lottery").
  31. Alias("l").Select("l.name,l.full,l.reduce, l.prizeId,p.beginDate,p.endDate,p.isLimitNumber,p.limitNumber,p.validityDates,p.validityTimeType").
  32. Join("left", "prize p", "l.prizeId=p.id").
  33. Where("l.id=?", lotteryId).Get(&prizeData)
  34. if err != nil {
  35. log.Panicln("查询奖品信息:", err)
  36. orm.Rollback()
  37. return entity.ErrorCode, "奖品信息查询失败"
  38. }
  39. if !bool {
  40. log.Panicln("查询奖品信息:", err)
  41. orm.Rollback()
  42. return entity.ErrorCode, "奖品信息查询失败"
  43. }
  44. //2、查看活动是否开始
  45. activity := entity.Activity{}
  46. bool, err = orm.Table("activity").Where("prizeId=? and state=1", prizeData.PrizeId).Get(&activity)
  47. if !bool {
  48. log.Panicln("查询奖品信息:", err)
  49. orm.Rollback()
  50. return entity.ErrorCode, "奖品信息查询失败"
  51. }
  52. if !activityTime(activity.BeginDate) {
  53. log.Panicln("活动没有开始不可领劵", activity)
  54. orm.Rollback()
  55. return entity.ErrorCode, "活动没有开始不可领劵"
  56. }
  57. //3、先判断奖券是否可以重复领取,在判断之前是否领取过
  58. if prizeData.IsLimitNumber == 1 {
  59. //查询之前是否领取过
  60. userLotteryList := []entity.UserPrize{}
  61. err = orm.Table("user_prize").
  62. Where("userId=? and appId=? and lotteryId=?", data.UserId, data.AppId, lotteryId).Find(&userLotteryList)
  63. if err != nil {
  64. log.Panicln("查询奖品信息:", err)
  65. orm.Rollback()
  66. return entity.ErrorCode, "查询领取次数失败"
  67. }
  68. if len(userLotteryList) >= prizeData.LimitNumber {
  69. orm.Rollback()
  70. return entity.ErrorCode, "你领取奖券数量已达上限"
  71. }
  72. }
  73. //5、领取奖券
  74. userLettry := entity.UserPrize{}
  75. userLettry.AppId = data.AppId
  76. userLettry.UserId = data.UserId
  77. userLettry.PrizeType = 0
  78. userLettry.CreateTime = time.Now().Local()
  79. userLettry.LotteryId = lotteryId
  80. userLettry.PrizeId = prizeData.PrizeId
  81. userLettry.ValidityDates = prizeData.ValidityDates
  82. userLettry.Name = prizeData.Name
  83. userLettry.Full = int64(prizeData.Full)
  84. userLettry.Reduce = int64(prizeData.Reduce)
  85. userLettry.UserName = data.UserName
  86. //0、有起止时间1、当天起几天可用2、次日起几天可用
  87. switch prizeData.ValidityTimeType {
  88. case 0:
  89. userLettry.EndDate = prizeData.EndDate
  90. userLettry.BeginDate = prizeData.BeginDate
  91. case 1:
  92. nowStr := time.Now().Format("2006-01-02")
  93. userLettry.BeginDate = nowStr
  94. userLettry.EndDate = service.ObtainAppointTimeString(nowStr, prizeData.ValidityDates)
  95. case 2:
  96. nowStr := time.Now().Format("2006-01-02")
  97. nextDayStr := service.ObtainAppointTimeString(nowStr, 1)
  98. userLettry.BeginDate = nextDayStr
  99. userLettry.EndDate = service.ObtainAppointTimeString(nextDayStr, prizeData.ValidityDates)
  100. }
  101. numb, err := orm.Table("user_prize").Insert(&userLettry)
  102. if err != nil || numb == int64(0) {
  103. log.Panicln("领取奖券失败:", err)
  104. orm.Rollback()
  105. return entity.ErrorCode, "领取奖券失败"
  106. }
  107. //6、修改奖券余额数量
  108. lottery := entity.Lottery{}
  109. lottery.Id = lotteryId
  110. _, err = orm.Exec("UPDATE lottery SET `stockNumber` = stockNumber-1, `receiveNumber` = receiveNumber+1 WHERE `id` = ?", lotteryId)
  111. if err != nil {
  112. log.Panicln("修改奖券库存失败:", err)
  113. orm.Rollback()
  114. return entity.ErrorCode, "修改奖券库存失败"
  115. }
  116. //7、修改redis余额
  117. if util.GetInt(code, "lottery_"+fmt.Sprint(lotteryId)) <= 0 {
  118. log.Panicln("奖券余额不足:", err)
  119. orm.Rollback()
  120. return entity.ErrorCode, "奖券余额不足"
  121. }
  122. if !util.DecrbyLimit(code, "lottery_"+fmt.Sprint(lotteryId), 1, 0) {
  123. log.Panicln("修改redis奖券库存失败:", err)
  124. orm.Rollback()
  125. return entity.ErrorCode, "修改redis奖券库存失败"
  126. }
  127. }
  128. orm.Commit()
  129. return entity.SuccessCode, "奖券领取成功"
  130. }
  131. //奖券使用
  132. func (service *ActivityService) ActivityUse(data *activity.LotteryOperation) (int64, string) {
  133. orm := entity.Engine.NewSession()
  134. defer orm.Close()
  135. err := orm.Begin()
  136. //nowStr := time.Now().Format("2006-01-02")
  137. for _, lotteryId := range data.LotteryIdArr {
  138. //1、先查看是否有这张奖券
  139. var userLottery entity.UserPrize
  140. bool := true
  141. bool, err = orm.Table("user_prize").
  142. Where("userId=? and appId=? and id=? ", data.UserId, data.AppId, lotteryId).Get(&userLottery)
  143. if err != nil {
  144. log.Println("查询奖券库存失败:", err)
  145. orm.Rollback()
  146. return entity.ErrorCode, "查询奖券库存失败"
  147. }
  148. if !bool {
  149. log.Println(data.UserId, "用户没有ID为", lotteryId, "的奖券")
  150. orm.Rollback()
  151. return entity.ErrorCode, "该用户没有此此奖券"
  152. }
  153. //2、奖券是否过期
  154. if activityTime(userLottery.EndDate) {
  155. log.Println(data.UserId, "此卷已过期不可使用", userLottery)
  156. orm.Rollback()
  157. return entity.ErrorCode, "此卷已过期不可使用"
  158. }
  159. //3、奖券状态改为已使用
  160. userLottery.UseDate = time.Now().Local()
  161. userLottery.PrizeType = 1
  162. numb, err := orm.Table("user_prize").ID(userLottery.Id).Cols("prizeType", "useDate").Update(userLottery)
  163. if err != nil || numb == 0 {
  164. log.Println("修改用户奖券失败:", err)
  165. orm.Rollback()
  166. return entity.ErrorCode, "修改用户奖券失败"
  167. }
  168. }
  169. orm.Commit()
  170. return entity.SuccessCode, "使用奖券成功"
  171. }
  172. //活动下的奖券
  173. func (service *ActivityService) ActivityLottery(in *activity.Request) (int64, string, []entity.LotteryJson, entity.Activity) {
  174. orm := entity.Engine.NewSession()
  175. //1、查找活动信息
  176. activityJson := entity.Activity{}
  177. _, err := orm.Table("activity").Alias("a").
  178. Where("id = ? ", in.ActivityId).Get(&activityJson)
  179. //2、先查找活动下的奖品Id
  180. lotteryJsonList := []entity.LotteryJson{}
  181. err = orm.Table("activity").Alias("a").
  182. Select("p.prizeType as lotteryType,l.full,l.reduce, p.beginDate,p.endDate,l.stockNumber,l.receiveNumber,p.limitNumber,( select count(up.id) FROM user_prize up where up.lotteryId = l.id AND up.userId ='"+in.UserId+"' ) AS count,( select count(up.id) FROM user_prize up where up.lotteryId = l.id AND up.userId ='"+in.UserId+"' and to_days(createTime) = to_days(now())) AS daycount,p.isLimitNumber,p.validityTimeType,p.validityDates,p.instructions,p.remark,l.id as lotteryId ,l.name ").
  183. Join("left", "prize p", " a.prizeId = p.Id").
  184. Join("left", "lottery l", " l.prizeId = p.id ").
  185. Where("a.id = ? and a.appId=? ", in.ActivityId, in.AppId).Find(&lotteryJsonList)
  186. if err != nil {
  187. log.Println("用户下的奖券查询失败:", err)
  188. return entity.ErrorCode, "用户下的奖券查询失败", lotteryJsonList, activityJson
  189. }
  190. for key, value := range lotteryJsonList {
  191. productList := []entity.ProductJson{}
  192. var code int64
  193. in.LotteryId=value.LotteryId
  194. code,_,productList=service.LotteryProduct(in)
  195. if (code==0){
  196. return entity.ErrorCode, "奖券对应商品查询失败", lotteryJsonList, activityJson
  197. }
  198. lotteryJsonList[key].UseProductList=productList
  199. }
  200. orm.Commit()
  201. return entity.SuccessCode, "活动下的奖券", lotteryJsonList, activityJson
  202. }
  203. //用户下的奖券(不包含待使用的 )
  204. func (service *ActivityService) UserLottery(data *activity.Request) (int64, string, []entity.UserPrizeJson, int64) {
  205. orm := entity.Engine.NewSession()
  206. defer orm.Close()
  207. err := orm.Begin()
  208. userLettryList := []entity.UserPrizeJson{}
  209. nowStr := time.Now().Format("2006-01-02")
  210. count := int64(0)
  211. switch data.Model {
  212. case 1:
  213. //查看可以使用的奖券(没有过期的)
  214. condition := ""
  215. if data.UserLottertId != 0 {
  216. condition = "( (up.prizeType=0 and up.beginDate<='" + nowStr + "' ) or up.id=" + fmt.Sprint(data.UserLottertId) + ") "
  217. } else {
  218. condition = " up.prizeType=0 and up.beginDate<='" + nowStr + "'"
  219. }
  220. count, err = orm.Table("user_prize").Alias("up").Select("up.*,p.instructions,p.remark,p.prizeType as lotteryType").
  221. Join("left", "activity a ", "a.prizeId=up.prizeId").
  222. Join("left", "prize p ", "a.prizeId=p.Id").
  223. Where("up.userId=? and up.appId=? and up.endDate>=?", data.UserId, data.AppId, nowStr).
  224. And(condition).
  225. Desc("up.createTime").
  226. FindAndCount(&userLettryList)
  227. case 0:
  228. //查看名下所有奖券
  229. count, err = orm.Table("user_prize").Alias("up").Select("up.*,p.instructions,p.remark,p.prizeType as lotteryType").
  230. Join("left", "activity a ", "a.prizeId=up.prizeId").
  231. Join("left", "prize p ", "a.prizeId=p.Id").
  232. Where("up.userId=? and up.appId=?", data.UserId, data.AppId).Desc("up.createTime").
  233. Limit(int(data.PageSize), (int(data.Page - 1))*int(data.PageSize)).
  234. FindAndCount(&userLettryList)
  235. case 2:
  236. //过期的奖券
  237. count, err = orm.Table("user_prize").Alias("up").Select("up.*,p.instructions,p.remark,p.prizeType as lotteryType").
  238. Join("left", "activity a ", "a.prizeId=up.prizeId").
  239. Join("left", "prize p ", "a.prizeId=p.Id").
  240. Where("up.userId=? and up.appId=? ", data.UserId, data.AppId ).
  241. And("up.prizeType=2").
  242. Desc("up.createTime").Limit(int(data.PageSize), (int(data.Page - 1))*int(data.PageSize)).
  243. FindAndCount(&userLettryList)
  244. case 3:
  245. //已用的奖券
  246. count, err = orm.Table("user_prize").Alias("up").Select("up.*,p.instructions,p.remark,p.prizeType as lotteryType").
  247. Join("left", "activity a ", "a.prizeId=up.prizeId").
  248. Join("left", "prize p ", "a.prizeId=p.Id").
  249. Where("up.userId=? and up.appId=? and (up.prizeType=1 or up.prizeType=3)", data.UserId, data.AppId, ).
  250. Desc("up.createTime").
  251. Limit(int(data.PageSize), (int(data.Page - 1))*int(data.PageSize)).FindAndCount(&userLettryList)
  252. case 4:
  253. //所有未使用奖券
  254. count, err = orm.Table("user_prize").Alias("up").Select("up.*,p.instructions,p.remark,p.prizeType as lotteryType").
  255. Join("left", "activity a ", "a.prizeId=up.prizeId").
  256. Join("left", "prize p ", "a.prizeId=p.Id").
  257. Where("up.userId=? and up.appId=? and up.endDate>=?", data.UserId, data.AppId, nowStr).
  258. And("up.prizeType=0").
  259. Limit(int(data.PageSize), (int(data.Page - 1))*int(data.PageSize)).
  260. Desc("up.createTime").
  261. FindAndCount(&userLettryList)
  262. }
  263. if err != nil {
  264. log.Println("用户下的奖券查询失败:", err)
  265. orm.Rollback()
  266. return entity.ErrorCode, "用户下的奖券查询失败", userLettryList, count
  267. }
  268. for key, value := range userLettryList {
  269. productList := []entity.ProductJson{}
  270. var code int64
  271. data.LotteryId=value.LotteryId
  272. code,_,productList=service.LotteryProduct(data)
  273. if (code==0){
  274. return entity.SuccessCode, "奖券对应商品查询失败", userLettryList, count
  275. }
  276. userLettryList[key].UseProductList=productList
  277. }
  278. orm.Commit()
  279. return entity.SuccessCode, "用户下的奖券查询成功", userLettryList, count
  280. }
  281. //计算几天之后的时间
  282. func (service *ActivityService) ObtainAppointTimeString(now string, beApartDay int) string {
  283. local, _ := time.LoadLocation("Local")
  284. t, _ := time.ParseInLocation("2006-01-02", now, local)
  285. stopTime := t.AddDate(0, 0, beApartDay)
  286. stopTimeStr := stopTime.Format("2006-01-02")
  287. return stopTimeStr
  288. }
  289. //查询过期的奖券改为已过期
  290. func (service *ActivityService) UpdateLottery(endDate string) (int64, string) {
  291. orm := entity.Engine.NewSession()
  292. defer orm.Close()
  293. err := orm.Begin()
  294. //1、先查找过期的奖券
  295. userPrizeList := []entity.UserPrize{}
  296. err = orm.Table("user_prize").
  297. Where("endDate<? and (prizeType=0 or prizeType=3) ", endDate).Find(&userPrizeList)
  298. if err != nil {
  299. log.Println("查询过期奖券失败:", err)
  300. orm.Rollback()
  301. return entity.ErrorCode, "查询过期奖券失败"
  302. }
  303. for _, value := range userPrizeList {
  304. value.PrizeType = 2
  305. numb, err := orm.Table("user_prize").ID(value.Id).Cols("prizeType").Update(value)
  306. if err != nil || numb == 0 {
  307. log.Println("修改奖券使用数量失败:", err)
  308. orm.Rollback()
  309. return entity.ErrorCode, "修改奖券使用数量失败"
  310. }
  311. }
  312. orm.Commit()
  313. return entity.SuccessCode, "过期奖券处理成功"
  314. }
  315. //活动创建及修改时间需大于当前时间
  316. func activityTime(beginDate interface{}) bool {
  317. timeData := time.Now().Format("2006-01-02")
  318. if timeData >= fmt.Sprint(beginDate) {
  319. return true
  320. }
  321. return false
  322. }
  323. //待使用与未使用状态之间变化
  324. func (service *ActivityService) LotteryStateChange(data *activity.UpdateStateReq) (int64, string) {
  325. orm := entity.Engine.NewSession()
  326. defer orm.Close()
  327. err := orm.Begin()
  328. //先查询奖券状态
  329. var userLottery entity.UserPrize
  330. bool := true
  331. bool, err = orm.Table("user_prize").
  332. Where("userId=? and appId=? and id=? ", data.UserId, data.AppId, data.UserLottertId).
  333. Asc("createTime").Get(&userLottery)
  334. if err != nil {
  335. log.Println("查询奖券库存失败:", err)
  336. orm.Rollback()
  337. return entity.ErrorCode, "查询奖券库存失败"
  338. }
  339. if !bool {
  340. log.Println(data.UserId, "用户没有ID为", data.UserLottertId, "的奖券")
  341. orm.Rollback()
  342. return entity.ErrorCode, "该用户没有此此奖券"
  343. }
  344. //未使用改为待使用
  345. if data.Model == 3 {
  346. if (userLottery.PrizeType == 0) {
  347. //奖券状态改为待使用
  348. userLottery.PrizeType = 3
  349. userLottery.OrderCode = data.OrderCode
  350. userLottery.UserName = data.UserName
  351. userLottery.UseDate = time.Now().Local()
  352. numb, err := orm.Table("user_prize").ID(userLottery.Id).Cols("prizeType", "orderCode", "userName", "useDate").Update(userLottery)
  353. if err != nil || numb == 0 {
  354. log.Println("修改用户奖券失败:", err)
  355. orm.Rollback()
  356. return entity.ErrorCode, "修改用户奖券失败"
  357. }
  358. //修改卷的使用量
  359. _, err = orm.Exec("UPDATE lottery SET `useNumber` = useNumber+1 WHERE `id` = ?", userLottery.LotteryId)
  360. if err != nil || numb == 0 {
  361. log.Println("修改奖券使用数量失败:", err)
  362. orm.Rollback()
  363. return entity.ErrorCode, "修改奖券使用数量失败"
  364. }
  365. orm.Commit()
  366. return entity.SuccessCode, "修改用户奖券成功"
  367. } else if userLottery.PrizeType == 3 {
  368. return entity.SuccessCode, "修改用户奖券成功"
  369. }
  370. return entity.ErrorCode, "该奖券状态不可修改"
  371. } else if data.Model == 0 {
  372. if (userLottery.PrizeType == 3) {
  373. //奖券状态改为未使用
  374. userLottery.PrizeType = 0
  375. userLottery.OrderCode = ""
  376. userLottery.UserName = ""
  377. numb, err := orm.Table("user_prize").ID(userLottery.Id).Cols("prizeType", "orderCode", "userName").Update(userLottery)
  378. if err != nil || numb == 0 {
  379. log.Println("修改用户奖券失败:", err)
  380. orm.Rollback()
  381. return entity.ErrorCode, "修改用户奖券失败"
  382. }
  383. //修改卷的使用量
  384. _, err = orm.Exec("UPDATE lottery SET `useNumber` = useNumber-1 WHERE `id` = ?", userLottery.LotteryId)
  385. if err != nil || numb == 0 {
  386. log.Println("修改奖券使用数量失败:", err)
  387. orm.Rollback()
  388. return entity.ErrorCode, "修改奖券使用数量失败"
  389. }
  390. orm.Commit()
  391. return entity.SuccessCode, "修改用户奖券成功"
  392. } else if (userLottery.PrizeType == 2) {
  393. orm.Commit()
  394. return entity.SuccessCode, "修改用户奖券成功"
  395. }
  396. return entity.ErrorCode, "该奖券状态不可修改"
  397. } else {
  398. //1、奖券是否过期
  399. if activityTime(userLottery.EndDate) {
  400. log.Println(data.UserId, "此卷已过期不可使用", userLottery)
  401. orm.Rollback()
  402. return entity.ErrorCode, "此卷已过期不可使用"
  403. }
  404. //2、奖券状态改为已使用
  405. userLottery.UseDate = time.Now().Local()
  406. userLottery.PrizeType = 1
  407. numb, err := orm.Table("user_prize").ID(userLottery.Id).Cols("prizeType", "useDate").Update(userLottery)
  408. if err != nil || numb == 0 {
  409. log.Println("修改用户奖券失败:", err)
  410. orm.Rollback()
  411. return entity.ErrorCode, "修改用户奖券失败"
  412. }
  413. orm.Commit()
  414. return entity.SuccessCode, "使用奖券成功"
  415. return entity.ErrorCode, "该奖券状态不可修改"
  416. }
  417. }
  418. //查询奖券对应的产品信息
  419. func (service *ActivityService)LotteryProduct(data *activity.Request) (int64, string, []entity.ProductJson) {
  420. orm := entity.Engine.NewSession()
  421. productList := []entity.ProductJson{}
  422. err := orm.Table("product").Alias("pr").
  423. Select("pr.*").
  424. Join("left", "activity a ", "FIND_IN_SET( pr.productCode,a.useProductList)").
  425. Join("left", "prize p ", "a.prizeId=p.Id").
  426. Join("left","lottery l" ,"l.prizeId=p.Id").
  427. Where("l.id=?", data.LotteryId).
  428. Find(&productList)
  429. if err != nil {
  430. log.Println("err:", err)
  431. return entity.ErrorCode, "查询奖券对应的产品信息失败", productList
  432. }
  433. return entity.SuccessCode, "查询奖券对应的产品信息成功", productList
  434. }
  435. //奖券的基本信息
  436. func (service *ActivityService)LotteryInfo(data *activity.Request) (int64, string, entity.UserPrizeJson) {
  437. orm := entity.Engine.NewSession()
  438. userLettryList := []entity.UserPrizeJson{}
  439. //已用的奖券
  440. err := orm.Table("user_prize").Alias("up").Select("up.*,p.instructions,p.remark,p.prizeType as lotteryType").
  441. Join("left", "activity a ", "a.prizeId=up.prizeId").
  442. Join("left", "prize p ", "a.prizeId=p.Id").
  443. Where("up.userId=? and up.appId=? and up.prizeType=0 and up.lotteryId=?",data.UserId,data.AppId,data.LotteryId).
  444. Find(&userLettryList)
  445. if err != nil {
  446. log.Println("err:", err)
  447. return entity.ErrorCode, "用户下的奖券查询失败", entity.UserPrizeJson{}
  448. }
  449. if len(userLettryList)==0{
  450. return entity.ErrorCode, "用户下的奖券查询失败", entity.UserPrizeJson{}
  451. }
  452. productList := []entity.ProductJson{}
  453. var code int64
  454. code,_,productList=service.LotteryProduct(data)
  455. if (code==0){
  456. return entity.ErrorCode, "查询奖券对应的产品信息失败", entity.UserPrizeJson{}
  457. }
  458. userLettryList[0].UseProductList=productList
  459. return entity.SuccessCode, "用户下的奖券查询成功", userLettryList[0]
  460. }