AuctionOrder.php 21 KB

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