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

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