互动
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.

414 lines
13 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package pc
  2. import (
  3. "fmt"
  4. "github.com/ouxuanserver/osmanthuswine/src/core"
  5. "hudongzhuanjia/controllers"
  6. "hudongzhuanjia/models"
  7. activity_service "hudongzhuanjia/services/activity"
  8. invitation_service "hudongzhuanjia/services/invitation"
  9. lottery_service "hudongzhuanjia/services/lottery"
  10. "hudongzhuanjia/utils/code"
  11. "hudongzhuanjia/utils/define"
  12. "time"
  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.Get(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.Get(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.Get(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. customerId := t.MustGetUID()
  97. customer := new(models.Customer)
  98. exist, err := models.Get(customer, customerId)
  99. t.CheckErr(err)
  100. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  101. activity := new(models.Activity)
  102. exist, err = models.Get(activity, activityId)
  103. t.CheckErr(err)
  104. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  105. // 订单的开启或关闭
  106. order := new(models.CustomerOrderOption)
  107. exist, err = order.GetByActivityId(activityId)
  108. t.CheckErr(err)
  109. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "订单活动不存在")
  110. result := make([]*OrderListResult, 0)
  111. core.GetXormAuto().Table(new(models.OrderDrawActivity)).Alias("a").
  112. Select("a.id as order_draw_activity_id, r.id as order_draw_rule_id, a.order_draw_activity_name").
  113. Join("LEFT", new(models.OrderDrawRule).Alias("r"),
  114. "a.id=r.order_draw_activity_id and r.is_delete=0").
  115. Where("a.is_delete=0 and a.activity_id=?", activityId).Find(&result)
  116. // 多地区的一个坑:分配给主会场的area_id不是主账号的area_id
  117. area := new(models.AreaStore)
  118. if customer.Pid == 0 {
  119. exist, err = area.GetMainAreaById(activityId)
  120. } else {
  121. exist, err = area.GetAreaStoreById(t.MustGetAreaId())
  122. }
  123. t.CheckErr(err)
  124. t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
  125. ruleIds := make([]int64, 0)
  126. for _, v := range result {
  127. ruleIds = append(ruleIds, v.OrderDrawRuleId)
  128. }
  129. ladders := make([]*OrderLadderResult, 0)
  130. err = core.GetXormAuto().Table(new(models.OrderDrawRuleLadder)).Alias("l").
  131. Select("id as order_draw_ladder_id, status, prize_name, prize_img, prize_number, order_draw_rule_id").
  132. Where("is_delete=0").In("order_draw_rule_id", ruleIds).Find(&ladders)
  133. t.CheckErr(err)
  134. ladderIds := make([]int64, 0)
  135. for _, ladder := range ladders {
  136. ladderIds = append(ladderIds, ladder.OrderDrawLadderId)
  137. }
  138. records := make([]map[string]int64, 0)
  139. err = core.GetXormAuto().Table(new(models.OrderDrawRecord)).Alias("r").
  140. Select("r.order_draw_rule_ladder_id as ladder_id, count(id) as num").
  141. Where("is_delete=0 and rehearsal_id=?", activity.RehearsalId).
  142. In("r.order_draw_rule_ladder_id", ladderIds).GroupBy("ladder_id").Find(&records)
  143. t.CheckErr(err)
  144. for i := range ladders {
  145. for j := range records {
  146. if ladders[i].OrderDrawLadderId == records[j]["ladder_id"] {
  147. ladders[i].PrizeNumber = ladders[i].PrizeNumber - records[j]["num"]
  148. }
  149. }
  150. }
  151. for i := range result {
  152. for j := range ladders {
  153. if result[i].OrderDrawRuleId == ladders[j].OrderDrawRuleId {
  154. result[i].PrizeNumber += ladders[j].PrizeNumber
  155. result[i].OrderDrawLadders = append(result[i].OrderDrawLadders, ladders[j])
  156. }
  157. }
  158. }
  159. t.JSON(map[string]interface{}{
  160. "total": len(result),
  161. "list": result,
  162. "status": order.Status,
  163. })
  164. }
  165. //抽奖奖品
  166. func (t *OrderDrawCtl) Prize() {
  167. ruleId := t.MustGetInt64("order_draw_rule_id")
  168. list := make([]*models.OrderDrawRuleLadder, 0)
  169. err := core.GetXormAuto().Where("is_delete=0 and order_draw_rule_id=?", ruleId).Find(&list)
  170. t.CheckErr(err)
  171. for index := range list {
  172. list[index].Des = "在该活动的所有订单用户中随机抽奖品数量的用户"
  173. }
  174. t.JSON(map[string]interface{}{
  175. "total": len(list),
  176. "lise": list,
  177. })
  178. }
  179. type OrderUsersResult struct {
  180. UserId int64 `json:"user_id"`
  181. Username string `json:"username"`
  182. Avatar string `json:"avatar"`
  183. }
  184. //统计人数和订单数量
  185. func (t *OrderDrawCtl) Users() {
  186. activityId := t.MustGetInt64("activity_id")
  187. ruleId := t.MustGetInt64("order_draw_rule_id")
  188. activity := new(models.Activity)
  189. exist, err := models.Get(activity, activityId)
  190. t.CheckErr(err)
  191. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  192. result := make([]*OrderUsersResult, 0)
  193. session := core.GetXormAuto().Table(new(models.CustomerOrder)).Alias("o").
  194. Select("o.buyer_id as user_id, u.nickname as username, u.avatar").Distinct("o.buyer_id").
  195. Join("LEFT", new(models.User).Alias("u"), "o.buyer_id=u.id and u.is_delete=0").
  196. Where("o.activity_id=? and o.is_delete=0 and o.rehearsal_id=?",
  197. activityId, activity.RehearsalId)
  198. moduleService, exist, err := activity_service.GetModuleService(define.MODULE_ORDERLY, activity.Id)
  199. t.CheckErr(err)
  200. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "模块服务不存在")
  201. if moduleService.BesideRepeat == define.MODULE_BESIDE_REPEAT {
  202. recordIds := make([]int64, 0)
  203. err = core.GetXormAuto().Table(new(models.OrderDrawRecord)).Select("user_id").
  204. Where("order_draw_rule_id=? and rehearsal_id=? and is_delete=0",
  205. ruleId, activity.RehearsalId).Find(&recordIds)
  206. t.CheckErr(err)
  207. session = session.NotIn("o.buyer_id", recordIds)
  208. }
  209. err = session.Find(&result)
  210. t.CheckErr(err)
  211. t.JSON(map[string]interface{}{
  212. "total": len(result),
  213. "list": result,
  214. })
  215. }
  216. // 订单抽奖动作
  217. func (t *OrderDrawCtl) Draw() {
  218. activityId := t.MustGetInt64("activity_id")
  219. ruleId := t.MustGetInt64("order_draw_rule_id")
  220. ladderId := t.MustGetInt64("order_draw_rule_ladder_id")
  221. number := t.MustGetInt("number")
  222. customerId := t.MustGetUID()
  223. customer := new(models.Customer)
  224. exist, err := models.Get(customer, customerId)
  225. t.CheckErr(err)
  226. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  227. activity := new(models.Activity)
  228. exist, err = models.Get(activity, activityId)
  229. t.CheckErr(err)
  230. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  231. t.CheckRunning(activity.Status)
  232. area := new(models.AreaStore)
  233. exist, err = area.GetByCustomerId(customerId, activityId)
  234. t.CheckErr(err)
  235. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "地区不存在")
  236. rule := new(models.OrderDrawRule)
  237. exist, err = models.Get(rule, ruleId)
  238. t.CheckErr(err)
  239. t.Assert(exist, code.MSG_ORDER_RULE_NOT_EXIST, "订单抽奖规则不存在")
  240. // 查询奖品
  241. ladder := new(models.OrderDrawRuleLadder)
  242. exist, err = models.Get(ladder, ladderId)
  243. t.CheckErr(err)
  244. t.Assert(exist, code.MSG_ORDER_LADDER_NOT_EXIST, "订单抽奖等级不存在")
  245. t.CheckRunning(ladder.Status)
  246. count, err := core.GetXormAuto().Where("order_draw_rule_id=? and order_draw_rule_ladder_id=? "+
  247. "and rehearsal_id=? and is_delete=0", ruleId, ladderId, activity.RehearsalId).Count(new(models.OrderDrawRecord))
  248. t.CheckErr(err)
  249. prizeNum := ladder.PrizeNumber - int(count)
  250. if prizeNum <= 0 || prizeNum < number {
  251. t.ERROR("奖品数量不足", code.MSG_LOTTERY_PRIZE_NOT_ENOUGH)
  252. }
  253. module, exist, err := activity_service.GetModuleService(define.MODULE_ORDERLY, activity.Id)
  254. t.CheckErr(err)
  255. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "模块服务不存在")
  256. userIds, err := lottery_service.GetOrderLotteryUserIds(module.BesideRepeat, activity.Id, rule.Id, ladder.Id, activity.RehearsalId, area.Id)
  257. if len(userIds) < number {
  258. t.ERROR("订单抽奖人数不足", code.MSG_LOTTERY_PEOPLE_NOT_ENOUGH)
  259. }
  260. lottery_service.RandLotteryUserIds(userIds)
  261. winnerIds := userIds[:number]
  262. winners, err := lottery_service.GetLotteryUsers(winnerIds)
  263. t.CheckErr(err)
  264. for i := range winners {
  265. winners[i].LadderId = ladder.Id
  266. winners[i].PrizeImg = ladder.PrizeImg
  267. winners[i].PrizeName = ladder.PrizeName
  268. // 订单抽奖 ==> 存入我的奖品
  269. userPrize := new(models.UserPrize)
  270. userPrize.ActivityId = activityId
  271. userPrize.RehearsalId = activity.RehearsalId
  272. userPrize.ActivityName = activity.Name
  273. userPrize.UserId = winners[i].UserId
  274. userPrize.PrizeImg = ladder.PrizeImg
  275. userPrize.PrizeName = ladder.PrizeName
  276. userPrize.PrizeType = 2
  277. userPrize.IsDelete = false
  278. userPrize.CreatedAt = time.Now()
  279. userPrize.UpdatedAt = time.Now()
  280. _, err = core.GetXormAuto().Insert(userPrize)
  281. t.CheckErr(err)
  282. record := new(models.OrderDrawRecord)
  283. record.UserPrizeId = userPrize.Id
  284. record.ActivityId = activityId
  285. record.RehearsalId = activity.RehearsalId
  286. record.OrderDrawActivityId = rule.OrderDrawActivityId
  287. record.OrderDrawRuleId = rule.Id
  288. record.UserId = winners[i].UserId
  289. record.OrderDrawRuleLadderId = ladder.Id
  290. record.PrizeName = ladder.PrizeName
  291. record.AreaId = area.Id
  292. record.AreaName = area.Name
  293. record.IsDelete = false
  294. record.CreatedAt = time.Now()
  295. record.UpdatedAt = time.Now()
  296. _, err = core.GetXormAuto().InsertOne(record)
  297. t.CheckErr(err)
  298. }
  299. t.JSON(winners)
  300. }
  301. type WinnerResult struct {
  302. UserId int64 `json:"user_id"`
  303. Username string `json:"user_name"`
  304. UserPhone string `json:"user_phone"`
  305. Avatar string `json:"avatar"`
  306. PrizeName string `json:"prize_name"`
  307. }
  308. func (t *OrderDrawCtl) ListOfWinners() {
  309. orderDrawRuleId := t.MustGetInt64("order_draw_rule_id")
  310. rehearsalId := t.MustGetInt64("rehearsal_id")
  311. result, err := lottery_service.GetOrderWinnersResult(orderDrawRuleId, rehearsalId)
  312. t.CheckErr(err)
  313. t.JSON(map[string]interface{}{
  314. "total": len(result),
  315. "list": result,
  316. })
  317. }
  318. // 获取所有订单
  319. type OrdersResult struct {
  320. UserId int64 `json:"user_id"`
  321. AreaName string `json:"area_name"`
  322. GoodsName string `json:"goods_name"`
  323. Nickname string `json:"nickname"`
  324. ExtraData string `json:"-"`
  325. Extra []map[string]interface{} `json:"extra"`
  326. }
  327. func (t *OrderDrawCtl) Orders() {
  328. activityId := t.MustGetInt64("activity_id")
  329. rehearsalId := t.MustGetInt64("rehearsal_id")
  330. orders := make([]*OrdersResult, 0)
  331. err := core.GetXormAuto().Table(new(models.CustomerOrder)).Alias("o").
  332. Select("o.area_name, o.goods_name, o.buyer_id as user_id, u.nickname, l.extra_data as extra_data, o.created_at").
  333. Distinct("o.id").Join("LEFT", new(models.InvitationLetter).Alias("l"),
  334. "o.buyer_id=l.user_id and o.activity_id=l.activity_id and l.is_delete=0").
  335. Join("LEFT", new(models.User).Alias("u"), "u.id=o.buyer_id and u.is_delete=0").
  336. Where("o.is_delete=0 and o.activity_id=? and o.rehearsal_id=?", activityId, rehearsalId).
  337. Asc("o.created_at").Find(&orders)
  338. t.CheckErr(err)
  339. items, err := invitation_service.GetOptionItem(activityId)
  340. t.CheckErr(err)
  341. for i := range orders {
  342. data, err := invitation_service.GetOptionValue(items, orders[i].ExtraData)
  343. t.CheckErr(err)
  344. orders[i].Extra = data
  345. }
  346. // 下订单人数
  347. buyerCount, err := core.GetXormAuto().Where("is_delete=0 and activity_id=? and rehearsal_id=?",
  348. activityId, rehearsalId).Distinct("buyer_id").Count(new(models.CustomerOrder))
  349. t.CheckErr(err)
  350. t.JSON(map[string]interface{}{
  351. "orders": orders,
  352. "total": len(orders),
  353. "buyer_count": buyerCount,
  354. })
  355. }