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

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