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

185 lines
5.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
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. auction_service "hudongzhuanjia/services/auction"
  7. ws_send_service "hudongzhuanjia/services/ws_send"
  8. "hudongzhuanjia/utils/code"
  9. "hudongzhuanjia/utils/define"
  10. "time"
  11. )
  12. type AuctionCtl struct {
  13. controllers.AuthorCtl
  14. }
  15. // 竞拍动作
  16. func (t *AuctionCtl) Auction() {
  17. auctionId := t.MustGetInt64("auction_activity_id")
  18. totalMoney := t.MustGetDouble("total_money")
  19. uid := t.MustGetUID()
  20. areaId := t.MustGetInt64("area_id")
  21. if totalMoney < 0 {
  22. t.ERROR("金额不能为0", code.MSG_ERR)
  23. }
  24. auction := new(models.NewAuctionActivity)
  25. exist, err := models.Get(auction, auctionId)
  26. t.CheckErr(err)
  27. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "竞拍活动不存在")
  28. t.CheckRunning(auction.Status)
  29. activity := new(models.Activity)
  30. exist, err = models.Get(activity, auction.ActivityId)
  31. t.CheckErr(err)
  32. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  33. t.CheckRunning(activity.Status)
  34. user := new(models.User)
  35. exist, err = models.Get(user, uid)
  36. t.CheckErr(err)
  37. t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
  38. area := new(models.AreaStore)
  39. exist, err = models.Get(area, areaId)
  40. t.CheckErr(err)
  41. t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
  42. player := new(models.AuctionPlayer)
  43. exist, err = player.GetByAuctionIdAndUid(auction.Id, uid, activity.RehearsalId)
  44. t.CheckErr(err)
  45. t.Assert(exist, code.MSG_AUCTION_NOT_EXIST, "竞拍编号不存在")
  46. history := new(models.AuctionHistory)
  47. history.AuctionActivityId = auction.Id
  48. history.AuctionGoodsName = auction.AuctionGoodsName
  49. history.ActivityId = activity.Id
  50. history.RehearsalId = activity.RehearsalId
  51. history.UserId = uid
  52. history.AreaId = area.Id
  53. history.UserName = user.Nickname
  54. history.AreaName = area.Name
  55. history.UserPhone = user.Phone
  56. history.Money = totalMoney
  57. history.Unit = auction.Unit
  58. history.PlayerCode = player.Code
  59. history.UserId = uid
  60. history.IsDelete = false
  61. history.CreatedAt = time.Now()
  62. history.UpdatedAt = time.Now()
  63. _, err = models.Add(history)
  64. t.CheckErr(err)
  65. if auction.AuctionModel == "加价竞拍" {
  66. maxHistory := new(models.AuctionHistory)
  67. exist, err := maxHistory.GetHighestMoney(activity.RehearsalId, auction.Id)
  68. t.CheckErr(err)
  69. if exist && (history.Money < maxHistory.Money || history.UserId != maxHistory.UserId) {
  70. history.IsDelete = true
  71. _, err = models.Update(history.Id, history, "is_delete")
  72. t.CheckErr(err)
  73. t.ERROR("加价失败, 价格低于当前最高价", code.MSG_ERR)
  74. return
  75. }
  76. go ws_send_service.SendAuction(fmt.Sprintf("%d", activity.Id),
  77. "", 0, map[string]interface{}{
  78. "type": "auction",
  79. "data": map[string]interface{}{
  80. "max_money": maxHistory.Money,
  81. "avatar": user.Avatar,
  82. "nickname": user.Nickname,
  83. "num": 0,
  84. "user_id": user.Id,
  85. "status": "进行中",
  86. "model": define.INCR_AUCTION,
  87. },
  88. })
  89. // 成功
  90. } else { // 减价竞拍
  91. record := new(models.AuctionResultRecord)
  92. count, err := record.CountHistory(activity.RehearsalId, auction.Id)
  93. t.CheckErr(err)
  94. if int(count) >= auction.GoodsNum {
  95. t.ERROR("所有商品已经竞拍完毕", code.MSG_ERR)
  96. }
  97. record.AuctionActivityId = auctionId
  98. record.AuctionGoodsName = auction.AuctionGoodsName
  99. record.ActivityId = auction.ActivityId
  100. record.UserId = uid
  101. record.UserPhone = user.Phone
  102. record.UserName = user.Nickname
  103. record.AreaId = area.Id
  104. record.AreaName = area.Name
  105. record.DealPrice = totalMoney
  106. record.RehearsalId = activity.RehearsalId
  107. record.PlayerCode = player.Code
  108. record.Unit = auction.Unit
  109. record.UpdatedAt = time.Now()
  110. record.CreatedAt = time.Now()
  111. record.IsDelete = false
  112. _, err = models.Add(record)
  113. t.CheckErr(err)
  114. go ws_send_service.SendAuction(fmt.Sprintf("%d", activity.Id),
  115. "", 0, map[string]interface{}{
  116. "type": "auction",
  117. "data": map[string]interface{}{
  118. "max_money": 0,
  119. "avatar": user.Avatar,
  120. "nickname": user.Nickname,
  121. "user_id": user.Id,
  122. "status": "进行中",
  123. "num": auction.GoodsNum - int(count) - 1,
  124. "model": define.DECR_AUCTION,
  125. },
  126. })
  127. }
  128. // 加价
  129. t.SUCCESS("加价成功")
  130. }
  131. func (t *AuctionCtl) ExistRecord() {
  132. auctionId := t.MustGetInt64("auction_activity_id")
  133. uid := t.MustGetUID()
  134. auction := new(models.NewAuctionActivity)
  135. exist, err := models.Get(auction, auctionId)
  136. t.CheckErr(err)
  137. t.Assert(exist, code.MSG_AUCTION_NOT_EXIST, "竞拍活动不存在")
  138. activity := new(models.Activity)
  139. exist, err = models.Get(activity, auction.ActivityId)
  140. t.CheckErr(err)
  141. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  142. record := new(models.AuctionResultRecord)
  143. exist, err = record.GetByUserIdAndAuctionId(uid, auctionId, activity.RehearsalId)
  144. t.CheckErr(err)
  145. t.JSON(map[string]interface{}{
  146. "show": exist,
  147. })
  148. }
  149. // 已经竞拍到了
  150. func (t *AuctionCtl) UserAuctions() {
  151. uid := t.MustGetUID()
  152. activityId := t.MustGetInt64("activity_id")
  153. activity := new(models.Activity)
  154. exist, err := models.Get(activity, activityId)
  155. t.CheckErr(err)
  156. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  157. records, err := auction_service.GetUserAuctions(activityId, activity.RehearsalId, uid)
  158. t.CheckErr(err)
  159. t.JSON(map[string]interface{}{
  160. "list": records,
  161. "total": len(records),
  162. })
  163. }