Auction.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace app\models\auction;
  3. use app\models\store\StoreProduct;
  4. use crmeb\services\SystemConfigService;
  5. use Monolog\Handler\IFTTTHandler;
  6. use think\facade\Db;
  7. use crmeb\traits\ModelTrait;
  8. use crmeb\basic\BaseModel;
  9. /**
  10. * TODO 场馆model
  11. * Class Article
  12. * @package app\models\article
  13. */
  14. class Auction extends BaseModel
  15. {
  16. /**
  17. * 数据表主键
  18. * @var string
  19. */
  20. protected $pk = 'id';
  21. /**
  22. * 模型名称
  23. * @var string
  24. */
  25. protected $name = 'auction';
  26. use ModelTrait;
  27. public function list($data, $uid)
  28. {
  29. $model = self::where([['delete_time', '=', 0], ['status' ,'=', '1']]);
  30. $model->page($data['page'], $data['limit']);
  31. $model->order('id DESC, sort DESC');
  32. $id = [];
  33. if ($data['advance']){
  34. $model->where('id', 'in', $id);
  35. }
  36. $list = $model->select();
  37. $list = count($list) ? $list->toArray() : [];
  38. if ($list){
  39. foreach ($list as $k =>$v)
  40. {
  41. $list[$k]['time'] = strtotime($v['radd_time']) - 1800;
  42. $list[$k]['e_time'] = strtotime($v['rend_time']);
  43. $list[$k]['day'] = date('Y-m-d H:i:s', strtotime($v['radd_time']) - 1800);
  44. if (strtotime($v['rend_time']) < time()){
  45. $booking = AuctionBooking::where([['uid', '=', $uid], ['frequency', '=', $v['frequency']+1], ['auction_id', '=', $v['id']]])->field('auction_id')->find();
  46. if ($booking){
  47. $list[$k]['sta'] = 2; // 进入
  48. $list[$k]['str'] = '进入';
  49. }else{
  50. $list[$k]['sta'] = 1; // 预约
  51. $list[$k]['str'] = '预约';
  52. }
  53. }else{
  54. $booking = AuctionBooking::where([['uid', '=', $uid], ['frequency', '=', $v['frequency']], ['auction_id', '=', $v['id']]])->field('auction_id')->find();
  55. if ($booking){
  56. $list[$k]['sta'] = 2; // 进入
  57. $list[$k]['str'] = '进入';
  58. }else{
  59. $list[$k]['sta'] = 1; // 预约
  60. $list[$k]['str'] = '预约';
  61. }
  62. }
  63. }
  64. }
  65. return $list;
  66. }
  67. /**
  68. * 每日修改场次
  69. * @return void
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. */
  74. public static function frequency()
  75. {
  76. $list = self::select();
  77. foreach ($list as $k => $v){
  78. if ($v['day_time'] < time()){
  79. $find = self::find($v['id']);
  80. $find['day_time'] = strtotime(date('Y-m-d 23:59:59'));
  81. $find['frequency'] = $v['frequency'] + 1;
  82. $find->save();
  83. }
  84. }
  85. }
  86. public static function recovery()
  87. {
  88. $auction = Auction::select();
  89. $recovery = (int)SystemConfigService::get('recovery');
  90. if ($auction){
  91. foreach ($auction as $k => $v) {
  92. if (strtotime($v['rend_time'])-300 < time()){
  93. //关闭场次五分钟前
  94. $orderCount = AuctionOrder::where('auction_id', '=', $v['id'])->where('status', '>',0)->where('price', '<', $recovery)->count();// 当前订单数量
  95. $bok = AuctionBooking::where([['auction_id', '=', $v['id']], ['frequency', '=', $v['frequency']]])->count();
  96. $count = ceil($bok * ($v['dispatch']/100));
  97. $orderId = AuctionOrder::where([['auction_id', '=', $v['id']], ['frequency', '=', $v['frequency']], ['status', '>', 0]])->column('product_id');
  98. if ($orderCount < $count){
  99. // 如果购买人数没有到放单人数
  100. for ($x=1 ; $x <= $count; $x++){
  101. $orderId = AuctionOrder::where([['auction_id', '=', $v['id']], ['frequency', '=', $v['frequency']], ['status', '>', 0]])->column('product_id');
  102. $product = AuctionProduct::where([['auction_id', '=', $v['id']],
  103. ['is_show', '=', 1],
  104. ['id', 'notIn', $orderId],
  105. ['uid', '<>', 1],
  106. ['hanging_price', '<', $recovery]]
  107. )->orderRaw('rand()')
  108. ->limit(1)
  109. ->find(); // 随机出来一个商品
  110. $orderData = [
  111. 'uid' => 1,
  112. 'collection_id' => $product['uid'],
  113. 'order_id' => getNewOrderId(),
  114. 'auction_id' => $v['id'],
  115. 'product_id' => $product['id'],
  116. 'name' => $product['name'],
  117. 'image' => $product['image'],
  118. 'price' => $product['hanging_price'],
  119. 'actual_price' => $product['hanging_price'] - $product['price'],
  120. 'create_time' => time(),
  121. 'frequency' => $v['frequency']
  122. ];
  123. AuctionOrder::create($orderData);
  124. unset($orderData);// 清空数组
  125. }
  126. }
  127. $auctionProduct = AuctionProduct::where([['auction_id', '=', $v['id']], ['is_show', '=', 1],['id', 'notIn', $orderId],['hanging_price', '>', $recovery]])->select();// 如果又商品成功回收价格
  128. if ($auctionProduct){
  129. // 商品价格超出规定值进行回收
  130. foreach ($auctionProduct as $key => $value){
  131. $orderData = [
  132. 'uid' => 1,
  133. 'collection_id' => $value['uid'],
  134. 'order_id' => getNewOrderId(),
  135. 'auction_id' => $v['id'],
  136. 'product_id' => $value['id'],
  137. 'name' => $value['name'],
  138. 'image' => $value['image'],
  139. 'price' => $value['hanging_price'],
  140. 'actual_price' => $value['hanging_price'] - $value['price'],
  141. 'create_time' => time(),
  142. 'frequency' => $v['frequency']
  143. ];
  144. AuctionOrder::insert($orderData);
  145. unset($orderData); // 清空数组
  146. }
  147. }
  148. }
  149. }
  150. }
  151. }
  152. }