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

233 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
  1. package client
  2. import (
  3. "fmt"
  4. "hudongzhuanjia/controllers"
  5. "hudongzhuanjia/models"
  6. invitation_service "hudongzhuanjia/services/invitation"
  7. "hudongzhuanjia/utils"
  8. "hudongzhuanjia/utils/code"
  9. "hudongzhuanjia/utils/define"
  10. "time"
  11. "github.com/ouxuanserver/osmanthuswine/src/core"
  12. "github.com/ouxuanserver/osmanthuswine/src/helper"
  13. )
  14. type OrderEntryCtl struct {
  15. controllers.AuthorCtl
  16. }
  17. // 商品 == > 用户查看所有商品
  18. func (t *OrderEntryCtl) List() {
  19. uid := t.MustGetUID()
  20. activityId := t.MustGetActivityId()
  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. orderGift := new(models.OrderGift)
  61. exist, err = orderGift.GetByActivityId(activityId)
  62. t.CheckErr(err)
  63. userPrize := new(models.UserPrize)
  64. userPrize.UserId = userId
  65. userPrize.ActivityId = activityId
  66. userPrize.RehearsalId = activity.RehearsalId
  67. userPrize.ActivityName = activity.Name
  68. userPrize.PrizeName = orderGift.GiftName
  69. userPrize.PrizeImg = orderGift.GiftPicUrl
  70. userPrize.PrizeType = 3
  71. userPrize.IsDelete = false
  72. userPrize.CreatedAt = time.Now()
  73. userPrize.UpdatedAt = time.Now()
  74. if exist && orderGift.Num == 0 { // 存在无限制
  75. _, err := core.GetXormAuto().Insert(userPrize)
  76. t.CheckErr(err)
  77. }
  78. order := new(models.CustomerOrder)
  79. order.UserPrizeId = userPrize.Id
  80. order.AreaId = entryPerson.AreaId
  81. order.AreaName = area.Name
  82. order.RehearsalId = activity.RehearsalId
  83. order.BuyerId = userId
  84. order.GoodsId = goodId
  85. order.ActivityId = activityId
  86. order.OrderEntryPersonId = entryPerson.Id
  87. order.GoodsName = good.Name
  88. order.TotalAmount = good.Price
  89. order.OutTradeNo = helper.CreateUUID()
  90. order.IsDelete = false
  91. order.UpdatedAt = time.Now()
  92. order.CreatedAt = time.Now()
  93. _, err = core.GetXormAuto().Insert(order)
  94. t.CheckErr(err)
  95. // 查出这个订单多少名
  96. // 防止并发出现错误
  97. if exist && orderGift.Num > 0 {
  98. count, err := core.GetXormAuto().Where("created_at >= ? and activity_id=? and rehearsal_id=? and is_delete=0",
  99. order.CreatedAt, activityId, activity.RehearsalId).Count(new(models.CustomerOrder))
  100. t.CheckErr(err)
  101. if orderGift.Num >= int(count) {
  102. _, err = core.GetXormAuto().InsertOne(userPrize)
  103. t.CheckErr(err)
  104. order.UserPrizeId = userPrize.Id
  105. _, err := core.GetXormAuto().Id(order.Id).Cols("user_prize_id").Update(order)
  106. t.CheckErr(err)
  107. }
  108. }
  109. t.SUCCESS("success")
  110. }
  111. func (t *OrderEntryCtl) DeleteOrder() {
  112. orderId := t.MustGetInt64("order_id")
  113. order := new(models.CustomerOrder)
  114. exist, err := core.GetXormAuto().Where("is_delete=0 and id=?", orderId).Get(order)
  115. t.CheckErr(err)
  116. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "订单不存在")
  117. _, err = new(models.CustomerOrder).SoftDeleteById(orderId)
  118. t.CheckErr(err)
  119. if order.UserPrizeId != 0 {
  120. _, err = new(models.UserPrize).SoftDeleteById(order.UserPrizeId)
  121. t.CheckErr(err)
  122. }
  123. t.SUCCESS("删除成功")
  124. }
  125. type OrderListResult struct {
  126. OrderId int64 `json:"order_id"`
  127. UserId int64 `json:"user_id"`
  128. EntryName string `json:"entry_name"`
  129. GoodName string `json:"good_name"`
  130. OrderTime string `json:"order_time"`
  131. OrderMoney float64 `json:"order_money"`
  132. Extra []map[string]interface{} `json:"extra"` // 额外信息
  133. ExtraData string `json:"-"`
  134. }
  135. func (t *OrderEntryCtl) EntryOrders() {
  136. uid := t.MustGetUID()
  137. activityId := t.MustGetInt64("activity_id")
  138. entry := new(models.OrderEntryPerson)
  139. exist, err := entry.GetById(uid)
  140. t.CheckErr(err)
  141. t.Assert(exist, code.MSG_ENTRYPEOPLE_NOT_EXIST, "录入人员不存在")
  142. activity := new(models.Activity)
  143. exist, err = models.GetById(activity, activityId)
  144. t.CheckErr(err)
  145. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  146. list := make([]*OrderListResult, 0)
  147. err = core.GetXormAuto().Table(new(models.CustomerOrder)).Alias("o").
  148. Select("l.extra_data as extra_data, g.name as good_name, o.buyer_id as user_id, o.total_amount as order_money, "+
  149. " o.id as order_id, DATE_FORMAT(o.created_at, '%Y-%m-%d %H:%i:%S') AS order_time").
  150. Join("LEFT", new(models.CustomerGoods).Alias("g"),
  151. "o.goods_id=g.id and o.activity_id=g.activity_id and g.is_delete=0").
  152. Join("LEFT", new(models.InvitationLetter).Alias("l"),
  153. "l.user_id = o.buyer_id and o.activity_id=l.activity_id and l.is_delete=0").
  154. Where("o.activity_id=? and o.order_entry_person_id=? and o.rehearsal_id=? and o.is_delete=0",
  155. activityId, uid, activity.RehearsalId).Desc("o.created_at").Find(&list)
  156. t.CheckErr(err)
  157. // 添加邀请函的内容
  158. optionItems, err := invitation_service.GetOptionItem(activityId)
  159. t.CheckErr(err)
  160. for i := range list {
  161. data, err := invitation_service.GetOptionValue(optionItems, list[i].ExtraData)
  162. t.CheckErr(err)
  163. list[i].Extra = data
  164. list[i].EntryName = entry.Name
  165. }
  166. t.JSON(map[string]interface{}{
  167. "list": list,
  168. "total": len(list),
  169. })
  170. }
  171. // 用户订单列表
  172. type UserOrdersResult struct {
  173. models.CustomerOrder `xorm:"extends"`
  174. Good *models.CustomerGoods `json:"good" xorm:"extends"`
  175. }
  176. // 用户查看订单列表
  177. func (t *OrderEntryCtl) UserOrders() {
  178. uid := t.MustGetUID()
  179. orders := make([]UserOrdersResult, 0)
  180. s := core.GetXormAuto().Table(new(models.CustomerOrder)).Alias("o").
  181. Join("LEFT", new(models.CustomerGoods).Alias("g"), "o.goods_id=g.id and g.is_delete=0").
  182. Desc("o.created_at").Where("o.buyer_id=? and o.is_delete=0", uid)
  183. if t.PageSize > 0 {
  184. s = s.Limit(t.PageSize, t.Page*t.PageSize)
  185. }
  186. total, err := s.Desc("o.created_at").FindAndCount(&orders)
  187. t.CheckErr(err)
  188. t.JSON(map[string]interface{}{
  189. "list": orders,
  190. "total": total,
  191. })
  192. }
  193. // 二维码
  194. func (t *OrderEntryCtl) Qrcode() {
  195. userId := t.MustGetInt64("user_id")
  196. activityId := t.MustGetInt64("activity_id")
  197. goodId := t.MustGetInt64("good_id")
  198. Url := fmt.Sprintf("%s/PcClient/Client/OrderEntryCtl/order?user_id=%d&activity_id=%d&good_id=%d",
  199. define.HOST, userId, activityId, goodId)
  200. qr, err := utils.Qrcode2Base64(Url)
  201. t.CheckErr(err)
  202. t.JSON(map[string]interface{}{
  203. "qrcode": qr,
  204. })
  205. }