AuctionOrder.php 23 KB

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