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

187 lines
5.4 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
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.MustGetInt("auction_activity_id")
  18. totalMoney := t.MustGetDouble("total_money")
  19. uid := t.GetAccountId()
  20. areaId := t.MustGetInt("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, activity.ArchId)
  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.ArchId = activity.ArchId
  49. history.AuctionGoodsName = auction.AuctionGoodsName
  50. history.ActivityId = activity.Id
  51. history.RehearsalId = activity.RehearsalId
  52. history.UserId = uid
  53. history.AreaId = area.Id
  54. history.UserName = user.Nickname
  55. history.AreaName = area.Name
  56. history.UserPhone = user.Phone
  57. history.Money = totalMoney
  58. history.Unit = auction.Unit
  59. history.PlayerCode = player.Code
  60. history.UserId = uid
  61. history.IsDelete = false
  62. history.CreatedAt = time.Now()
  63. history.UpdatedAt = time.Now()
  64. _, err = models.Add(history)
  65. t.CheckErr(err)
  66. if auction.AuctionModel == "加价竞拍" {
  67. maxHistory := new(models.AuctionHistory)
  68. exist, err := maxHistory.GetHighestMoney(activity.RehearsalId, auction.Id, activity.ArchId)
  69. t.CheckErr(err)
  70. if exist && (history.Money < maxHistory.Money || history.UserId != maxHistory.UserId) {
  71. history.IsDelete = true
  72. _, err = models.Update(history.Id, history, "is_delete")
  73. t.CheckErr(err)
  74. t.ERROR("加价失败, 价格低于当前最高价", code.MSG_ERR)
  75. return
  76. }
  77. go ws_send_service.SendAuction(fmt.Sprintf("%d", activity.Id),
  78. "", 0, map[string]interface{}{
  79. "type": "auction",
  80. "data": map[string]interface{}{
  81. "max_money": maxHistory.Money,
  82. "avatar": user.Avatar,
  83. "nickname": user.Nickname,
  84. "num": 0,
  85. "user_id": user.Id,
  86. "status": "进行中",
  87. "model": define.INCR_AUCTION,
  88. },
  89. })
  90. // 成功
  91. } else { // 减价竞拍
  92. record := new(models.AuctionResultRecord)
  93. count, err := record.CountHistory(activity.RehearsalId, auction.Id, activity.ArchId)
  94. t.CheckErr(err)
  95. if int(count) >= auction.GoodsNum {
  96. t.ERROR("所有商品已经竞拍完毕", code.MSG_ERR)
  97. }
  98. record.AuctionActivityId = auctionId
  99. record.AuctionGoodsName = auction.AuctionGoodsName
  100. record.ActivityId = auction.ActivityId
  101. record.UserId = uid
  102. record.UserPhone = user.Phone
  103. record.UserName = user.Nickname
  104. record.AreaId = area.Id
  105. record.AreaName = area.Name
  106. record.ArchId = activity.ArchId
  107. record.DealPrice = totalMoney
  108. record.RehearsalId = activity.RehearsalId
  109. record.PlayerCode = player.Code
  110. record.Unit = auction.Unit
  111. record.UpdatedAt = time.Now()
  112. record.CreatedAt = time.Now()
  113. record.IsDelete = false
  114. _, err = models.Add(record)
  115. t.CheckErr(err)
  116. go ws_send_service.SendAuction(fmt.Sprintf("%d", activity.Id),
  117. "", 0, map[string]interface{}{
  118. "type": "auction",
  119. "data": map[string]interface{}{
  120. "max_money": 0,
  121. "avatar": user.Avatar,
  122. "nickname": user.Nickname,
  123. "user_id": user.Id,
  124. "status": "进行中",
  125. "num": auction.GoodsNum - int(count) - 1,
  126. "model": define.DECR_AUCTION,
  127. },
  128. })
  129. }
  130. // 加价
  131. t.SUCCESS("加价成功")
  132. }
  133. func (t *AuctionCtl) ExistRecord() {
  134. auctionId := t.MustGetInt("auction_activity_id")
  135. uid := t.GetAccountId()
  136. auction := new(models.NewAuctionActivity)
  137. exist, err := models.Get(auction, auctionId)
  138. t.CheckErr(err)
  139. t.Assert(exist, code.MSG_AUCTION_NOT_EXIST, "竞拍活动不存在")
  140. activity := new(models.Activity)
  141. exist, err = models.Get(activity, auction.ActivityId)
  142. t.CheckErr(err)
  143. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  144. record := new(models.AuctionResultRecord)
  145. exist, err = record.GetByUserIdAndAuctionId(uid, auctionId, activity.ArchId, activity.RehearsalId)
  146. t.CheckErr(err)
  147. t.JSON(map[string]interface{}{
  148. "show": exist,
  149. })
  150. }
  151. // 已经竞拍到了
  152. func (t *AuctionCtl) UserAuctions() {
  153. uid := t.GetAccountId()
  154. activityId := t.MustGetInt("activity_id")
  155. activity := new(models.Activity)
  156. exist, err := models.Get(activity, activityId)
  157. t.CheckErr(err)
  158. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  159. records, err := auction_service.GetUserAuctions(activityId, activity.RehearsalId, uid, activity.ArchId)
  160. t.CheckErr(err)
  161. t.JSON(map[string]interface{}{
  162. "list": records,
  163. "total": len(records),
  164. })
  165. }