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

250 lines
7.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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. "fmt"
  4. "github.com/ouxuanserver/osmanthuswine/src/helper"
  5. "hudongzhuanjia/controllers"
  6. "hudongzhuanjia/models"
  7. invitation_service "hudongzhuanjia/services/invitation"
  8. "hudongzhuanjia/utils"
  9. "hudongzhuanjia/utils/code"
  10. "hudongzhuanjia/utils/define"
  11. "time"
  12. "github.com/ouxuanserver/osmanthuswine/src/core"
  13. )
  14. type OrderEntryCtl struct {
  15. controllers.AuthorCtl
  16. }
  17. // 商品 == > 用户查看所有商品
  18. func (t *OrderEntryCtl) List() {
  19. uid := t.MustGetUID()
  20. activityId := t.MustGetInt64("activity_id")
  21. goods := make([]*models.CustomerGoods, 0)
  22. err := core.GetXormAuto().Where("is_delete=0 and activity_id=?", activityId).
  23. Asc("created_at").Find(&goods)
  24. t.CheckErr(err)
  25. for index := range goods {
  26. url := fmt.Sprintf("%s/PcClient/Client/OrderEntryCtl/order?"+
  27. "user_id=%d&activity_id=%d&good_id=%d", define.HOST, uid, activityId, goods[index].Id)
  28. qrcode, err := utils.Qrcode2Base64(url)
  29. t.CheckErr(err)
  30. goods[index].Qrcode = qrcode
  31. }
  32. t.JSON(map[string]interface{}{
  33. "list": goods,
  34. "total": len(goods),
  35. })
  36. }
  37. // 下订单 == > 二维码跳转
  38. func (t *OrderEntryCtl) Order() {
  39. userId := t.MustGetInt64("user_id")
  40. activityId := t.MustGetInt64("activity_id")
  41. goodId := t.MustGetInt64("good_id")
  42. entryId := t.MustGetUID()
  43. activity := new(models.Activity)
  44. exist, err := models.GetById(activity, activityId)
  45. t.CheckErr(err)
  46. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  47. t.CheckRunning(activity.Status)
  48. entryPerson := new(models.OrderEntryPerson)
  49. exist, err = models.GetById(entryPerson, entryId)
  50. t.CheckErr(err)
  51. t.Assert(exist, code.MSG_ENTRYPEOPLE_NOT_EXIST, "录入人员不存在")
  52. area := new(models.AreaStore)
  53. exist, err = area.GetAreaStoreById(entryPerson.AreaId)
  54. t.CheckErr(err)
  55. t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
  56. good := new(models.CustomerGoods)
  57. exist, err = models.GetById(good, goodId)
  58. t.CheckErr(err)
  59. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "商品不存在")
  60. session := core.GetXormAuto().NewSession()
  61. defer session.Close()
  62. session.Begin()
  63. gift := new(models.OrderGift)
  64. exist, err = session.Where("is_delete=0 and activity_id=?", activityId).Get(gift)
  65. if err != nil {
  66. session.Rollback()
  67. t.CheckErr(err)
  68. }
  69. prize := new(models.UserPrize)
  70. prize.UserId = userId
  71. prize.ActivityId = activityId
  72. prize.RehearsalId = activity.RehearsalId
  73. prize.ActivityName = activity.Name
  74. prize.PrizeName = gift.GiftName
  75. prize.PrizeImg = gift.GiftPicUrl
  76. prize.PrizeType = 3
  77. prize.IsDelete = false
  78. prize.CreatedAt = time.Now()
  79. prize.UpdatedAt = time.Now()
  80. if exist {
  81. if gift.Num == 0 {
  82. _, err = session.InsertOne(prize)
  83. if err != nil {
  84. session.Rollback()
  85. t.CheckErr(err)
  86. }
  87. } else if gift.Num > 0 {
  88. count, err := core.GetXormAuto().Where("activity_id=? and rehearsal_id=? and is_delete=0",
  89. activityId, activity.RehearsalId).Count(new(models.CustomerOrder))
  90. if err != nil {
  91. session.Rollback()
  92. t.CheckErr(err)
  93. }
  94. if gift.Num > int(count) {
  95. _, err = session.InsertOne(prize)
  96. if err != nil {
  97. session.Rollback()
  98. t.CheckErr(err)
  99. }
  100. }
  101. }
  102. }
  103. order := new(models.CustomerOrder)
  104. order.UserPrizeId = prize.Id
  105. order.AreaId = entryPerson.AreaId
  106. order.AreaName = area.Name
  107. order.RehearsalId = activity.RehearsalId
  108. order.BuyerId = userId
  109. order.GoodsId = goodId
  110. order.ActivityId = activityId
  111. order.OrderEntryPersonId = entryPerson.Id
  112. order.GoodsName = good.Name
  113. order.TotalAmount = good.Price
  114. order.OutTradeNo = helper.CreateUUID()
  115. order.IsDelete = false
  116. order.UpdatedAt = time.Now()
  117. order.CreatedAt = time.Now()
  118. _, err = session.InsertOne(order)
  119. if err != nil {
  120. session.Rollback()
  121. t.CheckErr(err)
  122. }
  123. err = session.Commit()
  124. t.CheckErr(err)
  125. t.SUCCESS("success")
  126. }
  127. func (t *OrderEntryCtl) DeleteOrder() {
  128. orderId := t.MustGetInt64("order_id")
  129. order := new(models.CustomerOrder)
  130. exist, err := core.GetXormAuto().Where("is_delete=0 and id=?", orderId).Get(order)
  131. t.CheckErr(err)
  132. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "订单不存在")
  133. _, err = new(models.CustomerOrder).SoftDeleteById(orderId)
  134. t.CheckErr(err)
  135. if order.UserPrizeId != 0 {
  136. _, err = new(models.UserPrize).SoftDeleteById(order.UserPrizeId)
  137. t.CheckErr(err)
  138. }
  139. t.SUCCESS("删除成功")
  140. }
  141. type OrderListResult struct {
  142. OrderId int64 `json:"order_id"`
  143. UserId int64 `json:"user_id"`
  144. EntryName string `json:"entry_name"`
  145. GoodName string `json:"good_name"`
  146. OrderTime string `json:"order_time"`
  147. OrderMoney float64 `json:"order_money"`
  148. Extra []map[string]interface{} `json:"extra"` // 额外信息
  149. ExtraData string `json:"-"`
  150. }
  151. func (t *OrderEntryCtl) EntryOrders() {
  152. uid := t.MustGetUID()
  153. activityId := t.MustGetInt64("activity_id")
  154. entry := new(models.OrderEntryPerson)
  155. exist, err := entry.GetById(uid)
  156. t.CheckErr(err)
  157. t.Assert(exist, code.MSG_ENTRYPEOPLE_NOT_EXIST, "录入人员不存在")
  158. activity := new(models.Activity)
  159. exist, err = models.GetById(activity, activityId)
  160. t.CheckErr(err)
  161. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  162. list := make([]*OrderListResult, 0)
  163. err = core.GetXormAuto().Table(new(models.CustomerOrder)).Alias("o").
  164. Select("l.extra_data as extra_data, g.name as good_name, o.buyer_id as user_id, o.total_amount as order_money, "+
  165. " o.id as order_id, DATE_FORMAT(o.created_at, '%Y-%m-%d %H:%i:%S') AS order_time").
  166. Join("LEFT", new(models.CustomerGoods).Alias("g"),
  167. "o.goods_id=g.id and o.activity_id=g.activity_id and g.is_delete=0").
  168. Join("LEFT", new(models.InvitationLetter).Alias("l"),
  169. "l.user_id = o.buyer_id and o.activity_id=l.activity_id and l.is_delete=0").
  170. Where("o.activity_id=? and o.order_entry_person_id=? and o.rehearsal_id=? and o.is_delete=0",
  171. activityId, uid, activity.RehearsalId).Desc("o.created_at").Find(&list)
  172. t.CheckErr(err)
  173. // 添加邀请函的内容
  174. optionItems, err := invitation_service.GetOptionItem(activityId)
  175. t.CheckErr(err)
  176. for i := range list {
  177. data, err := invitation_service.GetOptionValue(optionItems, list[i].ExtraData)
  178. t.CheckErr(err)
  179. list[i].Extra = data
  180. list[i].EntryName = entry.Name
  181. }
  182. t.JSON(map[string]interface{}{
  183. "list": list,
  184. "total": len(list),
  185. })
  186. }
  187. // 用户订单列表
  188. type UserOrdersResult struct {
  189. models.CustomerOrder `xorm:"extends"`
  190. Good *models.CustomerGoods `json:"good" xorm:"extends"`
  191. }
  192. // 用户查看订单列表
  193. func (t *OrderEntryCtl) UserOrders() {
  194. uid := t.MustGetUID()
  195. orders := make([]UserOrdersResult, 0)
  196. s := core.GetXormAuto().Table(new(models.CustomerOrder)).Alias("o").
  197. Join("LEFT", new(models.CustomerGoods).Alias("g"), "o.goods_id=g.id and g.is_delete=0").
  198. Desc("o.created_at").Where("o.buyer_id=? and o.is_delete=0", uid)
  199. if t.PageSize > 0 {
  200. s = s.Limit(t.PageSize, t.Page*t.PageSize)
  201. }
  202. total, err := s.Desc("o.created_at").FindAndCount(&orders)
  203. t.CheckErr(err)
  204. t.JSON(map[string]interface{}{
  205. "list": orders,
  206. "total": total,
  207. })
  208. }
  209. // 二维码
  210. func (t *OrderEntryCtl) Qrcode() {
  211. userId := t.MustGetInt64("user_id")
  212. activityId := t.MustGetInt64("activity_id")
  213. goodId := t.MustGetInt64("good_id")
  214. Url := fmt.Sprintf("%s/PcClient/Client/OrderEntryCtl/order?user_id=%d&activity_id=%d&good_id=%d",
  215. define.HOST, userId, activityId, goodId)
  216. qr, err := utils.Qrcode2Base64(Url)
  217. t.CheckErr(err)
  218. t.JSON(map[string]interface{}{
  219. "qrcode": qr,
  220. })
  221. }