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

104 lines
3.2 KiB

5 years ago
5 years ago
5 years ago
  1. package auction_service
  2. import (
  3. "errors"
  4. "github.com/ouxuanserver/osmanthuswine/src/core"
  5. "hudongzhuanjia/models"
  6. "hudongzhuanjia/utils/define"
  7. "strconv"
  8. "time"
  9. )
  10. func GetCurrentAuction(aid, rid, uid int64) (map[string]interface{}, error) {
  11. auction := new(models.NewAuctionActivity)
  12. exist, err := auction.GetCurrent(aid)
  13. if err != nil {
  14. return nil, err
  15. }
  16. if !exist {
  17. return nil, errors.New("轮次尚未开启")
  18. }
  19. // 计算当前价格 和 当前数量
  20. if auction.AuctionModel == define.INCR_AUCTION { // 加价竞拍 最高价格
  21. history := new(models.AuctionHistory)
  22. exist, err = history.GetHighestMoney(rid, auction.Id)
  23. if err != nil {
  24. return nil, err
  25. }
  26. if !exist {
  27. auction.CurrentMoney = auction.StartPrice
  28. } else {
  29. auction.CurrentMoney = history.Money
  30. }
  31. } else { // 初始价格 - (竞拍时长 - (竞拍结束时间 - 现在时间)) * 减价速率
  32. if auction.AuctionEndTime == 0 {
  33. auction.CurrentMoney = auction.StartPrice
  34. } else {
  35. duration, _ := strconv.ParseInt(auction.AuctionDuration, 10, 64)
  36. reduceRate, _ := strconv.ParseInt(auction.ReduceRate, 10, 64)
  37. money := auction.StartPrice - float64((duration-(auction.AuctionEndTime-time.Now().Unix()))*reduceRate)
  38. auction.CurrentMoney = money
  39. }
  40. record := new(models.AuctionResultRecord)
  41. count, err := record.CountHistory(rid, aid)
  42. if err != nil {
  43. return nil, err
  44. }
  45. auction.GoodsNum -= int(count)
  46. }
  47. player := new(models.AuctionPlayer)
  48. exist, err = player.GetByAuctionIdAndUid(auction.Id, uid, rid)
  49. if err != nil {
  50. return nil, err
  51. }
  52. if !exist {
  53. // 领取号码, 开启事务: 获取人数和写入人数 保证原子性
  54. session := core.GetXormAuto().NewSession()
  55. if session.Begin(); err != nil { // 开启事务
  56. return nil, err
  57. }
  58. num, err := session.Where("is_delete=0 and auction_activity_id=? and rehearsal_id=?", auction.Id, rid).Count(player)
  59. if err != nil {
  60. session.Rollback()
  61. return nil, err
  62. }
  63. player.AuctionActivityId = auction.Id
  64. player.UserId = uid
  65. player.RehearsalId = rid
  66. player.Code = num + 1
  67. player.UpdatedAt = time.Now()
  68. player.CreatedAt = time.Now()
  69. _, err = session.Insert(player)
  70. if err != nil {
  71. session.Rollback()
  72. return nil, err
  73. }
  74. session.Commit()
  75. }
  76. return map[string]interface{}{
  77. "auction": auction,
  78. "player": player,
  79. }, nil
  80. }
  81. type UserAuctionsResult struct {
  82. Id int64 `json:"id"`
  83. AuctionActivityId int64 `json:"auction_activity_id"`
  84. AuctionGoodsName string `json:"auction_goods_name"`
  85. GoodsPicUrl string `json:"goods_pic_url"`
  86. DealPrice float64 `json:"deal_price"`
  87. Unit int64 `json:"unit"`
  88. }
  89. func GetUserAuctions(activityId, rehearsalId, userId int64) ([]*UserAuctionsResult, error) {
  90. records := make([]*UserAuctionsResult, 0)
  91. err := core.GetXormAuto().Table(new(models.AuctionResultRecord)).Alias("r").
  92. Select("r.id, a.id as auction_activity_id, a.auction_goods_name, a.goods_pic_url, r.deal_price, a.unit").
  93. Join("LEFT", new(models.NewAuctionActivity).Alias("a"),
  94. "a.id=r.auction_activity_id and a.is_delete=0").
  95. Where("r.is_delete=0 and r.activity_id=? and r.user_id=? and r.rehearsal_id=?",
  96. activityId, userId, rehearsalId).Desc("r.created_at").Find(&records)
  97. return records, err
  98. }