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

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. "github.com/ouxuanserver/osmanthuswine/src/core"
  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. if totalMoney < 0 {
  21. t.ERROR("金额不能为0", code.MSG_ERR)
  22. }
  23. auction := new(models.NewAuctionActivity)
  24. exist, err := models.GetById(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.GetById(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.GetById(user, uid)
  35. t.CheckErr(err)
  36. t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
  37. area := new(models.AreaStore)
  38. exist, err = models.GetById(area, user.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 = user.AreaId
  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 = core.GetXormAuto().InsertOne(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. _, err = history.SoftDelete()
  70. t.CheckErr(err)
  71. t.ERROR("加价失败, 价格低于当前最高价", code.MSG_ERR)
  72. return
  73. }
  74. go ws_send_service.SendAuction(fmt.Sprintf("%d", activity.Id),
  75. "", 0, map[string]interface{}{
  76. "type": "auction",
  77. "data": map[string]interface{}{
  78. "max_money": maxHistory.Money,
  79. "avatar": user.Avatar,
  80. "nickname": user.Nickname,
  81. "num": 0,
  82. "user_id": user.Id,
  83. "status": "进行中",
  84. "model": "加价竞拍",
  85. },
  86. })
  87. // 成功
  88. } else { // 减价竞拍
  89. record := new(models.AuctionResultRecord)
  90. count, err := record.CountHistory(activity.RehearsalId, auction.Id)
  91. t.CheckErr(err)
  92. if int(count) >= auction.GoodsNum {
  93. t.ERROR("所有商品已经竞拍完毕", code.MSG_ERR)
  94. }
  95. record.AuctionActivityId = auctionId
  96. record.AuctionGoodsName = auction.AuctionGoodsName
  97. record.ActivityId = auction.ActivityId
  98. record.UserId = uid
  99. record.UserPhone = user.Phone
  100. record.UserName = user.Nickname
  101. record.AreaId = user.AreaId
  102. record.AreaName = user.AreaName
  103. record.DealPrice = totalMoney
  104. record.RehearsalId = activity.RehearsalId
  105. record.PlayerCode = player.Code
  106. record.Unit = auction.Unit
  107. record.UpdatedAt = time.Now()
  108. record.CreatedAt = time.Now()
  109. record.IsDelete = false
  110. _, err = core.GetXormAuto().InsertOne(record)
  111. t.CheckErr(err)
  112. go ws_send_service.SendAuction(fmt.Sprintf("%d", activity.Id),
  113. "", 0, map[string]interface{}{
  114. "type": "auction",
  115. "data": map[string]interface{}{
  116. "max_money": 0,
  117. "avatar": user.Avatar,
  118. "nickname": user.Nickname,
  119. "user_id": user.Id,
  120. "status": "进行中",
  121. "num": auction.GoodsNum - int(count) - 1,
  122. "model": "减价竞拍",
  123. },
  124. })
  125. }
  126. // 加价
  127. t.STRING("加价成功")
  128. }
  129. func (t *AuctionCtl) ExistRecord() {
  130. auctionId := t.MustGetInt64("auction_activity_id")
  131. uid := t.MustGetUID()
  132. auction := new(models.NewAuctionActivity)
  133. exist, err := models.GetById(auction, auctionId)
  134. t.CheckErr(err)
  135. t.Assert(exist, code.MSG_AUCTION_NOT_EXIST, "竞拍活动不存在")
  136. activity := new(models.Activity)
  137. exist, err = models.GetById(activity, auction.ActivityId)
  138. t.CheckErr(err)
  139. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  140. record := new(models.AuctionResultRecord)
  141. exist, err = core.GetXormAuto().Where("is_delete=0 and user_id=? and auction_activity_id=? and rehearsal_id=?", uid, auctionId, activity.RehearsalId).Get(record)
  142. t.CheckErr(err)
  143. t.JSON(map[string]interface{}{
  144. "show": exist,
  145. })
  146. }
  147. // 已经竞拍到了
  148. func (t *AuctionCtl) UserAuctions() {
  149. uid := t.MustGetUID()
  150. activityId := t.MustGetInt64("activity_id")
  151. activity := new(models.Activity)
  152. exist, err := models.GetById(activity, activityId)
  153. t.CheckErr(err)
  154. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  155. records, err := auction_service.GetUserAuctions(activityId, activity.RehearsalId, uid)
  156. t.CheckErr(err)
  157. t.JSON(map[string]interface{}{
  158. "list": records,
  159. "total": len(records),
  160. })
  161. }