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

610 lines
17 KiB

5 years ago
5 years ago
5 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
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 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
4 years ago
5 years ago
4 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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
5 years ago
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
5 years ago
5 years ago
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
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
4 years ago
4 years ago
4 years ago
5 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
  1. package client
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hudongzhuanjia/controllers"
  6. "hudongzhuanjia/models"
  7. "hudongzhuanjia/utils"
  8. "hudongzhuanjia/utils/code"
  9. "hudongzhuanjia/utils/define"
  10. "strings"
  11. "git.ouxuan.net/tommy/osmanthuswine/src/core"
  12. )
  13. type OrderEntryCtl struct {
  14. controllers.AuthorCtl
  15. }
  16. // 用户查看所有商品
  17. func (t *OrderEntryCtl) List() {
  18. _type := t.GetAccountType()
  19. uid := t.GetAccountId()
  20. activityId := t.MustGetInt("activity_id")
  21. areaId := 0
  22. if _type == define.TYPE_ENTRYPEOPLE {
  23. entryPerson := models.OrderEntryPerson{}
  24. exist, err := models.Get(&entryPerson, uid)
  25. t.CheckErr(err)
  26. t.Assert(exist, code.MSG_ENTRYPEOPLE_NOT_EXIST, "录入人员信息异常")
  27. areaId = entryPerson.AreaId
  28. } else {
  29. areaId = t.MustGetInt("area_id")
  30. }
  31. area := &models.AreaStore{}
  32. exist, err := models.Get(area, areaId)
  33. t.CheckErr(err)
  34. t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
  35. if area.IsMainArea != 1 && area.AreaGoodsRuleSwitch != 1 {
  36. exist, err = area.GetMainAreaById(activityId)
  37. t.CheckErr(err)
  38. t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
  39. }
  40. // 库存
  41. goods, err := models.GetGoodsByActivityId(activityId, area.Id)
  42. t.CheckErr(err)
  43. goodIds := make([]int, 0)
  44. for _, g := range goods {
  45. goodIds = append(goodIds, g.Id)
  46. }
  47. res, err := models.GetSubOrderGoodNum(goodIds)
  48. t.CheckErr(err)
  49. for index := range goods {
  50. url := fmt.Sprintf("%s/PcClient/Client/OrderEntryCtl/order?"+
  51. "user_id=%d&activity_id=%d&good_id=%d", define.HOST, uid, activityId, goods[index].Id)
  52. qrcode, err := utils.Qrcode2Base64(url)
  53. t.CheckErr(err)
  54. goods[index].Qrcode = qrcode
  55. if goods[index].Stock == -1 {
  56. for _, v := range res {
  57. if goods[index].Id == v.GoodsId {
  58. goods[index].Stock -= v.GoodsNum
  59. }
  60. }
  61. }
  62. }
  63. t.JSON(map[string]interface{}{
  64. "list": goods,
  65. "total": len(goods),
  66. })
  67. }
  68. // 扫二维码下单
  69. func (t *OrderEntryCtl) Order() {
  70. userId := t.MustGetInt("user_id")
  71. goodId := t.MustGetInt("good_id")
  72. entryId := t.GetAccountId()
  73. entryPerson := models.OrderEntryPerson{}
  74. exist, err := models.Get(&entryPerson, entryId)
  75. t.CheckErr(err)
  76. t.Assert(exist, code.MSG_ENTRYPEOPLE_NOT_EXIST, "录入人员不存在")
  77. activity := models.Activity{}
  78. exist, err = models.Get(&activity, entryPerson.ActivityId)
  79. t.CheckErr(err)
  80. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  81. t.CheckRunning(activity.Status)
  82. area := models.AreaStore{}
  83. exist, err = models.Get(&area, entryPerson.AreaId)
  84. t.CheckErr(err)
  85. t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
  86. letter := &models.InvitationLetter{}
  87. exist, err = letter.GetByUserIdAndActivityId(userId, activity.Id, activity.ArchId, activity.RehearsalId)
  88. t.CheckErr(err)
  89. t.Assert(exist, code.MSG_INVITE_LETTER_NOT_EXIST, "邀请函不存在")
  90. values := make([]map[string]interface{}, 0)
  91. err = json.Unmarshal([]byte(strings.Trim(letter.ExtraData, `"`)), &values)
  92. t.CheckErr(err)
  93. var name, phone, address = "", "", ""
  94. for _, value := range values {
  95. if value["name"] == "姓名" {
  96. name = fmt.Sprint(value["val"])
  97. } else if value["name"] == "手机" {
  98. phone = fmt.Sprint(value["val"])
  99. } else if value["name"] == "地址" {
  100. address = fmt.Sprint(value["val"])
  101. }
  102. }
  103. s := core.GetXormAuto().NewSession()
  104. defer s.Close()
  105. err = s.Begin()
  106. if err != nil {
  107. s.Rollback()
  108. t.CheckErr(err)
  109. }
  110. good := models.CustomerGoods{}
  111. exist, err = s.Where("is_delete=0 and id=?", goodId).Get(&good)
  112. if err != nil || !exist {
  113. s.Rollback()
  114. t.ERROR("商品信息异常", code.MSG_ERR_Param)
  115. return
  116. }
  117. if good.Stock > -1 { // 库存
  118. // 找出商品库存
  119. ms, err := models.GetSubOrderGoodNumBySession(s, good.Id)
  120. if err != nil {
  121. s.Rollback()
  122. t.CheckErr(err)
  123. }
  124. for _, m := range ms {
  125. if m.GoodsNum == good.Id && good.Stock-m.GoodsNum <= 0 {
  126. s.Rollback()
  127. t.ERROR(good.Name+"商品库存不足", code.MSG_CUSTOMER_GOOD_NOT_ENOUGH)
  128. return
  129. }
  130. }
  131. }
  132. order := models.CustomerOrder{}
  133. // 查询库存
  134. total, err := s.Where("is_delete=0").Count(&order) // 订单总数
  135. if err != nil {
  136. s.Rollback()
  137. t.CheckErr(err)
  138. }
  139. order.AreaId = entryPerson.AreaId
  140. order.ArchId = activity.ArchId
  141. order.AreaName = area.Name
  142. order.BuyerId = userId
  143. order.Type = 0
  144. order.ActivityId = activity.Id
  145. order.RehearsalId = activity.RehearsalId
  146. order.OrderEntryPersonId = entryPerson.Id
  147. order.OrderEntryPersonName = entryPerson.Name
  148. order.TotalAmount = good.Price
  149. order.Receiver = name
  150. order.Phone = phone
  151. order.Address = address
  152. order.GoodsName = good.Name
  153. order.GoodsId = good.Id
  154. order.GoodsNum = 1
  155. order.OutTradeNo = utils.RandomStr(32)
  156. order.OrderNo = fmt.Sprint(define.DefaultOrderNo + int(total))
  157. order.Status = 1
  158. _, err = s.InsertOne(&order)
  159. if err != nil {
  160. s.Rollback()
  161. t.CheckErr(err)
  162. }
  163. sub := models.CustomerOrderSub{}
  164. sub.GoodsId = good.Id
  165. sub.GoodName = good.Name
  166. sub.GoodPrice = good.Price
  167. sub.GoodsNum = 1
  168. sub.OrderId = order.Id
  169. _, err = models.Add(&sub) // 存入子订单
  170. if err != nil {
  171. s.Rollback()
  172. t.CheckErr(err)
  173. }
  174. gift := models.OrderGift{}
  175. exist, err = s.Where("is_delete=0 and activity_id=?", activity.Id).Get(&gift)
  176. if err != nil {
  177. s.Rollback()
  178. t.CheckErr(err)
  179. }
  180. if exist {
  181. prize := models.UserPrize{}
  182. prize.UserId = userId
  183. prize.ActivityId = activity.Id
  184. prize.RehearsalId = activity.RehearsalId
  185. prize.ActivityName = activity.Name
  186. prize.PrizeName = gift.GiftName
  187. prize.PrizeImg = gift.GiftPicUrl
  188. prize.ArchId = activity.ArchId
  189. prize.CustomerOrderId = order.Id
  190. prize.PrizeType = 3
  191. if gift.Num == 0 {
  192. _, err = s.InsertOne(&prize)
  193. if err != nil {
  194. s.Rollback()
  195. t.CheckErr(err)
  196. }
  197. } else if gift.Num > 0 {
  198. count, err := s.Where("activity_id=? and rehearsal_id=? and arch_id=? and is_delete=0",
  199. activity.Id, activity.RehearsalId, activity.ArchId).NoAutoCondition().Count(&order)
  200. if err != nil {
  201. s.Rollback()
  202. t.CheckErr(err)
  203. }
  204. if gift.Num >= int(count) { // 大于等于
  205. _, err = s.InsertOne(&prize)
  206. if err != nil {
  207. s.Rollback()
  208. t.CheckErr(err)
  209. }
  210. }
  211. }
  212. if prize.Id > 0 {
  213. order.UserPrizeId = prize.Id
  214. _, err = s.ID(order.Id).NoAutoCondition().Cols("user_prize_id").Update(&order)
  215. if err != nil {
  216. s.Rollback()
  217. t.CheckErr(err)
  218. }
  219. }
  220. }
  221. err = s.Commit()
  222. if err != nil {
  223. s.Rollback()
  224. t.CheckErr(err)
  225. }
  226. t.SUCCESS("成功录入订单")
  227. }
  228. // 手动下单
  229. type ManualOrderGood struct {
  230. GoodId int `json:"good_id"`
  231. GoodNum int `json:"good_num"`
  232. }
  233. type ManualOrderParam struct {
  234. Name string `json:"name"`
  235. Phone string `json:"phone"`
  236. Goods []*ManualOrderGood `json:"goods"`
  237. }
  238. func (t *OrderEntryCtl) ManualOrder() {
  239. entryId := t.GetAccountId() // 录入人员id
  240. param := &ManualOrderParam{}
  241. t.Bind(param)
  242. param.Goods = make([]*ManualOrderGood, 0)
  243. err := json.Unmarshal([]byte(t.Request.REQUEST["goods"]), &param.Goods)
  244. t.CheckErr(err)
  245. if len(param.Goods) <= 0 {
  246. t.ERROR("商品不能为空", code.MSG_CUSTOMER_GOOD_NOT_EXIST)
  247. return
  248. }
  249. entryPerson := models.OrderEntryPerson{}
  250. exist, err := models.Get(&entryPerson, entryId)
  251. t.CheckErr(err)
  252. t.Assert(exist, code.MSG_ENTRYPEOPLE_NOT_EXIST, "录入人员不存在")
  253. activity := models.Activity{}
  254. exist, err = models.Get(&activity, entryPerson.ActivityId)
  255. t.CheckErr(err)
  256. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  257. t.CheckRunning(activity.Status)
  258. area := models.AreaStore{}
  259. exist, err = models.Get(&area, entryPerson.AreaId)
  260. t.CheckErr(err)
  261. t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
  262. goodIds := make([]int, 0)
  263. for _, g := range param.Goods {
  264. goodIds = append(goodIds, g.GoodId)
  265. }
  266. s := core.GetXormAuto().NewSession()
  267. defer s.Close()
  268. err = s.Begin()
  269. if err != nil {
  270. s.Rollback()
  271. t.CheckErr(err)
  272. }
  273. user := models.User{}
  274. exist, err = s.Where("is_delete=0 and phone=?", param.Phone).NoAutoCondition().Get(&user)
  275. if err != nil {
  276. s.Rollback()
  277. t.CheckErr(err)
  278. }
  279. if !exist {
  280. user.Phone = param.Phone
  281. user.Nickname = param.Name
  282. user.Password = utils.RandomStr(8)
  283. _, err = s.InsertOne(&user)
  284. if err != nil {
  285. s.Rollback()
  286. t.CheckErr(err)
  287. }
  288. }
  289. // 校验库存
  290. goods := make([]*models.CustomerGoods, 0)
  291. err = s.Where("is_delete=0").In("id", goodIds).Find(&goods)
  292. if err != nil {
  293. s.Rollback()
  294. t.CheckErr(err)
  295. }
  296. if len(goods) != len(param.Goods) {
  297. s.Rollback()
  298. t.ERROR("商品信息异常", code.MSG_ERR_Param)
  299. return
  300. }
  301. subs := make([]*models.CustomerOrderSub, 0)
  302. totalAmount := 0.00
  303. goodNum := 0
  304. for _, g := range param.Goods {
  305. for _, good := range goods {
  306. if g.GoodId == good.Id {
  307. subs = append(subs, &models.CustomerOrderSub{
  308. GoodsId: good.Id,
  309. GoodsNum: g.GoodNum,
  310. GoodName: good.Name,
  311. GoodPrice: good.Price,
  312. })
  313. totalAmount += good.Price * float64(g.GoodNum)
  314. goodNum += g.GoodNum
  315. }
  316. }
  317. }
  318. // ms := make([]map[string]int, 0)
  319. // err = s.Table(&models.CustomerOrderSub{}).Alias("s").Select("s.goods_id, COALESCE(SUM(s.goods_num), 0) as goods_num").
  320. // Join("left", (&models.CustomerOrder{}).Alias("o"), "o.id=s.order_id").
  321. // Where("o.activity_id=? and o.rehearsal_id=? and o.arch_id=?", activity.Id, activity.RehearsalId, activity.ArchId).
  322. // In("s.goods_id", goodIds).GroupBy("s.goods_id").Find(&ms)
  323. ms, err := models.GetSubOrderGoodNumBySession(s, goodIds)
  324. if err != nil {
  325. s.Rollback()
  326. t.CheckErr(err)
  327. }
  328. for _, m := range ms {
  329. for _, g := range goods {
  330. if g.Stock == -1 { // 无上限
  331. break
  332. }
  333. if m.GoodsId == g.Id && g.Stock-m.GoodsNum <= 0 {
  334. s.Rollback()
  335. t.ERROR(g.Name+"商品库存不足", code.MSG_CUSTOMER_GOOD_NOT_ENOUGH)
  336. return
  337. }
  338. }
  339. }
  340. order := models.CustomerOrder{}
  341. count, err := s.Where("is_delete=0").Count(&order) // 查看订单所在
  342. if err != nil {
  343. s.Rollback()
  344. t.CheckErr(err)
  345. }
  346. order.AreaId = entryPerson.AreaId
  347. order.AreaName = area.Name
  348. order.ArchId = activity.ArchId
  349. order.BuyerId = user.Id
  350. order.Type = 0
  351. order.ActivityId = activity.Id
  352. order.RehearsalId = activity.RehearsalId
  353. order.OrderEntryPersonId = entryPerson.Id
  354. order.OrderEntryPersonName = entryPerson.Name
  355. order.TotalAmount = totalAmount
  356. order.Receiver = param.Name
  357. order.Phone = param.Phone
  358. order.GoodsNum = goodNum
  359. order.GoodsId = subs[0].GoodsId
  360. order.GoodsName = subs[0].GoodName
  361. order.OutTradeNo = utils.RandomStr(32)
  362. order.OrderNo = fmt.Sprint(define.DefaultOrderNo + int(count))
  363. order.Status = 1
  364. _, err = s.InsertOne(&order)
  365. if err != nil {
  366. s.Rollback()
  367. t.CheckErr(err)
  368. }
  369. for _, sub := range subs {
  370. sub.OrderId = order.Id
  371. _, err = models.Add(sub) // 存入子订单
  372. if err != nil {
  373. s.Rollback()
  374. t.CheckErr(err)
  375. }
  376. }
  377. gift := models.OrderGift{}
  378. exist, err = s.Where("is_delete=0 and activity_id=?", activity.Id).Get(&gift)
  379. if err != nil {
  380. s.Rollback()
  381. t.CheckErr(err)
  382. }
  383. if exist {
  384. prize := models.UserPrize{}
  385. prize.UserId = user.Id
  386. prize.ActivityId = activity.Id
  387. prize.RehearsalId = activity.RehearsalId
  388. prize.ActivityName = activity.Name
  389. prize.PrizeName = gift.GiftName
  390. prize.PrizeImg = gift.GiftPicUrl
  391. prize.ArchId = activity.ArchId
  392. prize.CustomerOrderId = order.Id
  393. prize.PrizeType = 3
  394. if gift.Num == 0 {
  395. _, err = s.InsertOne(&prize)
  396. if err != nil {
  397. s.Rollback()
  398. t.CheckErr(err)
  399. }
  400. } else if gift.Num > 0 {
  401. count, err := s.Where("activity_id=? and rehearsal_id=? and arch_id=? and is_delete=0",
  402. activity.Id, activity.RehearsalId, activity.ArchId).NoAutoCondition().Count(&order)
  403. if err != nil {
  404. s.Rollback()
  405. t.CheckErr(err)
  406. }
  407. if gift.Num >= int(count) { // 大于等于
  408. _, err = s.InsertOne(&prize)
  409. if err != nil {
  410. s.Rollback()
  411. t.CheckErr(err)
  412. }
  413. }
  414. }
  415. if prize.Id > 0 {
  416. order.UserPrizeId = prize.Id
  417. _, err = s.ID(order.Id).NoAutoCondition().Cols("user_prize_id").Update(&order)
  418. if err != nil {
  419. s.Rollback()
  420. t.CheckErr(err)
  421. }
  422. }
  423. }
  424. err = s.Commit()
  425. if err != nil {
  426. s.Rollback()
  427. t.CheckErr(err)
  428. }
  429. t.SUCCESS("成功录入订单")
  430. }
  431. func (t *OrderEntryCtl) DeleteOrder() {
  432. orderId := t.MustGetInt("order_id")
  433. order := new(models.CustomerOrder)
  434. exist, err := models.Get(order, orderId)
  435. t.CheckErr(err)
  436. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "订单不存在")
  437. _, err = models.Del(order, order.Id)
  438. t.CheckErr(err)
  439. if order.UserPrizeId > 0 {
  440. _, err = models.Del(&models.UserPrize{}, order.UserPrizeId)
  441. t.CheckErr(err)
  442. }
  443. t.SUCCESS("删除成功")
  444. }
  445. type OrderListResult struct {
  446. OrderId int `json:"order_id"`
  447. UserId int `json:"user_id"`
  448. EntryName string `json:"entry_name"`
  449. GoodName string `json:"-"`
  450. OrderTime string `json:"order_time"`
  451. OrderMoney float64 `json:"order_money"`
  452. Receiver string `json:"receiver"`
  453. Phone string `json:"phone"`
  454. Address string `json:"address"`
  455. Extra interface{} `json:"extra"` // 额外信息
  456. ExtraData string `json:"-"`
  457. }
  458. func (t *OrderEntryCtl) EntryOrders() {
  459. uid := t.GetAccountId()
  460. entry := models.OrderEntryPerson{}
  461. exist, err := models.Get(&entry, uid)
  462. t.CheckErr(err)
  463. t.Assert(exist, code.MSG_ENTRYPEOPLE_NOT_EXIST, "录入人员不存在")
  464. activity := models.Activity{}
  465. exist, err = models.Get(&activity, entry.ActivityId)
  466. t.CheckErr(err)
  467. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  468. customer := models.Customer{}
  469. exist, err = models.Get(&customer, activity.CustomerId)
  470. t.CheckErr(err)
  471. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  472. list := make([]*OrderListResult, 0)
  473. //if customer.IsSpecial == 0 {
  474. // // 添加邀请函的内容
  475. // err = core.GetXormAuto().Table(&models.CustomerOrder{}).Alias("o").
  476. // Select("o.receiver as receiver, o.phone as phone, o.address as address,l.extra_data as extra_data, "+
  477. // " g.name as good_name, o.buyer_id as user_id, o.total_amount as order_money, o.id as order_id, "+
  478. // " DATE_FORMAT(o.created_at, '%Y-%m-%d %H:%i:%S') AS order_time").Join("LEFT",
  479. // (&models.CustomerGoods{}).Alias("g"), "o.goods_id=g.id and o.activity_id=g.activity_id").
  480. // Join("LEFT", (&models.InvitationLetter{}).Alias("l"), "l.user_id = o.buyer_id and o.activity_id=l.activity_id and o.arch_id=l.arch_id and l.is_delete=0").
  481. // Where("o.activity_id=? and o.order_entry_person_id=? and o.rehearsal_id=? and o.arch_id=? "+
  482. // " and o.is_delete=0", activity.Id, uid, activity.RehearsalId, activity.ArchId).
  483. // Desc("o.created_at").Find(&list)
  484. // t.CheckErr(err)
  485. // optionItems, err := invitation_service.GetOptionItem(activity.Id)
  486. // t.CheckErr(err)
  487. // for i := range list {
  488. // data, err := invitation_service.GetOptionValue(optionItems, list[i].ExtraData)
  489. // t.CheckErr(err)
  490. // list[i].Extra = data
  491. // list[i].EntryName = entry.Name
  492. // }
  493. //} else if customer.IsSpecial == 2 {
  494. err = core.GetXormAuto().Table(&models.CustomerOrder{}).Alias("o").Select("o.id as order_id, "+
  495. " o.receiver as receiver, o.phone as phone, o.address as address, o.buyer_id as user_id, o.order_entry_person_name as entry_name, "+
  496. " o.total_amount as order_money, DATE_FORMAT(o.created_at, '%Y-%m-%d %H:%i:%S') AS order_time").
  497. Where("o.activity_id=? and o.rehearsal_id=? and o.order_entry_person_id=? and o.arch_id=?",
  498. activity.Id, activity.RehearsalId, uid, activity.ArchId).Desc("o.created_at").Find(&list)
  499. t.CheckErr(err)
  500. orderIds := make([]int, 0)
  501. for _, v := range list {
  502. orderIds = append(orderIds, v.OrderId)
  503. }
  504. subs, err := models.GetCustomerOrderSubsByOrderIds(orderIds)
  505. t.CheckErr(err)
  506. for i := range list {
  507. s := make([]interface{}, 0)
  508. for _, sub := range subs {
  509. if sub.OrderId == list[i].OrderId {
  510. s = append(s, sub)
  511. }
  512. }
  513. list[i].Extra = s
  514. }
  515. //}
  516. t.JSON(map[string]interface{}{
  517. "list": list,
  518. "total": len(list),
  519. })
  520. }
  521. // 用户订单列表
  522. type UserOrdersResult struct {
  523. models.CustomerOrder `xorm:"extends"`
  524. Good *models.CustomerGoods `json:"good" xorm:"extends"`
  525. }
  526. // 用户查看订单列表
  527. func (t *OrderEntryCtl) UserOrders() {
  528. uid := t.GetAccountId()
  529. activityId := t.MustGetInt("activity_id")
  530. activity := new(models.Activity)
  531. exist, err := models.Get(activity, activityId)
  532. t.CheckErr(err)
  533. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  534. orders := make([]UserOrdersResult, 0)
  535. s := core.GetXormAuto().Table(new(models.CustomerOrder)).Alias("o").
  536. Join("LEFT", new(models.CustomerGoods).Alias("g"),
  537. "o.goods_id=g.id and g.is_delete=0").
  538. Where("o.buyer_id=? and o.is_delete=0 and o.activity_id=? and o.rehearsal_id=? and o.arch_id=?",
  539. uid, activity.Id, activity.RehearsalId, activity.ArchId)
  540. if t.PageSize > 0 {
  541. s = s.Limit(t.PageSize, t.Page*t.PageSize)
  542. }
  543. total, err := s.Desc("o.created_at").FindAndCount(&orders)
  544. t.CheckErr(err)
  545. t.JSON(map[string]interface{}{
  546. "list": orders,
  547. "total": total,
  548. })
  549. }
  550. // 二维码
  551. func (t *OrderEntryCtl) Qrcode() {
  552. userId := t.MustGetInt("user_id")
  553. activityId := t.MustGetInt("activity_id")
  554. goodId := t.MustGetInt("good_id")
  555. Url := fmt.Sprintf("%s/PcClient/Client/OrderEntryCtl/order?user_id=%d&activity_id=%d&good_id=%d",
  556. define.HOST, userId, activityId, goodId)
  557. qr, err := utils.Qrcode2Base64(Url)
  558. t.CheckErr(err)
  559. t.JSON(map[string]interface{}{
  560. "qrcode": qr,
  561. })
  562. }