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