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

595 lines
17 KiB

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