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

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