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

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