AuctionProduct.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/02
  6. */
  7. namespace app\models\auction;
  8. use crmeb\traits\ModelTrait;
  9. use crmeb\basic\BaseModel;
  10. /**
  11. * 竞拍上坪 Model
  12. * Class WechatNews
  13. * @package app\admin\model\wechat
  14. */
  15. class AuctionProduct extends BaseModel
  16. {
  17. use ModelTrait;
  18. protected $pk = 'id';
  19. protected $name = 'auction_product';
  20. protected $autoWriteTimestamp = true;
  21. /**
  22. * 竞拍商品列表
  23. * @param $data
  24. * @param $uid
  25. * @return array
  26. * @throws \think\db\exception\DataNotFoundException
  27. * @throws \think\db\exception\DbException
  28. * @throws \think\db\exception\ModelNotFoundException
  29. */
  30. public static function list($data, $uid){
  31. $model = self::where('is_show', 1)->where('auction_id', $data['id'])->order('id DESC');
  32. $model->page($data['page'], $data['limit']);
  33. if ($data['name']) $model->where('name', 'like', '%'.$data['name'].'%');
  34. $list = $model->select();
  35. $list = empty($list)? [] : $list->toArray();
  36. $lists = [];
  37. $auction = Auction::where('id', $data['id'])->find();
  38. if ($list){
  39. foreach ($list as $k => $v) {
  40. $order = AuctionOrder::where('product_id', $v['id'])->where('status', '>', 0)->where('frequency', $auction['frequency'])->find();
  41. if ($order){
  42. $list[$k]['status'] = 2;// 已被购买
  43. $list[$k]['str'] = '已卖完';
  44. }else{
  45. $list[$k]['status'] = 1;// 能购买
  46. $list[$k]['str'] = '购买';
  47. }
  48. if ($v['is_admin'] == 2){
  49. $time = AuctionTime::where([['auction_id', '=', $auction['id']], ['product_id', '=', $v['id']], ['add_time', '=', strtotime(date('Y-m-d', time()))]])->find();
  50. if (!$time){
  51. unset($list[$k]);
  52. } else{
  53. $lists[] = $list[$k];
  54. }
  55. }else{
  56. $lists[] = $list[$k];
  57. }
  58. }
  59. $productId = AuctionProduct::where('auction_id', $data['id'])->column('id');
  60. $orderList = AuctionOrder::alias('a')
  61. ->field('p.*')
  62. ->leftJoin('auction_product p', 'a.product_id = p.id')
  63. ->where([['a.product_id', 'in', $productId], ['a.frequency', '=', $auction['frequency']], ['a.status', '=', 3]])
  64. ->select();
  65. }
  66. $a = [];
  67. $b = [];
  68. foreach ($lists as $k => $v){
  69. if ($v['status'] == 1) $a[] = $v; // 未卖出的商品
  70. if ($v['status'] == 2) $b[] = $v; // 卖出的商品
  71. }
  72. foreach ($b as $k => $v){
  73. array_push($a, $v); // 卖出商品到最后
  74. }
  75. if (isset($orderList) and empty($orderList)){
  76. $orderList = count($orderList) == 0? [] : $orderList->toArray();
  77. foreach ($orderList as $v){
  78. $v['status'] = 2;// 已被购买
  79. $v['str'] = '已卖完';
  80. array_push($a, $v);
  81. }
  82. }
  83. return $a;
  84. }
  85. /**
  86. * 用户商品
  87. * @param $data
  88. * @param $uid
  89. * @return array
  90. * @throws \think\db\exception\DataNotFoundException
  91. * @throws \think\db\exception\DbException
  92. * @throws \think\db\exception\ModelNotFoundException
  93. */
  94. public static function user_product($data, $uid){
  95. $model = self::where('uid', $uid)->order('id DESC');
  96. $model->page($data['page'], $data['limit']);
  97. $list = $model->select();
  98. $list = empty($list)? [] : $list->toArray();
  99. return $list;
  100. }
  101. /**
  102. * 场次时间到期用户未出售商品下架
  103. * @return void
  104. */
  105. public static function off_the_shelf(){
  106. $auction = Auction::where('rend_time', '<', date('H:i:s'))->select();
  107. if ($auction){
  108. foreach ($auction as $k => $v) {
  109. $productId = AuctionTime::where('auction_id', '=', $v['id'])->where('add_time', '=', strtotime(date('Y-m-d', time())))->column('product_id');
  110. if ($productId){
  111. $orderId = AuctionOrder::where('product_id', 'in', $productId)->where('frequency', '=', $v['frequency'])->where('status','>', 0)->column('product_id');
  112. if ($orderId){
  113. foreach ($orderId as $value){
  114. if (in_array($value, $productId))unset($productId[$value]);
  115. }
  116. }
  117. $order = AuctionProduct::where([['id', 'in', $productId], ['auction_id', '=', $v['id']], ['is_admin', '=', 2]])->column('order');// 查找今天场次没买出去的商品订单
  118. AuctionOrder::where('order_id', 'in', $order)->save(['is_gs' => 0]);//订单为未挂售状态
  119. AuctionProduct::where([['id', 'in', $productId], ['auction_id', '=', $v['id']], ['is_admin', '=', 2]])->save(['is_show' => 0]);// 查找今天场次没买出去的商品下架
  120. AuctionTime::where('auction_id', '=', $v['id'])->where('add_time', '<=', strtotime(date('Y-m-d', time())))->delete(); // 清除今天挂售时间
  121. }
  122. }
  123. }
  124. }
  125. public static function bs($id)
  126. {
  127. $product = AuctionProduct::where('id',$id)->find();
  128. $auction = Auction::where('id', $product['auction_id'])->find();
  129. if (!$product) return app('json')->fail('商品不存在');
  130. $data['time'] = explode(',', $auction['site']);
  131. $date = date('w', time()); // 今天星期几
  132. $data['gs_time'] = '';
  133. $bs = 0;
  134. if (in_array(1, $data['time'])){
  135. if ($date >= 1 and $date <= 2){
  136. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Wednesday")); // 星期三
  137. $bs = 2;
  138. }elseif ($date >= 3 and $date <= 4){
  139. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Friday")); // 星期五
  140. $bs = 2;
  141. }elseif ($date >= 5 or $date == 0){
  142. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Monday")); // 下个星期一
  143. $bs = 3;
  144. }
  145. }else if (in_array(2, $data['time'])){
  146. if ($date >= 2 and $date <= 3){
  147. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Thursday")); // 星期四
  148. $bs = 2;
  149. }elseif ($date >= 4 and $date <= 5){
  150. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Saturday")); // 星期六
  151. $bs = 2;
  152. }elseif ($date >= 6 or $date == 0 or $date == 1){
  153. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Tuesday")); // 下个星期二
  154. $bs = 3;
  155. }
  156. }
  157. $hanging_price = round($product['hanging_price'] * $product['rise']/100, 2); // 溢价
  158. $anticipate = round($product['hanging_price'] * $product['deduct']/100, 2); // 扣除
  159. $data['hanging_price'] = (int)$product['hanging_price'] + ($hanging_price * $bs);
  160. $data['anticipate'] = $anticipate;
  161. return $data;
  162. }
  163. }