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

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