AuctionOrder.php 19 KB

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