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

185 lines
5.3 KiB

package client
import (
"fmt"
"hudongzhuanjia/controllers"
"hudongzhuanjia/models"
auction_service "hudongzhuanjia/services/auction"
ws_send_service "hudongzhuanjia/services/ws_send"
"hudongzhuanjia/utils/code"
"hudongzhuanjia/utils/define"
"time"
)
type AuctionCtl struct {
controllers.AuthorCtl
}
// 竞拍动作
func (t *AuctionCtl) Auction() {
auctionId := t.MustGetInt64("auction_activity_id")
totalMoney := t.MustGetDouble("total_money")
uid := t.MustGetUID()
areaId := t.MustGetInt64("area_id")
if totalMoney < 0 {
t.ERROR("金额不能为0", code.MSG_ERR)
}
auction := new(models.NewAuctionActivity)
exist, err := models.Get(auction, auctionId)
t.CheckErr(err)
t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "竞拍活动不存在")
t.CheckRunning(auction.Status)
activity := new(models.Activity)
exist, err = models.Get(activity, auction.ActivityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
t.CheckRunning(activity.Status)
user := new(models.User)
exist, err = models.Get(user, uid)
t.CheckErr(err)
t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
area := new(models.AreaStore)
exist, err = models.Get(area, areaId)
t.CheckErr(err)
t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
player := new(models.AuctionPlayer)
exist, err = player.GetByAuctionIdAndUid(auction.Id, uid, activity.RehearsalId)
t.CheckErr(err)
t.Assert(exist, code.MSG_AUCTION_NOT_EXIST, "竞拍编号不存在")
history := new(models.AuctionHistory)
history.AuctionActivityId = auction.Id
history.AuctionGoodsName = auction.AuctionGoodsName
history.ActivityId = activity.Id
history.RehearsalId = activity.RehearsalId
history.UserId = uid
history.AreaId = area.Id
history.UserName = user.Nickname
history.AreaName = area.Name
history.UserPhone = user.Phone
history.Money = totalMoney
history.Unit = auction.Unit
history.PlayerCode = player.Code
history.UserId = uid
history.IsDelete = false
history.CreatedAt = time.Now()
history.UpdatedAt = time.Now()
_, err = models.Add(history)
t.CheckErr(err)
if auction.AuctionModel == "加价竞拍" {
maxHistory := new(models.AuctionHistory)
exist, err := maxHistory.GetHighestMoney(activity.RehearsalId, auction.Id)
t.CheckErr(err)
if exist && (history.Money < maxHistory.Money || history.UserId != maxHistory.UserId) {
history.IsDelete = true
_, err = models.Update(history.Id, history, "is_delete")
t.CheckErr(err)
t.ERROR("加价失败, 价格低于当前最高价", code.MSG_ERR)
return
}
go ws_send_service.SendAuction(fmt.Sprintf("%d", activity.Id),
"", 0, map[string]interface{}{
"type": "auction",
"data": map[string]interface{}{
"max_money": maxHistory.Money,
"avatar": user.Avatar,
"nickname": user.Nickname,
"num": 0,
"user_id": user.Id,
"status": "进行中",
"model": define.INCR_AUCTION,
},
})
// 成功
} else { // 减价竞拍
record := new(models.AuctionResultRecord)
count, err := record.CountHistory(activity.RehearsalId, auction.Id)
t.CheckErr(err)
if int(count) >= auction.GoodsNum {
t.ERROR("所有商品已经竞拍完毕", code.MSG_ERR)
}
record.AuctionActivityId = auctionId
record.AuctionGoodsName = auction.AuctionGoodsName
record.ActivityId = auction.ActivityId
record.UserId = uid
record.UserPhone = user.Phone
record.UserName = user.Nickname
record.AreaId = area.Id
record.AreaName = area.Name
record.DealPrice = totalMoney
record.RehearsalId = activity.RehearsalId
record.PlayerCode = player.Code
record.Unit = auction.Unit
record.UpdatedAt = time.Now()
record.CreatedAt = time.Now()
record.IsDelete = false
_, err = models.Add(record)
t.CheckErr(err)
go ws_send_service.SendAuction(fmt.Sprintf("%d", activity.Id),
"", 0, map[string]interface{}{
"type": "auction",
"data": map[string]interface{}{
"max_money": 0,
"avatar": user.Avatar,
"nickname": user.Nickname,
"user_id": user.Id,
"status": "进行中",
"num": auction.GoodsNum - int(count) - 1,
"model": define.DECR_AUCTION,
},
})
}
// 加价
t.SUCCESS("加价成功")
}
func (t *AuctionCtl) ExistRecord() {
auctionId := t.MustGetInt64("auction_activity_id")
uid := t.MustGetUID()
auction := new(models.NewAuctionActivity)
exist, err := models.Get(auction, auctionId)
t.CheckErr(err)
t.Assert(exist, code.MSG_AUCTION_NOT_EXIST, "竞拍活动不存在")
activity := new(models.Activity)
exist, err = models.Get(activity, auction.ActivityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
record := new(models.AuctionResultRecord)
exist, err = record.GetByUserIdAndAuctionId(uid, auctionId, activity.RehearsalId)
t.CheckErr(err)
t.JSON(map[string]interface{}{
"show": exist,
})
}
// 已经竞拍到了
func (t *AuctionCtl) UserAuctions() {
uid := t.MustGetUID()
activityId := t.MustGetInt64("activity_id")
activity := new(models.Activity)
exist, err := models.Get(activity, activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
records, err := auction_service.GetUserAuctions(activityId, activity.RehearsalId, uid)
t.CheckErr(err)
t.JSON(map[string]interface{}{
"list": records,
"total": len(records),
})
}