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

255 lines
7.4 KiB

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