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

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