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

258 lines
7.6 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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.Get(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.Get(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.Get(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.BuyerId = userId
  108. order.GoodsId = goodId
  109. order.Type = 0
  110. order.ActivityId = activityId
  111. order.RehearsalId = activity.RehearsalId
  112. order.OrderEntryPersonId = entryPerson.Id
  113. order.GoodsName = good.Name
  114. order.TotalAmount = good.Price
  115. order.OutTradeNo = helper.CreateUUID()
  116. order.IsDelete = false
  117. order.UpdatedAt = time.Now()
  118. order.CreatedAt = time.Now()
  119. _, err = session.InsertOne(order)
  120. if err != nil {
  121. session.Rollback()
  122. t.CheckErr(err)
  123. }
  124. err = session.Commit()
  125. t.CheckErr(err)
  126. t.SUCCESS("success")
  127. }
  128. func (t *OrderEntryCtl) DeleteOrder() {
  129. orderId := t.MustGetInt64("order_id")
  130. order := new(models.CustomerOrder)
  131. exist, err := core.GetXormAuto().Where("is_delete=0 and id=?", orderId).Get(order)
  132. t.CheckErr(err)
  133. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "订单不存在")
  134. _, err = new(models.CustomerOrder).SoftDeleteById(orderId)
  135. t.CheckErr(err)
  136. if order.UserPrizeId != 0 {
  137. _, err = new(models.UserPrize).SoftDeleteById(order.UserPrizeId)
  138. t.CheckErr(err)
  139. }
  140. t.SUCCESS("删除成功")
  141. }
  142. type OrderListResult struct {
  143. OrderId int64 `json:"order_id"`
  144. UserId int64 `json:"user_id"`
  145. EntryName string `json:"entry_name"`
  146. GoodName string `json:"good_name"`
  147. OrderTime string `json:"order_time"`
  148. OrderMoney float64 `json:"order_money"`
  149. Extra []map[string]interface{} `json:"extra"` // 额外信息
  150. ExtraData string `json:"-"`
  151. }
  152. func (t *OrderEntryCtl) EntryOrders() {
  153. uid := t.MustGetUID()
  154. activityId := t.MustGetInt64("activity_id")
  155. entry := new(models.OrderEntryPerson)
  156. exist, err := models.Get(entry, uid)
  157. t.CheckErr(err)
  158. t.Assert(exist, code.MSG_ENTRYPEOPLE_NOT_EXIST, "录入人员不存在")
  159. activity := new(models.Activity)
  160. exist, err = models.Get(activity, activityId)
  161. t.CheckErr(err)
  162. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  163. list := make([]*OrderListResult, 0)
  164. err = core.GetXormAuto().Table(new(models.CustomerOrder)).Alias("o").
  165. Select("l.extra_data as extra_data, g.name as good_name, o.buyer_id as user_id, o.total_amount as order_money, "+
  166. " o.id as order_id, DATE_FORMAT(o.created_at, '%Y-%m-%d %H:%i:%S') AS order_time").
  167. Join("LEFT", new(models.CustomerGoods).Alias("g"),
  168. "o.goods_id=g.id and o.activity_id=g.activity_id and g.is_delete=0").
  169. Join("LEFT", new(models.InvitationLetter).Alias("l"),
  170. "l.user_id = o.buyer_id and o.activity_id=l.activity_id and l.is_delete=0").
  171. Where("o.activity_id=? and o.order_entry_person_id=? and o.rehearsal_id=? and o.is_delete=0",
  172. activityId, uid, activity.RehearsalId).Desc("o.created_at").Find(&list)
  173. t.CheckErr(err)
  174. // 添加邀请函的内容
  175. optionItems, err := invitation_service.GetOptionItem(activityId)
  176. t.CheckErr(err)
  177. for i := range list {
  178. data, err := invitation_service.GetOptionValue(optionItems, list[i].ExtraData)
  179. t.CheckErr(err)
  180. list[i].Extra = data
  181. list[i].EntryName = entry.Name
  182. }
  183. t.JSON(map[string]interface{}{
  184. "list": list,
  185. "total": len(list),
  186. })
  187. }
  188. // 用户订单列表
  189. type UserOrdersResult struct {
  190. models.CustomerOrder `xorm:"extends"`
  191. Good *models.CustomerGoods `json:"good" xorm:"extends"`
  192. }
  193. // 用户查看订单列表
  194. func (t *OrderEntryCtl) UserOrders() {
  195. uid := t.MustGetUID()
  196. activityId := t.MustGetInt64("activity_id")
  197. activity := new(models.Activity)
  198. exist, err := models.Get(activity, activityId)
  199. t.CheckErr(err)
  200. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  201. orders := make([]UserOrdersResult, 0)
  202. s := core.GetXormAuto().Table(new(models.CustomerOrder)).Alias("o").
  203. Join("LEFT", new(models.CustomerGoods).Alias("g"),
  204. "o.goods_id=g.id and g.is_delete=0").
  205. Where("o.buyer_id=? and o.is_delete=0 and o.activity_id=? and o.rehearsal_id=?",
  206. uid, activity.Id, activity.RehearsalId)
  207. if t.PageSize > 0 {
  208. s = s.Limit(t.PageSize, t.Page*t.PageSize)
  209. }
  210. total, err := s.Desc("o.created_at").FindAndCount(&orders)
  211. t.CheckErr(err)
  212. t.JSON(map[string]interface{}{
  213. "list": orders,
  214. "total": total,
  215. })
  216. }
  217. // 二维码
  218. func (t *OrderEntryCtl) Qrcode() {
  219. userId := t.MustGetInt64("user_id")
  220. activityId := t.MustGetInt64("activity_id")
  221. goodId := t.MustGetInt64("good_id")
  222. Url := fmt.Sprintf("%s/PcClient/Client/OrderEntryCtl/order?user_id=%d&activity_id=%d&good_id=%d",
  223. define.HOST, userId, activityId, goodId)
  224. qr, err := utils.Qrcode2Base64(Url)
  225. t.CheckErr(err)
  226. t.JSON(map[string]interface{}{
  227. "qrcode": qr,
  228. })
  229. }