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

601 lines
20 KiB

5 years ago
5 years ago
4 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
4 years ago
4 years ago
5 years ago
4 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
4 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
4 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
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 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. "git.ouxuan.net/tommy/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, o.receiver as username").
  208. Where("o.is_delete=0 and o.activity_id=? and o.rehearsal_id=? and o.arch_id=?",
  209. activity.Id, activity.RehearsalId, activity.ArchId)
  210. if moduleService.BesideRepeat == define.MODULE_BESIDE_REPEAT {
  211. recordIds := make([]int, 0)
  212. err = core.GetXormAuto().Table(&models.OrderDrawRecord{}).Select("customer_order_id").
  213. Where("activity_id=? and rehearsal_id=? and arch_id=? and is_delete=0",
  214. activity.Id, activity.RehearsalId, activity.ArchId).Find(&recordIds)
  215. t.CheckErr(err)
  216. s = s.NotIn("o.id", recordIds)
  217. }
  218. } else {
  219. s = s.Table(&models.CustomerOrder{}).Alias("o").Select("o.buyer_id as user_id, "+
  220. " o.phone, o.order_entry_person_name as entry_person_name, u.nickname as username, u.avatar").
  221. Distinct("o.buyer_id").Join("LEFT", (&models.User{}).Alias("u"),
  222. "o.buyer_id=u.id and u.is_delete=0").Where("o.activity_id=? and o.is_delete=0 "+
  223. " and o.rehearsal_id=? and o.arch_id=?", activityId, activity.RehearsalId, activity.ArchId)
  224. if moduleService.BesideRepeat == define.MODULE_BESIDE_REPEAT {
  225. recordIds := make([]int, 0)
  226. err = core.GetXormAuto().Table(&models.OrderDrawRecord{}).Select("user_id").
  227. Where("order_draw_rule_id=? and rehearsal_id=? and arch_id=? and is_delete=0",
  228. ruleId, activity.RehearsalId, activity.ArchId).Find(&recordIds)
  229. t.CheckErr(err)
  230. s = s.NotIn("o.buyer_id", recordIds)
  231. }
  232. }
  233. err = s.Find(&result)
  234. t.CheckErr(err)
  235. t.JSON(map[string]interface{}{
  236. "total": len(result),
  237. "list": result,
  238. })
  239. }
  240. // 订单抽奖动作
  241. func (t *OrderDrawCtl) Draw() {
  242. activityId := t.MustGetInt("activity_id")
  243. ladderId := t.MustGetInt("order_draw_rule_ladder_id")
  244. number := t.MustGetInt("number")
  245. customerId := t.GetAccountId()
  246. customer := models.Customer{}
  247. exist, err := models.Get(&customer, customerId)
  248. t.CheckErr(err)
  249. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  250. activity := models.Activity{}
  251. exist, err = models.Get(&activity, activityId)
  252. t.CheckErr(err)
  253. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  254. t.CheckRunning(activity.Status)
  255. area := &models.AreaStore{}
  256. exist, err = area.GetByCustomerId(customerId, activityId)
  257. t.CheckErr(err)
  258. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "地区不存在")
  259. // 查询奖品
  260. ladder := models.OrderDrawRuleLadder{}
  261. exist, err = models.Get(&ladder, ladderId)
  262. t.CheckErr(err)
  263. t.Assert(exist, code.MSG_ORDER_LADDER_NOT_EXIST, "订单抽奖等级不存在")
  264. t.CheckRunning(ladder.Status)
  265. rule := models.OrderDrawRule{}
  266. exist, err = models.Get(&rule, ladder.OrderDrawRuleId)
  267. t.CheckErr(err)
  268. t.Assert(exist, code.MSG_ORDER_RULE_NOT_EXIST, "订单抽奖规则不存在")
  269. count, err := (&models.OrderDrawRecord{}).Count(ladder.Id, activity.RehearsalId, activity.ArchId)
  270. t.CheckErr(err)
  271. prizeNum := ladder.PrizeNumber - int(count)
  272. if prizeNum <= 0 || prizeNum < number {
  273. t.ERROR("奖品数量不足", code.MSG_LOTTERY_PRIZE_NOT_ENOUGH)
  274. }
  275. module, exist, err := activity_service.GetModuleService(define.MODULE_ORDERLY, activity.Id)
  276. t.CheckErr(err)
  277. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "模块服务不存在")
  278. if customer.IsSpecial == 2 {
  279. orders, err := lottery_service.GetSpecialOrderLottery(module.BesideRepeat, activity.Id, rule.Id, ladder.Id, activity.RehearsalId, area.Id, activity.ArchId)
  280. if len(orders) < number {
  281. t.ERROR("抽奖订单数量不足", code.MSG_LOTTERY_PEOPLE_NOT_ENOUGH)
  282. }
  283. lottery_service.RandSpecialOrderLottery(orders)
  284. winnerOrders := orders[:number]
  285. userIds := make([]int, 0)
  286. for _, v := range winnerOrders {
  287. userIds = append(userIds, v.BuyerId)
  288. }
  289. users, err := models.GetUsersByIds(userIds)
  290. t.CheckErr(err)
  291. winners := make([]*lottery_service.SpecialOrderLotteryUser, 0)
  292. t.CheckErr(err)
  293. for _, o := range winnerOrders {
  294. for _, u := range users {
  295. if u.Id == o.BuyerId {
  296. winners = append(winners, &lottery_service.SpecialOrderLotteryUser{
  297. UserId: u.Id,
  298. Username: o.Receiver,
  299. UserPhone: o.Phone,
  300. Avatar: u.Avatar,
  301. PrizeName: ladder.PrizeName,
  302. LadderId: ladder.Id,
  303. PrizeImg: ladder.PrizeImg,
  304. EntryPersonName: o.OrderEntryPersonName,
  305. })
  306. }
  307. }
  308. prize := models.UserPrize{}
  309. prize.ActivityId = activityId
  310. prize.RehearsalId = activity.RehearsalId
  311. prize.ActivityName = activity.Name
  312. prize.UserId = o.BuyerId
  313. prize.CustomerOrderId = o.Id
  314. prize.PrizeImg = ladder.PrizeImg
  315. prize.PrizeName = ladder.PrizeName
  316. prize.ArchId = activity.ArchId
  317. prize.PrizeType = 2
  318. _, err = models.Add(&prize)
  319. t.CheckErr(err)
  320. record := models.OrderDrawRecord{}
  321. record.UserPrizeId = prize.Id
  322. record.ActivityId = activityId
  323. record.RehearsalId = activity.RehearsalId
  324. record.ArchId = activity.ArchId
  325. record.OrderDrawActivityId = rule.OrderDrawActivityId
  326. record.OrderDrawRuleId = rule.Id
  327. record.UserId = o.BuyerId
  328. record.CustomerOrderId = o.Id
  329. record.OrderDrawRuleLadderId = ladder.Id
  330. record.PrizeName = ladder.PrizeName
  331. record.OrderEntryPersonName = o.OrderEntryPersonName
  332. record.AreaId = area.Id
  333. record.AreaName = area.Name
  334. _, err = models.Add(&record)
  335. t.CheckErr(err)
  336. }
  337. t.JSON(winners)
  338. } else {
  339. userIds, err := lottery_service.GetOrderLotteryUserIds(module.BesideRepeat, activity.Id, rule.Id, ladder.Id, activity.RehearsalId, area.Id, activity.ArchId)
  340. if len(userIds) < number {
  341. t.ERROR("订单抽奖人数不足", code.MSG_LOTTERY_PEOPLE_NOT_ENOUGH)
  342. }
  343. lottery_service.RandLotteryUserIds(userIds)
  344. winnerIds := userIds[:number]
  345. winners, err := lottery_service.GetLotteryUsers(winnerIds)
  346. t.CheckErr(err)
  347. for i := range winners {
  348. winners[i].LadderId = ladder.Id
  349. winners[i].PrizeImg = ladder.PrizeImg
  350. winners[i].PrizeName = ladder.PrizeName
  351. prize := models.UserPrize{}
  352. prize.ActivityId = activityId
  353. prize.RehearsalId = activity.RehearsalId
  354. prize.ActivityName = activity.Name
  355. prize.UserId = winners[i].UserId
  356. prize.PrizeImg = ladder.PrizeImg
  357. prize.PrizeName = ladder.PrizeName
  358. prize.ArchId = activity.ArchId
  359. prize.PrizeType = 2
  360. _, err = models.Add(&prize)
  361. t.CheckErr(err)
  362. record := models.OrderDrawRecord{}
  363. record.UserPrizeId = prize.Id
  364. record.ActivityId = activityId
  365. record.RehearsalId = activity.RehearsalId
  366. record.ArchId = activity.ArchId
  367. record.OrderDrawActivityId = rule.OrderDrawActivityId
  368. record.OrderDrawRuleId = rule.Id
  369. record.UserId = winners[i].UserId
  370. record.OrderDrawRuleLadderId = ladder.Id
  371. record.PrizeName = ladder.PrizeName
  372. record.AreaId = area.Id
  373. record.AreaName = area.Name
  374. _, err = models.Add(&record)
  375. t.CheckErr(err)
  376. }
  377. t.JSON(winners)
  378. }
  379. }
  380. type WinnerResult struct {
  381. UserId int `json:"user_id"`
  382. Username string `json:"user_name"`
  383. UserPhone string `json:"user_phone"`
  384. Avatar string `json:"avatar"`
  385. PrizeName string `json:"prize_name"`
  386. }
  387. func (t *OrderDrawCtl) ListOfWinners() {
  388. ruleId := t.MustGetInt("order_draw_rule_id")
  389. rule := &models.OrderDrawRule{}
  390. exist, err := models.Get(rule, ruleId)
  391. t.CheckErr(err)
  392. t.Assert(exist, code.MSG_ORDER_RULE_NOT_EXIST, "订单抽奖规则不存在")
  393. lottery := &models.OrderDrawActivity{}
  394. exist, err = models.Get(lottery, rule.OrderDrawActivityId)
  395. t.CheckErr(err)
  396. t.Assert(exist, code.MSG_ORDER_RULE_NOT_EXIST, "订单抽奖规则不存在")
  397. activity := &models.Activity{}
  398. exist, err = models.Get(activity, lottery.ActivityId)
  399. t.CheckErr(err)
  400. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  401. customer := models.Customer{}
  402. exist, err = models.Get(&customer, activity.CustomerId)
  403. t.CheckErr(err)
  404. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  405. var result []*lottery_service.OrderWinnersResult
  406. if customer.IsSpecial == 2 {
  407. result, err = lottery_service.GetSpecialOrderWinnersResult(rule.Id, activity.RehearsalId, activity.ArchId)
  408. } else {
  409. result, err = lottery_service.GetOrderWinnersResult(rule.Id, activity.RehearsalId, activity.ArchId)
  410. }
  411. t.CheckErr(err)
  412. t.JSON(map[string]interface{}{
  413. "total": len(result),
  414. "list": result,
  415. })
  416. }
  417. // 获取所有订单
  418. type OrdersResult struct {
  419. UserId int `json:"user_id"`
  420. AreaName string `json:"area_name"`
  421. GoodsName string `json:"goods_name"`
  422. Nickname string `json:"nickname"`
  423. ExtraData string `json:"-"`
  424. Extra []map[string]interface{} `json:"extra"`
  425. }
  426. // 获取所有订单
  427. type SpecialOrdersResult struct {
  428. Id int `json:"id"`
  429. UserId int `json:"user_id"`
  430. AreaName string `json:"area_name"`
  431. EntryPersonName string `json:"entry_person_name"`
  432. Username string `json:"username"`
  433. Phone string `json:"phone"`
  434. Address string `json:"address"`
  435. Goods []*models.SubOrderResult `json:"goods"`
  436. }
  437. func (t *OrderDrawCtl) Orders() {
  438. activityId := t.MustGetInt("activity_id")
  439. activity := models.Activity{}
  440. exist, err := models.Get(&activity, activityId)
  441. t.CheckErr(err)
  442. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  443. customer := models.Customer{}
  444. exist, err = models.Get(&customer, activity.CustomerId)
  445. t.CheckErr(err)
  446. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  447. t.CheckErr(err)
  448. if customer.IsSpecial == 2 {
  449. count, err := (&models.CustomerOrder{}).CountCustomerOrder(activity.Id, activity.RehearsalId, activity.ArchId, 0)
  450. orders := make([]*SpecialOrdersResult, 0)
  451. err = core.GetXormAuto().Table(&models.CustomerOrder{}).Alias("o").Select("o.id as id, "+
  452. " o.area_name as area_name, o.order_entry_person_name as entry_person_name, o.buyer_id as user_id, "+
  453. " o.receiver as username, o.phone as phone, o.address as address, o.created_at").
  454. Where("o.is_delete=0 and o.activity_id=? and o.rehearsal_id=? and o.arch_id=?",
  455. activityId, activity.RehearsalId, activity.ArchId).Asc("o.created_at").Find(&orders)
  456. t.CheckErr(err)
  457. orderIds := make([]int, 0)
  458. for _, o := range orders {
  459. orderIds = append(orderIds, o.Id)
  460. }
  461. subs, err := models.GetCustomerOrderSubsByOrderIds(orderIds)
  462. t.CheckErr(err)
  463. for i := range orders {
  464. for j := range subs {
  465. if orders[i].Id == subs[j].OrderId {
  466. orders[i].Goods = append(orders[i].Goods, subs[j])
  467. }
  468. }
  469. }
  470. t.JSON(map[string]interface{}{
  471. "orders": orders,
  472. "total": len(orders),
  473. "buyer_count": count,
  474. })
  475. return
  476. } else {
  477. count, err := (&models.CustomerOrder{}).CountCustomerOrder(activity.Id, activity.RehearsalId, activity.ArchId, 1)
  478. orders := make([]*OrdersResult, 0)
  479. err = core.GetXormAuto().Table(&models.CustomerOrder{}).Alias("o").Select("o.id as id, "+
  480. " o.area_name, o.order_entry_person_name as entry_person_name, o.buyer_id as user_id, o.goods_name, "+
  481. " o.receiver as nickname, o.phone as phone, o.address as address, o.created_at, l.extra_data").
  482. Distinct("o.id").Join("LEFT", (&models.InvitationLetter{}).Alias("l"),
  483. "o.buyer_id=l.user_id and l.rehearsal_id=? and l.arch_id=?", activity.RehearsalId, activity.ArchId).
  484. Where("o.is_delete=0 and o.activity_id=? and o.rehearsal_id=? and o.arch_id=?",
  485. activityId, activity.RehearsalId, activity.ArchId).
  486. Asc("o.created_at").Find(&orders)
  487. t.CheckErr(err)
  488. items, err := invitation_service.GetOptionItem(activityId)
  489. t.CheckErr(err)
  490. for i := range orders {
  491. data, err := invitation_service.GetOptionValue(items, orders[i].ExtraData)
  492. t.CheckErr(err)
  493. orders[i].Extra = data
  494. }
  495. t.JSON(map[string]interface{}{
  496. "orders": orders,
  497. "total": len(orders),
  498. "buyer_count": count,
  499. })
  500. return
  501. }
  502. }
  503. // 录入人员排行榜
  504. func (t *OrderDrawCtl) OrderRank() {
  505. activityId := t.MustGetInt("activity_id")
  506. limit := t.MustGetInt("limit")
  507. activity := models.Activity{}
  508. exist, err := models.Get(&activity, activityId)
  509. t.CheckErr(err)
  510. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  511. order := &models.CustomerOrder{}
  512. res, err := order.SumCustomerOrder(activity.Id, activity.RehearsalId, activity.ArchId, limit)
  513. t.CheckErr(err)
  514. var total int64 = 0
  515. if len(res) == 1 && res[0]["num"] == "0" {
  516. res = nil
  517. } else {
  518. total, err = order.TotalCustomerOrderGoodsNum(activity.Id, activity.RehearsalId, activity.ArchId)
  519. }
  520. t.CheckErr(err)
  521. t.JSON(map[string]interface{}{
  522. "list": res,
  523. "total": total,
  524. })
  525. }