AuctionOrder.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/02
  6. */
  7. namespace app\models\auction;
  8. use app\admin\model\user\User;
  9. use app\admin\model\user\UserBill;
  10. use crmeb\services\product\Product;
  11. use crmeb\services\SystemConfigService;
  12. use crmeb\traits\ModelTrait;
  13. use crmeb\basic\BaseModel;
  14. /**
  15. * 预约 Model
  16. * Class WechatNews
  17. * @package app\admin\model\wechat
  18. */
  19. class AuctionOrder extends BaseModel
  20. {
  21. use ModelTrait;
  22. protected $pk = 'id';
  23. protected $name = 'auction_order';
  24. protected $autoWriteTimestamp = true;
  25. /**
  26. * 用户订单
  27. * @param $data
  28. * @param $uid
  29. * @return \think\Collection
  30. * @throws \think\db\exception\DataNotFoundException
  31. * @throws \think\db\exception\DbException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. */
  34. public static function userOrder($data, $uid)
  35. {
  36. if ($data['order_id']) {
  37. $list = self::alias('a')
  38. ->field('a.*,u.nickname,u.avatar,u.uid as user_id,c.nickname as c_nickname,pay.phone')
  39. ->leftJoin('user u', 'a.uid = u.uid')
  40. ->leftJoin('user c', 'a.collection_id = c.uid')
  41. ->leftJoin('auction_pay pay', 'a.collection_id = pay.uid')
  42. ->where('a.order_id', $data['order_id'])->find(); //详细订单
  43. $product = AuctionProduct::where('id', $list['product_id'])->find();
  44. $list['actual_price'] = round($list['price'] - $product['fictitious_price'], 2);// 实际支付价格
  45. $pay = AuctionPay::where('uid', $list['collection_id'])->select();
  46. $list['wx'] = [];
  47. $list['zfb'] = [];
  48. $list['bank'] = [];
  49. $list['time'] = strtotime($list['create_time']);
  50. if ($pay) {
  51. foreach ($pay as $k => $v) {
  52. if ($v['type'] == 1) {
  53. $list['wx'] = $v;
  54. } elseif ($v['type'] == 2) {
  55. $list['zfb'] = $v;
  56. } elseif ($v['type'] == 3) {
  57. $list['bank'] = $v;
  58. }
  59. }
  60. }
  61. } else {
  62. if ($data['type'] == 1) {
  63. $list = self::where([['uid', '=', $uid], ['status', '=', 1]])->order('id DESC')->page($data['page'], $data['limit'])->select(); //待上传订单
  64. } else if ($data['type'] == 2) {
  65. $list = self::where([['uid', '=', $uid], ['status', '=', 2]])->order('id DESC')->page($data['page'], $data['limit'])->select(); //待审核订单
  66. } else if ($data['type'] == 3) {
  67. $list = self::where([['uid', '=', $uid], ['status', '=', 3], ['is_gs', '=', 1]])->order('id DESC')->page($data['page'], $data['limit'])->select(); //完成订单
  68. } else if ($data['type'] == 4) {
  69. $list = AuctionProduct::where([['uid', '=', $uid], ['is_show', '=', 0]])
  70. ->page($data['page'], $data['limit'])
  71. ->select();
  72. if ($list) {
  73. foreach ($list as $k => $v) {
  74. $list[$k]['product_id'] = $v['id'];
  75. $list[$k]['is_gs'] = 0;
  76. $list[$k]['order_id'] = $list[$k]['order'];
  77. $list[$k]['price'] = $v['hanging_price'];
  78. }
  79. }
  80. } else {
  81. $list = self::where([['uid', '=', $uid]])->order('id DESC')->page($data['page'], $data['limit'])->select(); //过期订单
  82. }
  83. }
  84. $list = !empty($list) ? $list->toArray() : [];
  85. return $list;
  86. }
  87. /**
  88. * 卖家订单
  89. * @param $data
  90. * @param $uid
  91. * @return \think\Collection
  92. * @throws \think\db\exception\DataNotFoundException
  93. * @throws \think\db\exception\DbException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. */
  96. public static function seller_list($data, $uid)
  97. {
  98. if ($data['order_id']) {
  99. $list = self::alias('a')
  100. ->field('a.*,u.nickname,u.avatar,u.uid as user_id')
  101. ->leftJoin('user u', 'a.uid = u.uid')
  102. ->where('a.order_id', $data['order_id'])->find(); //详细订单
  103. $product = AuctionProduct::where('id', $list['product_id'])->find();
  104. $list['actual_price'] = $list['price'] - $product['fictitious_price'];// 实际支付价格
  105. $list = empty($list) ? [] : $list->toArray();
  106. return $list;
  107. } else {
  108. if ($data['type'] == 1) {
  109. $status = 1;
  110. } else if ($data['type'] == 2) {
  111. $status = 2;
  112. } else if ($data['type'] == 3) {
  113. $status = 3;
  114. } else {
  115. $status = 0;
  116. }
  117. if ($data['type'] == 4) {
  118. $product = AuctionTime::alias('a')
  119. ->field('FROM_UNIXTIME(a.add_time,"%Y-%m-%d") as gs_time,au.nickname,b.*')
  120. ->order('a.id DESC')
  121. ->where([['a.uid', '=', $uid]])
  122. ->where('b.is_admin', 2)
  123. ->page($data['page'], $data['limit'])
  124. ->leftJoin('auction_product b', 'a.product_id = b.id')
  125. ->leftJoin('auction au', 'au.id = a.auction_id')
  126. ->select();
  127. } else {
  128. if ($status == 0) {
  129. $list = self::alias('a')
  130. ->field('a.*,u.nickname,u.avatar,u.uid as user_id')
  131. ->leftJoin('user u', 'a.uid = u.uid')
  132. ->order('id DESC')
  133. ->where([['a.collection_id', '=', $uid]])
  134. ->page($data['page'], $data['limit'])
  135. ->select(); //待上传订单
  136. } else {
  137. $list = self::alias('a')
  138. ->field('a.*,u.nickname,u.avatar,u.uid as user_id')
  139. ->leftJoin('user u', 'a.uid = u.uid')
  140. ->order('id DESC')
  141. ->where([['a.collection_id', '=', $uid], ['a.status', '=', $status]])
  142. ->page($data['page'], $data['limit'])
  143. ->select(); //待上传订单
  144. }
  145. }
  146. }
  147. if ($data['type'] != 4) {
  148. $list = !empty($list) ? $list->toArray() : [];
  149. foreach ($list as $k => $v) {
  150. $pay = AuctionPay::where('uid', $v['uid'])->find();
  151. $list[$k]['phone'] = $pay['phone'];
  152. }
  153. return $list;
  154. } else {
  155. $product = empty($product) ? [] : $product->toArray();
  156. return $product;
  157. }
  158. }
  159. /**
  160. * 卖家操作
  161. * @param $id //商品所属人
  162. * @param $price //卖出价格
  163. * @param $product //商品详情
  164. * @return void
  165. * @throws \think\db\exception\DataNotFoundException
  166. * @throws \think\db\exception\DbException
  167. * @throws \think\db\exception\ModelNotFoundException
  168. */
  169. public static function earn($id, $price, $product)
  170. {
  171. $userModel = new \app\models\user\User();
  172. $productModel = new AuctionProduct();
  173. $user = $userModel->find($id);
  174. // if ($user['spread_uid'] > 0) {
  175. // $s_price = round(($price - $product['price']) * 0.1, 2); // 卖出价格减去购买价格的百分之十 为上级直推奖励
  176. // $spread = $userModel->find($user['spread_uid']);
  177. // $spread['integral'] = $spread['integral'] + $s_price; //积分增加
  178. // $spread->save();
  179. // \app\models\user\UserBill::income('直推奖励', $spread['uid'], 'anticipate', 'add_anticipate', $s_price, 0, $spread['integral'], '奖励趣豆');
  180. // }
  181. // $user['anticipate'] = $user['anticipate']-$price*($product['deduct']/100); // 扣除当前卖出价格百分比的预约卷
  182. // $user->save();
  183. // UserBill::expend('预约卷扣除', $user['uid'], 'anticipate','reduce_anticipate', $price*($product['deduct']/100), 0, $user['anticipate'] ,'卖出扣除预约卷');
  184. AuctionTime::where([['product_id', '=', $product['id']]])->delete();
  185. $productModel->where('id', $product['id'])->save(['is_show' => 0]); //下架等待挂售
  186. }
  187. /**
  188. * 购买成功退预约卷
  189. * @param $id
  190. * @return void
  191. * @throws \think\db\exception\DataNotFoundException
  192. * @throws \think\db\exception\DbException
  193. * @throws \think\db\exception\ModelNotFoundException
  194. */
  195. public static function return($id)
  196. {
  197. $data = self::find($id);
  198. $userModel = new User();
  199. $productModel = new AuctionProduct();
  200. $auctionModel = new Auction();
  201. $bookingModel = new AuctionBooking();
  202. $frozenModel = new AuctionFrozen();
  203. $user = $userModel->find($data['uid']);
  204. // if ($user['is_new'] == 1) {
  205. // if ($user['spread_uid']) {
  206. // $spread = $userModel->where('uid', $user['spread_uid'])->find();
  207. // if ($spread['green_time'] != strtotime(date('Y-m-d', strtotime('+1 day')))) {
  208. // $spread['green_time'] = strtotime(date('Y-m-d', strtotime('+1 day'))); // 开启明天的绿色通道
  209. // $spread->save();
  210. // }
  211. // }
  212. // $orderCount = AuctionOrder::where([['uid', '=', $user['uid']], ['status', '=', 3]])->count();
  213. // if ($orderCount >= 5) {
  214. // $user['is_new'] = 0;
  215. // }
  216. // }
  217. $product = $productModel->where('id', $data['product_id'])->find();
  218. $auction = $auctionModel->where('id', $product['auction_id'])->find();
  219. $booking = $bookingModel->where('auction_id', $auction['id'])->where('uid', $user['uid'])->where('frequency', $data['frequency'])->find();
  220. $fz = $frozenModel->where('order_id', $data['order_id'])->where('status', 1)->where('uid', $user['uid'])->find();
  221. if ($fz){
  222. $fz['status'] = 0;
  223. $anticipate = $fz['anticipate'] - $fz['deduction'];
  224. $user['anticipate'] = $user['anticipate'] + $anticipate;// 退还预约卷
  225. $user->save();
  226. $fz->save();
  227. UserBill::income('易趣卷退还',$user['uid'], 'anticipate', 'th_anticipate', $anticipate, $user['spread_uid'], $user['anticipate'], '退还订单'.$data['order_id'].'易趣卷,扣除' . $fz['deduction']);
  228. }else{
  229. if ($booking['status'] > 0) {
  230. $booking['status'] = 0;
  231. $anticipate = $booking['anticipate'] - $booking['deduction'];
  232. $user['anticipate'] = $user['anticipate'] + $anticipate;// 退还预约卷
  233. $user->save();
  234. $booking->save();
  235. UserBill::income('易趣卷退还',$user['uid'], 'anticipate', 'th_anticipate', $anticipate, $user['spread_uid'], $user['anticipate'], '退还预约易趣卷扣除' . $booking['deduction']);
  236. }
  237. }
  238. }
  239. /**
  240. * 订单过期
  241. * @return void
  242. * @throws \think\db\exception\DataNotFoundException
  243. * @throws \think\db\exception\DbException
  244. * @throws \think\db\exception\ModelNotFoundException
  245. */
  246. public static function deduction()
  247. {
  248. $one = (int)SystemConfigService::get('one_time');
  249. $tow = (int)SystemConfigService::get('tow_time');
  250. $hour = time() - ($one * 60); // 一小时以前
  251. $time = time() - ($tow * 60); // 一个半小时
  252. $order = AuctionOrder::where('create_time', '<', $hour)->where('status', '=', 1)->select(); // 查询不在当前一个半小时内的所有订单
  253. if ($order) {
  254. foreach ($order as $K => $v) {
  255. $product = AuctionProduct::where('id', $v['product_id'])->find();
  256. $auction = Auction::where('id', $product['auction_id'])->find();
  257. $kc_time = strtotime($auction['radd_time']);//开场时间
  258. if ($kc_time < time()){
  259. if (strtotime($v['create_time']) < $kc_time) $v['create_time'] = date('Y-m-d H:i:s', $kc_time);
  260. $booking = AuctionBooking::where([['uid', '=', $v['uid']], ['status', '=', 1], ['auction_id', '=', $auction['id']], ['frequency', '=', $auction['frequency']]])->find(); // 找到预约订单
  261. $fz = AuctionFrozen::where('order_id', $v['order_id'])->where('status', 1)->find();
  262. $userl = \app\models\user\User::where('uid', $v['uid'])->find();//买家
  263. if ($fz) {
  264. if (strtotime($v['create_time']) <= $hour and strtotime($v['create_time']) > $time) {
  265. // 订单在一个小时到一个半小时内
  266. if ($fz['deduction'] == 0) {
  267. $user = \app\models\user\User::where('uid', $v['collection_id'])->find();
  268. $user['anticipate'] = $user['anticipate'] + $fz['anticipate'] / 2; // 卖家增加预约卷
  269. $fz['deduction'] = $fz['anticipate'] / 2;
  270. UserBill::income('超时增加', $v['collection_id'], 'anticipate', 'ad_anticipate', $fz['anticipate'] / 2, $v['uid'], $user['anticipate'], '(' . $userl['nickname'] . '-' . $userl['uid'] . ')超时付款,增加易趣卷');
  271. UserBill::expend('超时扣除', $v['uid'], 'anticipate', 'dec_anticipate', $fz['anticipate'] / 2, $v['uid'], $fz['anticipate'] / 2, '超时付款,扣除订单'.$v['order_id'].'冻结'. ($fz['anticipate'] / 2) . '易趣卷');
  272. $fz->save();
  273. $user->save();
  274. }
  275. } elseif (strtotime($v['create_time']) <= $time) {
  276. $user = \app\models\user\User::where('uid', $v['collection_id'])->find(); //卖家
  277. $dedu = $fz['anticipate'] - $fz['deduction'];
  278. $user['anticipate'] += $dedu; // 卖家增加预约卷
  279. $fz['deduction'] = $fz['anticipate'];
  280. $fz['status'] = 2;
  281. $product['succeed_time'] = 0;
  282. $product->save();
  283. $user->save();
  284. \app\models\user\User::where('uid', $v['uid'])->update(['status' => 0, 'freeze_time' => strtotime('+1 day')]); // 冻结用户
  285. UserBill::income('超时增加', $v['collection_id'], 'anticipate', 'ad_anticipate', $dedu, $v['uid'], $user['anticipate'], '(' . $userl['nickname'] . '-' . $userl['uid'] . ')超时付款,增加易趣卷');
  286. UserBill::expend('超时扣除', $v['uid'], 'anticipate', 'dec_anticipate', $dedu, $v['uid'], 0, '超时付款,扣除订单'.$v['order_id'].'冻结' . $dedu . '易趣卷');
  287. $fz->save();
  288. AuctionOrder::where('id', $v['id'])->where('status', '=', 1)->update(['status' => 0]); // 修改为已过期订单
  289. }
  290. } else {
  291. if (strtotime($v['create_time']) <= $hour and strtotime($v['create_time']) > $time) {
  292. // 订单在一个小时到一个半小时内
  293. if ($booking) {
  294. if ($booking['deduction'] == 0) {
  295. $user = \app\models\user\User::where('uid', $v['collection_id'])->find();
  296. $user['anticipate'] = $user['anticipate'] + $booking['anticipate'] / 2; // 卖家增加预约卷
  297. $booking['deduction'] = $booking['anticipate'] / 2;
  298. UserBill::income('超时增加', $v['collection_id'], 'anticipate', 'ad_anticipate', $booking['anticipate'] / 2, $v['uid'], $user['anticipate'], '(' . $userl['nickname'] . '-' . $userl['uid'] . ')超时付款,增加易趣卷');
  299. UserBill::expend('超时扣除', $v['uid'], 'anticipate', 'dec_anticipate', $booking['anticipate'] / 2, $v['uid'], $booking['anticipate'] / 2, '超时付款,扣除预约冻结' . ($booking['anticipate'] / 2) . '易趣卷');
  300. $booking->save();
  301. $user->save();
  302. }
  303. }
  304. } elseif (strtotime($v['create_time']) <= $time) {
  305. if ($booking) {
  306. $user = \app\models\user\User::where('uid', $v['collection_id'])->find(); //卖家
  307. $dedu = $booking['anticipate'] - $booking['deduction'];
  308. $user['anticipate'] += $dedu; // 卖家增加预约卷
  309. $booking['deduction'] = $booking['anticipate'];
  310. $booking['status'] = 2;
  311. $product['succeed_time'] = 0;
  312. $product->save();
  313. $user->save();
  314. \app\models\user\User::where('uid', $v['uid'])->update(['status' => 0, 'freeze_time' => strtotime('+1 day')]); // 冻结用户
  315. UserBill::income('超时增加', $v['collection_id'], 'anticipate', 'ad_anticipate', $dedu, $v['uid'], $user['anticipate'], '(' . $userl['nickname'] . '-' . $userl['uid'] . ')超时付款,增加易趣卷');
  316. UserBill::expend('超时扣除', $v['uid'], 'anticipate', 'dec_anticipate', $dedu, $v['uid'], 0, '超时付款,扣除预约冻结' . $dedu . '易趣卷');
  317. $booking->save();
  318. AuctionOrder::where('id', $v['id'])->where('status', '=', 1)->update(['status' => 0]); // 修改为已过期订单
  319. }
  320. }
  321. }
  322. }
  323. }
  324. }
  325. }
  326. /**
  327. * 退回预约卷
  328. * @return void
  329. * @throws \think\db\exception\DataNotFoundException
  330. * @throws \think\db\exception\DbException
  331. * @throws \think\db\exception\ModelNotFoundException
  332. */
  333. public static function th()
  334. {
  335. $auction = Auction::where('rend_time', '<', date('H:i:s', time()))->select();
  336. if ($auction) {
  337. foreach ($auction as $k => $v) {
  338. $booking = AuctionBooking::where([['auction_id', '=', $v['id']], ['status', '=', 1]])->select();
  339. if ($booking) {
  340. foreach ($booking as $key => $value) {
  341. $product = AuctionProduct::where('auction_id', $value['auction_id'])->column('id');
  342. $order = AuctionOrder::where([['product_id', 'in', $product], ['frequency', '=', $value['frequency']], ['uid', '=', $value['uid']]])->find();
  343. if (!$order) {
  344. $find = AuctionBooking::find($value['id']);
  345. $find['status'] = 0;
  346. $user = User::where('uid', $value['uid'])->find();
  347. $user['anticipate'] = $user['anticipate'] + $value['anticipate'];
  348. $user->save();
  349. $find->save();
  350. UserBill::income('退回易趣卷', $user['uid'], 'anticipate', 'add_anticipate', $value['anticipate'], 0, $user['anticipate'], '易趣卷' . $value['anticipate'] . '退回');
  351. }
  352. }
  353. }
  354. }
  355. }
  356. }
  357. public static function goods()
  358. {
  359. $order = AuctionOrder::where('status', 2)->select();
  360. if ($order) {
  361. foreach ($order as &$item) {
  362. $auction = Auction::where('id', $item['auction_id'])->value('goods_time');
  363. $time = $item['voucher_time'] + ($auction * 60); // 自动放货时间
  364. if (time() > $time) {
  365. // 如果当前时间已经过了
  366. $product = AuctionProduct::where('id', $item['product_id'])->find();
  367. if ($product['uid'] == $item['uid']) {
  368. self::where('id', $item['id'])->update(['status' => 3]);
  369. }else{
  370. $product['uid'] = $item['uid'];// 商品拥有人更新
  371. $product['add_time'] = 111111;
  372. $product['order'] = $item['order_id'];
  373. self::where('id', $item['id'])->update(['status' => 3]);
  374. self::earn($item['uid'], $item['price'], $product);
  375. self::return($item['id']);
  376. $product->save();
  377. }
  378. }
  379. }
  380. }
  381. }
  382. public static function auction_time()
  383. {
  384. $auction = Auction::where('status', 1)->select();
  385. if ($auction){
  386. foreach ($auction as $item){
  387. $addtime = strtotime($item['radd_time']);
  388. $rendtime = strtotime($item['rend_time']);
  389. if (($addtime-360) < time() and $addtime > time()){
  390. $model = AuctionProduct::where('is_show', 1)->where('auction_id', $item['id'])->order('id DESC');
  391. $list = $model->select();
  392. $list = empty($list)? [] : $list->toArray();
  393. $lists = [];
  394. if ($list){
  395. foreach ($list as $k => $v) {
  396. $order = AuctionOrder::where('product_id', $v['id'])->where('status', '>', 0)->where('auction_id', $item['id'])->where('frequency', $item['frequency'])->find();
  397. if ($order){
  398. $list[$k]['status'] = 2;// 已被购买
  399. $list[$k]['str'] = '已卖完';
  400. }else{
  401. $list[$k]['status'] = 1;// 能购买
  402. $list[$k]['str'] = '购买';
  403. }
  404. if ($v['is_admin'] == 2){
  405. $time = AuctionTime::where([['product_id', '=', $v['id']], ['add_time', '<=', strtotime(date('Y-m-d', time()))]])->find();
  406. if (!$time){
  407. unset($list[$k]);
  408. } else{
  409. $lists[] = $list[$k];
  410. }
  411. }else{
  412. $lists[] = $list[$k];
  413. }
  414. }
  415. }
  416. $redis = new \Redis();
  417. $redis->connect('127.0.0.1','6379', 3600);
  418. foreach ($lists as $k => $v) {
  419. if ($v['status'] == 1) {
  420. if (!$redis->llen($v['id']) > 0) {
  421. $redis->lPush($v['id'], json_encode($v));
  422. }
  423. }
  424. }
  425. }
  426. }
  427. }
  428. }
  429. }