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

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