AuctionProduct.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. if ($data['name']) $model->where('name', 'like', '%'.$data['name'].'%');
  33. $list = $model->select();
  34. $list = empty($list)? [] : $list->toArray();
  35. $lists = [];
  36. $auction = Auction::where('id', $data['id'])->find();
  37. if ($list){
  38. foreach ($list as $k => $v) {
  39. $order = AuctionOrder::where('product_id', $v['id'])->where('status', '>', 0)->where('frequency', $auction['frequency'])->find();
  40. if ($order){
  41. $list[$k]['status'] = 2;// 已被购买
  42. $list[$k]['str'] = '已卖完';
  43. }else{
  44. $list[$k]['status'] = 1;// 能购买
  45. $list[$k]['str'] = '购买';
  46. }
  47. if ($v['is_admin'] == 2){
  48. $time = AuctionTime::where([['auction_id', '=', $auction['id']], ['product_id', '=', $v['id']], ['add_time', '=', strtotime(date('Y-m-d', time()))]])->find();
  49. if (!$time){
  50. unset($list[$k]);
  51. } else{
  52. $lists[] = $list[$k];
  53. }
  54. }else{
  55. $lists[] = $list[$k];
  56. }
  57. }
  58. $productId = AuctionProduct::where('auction_id', $data['id'])->column('id');
  59. $orderList = AuctionOrder::alias('a')
  60. ->field('p.*')
  61. ->leftJoin('auction_product p', 'a.product_id = p.id')
  62. ->where([['a.product_id', 'in', $productId], ['a.frequency', '=', $auction['frequency']], ['a.status', '=', 3]])
  63. ->select();
  64. }else{
  65. $productId = AuctionProduct::where('auction_id', $data['id'])->column('id');
  66. $orderList = AuctionOrder::alias('a')
  67. ->field('p.*')
  68. ->leftJoin('auction_product p', 'a.product_id = p.id')
  69. ->where([['a.product_id', 'in', $productId], ['a.frequency', '=', $auction['frequency']], ['a.status', '=', 3]])
  70. ->select();
  71. }
  72. $a = [];
  73. $b = [];
  74. $redis = new \Redis();
  75. $redis->connect('127.0.0.1','6379', 3600);
  76. foreach ($lists as $k => $v){
  77. if ($v['status'] == 1){
  78. if (!$redis->llen($v['id']) > 0){
  79. $redis->lPush($v['id'], json_encode($v));
  80. }
  81. $a[] = $v; // 未卖出的商品
  82. }
  83. if ($v['status'] == 2) $b[] = $v; // 卖出的商品
  84. }
  85. foreach ($b as $k => $v){
  86. array_push($a, $v); // 卖出商品到最后
  87. }
  88. if (isset($orderList) and !empty($orderList)){
  89. $orderList = count($orderList) == 0? [] : $orderList->toArray();
  90. foreach ($orderList as $v){
  91. $v['status'] = 2;// 已被购买
  92. $v['str'] = '已卖完';
  93. array_push($a, $v);
  94. }
  95. }
  96. $data['page'] = ($data['page'] - 1)*$data['limit'];
  97. $List = array_slice($a, $data['page'], $data['limit']);
  98. return $List;
  99. }
  100. /**
  101. * 用户商品
  102. * @param $data
  103. * @param $uid
  104. * @return array
  105. * @throws \think\db\exception\DataNotFoundException
  106. * @throws \think\db\exception\DbException
  107. * @throws \think\db\exception\ModelNotFoundException
  108. */
  109. public static function user_product($data, $uid){
  110. $model = self::where('uid', $uid)->order('id DESC');
  111. $model->page($data['page'], $data['limit']);
  112. $list = $model->select();
  113. $list = empty($list)? [] : $list->toArray();
  114. return $list;
  115. }
  116. /**
  117. * 场次时间到期用户未出售商品下架
  118. * @return void
  119. */
  120. public static function off_the_shelf(){
  121. $auction = Auction::where('rend_time', '<', date('H:i:s'))->select();
  122. if ($auction){
  123. foreach ($auction as $k => $v) {
  124. $productId = AuctionTime::where('auction_id', '=', $v['id'])->where('add_time', '=', strtotime(date('Y-m-d', time())))->column('product_id');
  125. if ($productId){
  126. $orderId = AuctionOrder::where('product_id', 'in', $productId)->where('frequency', '=', $v['frequency'])->where('status','>', 0)->column('product_id');
  127. if ($orderId){
  128. foreach ($orderId as $value){
  129. if (in_array($value, $productId))unset($productId[$value]);
  130. }
  131. }
  132. if ($productId){
  133. foreach ($productId as $item){
  134. $product = AuctionProduct::where([['id', '=', $item], ['is_admin', '=', 2]])->find();// 查找今天场次没买出去的商品下架
  135. if ($product){
  136. $product['is_show'] = 0;
  137. $product['hanging_price'] = $product['price'];
  138. $product->save();
  139. }
  140. }
  141. }
  142. $order = AuctionProduct::where([['id', 'in', $productId], ['auction_id', '=', $v['id']], ['is_admin', '=', 2]])->column('order');// 查找今天场次没买出去的商品订单
  143. AuctionOrder::where('order_id', 'in', $order)->save(['is_gs' => 0]);//订单为未挂售状态
  144. AuctionTime::where('auction_id', '=', $v['id'])->where('add_time', '<=', strtotime(date('Y-m-d', time())))->delete(); // 清除今天挂售时间
  145. }
  146. }
  147. }
  148. }
  149. public static function bs($id)
  150. {
  151. $product = AuctionProduct::where('id',$id)->find();
  152. $auction = Auction::where('id', $product['auction_id'])->find();
  153. if (!$product) return app('json')->fail('商品不存在');
  154. $data['time'] = explode(',', $auction['site']);
  155. $date = date('w', time()); // 今天星期几
  156. $data['gs_time'] = '';
  157. $bs = 0;
  158. if (in_array(1, $data['time'])){
  159. if ($date >= 1 and $date <= 2){
  160. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Wednesday")); // 星期三
  161. $bs = 3 - $date;
  162. }elseif ($date >= 3 and $date <= 4){
  163. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Friday")); // 星期五
  164. $bs = 5 - $date;
  165. }elseif ($date >= 5 or $date == 0){
  166. if ($date == 5) $bs = 3;
  167. if ($date == 6) $bs = 2;
  168. if ($date == 0) $bs = 1;
  169. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Monday")); // 下个星期一
  170. }
  171. }else if (in_array(2, $data['time'])){
  172. if ($date >= 2 and $date <= 3){
  173. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Thursday")); // 星期四
  174. $bs = 4 - $date;
  175. }elseif ($date >= 4 and $date <= 5){
  176. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Saturday")); // 星期六
  177. $bs = 6 - $date;
  178. }elseif ($date >= 6 or $date == 0 or $date == 1){
  179. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Tuesday")); // 下个星期二
  180. if ($date == 6) $bs = 3;
  181. if ($date == 0) $bs = 2;
  182. if ($date == 1) $bs = 1;
  183. }
  184. }
  185. $hanging_price = round($product['hanging_price'] * $product['rise']/100, 2); // 溢价
  186. $anticipate = round($product['hanging_price'] * $product['deduct']/100, 2); // 扣除
  187. $give = round($product['hanging_price'] * $product['give']/100, 2);// 赠送趣豆
  188. $data['price'] = $product['hanging_price'];
  189. $data['hanging_price'] = $product['hanging_price'] + ($hanging_price * $bs);
  190. $data['anticipate'] = round($anticipate*$bs,2);
  191. $data['give'] = round($give*$bs,2);
  192. return $data;
  193. }
  194. }