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