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

264 lines
7.8 KiB

5 years ago
  1. package pc
  2. import (
  3. "fmt"
  4. "github.com/ouxuanserver/osmanthuswine/src/core"
  5. "hudongzhuanjia/controllers"
  6. "hudongzhuanjia/models"
  7. ws_send_service "hudongzhuanjia/services/ws_send"
  8. "hudongzhuanjia/utils"
  9. "hudongzhuanjia/utils/code"
  10. "hudongzhuanjia/utils/define"
  11. "strconv"
  12. "time"
  13. )
  14. //竞拍
  15. type AuctionCtl struct {
  16. controllers.AuthorCtl
  17. }
  18. // 竞拍准备状态
  19. func (t *AuctionCtl) ReadyAuction() {
  20. auctionId := t.MustGetInt64("auction_activity_id")
  21. auction := new(models.NewAuctionActivity)
  22. exist, err := models.GetById(auction, auctionId)
  23. t.CheckErr(err)
  24. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "竞拍活动不存在")
  25. if auction.Status != define.StatusNotBegin {
  26. t.ERROR(fmt.Sprintf("该活动%s", auction.Status), code.MSG_ERR)
  27. }
  28. _, err = auction.UpdateToStatusByAid(auction.ActivityId, define.StatusReady, define.StatusNotBegin)
  29. t.CheckErr(err)
  30. _, err = auction.UpdateStatusById(auction.Id, define.StatusReady)
  31. t.CheckErr(err)
  32. t.SUCCESS("success")
  33. }
  34. //开始竞拍
  35. // auction doc
  36. // @Summary auction
  37. // @Description 开始竞拍
  38. // @Tags pc
  39. // @Accept json
  40. // @Produce json
  41. // @Param auction_activity_id query int true "竞拍id"
  42. // @Success 200 {string} string "success"
  43. // @Router /Pc/AuctionCtl/startAuction [get]
  44. func (t *AuctionCtl) StartAuction() {
  45. auctionId := t.MustGetInt64("auction_activity_id")
  46. auction := new(models.NewAuctionActivity)
  47. exist, err := models.GetById(auction, auctionId)
  48. t.CheckErr(err)
  49. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "竞拍活动不存在")
  50. //彩排
  51. if auction.Status != define.StatusReady {
  52. t.ERROR(fmt.Sprintf("该活动%s", auction.Status), code.MSG_ERR)
  53. }
  54. auction.Status = define.StatusRunning
  55. auction.UpdatedAt = time.Now()
  56. duration, _ := strconv.ParseInt(auction.AuctionDuration, 10, 64)
  57. auction.AuctionEndTime = time.Now().Unix() + duration
  58. _, err = core.GetXormAuto().Where("is_delete=0 and id=?", auctionId).
  59. Cols("status", "auction_end_time", "updated_at").Update(auction)
  60. t.CheckErr(err)
  61. t.STRING("操作成功")
  62. }
  63. //停止竞拍
  64. // auction doc
  65. // @Summary auction
  66. // @Description 结束竞拍
  67. // @Tags pc
  68. // @Accept json
  69. // @Produce json
  70. // @Param auction_activity_id query int true "竞拍id"
  71. // @Success 200 {string} string "success"
  72. // @Router /Pc/AuctionCtl/stopAuction [get]
  73. func (t *AuctionCtl) StopAuction() {
  74. auctionId := t.MustGetInt64("auction_activity_id")
  75. rehearsalId := t.MustGetInt64("rehearsal_id")
  76. auction := new(models.NewAuctionActivity)
  77. exist, err := models.GetById(auction, auctionId)
  78. t.CheckErr(err)
  79. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "竞拍活动不存在")
  80. if auction.Status != define.StatusRunning {
  81. t.ERROR(fmt.Sprintf("该活动%s", auction.Status), code.MSG_ERR)
  82. }
  83. auction.Status = define.StatusEnding
  84. auction.UpdatedAt = time.Now()
  85. auction.AuctionEndTime = 0 // 消除结束时间计时
  86. _, err = core.GetXormAuto().Where("is_delete=0 and id=?", auctionId).
  87. Cols("status", "auction_end_time", "updated_at").Update(auction)
  88. t.CheckErr(err)
  89. // todo: 记录用户最高价
  90. if auction.AuctionModel == define.INCR_AUCTION {
  91. history := new(models.AuctionHistory)
  92. _, err := history.GetHighestMoney(rehearsalId, auction.Id) // 区分彩排数据
  93. t.CheckErr(err)
  94. user := new(models.User)
  95. exist, err := models.GetById(user, history.UserId)
  96. t.CheckErr(err)
  97. if exist {
  98. record := new(models.AuctionResultRecord)
  99. record.AuctionActivityId = auctionId
  100. record.RehearsalId = rehearsalId // 彩排
  101. record.AuctionGoodsName = auction.AuctionGoodsName
  102. record.ActivityId = auction.ActivityId
  103. record.UserId = history.UserId
  104. record.UserPhone = user.Phone
  105. record.UserName = user.Nickname
  106. record.Unit = history.Unit
  107. record.DealPrice = history.Money
  108. record.PlayerCode = history.PlayerCode
  109. record.UpdatedAt = time.Now()
  110. record.CreatedAt = time.Now()
  111. record.IsDelete = false
  112. go ws_send_service.SendAuction(fmt.Sprintf("%d", record.ActivityId),
  113. "", 0, map[string]interface{}{
  114. "type": "auction",
  115. "data": map[string]interface{}{
  116. "auction_activity_id": auction.Id,
  117. "activity_id": auction.ActivityId,
  118. "max_money": history.Money,
  119. "user_id": user.Id,
  120. "avatar": user.Avatar,
  121. "nickname": user.Nickname,
  122. "num": 0,
  123. "status": "已结束",
  124. "model": auction.AuctionModel,
  125. },
  126. })
  127. _, err = core.GetXormAuto().InsertOne(record)
  128. t.CheckErr(err)
  129. }
  130. } else {
  131. go ws_send_service.SendAuction(fmt.Sprintf("%d", auction.ActivityId),
  132. "", 0, map[string]interface{}{
  133. "type": "auction",
  134. "customer_id": 0,
  135. "user_id": 0,
  136. "data": map[string]interface{}{
  137. "auction_activity_id": auction.Id,
  138. "activity_id": auction.ActivityId,
  139. "max_money": 0,
  140. "user_id": 0,
  141. "avatar": "",
  142. "nickname": "",
  143. "num": 0,
  144. "status": "已结束",
  145. "model": auction.AuctionModel,
  146. },
  147. })
  148. }
  149. t.STRING("操作成功")
  150. }
  151. // 增加数据
  152. func (t *AuctionCtl) History() {
  153. auctionId := t.MustGetInt64("auction_activity_id")
  154. rehearsalId := t.MustGetInt64("rehearsal_id")
  155. auction := new(models.NewAuctionActivity)
  156. exist, err := models.GetById(auction, auctionId)
  157. t.CheckErr(err)
  158. t.Assert(exist, code.MSG_AUCTION_NOT_EXIST, "竞拍不存在")
  159. orderBy := "money asc"
  160. if auction.AuctionModel == "加价竞拍" {
  161. orderBy = "money desc"
  162. }
  163. histories, err := models.GetAuctionHistoriesByAuctionId(auctionId, rehearsalId, orderBy)
  164. t.CheckErr(err)
  165. userIdMap := make(map[int64]struct{}, 0) // 去重操作
  166. for u := range histories {
  167. if _, ok := userIdMap[histories[u].UserId]; ok {
  168. //去掉重复的
  169. continue
  170. } else {
  171. userIdMap[histories[u].UserId] = struct{}{} // 增加,下次进行检测
  172. }
  173. auction.Histories = append(auction.Histories, histories[u])
  174. }
  175. t.JSON(map[string]interface{}{
  176. "auction": auction,
  177. })
  178. }
  179. func (t *AuctionCtl) List() {
  180. activityId := t.MustGetInt64("activity_id")
  181. rehearsalId := t.MustGetInt64("rehearsal_id")
  182. auctions, err := models.GetAuctionsByActivityId(activityId, "created_at asc")
  183. t.CheckErr(err)
  184. // 更具某个数据进行
  185. upIds := make([]int64, 0)
  186. for _, item := range auctions {
  187. if item.AuctionModel == "加价竞拍" {
  188. upIds = append(upIds, item.Id)
  189. }
  190. }
  191. upH, err := models.GetAuctionHistoriesByAuctionIds(upIds, rehearsalId, "money desc")
  192. t.CheckErr(err)
  193. for i := range auctions {
  194. if auctions[i].AuctionModel == "加价竞拍" {
  195. userIdMap := make(map[int64]struct{}, 0) // 去重操作
  196. for u := range upH {
  197. if _, ok := userIdMap[upH[u].UserId]; ok {
  198. //去掉重复的
  199. continue
  200. }
  201. if auctions[i].Id == upH[u].AuctionActivityId && len(auctions[i].Histories) <= 3 {
  202. auctions[i].Histories = append(auctions[i].Histories, upH[u])
  203. userIdMap[upH[u].UserId] = struct{}{} // 增加,下次进行检测
  204. }
  205. }
  206. }
  207. }
  208. t.JSON(map[string]interface{}{
  209. "total": len(auctions),
  210. "list": auctions,
  211. })
  212. }
  213. // 成交记录
  214. func (t *AuctionCtl) Records() {
  215. rehearsalId := t.MustGetInt64("rehearsal_id") // 彩排
  216. auctionId := t.MustGetInt64("auction_activity_id")
  217. records, err := models.GetAuctionRecordsByAuctionId(auctionId, rehearsalId, "created_at desc")
  218. t.CheckErr(err)
  219. t.JSON(map[string]interface{}{
  220. "records": records,
  221. "total": len(records),
  222. })
  223. }
  224. //获取二维码
  225. func (t *AuctionCtl) Qrcode() {
  226. //将服务器得地址和activity_id,动态生成二维码
  227. qrcode, err := utils.GenH5Qrcode(define.H5Auction, map[string]interface{}{
  228. "activity_id": t.MustGetInt64("activity_id"),
  229. "customer_id": t.MustGetUID(),
  230. "auction_id": t.MustGetInt64("auction_activity_id"),
  231. "rehearsal_id": t.MustGetInt64("rehearsal_id"),
  232. })
  233. t.CheckErr(err)
  234. t.JSON(map[string]interface{}{
  235. "qrcode": qrcode,
  236. })
  237. }