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
104 lines
3.2 KiB
package auction_service
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/ouxuanserver/osmanthuswine/src/core"
|
|
"hudongzhuanjia/models"
|
|
"hudongzhuanjia/utils/define"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
func GetCurrentAuction(aid, rid, uid int64) (map[string]interface{}, error) {
|
|
auction := new(models.NewAuctionActivity)
|
|
exist, err := auction.GetCurrent(aid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !exist {
|
|
return nil, errors.New("轮次尚未开启")
|
|
}
|
|
|
|
// 计算当前价格 和 当前数量
|
|
if auction.AuctionModel == define.INCR_AUCTION { // 加价竞拍 最高价格
|
|
history := new(models.AuctionHistory)
|
|
exist, err = history.GetHighestMoney(rid, auction.Id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !exist {
|
|
auction.CurrentMoney = auction.StartPrice
|
|
} else {
|
|
auction.CurrentMoney = history.Money
|
|
}
|
|
} else { // 初始价格 - (竞拍时长 - (竞拍结束时间 - 现在时间)) * 减价速率
|
|
if auction.AuctionEndTime == 0 {
|
|
auction.CurrentMoney = auction.StartPrice
|
|
} else {
|
|
duration, _ := strconv.ParseInt(auction.AuctionDuration, 10, 64)
|
|
reduceRate, _ := strconv.ParseInt(auction.ReduceRate, 10, 64)
|
|
money := auction.StartPrice - float64((duration-(auction.AuctionEndTime-time.Now().Unix()))*reduceRate)
|
|
auction.CurrentMoney = money
|
|
}
|
|
record := new(models.AuctionResultRecord)
|
|
count, err := record.CountHistory(rid, aid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
auction.GoodsNum -= int(count)
|
|
}
|
|
|
|
player := new(models.AuctionPlayer)
|
|
exist, err = player.GetByAuctionIdAndUid(auction.Id, uid, rid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !exist {
|
|
// 领取号码, 开启事务: 获取人数和写入人数 保证原子性
|
|
session := core.GetXormAuto().NewSession()
|
|
if session.Begin(); err != nil { // 开启事务
|
|
return nil, err
|
|
}
|
|
num, err := session.Where("is_delete=0 and auction_activity_id=? and rehearsal_id=?", auction.Id, rid).Count(player)
|
|
if err != nil {
|
|
session.Rollback()
|
|
return nil, err
|
|
}
|
|
player.AuctionActivityId = auction.Id
|
|
player.UserId = uid
|
|
player.RehearsalId = rid
|
|
player.Code = num + 1
|
|
player.UpdatedAt = time.Now()
|
|
player.CreatedAt = time.Now()
|
|
_, err = session.Insert(player)
|
|
if err != nil {
|
|
session.Rollback()
|
|
return nil, err
|
|
}
|
|
session.Commit()
|
|
}
|
|
return map[string]interface{}{
|
|
"auction": auction,
|
|
"player": player,
|
|
}, nil
|
|
}
|
|
|
|
type UserAuctionsResult struct {
|
|
Id int64 `json:"id"`
|
|
AuctionActivityId int64 `json:"auction_activity_id"`
|
|
AuctionGoodsName string `json:"auction_goods_name"`
|
|
GoodsPicUrl string `json:"goods_pic_url"`
|
|
DealPrice float64 `json:"deal_price"`
|
|
Unit int64 `json:"unit"`
|
|
}
|
|
|
|
func GetUserAuctions(activityId, rehearsalId, userId int64) ([]*UserAuctionsResult, error) {
|
|
records := make([]*UserAuctionsResult, 0)
|
|
err := core.GetXormAuto().Table(new(models.AuctionResultRecord)).Alias("r").
|
|
Select("r.id, a.id as auction_activity_id, a.auction_goods_name, a.goods_pic_url, r.deal_price, a.unit").
|
|
Join("LEFT", new(models.NewAuctionActivity).Alias("a"),
|
|
"a.id=r.auction_activity_id and a.is_delete=0").
|
|
Where("r.is_delete=0 and r.activity_id=? and r.user_id=? and r.rehearsal_id=?",
|
|
activityId, userId, rehearsalId).Desc("r.created_at").Find(&records)
|
|
return records, err
|
|
}
|