OrderService.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\GoodsModel;
  13. use App\Models\MemberModel;
  14. use App\Models\OrderGoodsModel;
  15. use App\Models\OrderModel;
  16. use App\Models\StoreModel;
  17. use App\Services\BaseService;
  18. use App\Services\ConfigService;
  19. use App\Services\PaymentService;
  20. use App\Services\RedisService;
  21. use Illuminate\Support\Facades\DB;
  22. /**
  23. * 订单-服务类
  24. * @author laravel开发员
  25. * @since 2020/11/11
  26. * @package App\Services\Api
  27. */
  28. class OrderService extends BaseService
  29. {
  30. // 静态对象
  31. protected static $instance = null;
  32. /**
  33. * 构造函数
  34. * @author laravel开发员
  35. * @since 2020/11/11
  36. */
  37. public function __construct()
  38. {
  39. $this->model = new OrderModel();
  40. }
  41. /**
  42. * 静态入口
  43. */
  44. public static function make()
  45. {
  46. if (!self::$instance) {
  47. self::$instance = new static();
  48. }
  49. return self::$instance;
  50. }
  51. /**
  52. * 订单列表
  53. * @param $params
  54. * @param int $pageSize
  55. * @return array
  56. */
  57. public function getDataList($params, $pageSize = 15)
  58. {
  59. $model = $this->getQuery($params);
  60. // 数据
  61. $list = $model->where(function ($query) use ($params) {
  62. $status = isset($params['status']) ? $params['status'] : 0;
  63. // 进行中
  64. if ($status == 9) {
  65. $query->where('a.refund_status', '>', 0);
  66. }elseif ($status > 0 && is_array($status)) {
  67. $query->whereIn('a.status', $status)->where('a.refund_status', 0);
  68. } else if ($status == 4) {
  69. $query->where('a.status', $status)->where('a.refund_status', 0);
  70. } else if ($status > 0) {
  71. $query->where('a.status', $status)->where('a.refund_status', 0);
  72. }
  73. })->select(['a.*'])
  74. ->orderBy('a.status', 'asc')
  75. ->orderBy('a.create_time', 'desc')
  76. ->orderBy('a.id', 'desc')
  77. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  78. $list = $list ? $list->toArray() : [];
  79. if ($list) {
  80. $statusArr = [1 => '待支付', 2 => '待发货', 3 => '待收货',4=>'确认收货'];
  81. $refundStatusArr = [1 => '已退款', 2 => '退款中', 3 => '退款审核',4=>'退款驳回'];
  82. foreach ($list['data'] as &$item) {
  83. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y/m/d H:i') : '';
  84. $status = isset($item['status']) ? $item['status'] : 0;
  85. $item['status_text'] = '待支付';
  86. if ($status) {
  87. $item['status_text'] = isset($statusArr[$status]) ? $statusArr[$status] : '';
  88. }
  89. if($item['refund_status']>0){
  90. $item['status_text'] = '退款/售后';
  91. }
  92. $item['goods'] = isset($item['goods']) && $item['goods'] ? $item['goods'] : [];
  93. }
  94. unset($item);
  95. }
  96. return [
  97. 'pageSize' => $pageSize,
  98. 'total' => isset($list['total']) ? $list['total'] : 0,
  99. 'list' => isset($list['data']) ? $list['data'] : []
  100. ];
  101. }
  102. /**
  103. * 查询条件
  104. * @param $params
  105. * @return mixed
  106. */
  107. public function getQuery($params)
  108. {
  109. $where = ['a.mark' => 1];
  110. return $this->model->from('orders as a')->with(['orderGoods','store'])
  111. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  112. ->where($where)
  113. ->where(function ($query) use ($params) {
  114. $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
  115. if ($userId > 0) {
  116. $query->where('a.user_id', $userId);
  117. }
  118. })
  119. ->where(function ($query) use ($params) {
  120. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  121. if ($keyword) {
  122. $query->where('a.order_no', 'like', "%{$keyword}%")
  123. ->orWhere('b.mobile', 'like', "%{$keyword}%");
  124. }
  125. });
  126. }
  127. /**
  128. * 订单详情
  129. * @param $id
  130. */
  131. public function getOrderInfo($id)
  132. {
  133. $statusArr = [1 => '待支付', 2 => '待发货', 3 => '待收货',4=>'已完成'];
  134. $info = $this->model->from('orders as a')->with(['goods'])
  135. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  136. ->where(['a.id' => $id, 'a.mark' => 1])
  137. ->select(['a.*'])
  138. ->first();
  139. if ($info) {
  140. $info = $info->toArray();
  141. $info['create_time'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
  142. $status = isset($info['status']) ? $info['status'] : 0;
  143. $info['status_text'] = '待付款';
  144. if ($status) {
  145. $info['status_text'] = isset($statusArr[$status]) ? $statusArr[$status] : '';
  146. }
  147. }
  148. return $info;
  149. }
  150. /**
  151. * 创建订单
  152. * @param $userId 用户
  153. * @param $params 参数
  154. * @return array|false
  155. */
  156. public function createOrder($userId, $params)
  157. {
  158. $addressId = isset($params['address_id']) && $params['address_id'] ? $params['address_id'] : 0;
  159. $goods = isset($params['goods']) && $params['goods'] ? $params['goods'] : [];
  160. $ids = $goods ? array_column($goods,'id') : [];
  161. // 参数验证
  162. if (empty($goods) || empty($ids)) {
  163. $this->error = '商品参数不为空';
  164. return false;
  165. }
  166. if ($addressId <= 0) {
  167. $this->error = '请选择收货地址';
  168. return false;
  169. }
  170. // 缓存锁
  171. $cacheLockKey = "caches:orders:submit_lock:{$userId}";
  172. if (RedisService::get($cacheLockKey)) {
  173. $this->error = '订单处理中~';
  174. return false;
  175. }
  176. // 商品数据
  177. $orderNo = get_order_num('GX');
  178. RedisService::set($cacheLockKey, ['params' => $params, 'user_id' => $userId], rand(3, 5));
  179. $result = GoodsService::make()->getOrderGoods($ids, $goods, $orderNo, $userId);
  180. if (empty($result)) {
  181. RedisService::clear($cacheLockKey);
  182. $this->error = GoodsService::make()->getError();
  183. return false;
  184. }
  185. $orderGoods = isset($result['goods']) ? $result['goods'] : [];
  186. $orderTotal = isset($result['total']) ? $result['total'] : 0;
  187. $orderCount = isset($result['count']) ? $result['count'] : 0;
  188. $storeId = isset($result['store_id']) ? $result['store_id'] : 0;
  189. if (empty($orderGoods)) {
  190. RedisService::clear($cacheLockKey);
  191. $this->error = '获取订单商品错误~';
  192. return false;
  193. }
  194. if ($orderTotal <= 0) {
  195. RedisService::clear($cacheLockKey);
  196. $this->error = '订单金额错误~';
  197. return false;
  198. }
  199. if ($orderCount <= 0) {
  200. RedisService::clear($cacheLockKey);
  201. $this->error = '订单商品数量错误~';
  202. return false;
  203. }
  204. // 用户信息
  205. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  206. ->select(['id', 'openid', 'mobile', 'nickname', 'realname', 'balance', 'status'])
  207. ->first();
  208. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  209. $openid = isset($userInfo['openid']) ? $userInfo['openid'] : '';
  210. if (empty($userInfo) || $status != 1) {
  211. $this->error = 1045;
  212. RedisService::clear($cacheLockKey);
  213. return false;
  214. }
  215. if (empty($openid)) {
  216. $this->error = '用户微信未授权,请重新授权登录';
  217. RedisService::clear($cacheLockKey);
  218. return false;
  219. }
  220. // 收货地址信息
  221. $addressInfo = MemberAddressService::make()->getBindInfo($userId, $addressId);
  222. $realname = isset($addressInfo['realname']) ? $addressInfo['realname'] : '';
  223. $mobile = isset($addressInfo['mobile']) ? $addressInfo['mobile'] : '';
  224. $area = isset($addressInfo['area']) ? $addressInfo['area'] : '';
  225. $address = isset($addressInfo['address']) ? $addressInfo['address'] : '';
  226. if (empty($addressInfo) || empty($realname) || empty($mobile) || empty($area) || empty($address)) {
  227. RedisService::clear($cacheLockKey);
  228. $this->error = '收货地址信息错误,请核对后重试~';
  229. return false;
  230. }
  231. // 商家佣金
  232. $storeInfo = StoreModel::where(['id' => $storeId])->first();
  233. $bonusRate = isset($storeInfo['bonus_rate']) ? floatval($storeInfo['bonus_rate']) : 0;
  234. $storeBonusRate = ConfigService::make()->getConfigByCode('store_bonus_rate', 0);
  235. $storeBonusRate = $storeBonusRate > 0 && $storeBonusRate <= 100 ? $storeBonusRate : 0;
  236. $bonusRate = $bonusRate > 0 && $bonusRate <= 100 ? $bonusRate : $storeBonusRate;
  237. $bonus = moneyFormat($orderTotal * $bonusRate/100, 2);
  238. $orderTotal = 0.1;
  239. // 订单数据
  240. $order = [
  241. 'order_no' => $orderNo,
  242. 'user_id' => $userId,
  243. 'store_id' => $storeId,
  244. 'total' => $orderTotal,
  245. 'num' => $orderCount,
  246. 'pay_total' => $orderTotal,
  247. 'receiver_name' => $realname,
  248. 'receiver_mobile' => $mobile,
  249. 'receiver_area' => $area,
  250. 'receiver_address' => $address,
  251. 'bonus' => $bonus,
  252. 'create_time' => time(),
  253. 'update_time' => time(),
  254. 'status' => 1,
  255. 'mark' => 1,
  256. ];
  257. // 订单处理
  258. DB::beginTransaction();
  259. if (!$orderId = $this->model->insertGetId($order)) {
  260. DB::rollBack();
  261. $this->error = '创建订单失败';
  262. RedisService::clear($cacheLockKey);
  263. return false;
  264. }
  265. // 订单商品
  266. if($orderGoods && !OrderGoodsModel::insert($orderGoods)){
  267. DB::rollBack();
  268. $this->error = '处理订单商品错误';
  269. RedisService::clear($cacheLockKey);
  270. return false;
  271. }
  272. // 获取支付参数
  273. /* TODO 支付处理 */
  274. $payOrder = [
  275. 'type' => 1,
  276. 'order_no' => $orderNo,
  277. 'pay_money' => $orderTotal,
  278. 'body' => '购物消费',
  279. 'openid' => $openid
  280. ];
  281. // 调起支付
  282. $payment = PaymentService::make()->minPay($userInfo, $payOrder, 'store');
  283. if (empty($payment)) {
  284. DB::rollBack();
  285. RedisService::clear($cacheLockKey);
  286. $this->error = PaymentService::make()->getError();
  287. return false;
  288. }
  289. // 用户操作记录
  290. DB::commit();
  291. $this->error = '订单创建成功,请前往支付~';
  292. RedisService::clear($cacheLockKey);
  293. return [
  294. 'order_id' => $orderId,
  295. 'payment' => $payment,
  296. 'total' => $payOrder['pay_money'],
  297. 'pay_type' => 10,
  298. ];
  299. }
  300. /**
  301. * 订单支付
  302. * @param $userId
  303. * @param $id
  304. * @return array|false
  305. */
  306. public function pay($userId, $id)
  307. {
  308. if ($id <= 0) {
  309. $this->error = '请选择支付订单';
  310. return false;
  311. }
  312. // 缓存锁
  313. $cacheLockKey = "caches:orders:pay_lock:{$userId}_{$id}";
  314. if (RedisService::get($cacheLockKey)) {
  315. $this->error = '订单处理中~';
  316. return false;
  317. }
  318. // 商品数据
  319. RedisService::set($cacheLockKey, ['order_id' => $id, 'user_id' => $userId], rand(3, 5));
  320. // 用户信息
  321. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  322. ->select(['id', 'openid', 'mobile', 'nickname', 'realname', 'balance', 'status'])
  323. ->first();
  324. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  325. $openid = isset($userInfo['openid']) ? $userInfo['openid'] : '';
  326. if (empty($userInfo) || $status != 1) {
  327. $this->error = 1045;
  328. RedisService::clear($cacheLockKey);
  329. return false;
  330. }
  331. if (empty($openid)) {
  332. $this->error = '用户微信未授权,请重新授权登录';
  333. RedisService::clear($cacheLockKey);
  334. return false;
  335. }
  336. // 订单信息
  337. $info = $this->model->where(['id'=> $id,'mark'=>1])
  338. ->select(['id','order_no','pay_total','status'])
  339. ->first();
  340. $orderTotal = isset($info['pay_total'])? $info['pay_total'] : 0;
  341. $orderNo = isset($info['order_no'])? $info['order_no'] : '';
  342. $status = isset($info['status'])? $info['status'] : 0;
  343. if(empty($info) || empty($orderNo)){
  344. $this->error = '订单信息不存在';
  345. RedisService::clear($cacheLockKey);
  346. return false;
  347. }
  348. if($status != 1){
  349. $this->error = '订单已支付';
  350. RedisService::clear($cacheLockKey);
  351. return false;
  352. }
  353. // 获取支付参数
  354. /* TODO 支付处理 */
  355. $payOrder = [
  356. 'type' => 1,
  357. 'order_no' => $orderNo,
  358. 'pay_money' => $orderTotal,
  359. 'body' => '购物消费',
  360. 'openid' => $openid
  361. ];
  362. // 调起支付
  363. $payment = PaymentService::make()->minPay($userInfo, $payOrder, 'store');
  364. if (empty($payment)) {
  365. DB::rollBack();
  366. RedisService::clear($cacheLockKey);
  367. $this->error = PaymentService::make()->getError();
  368. return false;
  369. }
  370. // 用户操作记录
  371. DB::commit();
  372. $this->error = '支付请求成功,请前往支付~';
  373. RedisService::clear($cacheLockKey);
  374. return [
  375. 'order_id' => $id,
  376. 'payment' => $payment,
  377. 'total' => $payOrder['pay_money'],
  378. 'pay_type' => 10,
  379. ];
  380. }
  381. /**
  382. * 订单取消
  383. * @param $userId
  384. * @param $id
  385. * @return array|false
  386. */
  387. public function cancel($userId, $id)
  388. {
  389. if ($id <= 0) {
  390. $this->error = '请选择订单';
  391. return false;
  392. }
  393. // 缓存锁
  394. $cacheLockKey = "caches:orders:cancel_lock:{$userId}_{$id}";
  395. if (RedisService::get($cacheLockKey)) {
  396. $this->error = '订单处理中~';
  397. return false;
  398. }
  399. // 商品数据
  400. RedisService::set($cacheLockKey, ['order_id' => $id, 'user_id' => $userId], rand(3, 5));
  401. // 用户信息
  402. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  403. ->select(['id', 'openid', 'mobile', 'nickname', 'realname', 'balance', 'status'])
  404. ->first();
  405. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  406. if (empty($userInfo) || $status != 1) {
  407. $this->error = 1045;
  408. RedisService::clear($cacheLockKey);
  409. return false;
  410. }
  411. // 订单信息
  412. $info = $this->model->where(['id'=> $id,'mark'=>1])
  413. ->select(['id','order_no','pay_total','status'])
  414. ->first();
  415. $orderNo = isset($info['order_no'])? $info['order_no'] : '';
  416. $status = isset($info['status'])? $info['status'] : 0;
  417. if(empty($info) || empty($orderNo)){
  418. $this->error = '订单信息不存在';
  419. RedisService::clear($cacheLockKey);
  420. return false;
  421. }
  422. if($status != 1){
  423. $this->error = '订单已支付';
  424. RedisService::clear($cacheLockKey);
  425. return false;
  426. }
  427. $this->model->where(['user_id'=> $userId,'mark'=>0])->where('update_time','<=', time() - 300)->delete();
  428. $this->model->where(['id'=> $id])->update(['mark'=>0,'update_time'=>time()]);
  429. return ['id'=> $id];
  430. }
  431. /**
  432. * 订单完成
  433. * @param $userId 订单用户ID
  434. * @param $id 订单ID
  435. * @return array|false
  436. */
  437. public function complete($userId, $id)
  438. {
  439. if ($id <= 0) {
  440. $this->error = '请选择订单';
  441. return false;
  442. }
  443. // 缓存锁
  444. $cacheLockKey = "caches:orders:complete_lock:{$userId}_{$id}";
  445. if (RedisService::get($cacheLockKey)) {
  446. $this->error = '订单处理中~';
  447. return false;
  448. }
  449. // 商品数据
  450. RedisService::set($cacheLockKey, ['order_id' => $id, 'user_id' => $userId], rand(3, 5));
  451. // 用户信息
  452. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  453. ->select(['id', 'openid', 'mobile','parent_id', 'nickname', 'realname', 'balance', 'status'])
  454. ->first();
  455. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  456. $parentId = isset($userInfo['parent_id']) ? $userInfo['parent_id'] : 0;
  457. if (empty($userInfo) || $status != 1) {
  458. $this->error = 1045;
  459. RedisService::clear($cacheLockKey);
  460. return false;
  461. }
  462. // 订单信息
  463. $info = $this->model->with(['orderGoods'])->where(['id'=> $id,'mark'=>1])
  464. ->select(['id','order_no','store_id','pay_total','bonus','delivery_no','delivery_company','delivery_code','status'])
  465. ->first();
  466. $orderNo = isset($info['order_no'])? $info['order_no'] : '';
  467. $deliveryNo = isset($info['delivery_no'])? $info['delivery_no'] : '';
  468. $deliverCompany = isset($info['delivery_company'])? $info['delivery_company'] : '';
  469. $deliverCode = isset($info['delivery_code'])? $info['delivery_code'] : '';
  470. $storeId = isset($info['store_id'])? $info['store_id'] : 0;
  471. $orderTotal = isset($info['pay_total'])? $info['pay_total'] : 0;
  472. $bonus = isset($info['bonus'])? $info['bonus'] : 0;
  473. $status = isset($info['status'])? $info['status'] : 0;
  474. $orderGoods = isset($info['order_goods'])? $info['order_goods'] : [];
  475. if(empty($info) || empty($orderNo)){
  476. $this->error = '订单信息不存在';
  477. RedisService::clear($cacheLockKey);
  478. return false;
  479. }
  480. if($status != 3 || (empty($deliveryNo) || empty($deliverCompany) || empty($deliverCode))){
  481. $this->error = '订单未发货';
  482. RedisService::clear($cacheLockKey);
  483. return false;
  484. }
  485. if(empty($deliveryNo) || empty($deliverCompany) || empty($deliverCode)){
  486. $this->error = '订单发货信息错误,请联系客服';
  487. RedisService::clear($cacheLockKey);
  488. return false;
  489. }
  490. DB::beginTransaction();
  491. $this->model->where(['id'=> $id])->update(['status'=> '4','update_time'=>time()]);
  492. // 商家订单数据统计
  493. $updateData = ['order_count'=>DB::raw('order_count+1'),'order_total'=>DB::raw("order_total + {$orderTotal}")];
  494. StoreModel::where(['id'=> $storeId])->update($updateData);
  495. // 商品销量数据
  496. if($orderGoods){
  497. $counts = [];
  498. foreach($orderGoods as $item){
  499. $counts[$item['goods_id']] = isset($counts[$item['goods_id']])? $counts[$item['goods_id']] : 0;
  500. $counts[$item['goods_id']] += $item['num'];
  501. }
  502. if($counts){
  503. foreach ($counts as $id => $v){
  504. GoodsModel::where(['id'=> $id])->update(['sales'=>DB::raw("sales + {$v}"),'update_time'=>time()]);
  505. }
  506. }
  507. }
  508. // 结算商家收益
  509. if(SettleService::make()->storeBonus($storeId, $bonus, $info) < 0){
  510. DB::rollBack();
  511. $this->error = SettleService::make()->getError();
  512. RedisService::clear($cacheLockKey);
  513. return false;
  514. }
  515. // 代理佣金结算
  516. if(SettleService::make()->agentBonus($userId, $orderTotal, $info, $parentId) < 0){
  517. DB::rollBack();
  518. $this->error = SettleService::make()->getError();
  519. RedisService::clear($cacheLockKey);
  520. return false;
  521. }
  522. DB::commit();
  523. return ['id'=> $id];
  524. }
  525. /**
  526. * 今日数量统计数据
  527. * @param $userId
  528. * @param int $type
  529. * @return array|mixed
  530. */
  531. public function getCountByDay($userId, $status = 3)
  532. {
  533. $cacheKey = "caches:orders:count_day_{$userId}_{$status}";
  534. $data = RedisService::get($cacheKey);
  535. if ($data) {
  536. return $data;
  537. }
  538. $data = $this->model->where(['user_id' => $userId, 'status' => $status, 'mark' => 1])
  539. ->where('create_time', '>=', strtotime(date('Y-m-d')))
  540. ->count('id');
  541. if ($data) {
  542. RedisService::set($cacheKey, $data, rand(5, 10));
  543. }
  544. return $data;
  545. }
  546. /**
  547. * 订单数
  548. * @param $userId
  549. * @param int $status
  550. * @return array|mixed
  551. */
  552. public function getCountByStatus($userId, $status = 3)
  553. {
  554. $cacheKey = "caches:orders:count_status_{$userId}_{$status}";
  555. $data = RedisService::get($cacheKey);
  556. if ($data) {
  557. return $data;
  558. }
  559. $data = $this->model->where(['user_id' => $userId, 'status' => $status, 'mark' => 1])
  560. ->count('id');
  561. if ($data) {
  562. RedisService::set($cacheKey, $data, rand(5, 10));
  563. }
  564. return $data;
  565. }
  566. /**
  567. * 获取订单审核状态
  568. * @param $userId
  569. * @return array|mixed
  570. */
  571. public function checkOrderStatus($userId)
  572. {
  573. $cacheKey = "caches:orders:checkOrder:{$userId}";
  574. $data = RedisService::get($cacheKey);
  575. if ($data) {
  576. return $data;
  577. }
  578. $data = $this->model->with(['confirm'])->where(function ($query) {
  579. $query->where(function ($query) {
  580. $query->where('status', 2)->where('confirm_at', '>=', date('Y-m-d H:i:s', time() - 3600));
  581. })->orWhere(function ($query) {
  582. $query->where('status', 9)->where('confirm_at', '>=', date('Y-m-d H:i:s', time() - 600));
  583. })->orWhere(function ($query) {
  584. $query->where('status', 3)->where('confirm_at', '>=', date('Y-m-d H:i:s', time() - 600));
  585. })->orWhere('status', 1);
  586. })
  587. ->where(['user_id' => $userId, 'mark' => 1])
  588. ->select(['id', 'order_no', 'user_id', 'goods_id', 'status'])
  589. ->orderBy('id', 'desc')
  590. ->first();
  591. $data = $data ? $data->toArray() : [];
  592. $result = [];
  593. if ($data) {
  594. $orderId = isset($data['id']) ? $data['id'] : 0;
  595. $status = isset($data['status']) ? $data['status'] : 0;
  596. $goodsId = isset($data['goods_id']) ? $data['goods_id'] : 0;
  597. $confirmOrder = isset($data['confirm']) ? $data['confirm'] : [];
  598. $confirmOrderId = isset($confirmOrder['id']) ? $confirmOrder['id'] : 0;
  599. $confirmOrderUser = isset($confirmOrder['user']) ? $confirmOrder['user'] : [];
  600. $realname = isset($confirmOrderUser['realname']) ? $confirmOrderUser['realname'] : '';
  601. if ($status == 9) {
  602. $message = "抱歉,订单已被其他师傅接走";
  603. } else if ($status == 2) {
  604. $message = "恭喜您,抢单成功,请前往完成订单!";
  605. } else if ($status == 3) {
  606. $message = "恭喜您,订单已经完成!";
  607. } else {
  608. $pickerCount = $this->model->where(['goods_id' => $goodsId, 'mark' => 1])->whereIn('status', [1, 2])->count('id');
  609. $pickerCount = $pickerCount <= 3 ? 3 : $pickerCount;
  610. $message = "<p style='padding: 5px 0;'>正在抢单</p><p>({$pickerCount}位师傅正在抢单中)</p>";
  611. }
  612. $result = ['order_id' => $orderId, 'goods_id' => $goodsId, 'confirm_order_id' => $confirmOrderId, 'status' => $status, 'message' => $message];
  613. RedisService::set($cacheKey, $result, rand(10, 20));
  614. }
  615. return $result;
  616. }
  617. }