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

275 lines
8.1 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
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. if auction.AuctionModel == define.INCR_AUCTION {
  69. history := new(models.AuctionHistory)
  70. _, err := history.GetHighestMoney(activity.RehearsalId, auction.Id, activity.ArchId) // 区分彩排数据
  71. t.CheckErr(err)
  72. user := new(models.User)
  73. exist, err := models.Get(user, history.UserId)
  74. t.CheckErr(err)
  75. record := new(models.AuctionResultRecord)
  76. if exist {
  77. record.AuctionActivityId = auctionId
  78. record.RehearsalId = history.RehearsalId // 彩排
  79. record.ArchId = activity.ArchId
  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. func (t *AuctionCtl) History() {
  131. auctionId := t.MustGetInt64("auction_activity_id")
  132. rehearsalId := t.MustGetInt64("rehearsal_id")
  133. auction := new(models.NewAuctionActivity)
  134. exist, err := models.Get(auction, auctionId)
  135. t.CheckErr(err)
  136. t.Assert(exist, code.MSG_AUCTION_NOT_EXIST, "竞拍不存在")
  137. activity := models.Activity{}
  138. exist, err = models.Get(activity, auction.ActivityId)
  139. t.CheckErr(err)
  140. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  141. orderBy := "money asc"
  142. if auction.AuctionModel == "加价竞拍" {
  143. orderBy = "money desc"
  144. }
  145. histories, err := models.GetAuctionHistoriesByAuctionId(auctionId, rehearsalId, orderBy, activity.ArchId)
  146. t.CheckErr(err)
  147. userIdMap := make(map[int64]struct{}, 0) // 去重操作
  148. for u := range histories {
  149. if _, ok := userIdMap[histories[u].UserId]; ok {
  150. //去掉重复的
  151. continue
  152. } else {
  153. userIdMap[histories[u].UserId] = struct{}{} // 增加,下次进行检测
  154. }
  155. auction.Histories = append(auction.Histories, histories[u])
  156. }
  157. t.JSON(map[string]interface{}{
  158. "auction": auction,
  159. })
  160. }
  161. func (t *AuctionCtl) List() {
  162. activityId := t.MustGetInt64("activity_id")
  163. rehearsalId := t.MustGetInt64("rehearsal_id")
  164. activity := models.Activity{}
  165. exist, err := models.Get(activity, activityId)
  166. t.CheckErr(err)
  167. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  168. auctions, err := models.GetAuctionsByActivityId(activityId)
  169. t.CheckErr(err)
  170. // 更具某个数据进行
  171. upIds := make([]int64, 0)
  172. for _, item := range auctions {
  173. if item.AuctionModel == "加价竞拍" {
  174. upIds = append(upIds, item.Id)
  175. }
  176. }
  177. upH, err := models.GetAuctionHistoriesByAuctionIds(upIds, rehearsalId, activity.ArchId)
  178. t.CheckErr(err)
  179. for i := range auctions {
  180. if auctions[i].AuctionModel == define.INCR_AUCTION {
  181. userIdMap := make(map[int64]struct{}, 0) // 去重操作
  182. for u := range upH {
  183. if _, ok := userIdMap[upH[u].UserId]; ok {
  184. //去掉重复的
  185. continue
  186. }
  187. if auctions[i].Id == upH[u].AuctionActivityId && len(auctions[i].Histories) <= 3 {
  188. auctions[i].Histories = append(auctions[i].Histories, upH[u])
  189. userIdMap[upH[u].UserId] = struct{}{} // 增加,下次进行检测
  190. }
  191. }
  192. }
  193. }
  194. t.JSON(map[string]interface{}{
  195. "total": len(auctions),
  196. "list": auctions,
  197. })
  198. }
  199. // 成交记录
  200. func (t *AuctionCtl) Records() {
  201. auctionId := t.MustGetInt64("auction_activity_id")
  202. auction := &models.NewAuctionActivity{}
  203. exist, err := models.Get(auction, auctionId)
  204. t.CheckErr(err)
  205. t.Assert(exist, code.MSG_AUCTION_NOT_EXIST, "竞拍活动不存在")
  206. activity := &models.Activity{}
  207. exist, err = models.Get(activity, auction.ActivityId)
  208. t.CheckErr(err)
  209. t.Assert(exist, code.MSG_AUCTION_NOT_EXIST, "互动不存在")
  210. records, err := models.GetAuctionRecordsByAuctionId(auctionId, activity.RehearsalId, activity.ArchId)
  211. t.CheckErr(err)
  212. t.JSON(map[string]interface{}{
  213. "records": records,
  214. "total": len(records),
  215. })
  216. }
  217. //获取二维码
  218. func (t *AuctionCtl) Qrcode() {
  219. //将服务器得地址和activity_id,动态生成二维码
  220. uid := t.MustGetUID()
  221. activityId := t.MustGetInt64("activity_id")
  222. auctionId := t.MustGetInt64("auction_activity_id")
  223. activity := &models.Activity{}
  224. exist, err := models.Get(activity, activityId)
  225. t.CheckErr(err)
  226. t.Assert(exist, code.MSG_AUCTION_NOT_EXIST, "互动不存在")
  227. area := new(models.AreaStore)
  228. exist, err = area.GetByCustomerId(uid, activityId)
  229. t.CheckErr(err)
  230. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  231. qrcode, err := utils.GenH5Qrcode(define.H5Auction, map[string]interface{}{
  232. "activity_id": activityId,
  233. "area_id": area.Id,
  234. "customer_id": uid,
  235. "auction_id": auctionId,
  236. "rehearsal_id": activity.RehearsalId,
  237. })
  238. t.CheckErr(err)
  239. t.JSON(map[string]interface{}{
  240. "qrcode": qrcode,
  241. })
  242. }