互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

451 lines
15 KiB

5 years ago
  1. package pc
  2. import (
  3. "fmt"
  4. "hudongzhuanjia/controllers"
  5. "hudongzhuanjia/models"
  6. activity_service "hudongzhuanjia/services/activity"
  7. invitation_service "hudongzhuanjia/services/invitation"
  8. lottery_service "hudongzhuanjia/services/lottery"
  9. "hudongzhuanjia/utils/code"
  10. "hudongzhuanjia/utils/define"
  11. "time"
  12. "github.com/ouxuanserver/osmanthuswine/src/core"
  13. )
  14. // 订单
  15. type OrderDrawCtl struct {
  16. controllers.AuthorCtl
  17. }
  18. // 开启订单活动
  19. func (t *OrderDrawCtl) Switch() {
  20. activityId := t.MustGetInt64("activity_id")
  21. status := t.MustGetInt("status")
  22. activity := new(models.Activity)
  23. exist, err := models.GetById(activity, activityId)
  24. t.CheckErr(err)
  25. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  26. option := new(models.CustomerOrderOption)
  27. exist, err = option.GetByActivityId(activityId)
  28. t.CheckErr(err)
  29. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "订单活动不存在")
  30. if option.Status == status {
  31. t.SUCCESS("操作成功")
  32. return
  33. }
  34. _, err = option.Switch(activityId, status)
  35. t.CheckErr(err)
  36. t.CheckErr(err)
  37. t.SUCCESS("操作成功")
  38. }
  39. // ===================== 订单抽奖
  40. // 开始抽奖
  41. func (t *OrderDrawCtl) Start() {
  42. ladderId := t.MustGetInt64("order_draw_ladder_id")
  43. ladder := new(models.OrderDrawRuleLadder)
  44. exist, err := models.GetById(ladder, ladderId)
  45. t.CheckErr(err)
  46. t.Assert(exist, code.MSG_ORDER_RULE_NOT_EXIST, "订单抽奖规则不存在")
  47. if ladder.Status != define.StatusNotBegin {
  48. t.ERROR(fmt.Sprintf("该活动%s", ladder.Status), code.MSG_ERR)
  49. }
  50. ladder.Status = define.StatusRunning
  51. ladder.UpdatedAt = time.Now()
  52. err = models.Save(map[string]interface{}{
  53. "id=": ladder.Id,
  54. "is_delete=": 0,
  55. }, ladder, "status", "updated_at")
  56. t.CheckErr(err)
  57. t.SUCCESS("操作成功")
  58. }
  59. // 开始抽奖
  60. func (t *OrderDrawCtl) Stop() {
  61. ladderId := t.MustGetInt64("order_draw_ladder_id")
  62. ladder := new(models.OrderDrawRuleLadder)
  63. exist, err := models.GetById(ladder, ladderId)
  64. t.CheckErr(err)
  65. t.Assert(exist, code.MSG_ORDER_RULE_NOT_EXIST, "订单抽奖规则不存在")
  66. if ladder.Status != define.StatusRunning {
  67. t.ERROR(fmt.Sprintf("该活动%s", ladder.Status), code.MSG_ERR)
  68. }
  69. ladder.Status = define.StatusEnding
  70. ladder.UpdatedAt = time.Now()
  71. err = models.Save(map[string]interface{}{
  72. "id=": ladder.Id,
  73. "is_delete=": 0,
  74. }, ladder, "status", "updated_at")
  75. t.CheckErr(err)
  76. t.SUCCESS("操作成功")
  77. }
  78. type OrderListResult struct {
  79. OrderDrawActivityId int64 `json:"order_draw_activity_id"`
  80. OrderDrawRuleId int64 `json:"order_draw_rule_id"`
  81. OrderDrawActivityName string `json:"order_draw_activity_name"`
  82. OrderDrawLadders []*OrderLadderResult `json:"order_draw_ladders"`
  83. PrizeNumber int64 `json:"prize_number"`
  84. }
  85. type OrderLadderResult struct {
  86. OrderDrawRuleId int64 `json:"order_draw_rule_id"`
  87. OrderDrawLadderId int64 `json:"order_draw_ladder_id"`
  88. PrizeName string `json:"prize_name"`
  89. Status string `json:"status"`
  90. PrizeImg string `json:"prize_img"`
  91. PrizeNumber int64 `json:"prize_number"`
  92. }
  93. //获取所有订单奖品
  94. func (t *OrderDrawCtl) List() {
  95. activityId := t.MustGetInt64("activity_id")
  96. activity := new(models.Activity)
  97. exist, err := models.GetById(activity, activityId)
  98. t.CheckErr(err)
  99. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  100. // 订单的开启或关闭
  101. order := new(models.CustomerOrderOption)
  102. exist, err = order.GetByActivityId(activityId)
  103. t.CheckErr(err)
  104. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "订单活动不存在")
  105. result := make([]*OrderListResult, 0)
  106. core.GetXormAuto().Table(new(models.OrderDrawActivity)).Alias("a").
  107. Select("a.id as order_draw_activity_id, r.id as order_draw_rule_id, a.order_draw_activity_name").
  108. Join("LEFT", new(models.OrderDrawRule).Alias("r"),
  109. "a.id=r.order_draw_activity_id and r.is_delete=0").
  110. Where("a.is_delete=0 and a.activity_id=?", activityId).Find(&result)
  111. // 多地区的一个坑:分配给主会场的area_id不是主账号的area_id
  112. area := new(models.AreaStore)
  113. if t.MustGetCustomerPid() == 0 {
  114. exist, err = area.GetMainAreaById(activityId)
  115. } else {
  116. exist, err = area.GetAreaStoreById(t.MustGetAreaId())
  117. }
  118. t.CheckErr(err)
  119. t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
  120. ruleIds := make([]int64, 0)
  121. for _, v := range result {
  122. ruleIds = append(ruleIds, v.OrderDrawRuleId)
  123. }
  124. ladders := make([]*OrderLadderResult, 0)
  125. err = core.GetXormAuto().Table(new(models.OrderDrawRuleLadder)).Alias("l").
  126. Select("id as order_draw_ladder_id, status, prize_name, prize_img, prize_number, order_draw_rule_id").
  127. Where("is_delete=0").In("order_draw_rule_id", ruleIds).Find(&ladders)
  128. t.CheckErr(err)
  129. ladderIds := make([]int64, 0)
  130. for _, ladder := range ladders {
  131. ladderIds = append(ladderIds, ladder.OrderDrawLadderId)
  132. }
  133. records := make([]map[string]int64, 0)
  134. err = core.GetXormAuto().Table(new(models.OrderDrawRecord)).Alias("r").
  135. Select("r.order_draw_rule_ladder_id as ladder_id, count(id) as num").
  136. Where("is_delete=0 and rehearsal_id=?", activity.RehearsalId).
  137. In("r.order_draw_rule_ladder_id", ladderIds).GroupBy("ladder_id").Find(&records)
  138. t.CheckErr(err)
  139. for i := range ladders {
  140. for j := range records {
  141. if ladders[i].OrderDrawLadderId == records[j]["ladder_id"] {
  142. ladders[i].PrizeNumber = ladders[i].PrizeNumber - records[j]["num"]
  143. }
  144. }
  145. }
  146. for i := range result {
  147. for j := range ladders {
  148. if result[i].OrderDrawRuleId == ladders[j].OrderDrawRuleId {
  149. result[i].PrizeNumber += ladders[j].PrizeNumber
  150. result[i].OrderDrawLadders = append(result[i].OrderDrawLadders, ladders[j])
  151. }
  152. }
  153. }
  154. t.JSON(map[string]interface{}{
  155. "total": len(result),
  156. "list": result,
  157. "status": order.Status,
  158. })
  159. }
  160. //抽奖奖品
  161. func (t *OrderDrawCtl) Prize() {
  162. ruleId := t.MustGetInt64("order_draw_rule_id")
  163. list := make([]*models.OrderDrawRuleLadder, 0)
  164. err := core.GetXormAuto().Where("is_delete=0 and order_draw_rule_id=?", ruleId).Find(&list)
  165. t.CheckErr(err)
  166. for index := range list {
  167. list[index].Des = "在该活动的所有订单用户中随机抽奖品数量的用户"
  168. }
  169. t.JSON(map[string]interface{}{
  170. "total": len(list),
  171. "lise": list,
  172. })
  173. }
  174. type OrderUsersResult struct {
  175. UserId int64 `json:"user_id"`
  176. Username string `json:"username"`
  177. Avatar string `json:"avatar"`
  178. }
  179. //统计人数和订单数量
  180. func (t *OrderDrawCtl) Users() {
  181. activityId := t.MustGetInt64("activity_id")
  182. ruleId := t.MustGetInt64("order_draw_rule_id")
  183. activity := new(models.Activity)
  184. exist, err := models.GetById(activity, activityId)
  185. t.CheckErr(err)
  186. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  187. result := make([]*OrderUsersResult, 0)
  188. session := core.GetXormAuto().Table(new(models.CustomerOrder)).Alias("o").
  189. Select("o.buyer_id as user_id, u.nickname as username, u.avatar").Distinct("o.buyer_id").
  190. Join("LEFT", new(models.User).Alias("u"), "o.buyer_id=u.id and u.is_delete=0").
  191. Where("o.activity_id=? and o.is_delete=0 and o.rehearsal_id=?",
  192. activityId, activity.RehearsalId)
  193. moduleService, exist, err := activity_service.GetModuleService(define.MODULE_ORDERLY, activity.Id)
  194. t.CheckErr(err)
  195. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "模块服务不存在")
  196. if moduleService.BesideRepeat == define.MODULE_BESIDE_REPEAT {
  197. recordIds := make([]int64, 0)
  198. err = core.GetXormAuto().Table(new(models.OrderDrawRecord)).Select("user_id").
  199. Where("order_draw_rule_id=? and rehearsal_id=? and is_delete=0",
  200. ruleId, activity.RehearsalId).Find(&recordIds)
  201. t.CheckErr(err)
  202. session = session.NotIn("o.buyer_id", recordIds)
  203. }
  204. err = session.Find(&result)
  205. t.CheckErr(err)
  206. t.JSON(map[string]interface{}{
  207. "total": len(result),
  208. "list": result,
  209. })
  210. }
  211. // 订单抽奖动作
  212. func (t *OrderDrawCtl) Draw() {
  213. activityId := t.MustGetInt64("activity_id")
  214. ruleId := t.MustGetInt64("order_draw_rule_id")
  215. ladderId := t.MustGetInt64("order_draw_rule_ladder_id")
  216. number := t.MustGetInt("number")
  217. activity := new(models.Activity)
  218. exist, err := models.GetById(activity, activityId)
  219. t.CheckErr(err)
  220. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  221. t.CheckRunning(activity.Status)
  222. area := new(models.AreaStore)
  223. if t.MustGetCustomerPid() == 0 {
  224. exist, err = area.GetMainAreaById(activity.Id)
  225. } else {
  226. exist, err = area.GetAreaStoreById(t.MustGetAreaId())
  227. }
  228. t.CheckErr(err)
  229. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "地区不存在")
  230. rule := new(models.OrderDrawRule)
  231. exist, err = models.GetById(rule, ruleId)
  232. t.CheckErr(err)
  233. t.Assert(exist, code.MSG_ORDER_RULE_NOT_EXIST, "订单抽奖规则不存在")
  234. //t.CheckRunning(rule.OrderDrawStatus)
  235. // 查询奖品
  236. ladder := new(models.OrderDrawRuleLadder)
  237. exist, err = models.GetById(ladder, ladderId)
  238. t.CheckErr(err)
  239. t.Assert(exist, code.MSG_ORDER_LADDER_NOT_EXIST, "订单抽奖等级不存在")
  240. t.CheckRunning(ladder.Status)
  241. count, err := core.GetXormAuto().Where("order_draw_rule_id=? and order_draw_rule_ladder_id=? "+
  242. "and rehearsal_id=? and is_delete=0", ruleId, ladderId, activity.RehearsalId).Count(new(models.OrderDrawRecord))
  243. t.CheckErr(err)
  244. prizeNum := ladder.PrizeNumber - int(count)
  245. if prizeNum <= 0 || prizeNum < number {
  246. t.ERROR("奖品数量不足", code.MSG_LOTTERY_PRIZE_NOT_ENOUGH)
  247. }
  248. moduleService, exist, err := activity_service.GetModuleService(define.MODULE_ORDERLY, activity.Id)
  249. t.CheckErr(err)
  250. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "模块服务不存在")
  251. orderUsers := make([]*lottery_service.LotteryUser, 0)
  252. if moduleService.BesideRepeat == define.MODULE_BESIDE_REPEAT {
  253. //查询已经中奖的用户,剔除已经中奖的用户
  254. recordIds := make([]int64, 0)
  255. err = core.GetXormAuto().Table(new(models.OrderDrawRecord)).Select("user_id").
  256. Where("order_draw_rule_id=? and rehearsal_id=? and is_delete=0",
  257. ruleId, activity.RehearsalId).Find(&recordIds)
  258. err = core.GetXormAuto().Table(new(models.CustomerOrder)).
  259. Select("buyer_id as user_id").Distinct("buyer_id").
  260. Where("activity_id=? and rehearsal_id=? and is_delete=0",
  261. activity.Id, activity.RehearsalId).NotIn("buyer_id", recordIds).
  262. Find(&orderUsers)
  263. t.CheckErr(err)
  264. if len(orderUsers) < 0 {
  265. t.ERROR("剔除已经中奖的用户后,已经没有用户可以抽了", code.MSG_ERR)
  266. }
  267. } else {
  268. // 不去除
  269. err = core.GetXormAuto().Table(new(models.CustomerOrder)).Select("buyer_id as user_id").Distinct("buyer_id").
  270. Where("activity_id=? and rehearsal_id=? and is_delete=0",
  271. activity.Id, activity.RehearsalId).Find(&orderUsers)
  272. t.CheckErr(err)
  273. }
  274. if len(orderUsers) < number {
  275. t.ERROR("订单抽奖人数不足", code.MSG_LOTTERY_PEOPLE_NOT_ENOUGH)
  276. }
  277. lottery_service.RandLotteryUser(orderUsers)
  278. winners := orderUsers[:number]
  279. userIds := make([]int64, 0)
  280. for _, winner := range winners {
  281. userIds = append(userIds, winner.UserId)
  282. }
  283. users := make([]*models.User, 0)
  284. err = core.GetXormAuto().Where("is_delete=0").In("id", userIds).Find(&users)
  285. t.CheckErr(err)
  286. for i := range winners {
  287. for j := range users {
  288. if winners[i].UserId == users[j].Id {
  289. winners[i].Username = users[j].Nickname
  290. winners[i].Avatar = users[j].Avatar
  291. winners[i].UserPhone = users[j].Phone
  292. winners[i].LadderId = ladder.Id
  293. winners[i].PrizeImg = ladder.PrizeImg
  294. winners[i].PrizeName = ladder.PrizeName
  295. break
  296. }
  297. }
  298. // 订单抽奖 ==> 存入我的奖品
  299. userPrize := new(models.UserPrize)
  300. userPrize.ActivityId = activityId
  301. userPrize.RehearsalId = activity.RehearsalId
  302. userPrize.ActivityName = activity.Name
  303. userPrize.UserId = winners[i].UserId
  304. userPrize.PrizeImg = ladder.PrizeImg
  305. userPrize.PrizeName = ladder.PrizeName
  306. userPrize.PrizeType = 2
  307. userPrize.IsDelete = false
  308. userPrize.CreatedAt = time.Now()
  309. userPrize.UpdatedAt = time.Now()
  310. _, err = core.GetXormAuto().Insert(userPrize)
  311. t.CheckErr(err)
  312. record := new(models.OrderDrawRecord)
  313. record.UserPrizeId = userPrize.Id
  314. record.ActivityId = activityId
  315. record.RehearsalId = activity.RehearsalId
  316. record.OrderDrawActivityId = rule.OrderDrawActivityId
  317. record.OrderDrawRuleId = rule.Id
  318. record.UserId = winners[i].UserId
  319. record.OrderDrawRuleLadderId = ladder.Id
  320. record.PrizeName = ladder.PrizeName
  321. record.AreaId = area.Id
  322. record.AreaName = area.Name
  323. record.IsDelete = false
  324. record.CreatedAt = time.Now()
  325. record.UpdatedAt = time.Now()
  326. _, err = core.GetXormAuto().InsertOne(record)
  327. t.CheckErr(err)
  328. }
  329. t.RAW(winners)
  330. }
  331. type WinnerResult struct {
  332. UserId int64 `json:"user_id"`
  333. Username string `json:"user_name"`
  334. UserPhone string `json:"user_phone"`
  335. Avatar string `json:"avatar"`
  336. PrizeName string `json:"prize_name"`
  337. }
  338. func (t *OrderDrawCtl) ListOfWinners() {
  339. orderDrawRuleId := t.MustGetInt64("order_draw_rule_id")
  340. rehearsalId := t.MustGetInt64("rehearsal_id")
  341. result := make([]*WinnerResult, 0)
  342. err := core.GetXormAuto().Table(new(models.OrderDrawRecord)).Alias("r").
  343. Select("r.user_id, u.nickname as username, u.phone as user_phone, u.avatar, r.prize_name").
  344. Join("LEFT", new(models.User).Alias("u"), "u.id=r.user_id and u.is_delete=0").
  345. Where("r.is_delete=0 and r.order_draw_rule_id=? and r.rehearsal_id=?",
  346. orderDrawRuleId, rehearsalId).Find(&result)
  347. t.CheckErr(err)
  348. t.JSON(map[string]interface{}{
  349. "total": len(result),
  350. "list": result,
  351. })
  352. }
  353. // 获取所有订单
  354. type OrdersResult struct {
  355. UserId int64 `json:"user_id"`
  356. AreaName string `json:"area_name"`
  357. GoodsName string `json:"goods_name"`
  358. Nickname string `json:"nickname"`
  359. ExtraData string `json:"-"`
  360. Extra []map[string]interface{} `json:"extra"`
  361. }
  362. func (t *OrderDrawCtl) Orders() {
  363. activityId := t.MustGetInt64("activity_id")
  364. rehearsalId := t.MustGetInt64("rehearsal_id")
  365. orders := make([]*OrdersResult, 0)
  366. err := core.GetXormAuto().Table(new(models.CustomerOrder)).Alias("o").
  367. Select("o.area_name, o.goods_name, o.buyer_id as user_id, u.nickname, l.extra_data as extra_data, o.created_at").
  368. Distinct("o.id").Join("LEFT", new(models.InvitationLetter).Alias("l"),
  369. "o.buyer_id=l.user_id and o.activity_id=l.activity_id and l.is_delete=0").
  370. Join("LEFT", new(models.User).Alias("u"), "u.id=o.buyer_id and u.is_delete=0").
  371. Where("o.is_delete=0 and o.activity_id=? and o.rehearsal_id=?", activityId, rehearsalId).
  372. Asc("o.created_at").Find(&orders)
  373. t.CheckErr(err)
  374. items, err := invitation_service.GetOptionItem(activityId)
  375. t.CheckErr(err)
  376. for i := range orders {
  377. data, err := invitation_service.GetOptionValue(items, orders[i].ExtraData)
  378. t.CheckErr(err)
  379. orders[i].Extra = data
  380. }
  381. // 下订单人数
  382. buyerCount, err := core.GetXormAuto().Where("is_delete=0 and activity_id=? and rehearsal_id=?",
  383. activityId, rehearsalId).Distinct("buyer_id").Count(new(models.CustomerOrder))
  384. t.CheckErr(err)
  385. t.JSON(map[string]interface{}{
  386. "orders": orders,
  387. "total": len(orders),
  388. "buyer_count": buyerCount,
  389. })
  390. }