AuctionOrder.php 22 KB

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