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

602 lines
20 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
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
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. "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) Block() {
  20. activityId := t.MustGetInt("activity_id")
  21. status := t.MustGet("status")
  22. module, exist, err := activity_service.GetModuleService(define.MODULE_ORDERLY, activityId)
  23. t.CheckErr(err)
  24. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "模块不存在")
  25. module.BesideRepeat = status
  26. _, err = models.Update(module.Id, module, "beside_repeat")
  27. t.CheckErr(err)
  28. t.SUCCESS("操作成功")
  29. }
  30. // 开启订单活动
  31. func (t *OrderDrawCtl) Switch() {
  32. activityId := t.MustGetInt("activity_id")
  33. status := t.MustGetInt("status")
  34. activity := models.Activity{}
  35. exist, err := models.Get(&activity, activityId)
  36. t.CheckErr(err)
  37. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  38. option := &models.CustomerOrderOption{}
  39. exist, err = option.GetByActivityId(activityId)
  40. t.CheckErr(err)
  41. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "订单活动不存在")
  42. if option.Status == status {
  43. t.SUCCESS("操作成功")
  44. return
  45. }
  46. option.Status = status
  47. _, err = models.Update(option.Id, option, "status")
  48. t.CheckErr(err)
  49. t.SUCCESS("操作成功")
  50. }
  51. // ===================== 订单抽奖
  52. // 开始抽奖
  53. func (t *OrderDrawCtl) Start() {
  54. ladderId := t.MustGetInt("order_draw_ladder_id")
  55. ladder := new(models.OrderDrawRuleLadder)
  56. exist, err := models.Get(ladder, ladderId)
  57. t.CheckErr(err)
  58. t.Assert(exist, code.MSG_ORDER_RULE_NOT_EXIST, "订单抽奖规则不存在")
  59. if ladder.Status != define.StatusNotBegin {
  60. t.ERROR(fmt.Sprintf("该活动%s", ladder.Status), code.MSG_ERR)
  61. }
  62. ladder.Status = define.StatusRunning
  63. ladder.UpdatedAt = time.Now()
  64. err = models.Save(map[string]interface{}{
  65. "id=": ladder.Id,
  66. "is_delete=": 0,
  67. }, ladder, "status", "updated_at")
  68. t.CheckErr(err)
  69. t.SUCCESS("操作成功")
  70. }
  71. // 开始抽奖
  72. func (t *OrderDrawCtl) Stop() {
  73. ladderId := t.MustGetInt("order_draw_ladder_id")
  74. ladder := new(models.OrderDrawRuleLadder)
  75. exist, err := models.Get(ladder, ladderId)
  76. t.CheckErr(err)
  77. t.Assert(exist, code.MSG_ORDER_RULE_NOT_EXIST, "订单抽奖规则不存在")
  78. if ladder.Status != define.StatusRunning {
  79. t.ERROR(fmt.Sprintf("该活动%s", ladder.Status), code.MSG_ERR)
  80. }
  81. ladder.Status = define.StatusEnding
  82. ladder.UpdatedAt = time.Now()
  83. err = models.Save(map[string]interface{}{
  84. "id=": ladder.Id,
  85. "is_delete=": 0,
  86. }, ladder, "status", "updated_at")
  87. t.CheckErr(err)
  88. t.SUCCESS("操作成功")
  89. }
  90. type OrderLadderResult struct {
  91. OrderDrawRuleId int `json:"order_draw_rule_id"`
  92. OrderDrawLadderId int `json:"order_draw_ladder_id"`
  93. PrizeName string `json:"prize_name"`
  94. Status string `json:"status"`
  95. PrizeImg string `json:"prize_img"`
  96. PrizeNumber int `json:"prize_number"`
  97. }
  98. type OrderListResult struct {
  99. OrderDrawActivityId int `json:"order_draw_activity_id"`
  100. OrderDrawRuleId int `json:"order_draw_rule_id"`
  101. OrderDrawActivityName string `json:"order_draw_activity_name"`
  102. OrderDrawLadders []*OrderLadderResult `json:"order_draw_ladders"`
  103. PrizeNumber int `json:"prize_number"`
  104. }
  105. //获取所有订单奖品
  106. func (t *OrderDrawCtl) List() {
  107. activityId := t.MustGetInt("activity_id")
  108. customerId := t.GetAccountId()
  109. //
  110. area := &models.AreaStore{}
  111. exist, err := area.GetByCustomerId(customerId, activityId)
  112. t.CheckErr(err)
  113. t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区信息异常")
  114. activity := models.Activity{}
  115. exist, err = models.Get(&activity, activityId)
  116. t.CheckErr(err)
  117. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动信息异常")
  118. // 订单的开启或关闭
  119. option := &models.CustomerOrderOption{}
  120. exist, err = option.GetByActivityId(activityId)
  121. t.CheckErr(err)
  122. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "订单信息异常")
  123. result := make([]*OrderListResult, 0)
  124. core.GetXormAuto().Table(&models.OrderDrawActivity{}).Alias("a").
  125. Select("a.id as order_draw_activity_id, r.id as order_draw_rule_id, a.order_draw_activity_name").
  126. Join("LEFT", (&models.OrderDrawRule{}).Alias("r"),
  127. "a.id=r.order_draw_activity_id and r.is_delete=0").
  128. Where("a.is_delete=0 and a.activity_id=?", activityId).Find(&result)
  129. ruleIds := make([]int, 0)
  130. for _, v := range result {
  131. ruleIds = append(ruleIds, v.OrderDrawRuleId)
  132. }
  133. ladders := make([]*OrderLadderResult, 0)
  134. err = core.GetXormAuto().Table(&models.OrderDrawRuleLadder{}).Alias("l").
  135. Select("id as order_draw_ladder_id, status, prize_name, prize_img, prize_number, order_draw_rule_id").
  136. Where("is_delete=0").In("order_draw_rule_id", ruleIds).Find(&ladders)
  137. t.CheckErr(err)
  138. ladderIds := make([]int, 0)
  139. for _, ladder := range ladders {
  140. ladderIds = append(ladderIds, ladder.OrderDrawLadderId)
  141. }
  142. records := make([]map[string]int, 0)
  143. err = core.GetXormAuto().Table(&models.OrderDrawRecord{}).Alias("r").
  144. Select("r.order_draw_rule_ladder_id as ladder_id, count(id) as num").
  145. Where("is_delete=0 and rehearsal_id=? and arch_id=? and area_id=?",
  146. activity.RehearsalId, activity.ArchId, area.Id).
  147. In("r.order_draw_rule_ladder_id", ladderIds).GroupBy("ladder_id").Find(&records)
  148. t.CheckErr(err)
  149. for i := range ladders {
  150. for j := range records {
  151. if ladders[i].OrderDrawLadderId == records[j]["ladder_id"] {
  152. ladders[i].PrizeNumber = ladders[i].PrizeNumber - records[j]["num"]
  153. }
  154. }
  155. }
  156. for i := range result {
  157. for j := range ladders {
  158. if result[i].OrderDrawRuleId == ladders[j].OrderDrawRuleId {
  159. result[i].PrizeNumber += ladders[j].PrizeNumber
  160. result[i].OrderDrawLadders = append(result[i].OrderDrawLadders, ladders[j])
  161. }
  162. }
  163. }
  164. t.JSON(map[string]interface{}{
  165. "total": len(result),
  166. "list": result,
  167. "status": option.Status,
  168. })
  169. }
  170. //抽奖奖品
  171. func (t *OrderDrawCtl) Prize() {
  172. ruleId := t.MustGetInt("order_draw_rule_id")
  173. list, err := models.GetOrderDrawRuleLaddersByRuleId(ruleId)
  174. t.CheckErr(err)
  175. t.JSON(map[string]interface{}{
  176. "total": len(list),
  177. "lise": list,
  178. })
  179. }
  180. type OrderUsersResult struct {
  181. UserId int `json:"user_id"`
  182. Username string `json:"username"`
  183. Avatar string `json:"avatar"`
  184. Phone string `json:"phone"`
  185. EntryPersonName string `json:"entry_person_name"`
  186. }
  187. //统计人数和订单数量
  188. func (t *OrderDrawCtl) 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. customer := models.Customer{}
  196. exist, err = models.Get(&customer, activity.CustomerId)
  197. t.CheckErr(err)
  198. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  199. moduleService, exist, err := activity_service.GetModuleService(define.MODULE_ORDERLY, activity.Id)
  200. t.CheckErr(err)
  201. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "模块服务不存在")
  202. result := make([]*OrderUsersResult, 0)
  203. s := core.GetXormAuto().NewSession()
  204. defer s.Close()
  205. if customer.IsSpecial == 2 {
  206. s = s.Table(&models.CustomerOrder{}).Alias("o").Select("o.buyer_id as user_id, "+
  207. " o.phone, o.order_entry_person_name as entry_person_name, u.nickname as username, u.avatar").
  208. Distinct("o.buyer_id").Join("LEFT", (&models.User{}).Alias("u"),
  209. "o.buyer_id=u.id and u.is_delete=0").Where("o.activity_id=? and o.is_delete=0 "+
  210. " and o.rehearsal_id=? and o.arch_id=?", activityId, activity.RehearsalId, activity.ArchId)
  211. if moduleService.BesideRepeat == define.MODULE_BESIDE_REPEAT {
  212. recordIds := make([]int, 0)
  213. err = core.GetXormAuto().Table(&models.OrderDrawRecord{}).Select("customer_order_id").
  214. Where("order_draw_rule_id=? and rehearsal_id=? and arch_id=? and is_delete=0",
  215. ruleId, activity.RehearsalId, activity.ArchId).Find(&recordIds)
  216. t.CheckErr(err)
  217. s = s.NotIn("o.id", recordIds)
  218. }
  219. } else {
  220. s = s.Table(&models.CustomerOrder{}).Alias("o").Select("o.buyer_id as user_id, "+
  221. " o.phone, o.order_entry_person_name as entry_person_name, u.nickname as username, u.avatar").
  222. Distinct("o.buyer_id").Join("LEFT", (&models.User{}).Alias("u"),
  223. "o.buyer_id=u.id and u.is_delete=0").Where("o.activity_id=? and o.is_delete=0 "+
  224. " and o.rehearsal_id=? and o.arch_id=?", activityId, activity.RehearsalId, activity.ArchId)
  225. if moduleService.BesideRepeat == define.MODULE_BESIDE_REPEAT {
  226. recordIds := make([]int, 0)
  227. err = core.GetXormAuto().Table(&models.OrderDrawRecord{}).Select("user_id").
  228. Where("order_draw_rule_id=? and rehearsal_id=? and arch_id=? and is_delete=0",
  229. ruleId, activity.RehearsalId, activity.ArchId).Find(&recordIds)
  230. t.CheckErr(err)
  231. s = s.NotIn("o.buyer_id", recordIds)
  232. }
  233. }
  234. err = s.Find(&result)
  235. t.CheckErr(err)
  236. t.JSON(map[string]interface{}{
  237. "total": len(result),
  238. "list": result,
  239. })
  240. }
  241. // 订单抽奖动作
  242. func (t *OrderDrawCtl) Draw() {
  243. activityId := t.MustGetInt("activity_id")
  244. ladderId := t.MustGetInt("order_draw_rule_ladder_id")
  245. number := t.MustGetInt("number")
  246. customerId := t.GetAccountId()
  247. customer := models.Customer{}
  248. exist, err := models.Get(&customer, customerId)
  249. t.CheckErr(err)
  250. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  251. activity := models.Activity{}
  252. exist, err = models.Get(&activity, activityId)
  253. t.CheckErr(err)
  254. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  255. t.CheckRunning(activity.Status)
  256. area := &models.AreaStore{}
  257. exist, err = area.GetByCustomerId(customerId, activityId)
  258. t.CheckErr(err)
  259. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "地区不存在")
  260. // 查询奖品
  261. ladder := models.OrderDrawRuleLadder{}
  262. exist, err = models.Get(&ladder, ladderId)
  263. t.CheckErr(err)
  264. t.Assert(exist, code.MSG_ORDER_LADDER_NOT_EXIST, "订单抽奖等级不存在")
  265. t.CheckRunning(ladder.Status)
  266. rule := models.OrderDrawRule{}
  267. exist, err = models.Get(&rule, ladder.OrderDrawRuleId)
  268. t.CheckErr(err)
  269. t.Assert(exist, code.MSG_ORDER_RULE_NOT_EXIST, "订单抽奖规则不存在")
  270. count, err := (&models.OrderDrawRecord{}).Count(ladder.Id, activity.RehearsalId, activity.ArchId)
  271. t.CheckErr(err)
  272. prizeNum := ladder.PrizeNumber - int(count)
  273. if prizeNum <= 0 || prizeNum < number {
  274. t.ERROR("奖品数量不足", code.MSG_LOTTERY_PRIZE_NOT_ENOUGH)
  275. }
  276. module, exist, err := activity_service.GetModuleService(define.MODULE_ORDERLY, activity.Id)
  277. t.CheckErr(err)
  278. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "模块服务不存在")
  279. if customer.IsSpecial == 2 {
  280. orders, err := lottery_service.GetSpecialOrderLottery(module.BesideRepeat, activity.Id, rule.Id, ladder.Id, activity.RehearsalId, area.Id, activity.ArchId)
  281. if len(orders) < number {
  282. t.ERROR("抽奖订单数量不足", code.MSG_LOTTERY_PEOPLE_NOT_ENOUGH)
  283. }
  284. lottery_service.RandSpecialOrderLottery(orders)
  285. winnerOrders := orders[:number]
  286. userIds := make([]int, 0)
  287. for _, v := range winnerOrders {
  288. userIds = append(userIds, v.BuyerId)
  289. }
  290. users, err := models.GetUsersByIds(userIds)
  291. t.CheckErr(err)
  292. winners := make([]*lottery_service.SpecialOrderLotteryUser, 0)
  293. t.CheckErr(err)
  294. for _, o := range winnerOrders {
  295. for _, u := range users {
  296. if u.Id == o.BuyerId {
  297. winners = append(winners, &lottery_service.SpecialOrderLotteryUser{
  298. UserId: u.Id,
  299. Username: o.Receiver,
  300. UserPhone: o.Phone,
  301. Avatar: u.Avatar,
  302. PrizeName: ladder.PrizeName,
  303. LadderId: ladder.Id,
  304. PrizeImg: ladder.PrizeImg,
  305. EntryPersonName: o.OrderEntryPersonName,
  306. })
  307. }
  308. }
  309. prize := models.UserPrize{}
  310. prize.ActivityId = activityId
  311. prize.RehearsalId = activity.RehearsalId
  312. prize.ActivityName = activity.Name
  313. prize.UserId = o.BuyerId
  314. prize.CustomerOrderId = o.Id
  315. prize.PrizeImg = ladder.PrizeImg
  316. prize.PrizeName = ladder.PrizeName
  317. prize.ArchId = activity.ArchId
  318. prize.PrizeType = 2
  319. _, err = models.Add(&prize)
  320. t.CheckErr(err)
  321. record := models.OrderDrawRecord{}
  322. record.UserPrizeId = prize.Id
  323. record.ActivityId = activityId
  324. record.RehearsalId = activity.RehearsalId
  325. record.ArchId = activity.ArchId
  326. record.OrderDrawActivityId = rule.OrderDrawActivityId
  327. record.OrderDrawRuleId = rule.Id
  328. record.UserId = o.BuyerId
  329. record.CustomerOrderId = o.BuyerId
  330. record.OrderDrawRuleLadderId = ladder.Id
  331. record.PrizeName = ladder.PrizeName
  332. record.OrderEntryPersonName = o.OrderEntryPersonName
  333. record.AreaId = area.Id
  334. record.AreaName = area.Name
  335. _, err = models.Add(&record)
  336. t.CheckErr(err)
  337. }
  338. t.JSON(winners)
  339. } else {
  340. userIds, err := lottery_service.GetOrderLotteryUserIds(module.BesideRepeat, activity.Id, rule.Id, ladder.Id, activity.RehearsalId, area.Id, activity.ArchId)
  341. if len(userIds) < number {
  342. t.ERROR("订单抽奖人数不足", code.MSG_LOTTERY_PEOPLE_NOT_ENOUGH)
  343. }
  344. lottery_service.RandLotteryUserIds(userIds)
  345. winnerIds := userIds[:number]
  346. winners, err := lottery_service.GetLotteryUsers(winnerIds)
  347. t.CheckErr(err)
  348. for i := range winners {
  349. winners[i].LadderId = ladder.Id
  350. winners[i].PrizeImg = ladder.PrizeImg
  351. winners[i].PrizeName = ladder.PrizeName
  352. prize := models.UserPrize{}
  353. prize.ActivityId = activityId
  354. prize.RehearsalId = activity.RehearsalId
  355. prize.ActivityName = activity.Name
  356. prize.UserId = winners[i].UserId
  357. prize.PrizeImg = ladder.PrizeImg
  358. prize.PrizeName = ladder.PrizeName
  359. prize.ArchId = activity.ArchId
  360. prize.PrizeType = 2
  361. _, err = models.Add(&prize)
  362. t.CheckErr(err)
  363. record := models.OrderDrawRecord{}
  364. record.UserPrizeId = prize.Id
  365. record.ActivityId = activityId
  366. record.RehearsalId = activity.RehearsalId
  367. record.ArchId = activity.ArchId
  368. record.OrderDrawActivityId = rule.OrderDrawActivityId
  369. record.OrderDrawRuleId = rule.Id
  370. record.UserId = winners[i].UserId
  371. record.OrderDrawRuleLadderId = ladder.Id
  372. record.PrizeName = ladder.PrizeName
  373. record.AreaId = area.Id
  374. record.AreaName = area.Name
  375. _, err = models.Add(&record)
  376. t.CheckErr(err)
  377. }
  378. t.JSON(winners)
  379. }
  380. }
  381. type WinnerResult struct {
  382. UserId int `json:"user_id"`
  383. Username string `json:"user_name"`
  384. UserPhone string `json:"user_phone"`
  385. Avatar string `json:"avatar"`
  386. PrizeName string `json:"prize_name"`
  387. }
  388. func (t *OrderDrawCtl) ListOfWinners() {
  389. ruleId := t.MustGetInt("order_draw_rule_id")
  390. rule := &models.OrderDrawRule{}
  391. exist, err := models.Get(rule, ruleId)
  392. t.CheckErr(err)
  393. t.Assert(exist, code.MSG_ORDER_RULE_NOT_EXIST, "订单抽奖规则不存在")
  394. lottery := &models.OrderDrawActivity{}
  395. exist, err = models.Get(lottery, rule.OrderDrawActivityId)
  396. t.CheckErr(err)
  397. t.Assert(exist, code.MSG_ORDER_RULE_NOT_EXIST, "订单抽奖规则不存在")
  398. activity := &models.Activity{}
  399. exist, err = models.Get(activity, lottery.ActivityId)
  400. t.CheckErr(err)
  401. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  402. customer := models.Customer{}
  403. exist, err = models.Get(&customer, activity.CustomerId)
  404. t.CheckErr(err)
  405. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  406. var result []*lottery_service.OrderWinnersResult
  407. if customer.IsSpecial == 2 {
  408. result, err = lottery_service.GetSpecialOrderWinnersResult(rule.Id, activity.RehearsalId, activity.ArchId)
  409. } else {
  410. result, err = lottery_service.GetOrderWinnersResult(rule.Id, activity.RehearsalId, activity.ArchId)
  411. }
  412. t.CheckErr(err)
  413. t.JSON(map[string]interface{}{
  414. "total": len(result),
  415. "list": result,
  416. })
  417. }
  418. // 获取所有订单
  419. type OrdersResult struct {
  420. UserId int `json:"user_id"`
  421. AreaName string `json:"area_name"`
  422. GoodsName string `json:"goods_name"`
  423. Nickname string `json:"nickname"`
  424. ExtraData string `json:"-"`
  425. Extra []map[string]interface{} `json:"extra"`
  426. }
  427. // 获取所有订单
  428. type SpecialOrdersResult struct {
  429. Id int `json:"id"`
  430. UserId int `json:"user_id"`
  431. AreaName string `json:"area_name"`
  432. EntryPersonName string `json:"entry_person_name"`
  433. Username string `json:"username"`
  434. Phone string `json:"phone"`
  435. Address string `json:"address"`
  436. Goods []map[string]string `json:"goods"`
  437. }
  438. func (t *OrderDrawCtl) Orders() {
  439. activityId := t.MustGetInt("activity_id")
  440. activity := models.Activity{}
  441. exist, err := models.Get(&activity, activityId)
  442. t.CheckErr(err)
  443. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  444. customer := models.Customer{}
  445. exist, err = models.Get(&customer, activity.CustomerId)
  446. t.CheckErr(err)
  447. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  448. // 下订单人数
  449. buyerCount, err := (&models.CustomerOrder{}).CountCustomerOrder(activity.Id, activity.RehearsalId, activity.ArchId)
  450. t.CheckErr(err)
  451. if customer.IsSpecial == 2 {
  452. orders := make([]*SpecialOrdersResult, 0)
  453. err = core.GetXormAuto().Table(&models.CustomerOrder{}).Alias("o").Select("o.id as id, "+
  454. " o.area_name as area_name, o.order_entry_person_name as entry_person_name, o.buyer_id as user_id, "+
  455. " o.receiver as username, o.phone as phone, o.address as address, o.created_at").
  456. Where("o.is_delete=0 and o.activity_id=? and o.rehearsal_id=? and o.arch_id=?",
  457. activityId, activity.RehearsalId, activity.ArchId).Asc("o.created_at").Find(&orders)
  458. t.CheckErr(err)
  459. orderIds := make([]int, 0)
  460. for _, o := range orders {
  461. orderIds = append(orderIds, o.Id)
  462. }
  463. subs, err := models.GetCustomerOrderSubsByOrderIds(orderIds)
  464. t.CheckErr(err)
  465. for i := range orders {
  466. for j := range subs {
  467. if fmt.Sprint(orders[i].Id) == subs[j]["order_id"] {
  468. orders[i].Goods = append(orders[i].Goods, subs[j])
  469. }
  470. }
  471. }
  472. t.JSON(map[string]interface{}{
  473. "orders": orders,
  474. "total": len(orders),
  475. "buyer_count": buyerCount,
  476. })
  477. return
  478. } else {
  479. orders := make([]*OrdersResult, 0)
  480. err = core.GetXormAuto().Table(&models.CustomerOrder{}).Alias("o").
  481. Select("o.area_name, o.goods_name, o.buyer_id as user_id, u.nickname, l.extra_data as extra_data, o.created_at").
  482. Distinct("o.id").Join("LEFT", (&models.InvitationLetter{}).Alias("l"),
  483. "o.buyer_id=l.user_id and o.activity_id=l.activity_id and l.arch_id=o.arch_id").
  484. Join("LEFT", (&models.User{}).Alias("u"), "u.id=o.buyer_id").
  485. Where("o.is_delete=0 and o.activity_id=? and o.rehearsal_id=? and o.arch_id=?",
  486. activityId, activity.RehearsalId, activity.ArchId).
  487. Asc("o.created_at").Find(&orders)
  488. t.CheckErr(err)
  489. items, err := invitation_service.GetOptionItem(activityId)
  490. t.CheckErr(err)
  491. for i := range orders {
  492. data, err := invitation_service.GetOptionValue(items, orders[i].ExtraData)
  493. t.CheckErr(err)
  494. orders[i].Extra = data
  495. }
  496. t.JSON(map[string]interface{}{
  497. "orders": orders,
  498. "total": len(orders),
  499. "buyer_count": buyerCount,
  500. })
  501. return
  502. }
  503. }
  504. // 录入人员排行榜
  505. func (t *OrderDrawCtl) OrderRank() {
  506. activityId := t.MustGetInt("activity_id")
  507. limit := t.MustGetInt("limit")
  508. activity := models.Activity{}
  509. exist, err := models.Get(&activity, activityId)
  510. t.CheckErr(err)
  511. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  512. order := &models.CustomerOrder{}
  513. res, err := order.SumCustomerOrder(activity.Id, activity.RehearsalId, activity.ArchId, limit)
  514. t.CheckErr(err)
  515. var total int64 = 0
  516. if len(res) == 1 && res[0]["num"] == "0" {
  517. res = nil
  518. } else {
  519. total, err = order.TotalCustomerOrderGoodsNum(activity.Id, activity.RehearsalId, activity.ArchId)
  520. }
  521. t.CheckErr(err)
  522. t.JSON(map[string]interface{}{
  523. "list": res,
  524. "total": total,
  525. })
  526. }