AuctionOrder.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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]])->page($data['page'], $data['limit'])->select(); //待上传订单
  62. }else if($data['type'] == 2){
  63. $list = self::where([['uid', '=', $uid], ['status', '=', 2]])->page($data['page'], $data['limit'])->select(); //待审核订单
  64. }else if($data['type'] == 3) {
  65. $list = self::where([['uid', '=', $uid], ['status', '=', 3], ['is_gs', '=', 1]])->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]])->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. $user = $userModel->find($data['uid']);
  194. if ($user['is_new'] == 1){
  195. if ($user['spread_uid']) {
  196. $spread = $userModel->where('uid', $user['spread_uid'])->find();
  197. if ($spread['green_time'] != strtotime(date('Y-m-d', strtotime('+1 day')))){
  198. $spread['green_time'] = strtotime(date('Y-m-d', strtotime('+1 day'))); // 开启明天的绿色通道
  199. $spread->save();
  200. }
  201. }
  202. $orderCount = AuctionOrder::where([['uid', '=', $user['uid']], ['status','=', 3]])->count();
  203. if ($orderCount >= 5){
  204. $user['is_new'] = 0;
  205. }
  206. }
  207. $product = $productModel->where('id', $data['product_id'])->find();
  208. $auction = $auctionModel->where('id', $product['auction_id'])->find();
  209. $time = strtotime(date('Y-m-d' ,strtotime($data['create_time']))); // 订单当天时间段
  210. $totime = strtotime(date('Y-m-d' ,strtotime($data['create_time']))) + 86400; // 订单凌晨时间段
  211. $booking = $bookingModel->where('auction_id', $auction['id'])->where('uid', $user['uid'])->whereBetweenTime('create_time', $time, $totime)->find();
  212. if ($booking['status'] > 0){
  213. $booking['status'] = 0;
  214. $booking->save();
  215. $anticipate = $booking['anticipate'] - $booking['deduction'];
  216. $user['anticipate'] = $user['anticipate'] + $anticipate;// 退还预约卷
  217. $user->save();
  218. \app\models\user\UserBill::create([
  219. 'uid' => $user['uid'],
  220. 'pm' => 1,
  221. 'title' => '艺金券退还',
  222. 'category' => 'anticipate',
  223. 'type' => 'th_anticipate',
  224. 'mark' => '退还'.$anticipate.'艺金券扣除'.$booking['deduction'],
  225. 'add_time' => time(),
  226. 'number' => $anticipate,
  227. 'balance' => $user['anticipate']
  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. if (strtotime($v['create_time']) <= $hour and strtotime($v['create_time']) > $time){
  251. // 订单在一个小时到一个半小时内
  252. if ($booking){
  253. if ($booking['deduction'] == 0){
  254. $user = \app\models\user\User::where('uid', $v['collection_id'])->find();
  255. $user['anticipate'] = $user['anticipate'] + $booking['anticipate']/2; // 卖家增加预约卷
  256. $booking['deduction'] = $booking['anticipate']/2;
  257. UserBill::income('超时增加', $v['collection_id'], 'anticipate', 'ad_anticipate', $booking['anticipate']/2, $v['uid'], $user['anticipate'], '('.$user['nickname'].'-'.$user['uid'].')超时付款');
  258. UserBill::expend('超时扣除', $v['uid'], 'anticipate', 'dec_anticipate', $booking['anticipate']/2, $v['uid'], $booking['anticipate']/2, '超时付款,扣除冻结'.($booking['anticipate']/2).'艺金券');
  259. $booking->save();
  260. $user->save();
  261. }
  262. }
  263. }elseif (strtotime($v['create_time']) <= $time){
  264. if ($booking) {
  265. $user = \app\models\user\User::where('uid', $v['collection_id'])->find(); //卖家
  266. $dedu = $booking['anticipate'] - $booking['deduction'];
  267. $user['anticipate'] += $dedu; // 卖家增加预约卷
  268. $booking['deduction'] = $booking['anticipate'];
  269. $user->save();
  270. \app\models\user\User::where('uid', $v['uid'])->update(['status' => 0,'freeze_time' => strtotime('+1 day')]); // 冻结用户
  271. UserBill::income('超时增加', $v['collection_id'], 'anticipate', 'ad_anticipate', $dedu, $v['uid'], $user['anticipate'], '('.$user['nickname'].'-'.$user['uid'].')超时付款');
  272. UserBill::expend('超时扣除', $v['uid'], 'anticipate', 'dec_anticipate', $dedu, $v['uid'], 0, '超时付款,扣除冻结'.$dedu.'艺金券');
  273. $booking->save();
  274. AuctionBooking::where('id', $booking['id'])->update(['status' => 2]); // 修改预约订单状态 为扣除
  275. AuctionOrder::where('create_time', '<', $hour)->where('status', '=', 1)->update(['status' => 0]); // 修改为已过期订单
  276. }
  277. }
  278. }
  279. }
  280. }
  281. /**
  282. * 退回预约卷
  283. * @return void
  284. * @throws \think\db\exception\DataNotFoundException
  285. * @throws \think\db\exception\DbException
  286. * @throws \think\db\exception\ModelNotFoundException
  287. */
  288. public static function th()
  289. {
  290. $auction = Auction::where('rend_time', '<', date('H:i:s', time()))->select();
  291. if ($auction){
  292. foreach ($auction as $k => $v){
  293. $booking = AuctionBooking::where([['auction_id', '=', $v['id']], ['status','=' , 1]])->select();
  294. if ($booking){
  295. foreach ($booking as $key => $value){
  296. $product = AuctionProduct::where('auction_id', $value['auction_id'])->column('id');
  297. $order = AuctionOrder::where([['product_id', 'in', $product], ['frequency', '=', $value['frequency']], ['uid', '=', $value['uid']]])->find();
  298. if (!$order){
  299. $find = AuctionBooking::find($value['id']);
  300. $find['status'] = 0;
  301. $user = User::where('uid', $value['uid'])->find();
  302. $user['anticipate'] = $user['anticipate'] + $value['anticipate'];
  303. $user->save();
  304. $find->save();
  305. UserBill::income('退回艺金券',$user['uid'], 'anticipate', 'add_anticipate', $value['anticipate'], 0, $user['anticipate'], '艺金券'.$value['anticipate'].'退回');
  306. }
  307. }
  308. }
  309. }
  310. }
  311. }
  312. public static function goods()
  313. {
  314. $order = AuctionOrder::where('status', 2)->select();
  315. if ($order){
  316. foreach ($order as &$item)
  317. {
  318. $auction = Auction::where('id', $item['auction_id'])->value('goods_time');
  319. $time = $item['voucher_time'] + ($auction * 60); // 自动放货时间
  320. if (time() > $time){
  321. // 如果当前时间已经过了
  322. $product = AuctionProduct::where('id', $item['product_id'])->find();
  323. $product['uid'] = $item['uid'];// 商品拥有人更新
  324. $product['add_time'] = time();
  325. $product['order'] = $item['order_id'];
  326. self::where('id', $item['id'])->update(['status' => 3]);
  327. self::earn($item['uid'],$item['price'], $product);
  328. self::return($item['id']);
  329. $product->save();
  330. }
  331. }
  332. }
  333. }
  334. }