OrderService.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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\Kd100Service;
  20. use App\Services\PaymentService;
  21. use App\Services\RedisService;
  22. use Illuminate\Support\Facades\DB;
  23. /**
  24. * 订单-服务类
  25. * @author laravel开发员
  26. * @since 2020/11/11
  27. * @package App\Services\Api
  28. */
  29. class OrderService extends BaseService
  30. {
  31. // 静态对象
  32. protected static $instance = null;
  33. /**
  34. * 构造函数
  35. * @author laravel开发员
  36. * @since 2020/11/11
  37. */
  38. public function __construct()
  39. {
  40. $this->model = new OrderModel();
  41. }
  42. /**
  43. * 静态入口
  44. */
  45. public static function make()
  46. {
  47. if (!self::$instance) {
  48. self::$instance = new static();
  49. }
  50. return self::$instance;
  51. }
  52. /**
  53. * 订单列表
  54. * @param $params
  55. * @param int $pageSize
  56. * @return array
  57. */
  58. public function getDataList($params, $pageSize = 15)
  59. {
  60. $model = $this->getQuery($params);
  61. // 数据
  62. $list = $model->where(function ($query) use ($params) {
  63. $status = isset($params['status']) ? $params['status'] : 0;
  64. // 进行中
  65. if ($status == 9) {
  66. $query->where('a.refund_status', '>', 0);
  67. } elseif ($status > 0 && is_array($status)) {
  68. $query->whereIn('a.status', $status)->where('a.refund_status', 0);
  69. } else if ($status == 4) {
  70. $query->where('a.status', $status)->where('a.refund_status', 0);
  71. } else if ($status > 0) {
  72. $query->where('a.status', $status)->where('a.refund_status', 0);
  73. }
  74. })->select(['a.*'])
  75. ->orderBy('a.status', 'asc')
  76. ->orderBy('a.create_time', 'desc')
  77. ->orderBy('a.id', 'desc')
  78. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  79. $list = $list ? $list->toArray() : [];
  80. if ($list) {
  81. $statusArr = [1 => '待支付', 2 => '待发货', 3 => '待收货', 4 => '确认收货'];
  82. $refundStatusArr = [1 => '已退款', 2 => '退款中', 3 => '退款审核', 4 => '退款驳回'];
  83. foreach ($list['data'] as &$item) {
  84. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y/m/d H:i') : '';
  85. $status = isset($item['status']) ? $item['status'] : 0;
  86. $item['status_text'] = '待支付';
  87. if ($status) {
  88. $item['status_text'] = isset($statusArr[$status]) ? $statusArr[$status] : '';
  89. }
  90. if ($item['refund_status'] > 0) {
  91. $item['status_text'] = '退款/售后';
  92. }
  93. $item['goods'] = isset($item['goods']) && $item['goods'] ? $item['goods'] : [];
  94. }
  95. unset($item);
  96. }
  97. return [
  98. 'pageSize' => $pageSize,
  99. 'total' => isset($list['total']) ? $list['total'] : 0,
  100. 'list' => isset($list['data']) ? $list['data'] : []
  101. ];
  102. }
  103. /**
  104. * 查询条件
  105. * @param $params
  106. * @return mixed
  107. */
  108. public function getQuery($params)
  109. {
  110. $where = ['a.mark' => 1];
  111. return $this->model->from('orders as a')->with(['orderGoods', 'store'])
  112. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  113. ->where($where)
  114. ->where(function ($query) use ($params) {
  115. $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
  116. if ($userId > 0) {
  117. $query->where('a.user_id', $userId);
  118. }
  119. })
  120. ->where(function ($query) use ($params) {
  121. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  122. if ($keyword) {
  123. $query->where('a.order_no', 'like', "%{$keyword}%")
  124. ->orWhere('b.mobile', 'like', "%{$keyword}%");
  125. }
  126. });
  127. }
  128. /**
  129. * 订单详情
  130. * @param $id
  131. */
  132. public function getOrderInfo($id)
  133. {
  134. $statusArr = [1 => '待支付', 2 => '待发货', 3 => '待收货', 4 => '已完成'];
  135. $info = $this->model->from('orders as a')->with(['orderGoods','store'])
  136. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  137. ->where(['a.id' => $id, 'a.mark' => 1])
  138. ->select(['a.*'])
  139. ->first();
  140. if ($info) {
  141. $info = $info->toArray();
  142. $info['create_time'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
  143. $status = isset($info['status']) ? $info['status'] : 0;
  144. $info['status_text'] = '待付款';
  145. if ($status) {
  146. $info['status_text'] = isset($statusArr[$status]) ? $statusArr[$status] : '';
  147. }
  148. }
  149. return $info;
  150. }
  151. /**
  152. * 创建订单
  153. * @param $userId 用户
  154. * @param $params 参数
  155. * @return array|false
  156. */
  157. public function createOrder($userId, $params)
  158. {
  159. $addressId = isset($params['address_id']) && $params['address_id'] ? $params['address_id'] : 0;
  160. $goods = isset($params['goods']) && $params['goods'] ? $params['goods'] : [];
  161. $ids = $goods ? array_column($goods, 'id') : [];
  162. // 参数验证
  163. if (empty($goods) || empty($ids)) {
  164. $this->error = '商品参数不为空';
  165. return false;
  166. }
  167. if ($addressId <= 0) {
  168. $this->error = '请选择收货地址';
  169. return false;
  170. }
  171. // 缓存锁
  172. $cacheLockKey = "caches:orders:submit_lock:{$userId}";
  173. if (RedisService::get($cacheLockKey)) {
  174. $this->error = '订单处理中~';
  175. return false;
  176. }
  177. // 商品数据
  178. $orderNo = get_order_num('GX');
  179. RedisService::set($cacheLockKey, ['params' => $params, 'user_id' => $userId], rand(3, 5));
  180. $result = GoodsService::make()->getOrderGoods($ids, $goods, $orderNo, $userId);
  181. if (empty($result)) {
  182. RedisService::clear($cacheLockKey);
  183. $this->error = GoodsService::make()->getError();
  184. return false;
  185. }
  186. $orderGoods = isset($result['goods']) ? $result['goods'] : [];
  187. $orderTotal = isset($result['total']) ? $result['total'] : 0;
  188. $orderCount = isset($result['count']) ? $result['count'] : 0;
  189. $storeId = isset($result['store_id']) ? $result['store_id'] : 0;
  190. if (empty($orderGoods)) {
  191. RedisService::clear($cacheLockKey);
  192. $this->error = '获取订单商品错误~';
  193. return false;
  194. }
  195. if ($orderTotal <= 0) {
  196. RedisService::clear($cacheLockKey);
  197. $this->error = '订单金额错误~';
  198. return false;
  199. }
  200. if ($orderCount <= 0) {
  201. RedisService::clear($cacheLockKey);
  202. $this->error = '订单商品数量错误~';
  203. return false;
  204. }
  205. // 用户信息
  206. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  207. ->select(['id', 'openid', 'mobile', 'nickname', 'realname', 'balance', 'status'])
  208. ->first();
  209. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  210. $openid = isset($userInfo['openid']) ? $userInfo['openid'] : '';
  211. if (empty($userInfo) || $status != 1) {
  212. $this->error = 1045;
  213. RedisService::clear($cacheLockKey);
  214. return false;
  215. }
  216. if (empty($openid)) {
  217. $this->error = '用户微信未授权,请重新授权登录';
  218. RedisService::clear($cacheLockKey);
  219. return false;
  220. }
  221. // 收货地址信息
  222. $addressInfo = MemberAddressService::make()->getBindInfo($userId, $addressId);
  223. $realname = isset($addressInfo['realname']) ? $addressInfo['realname'] : '';
  224. $mobile = isset($addressInfo['mobile']) ? $addressInfo['mobile'] : '';
  225. $area = isset($addressInfo['area']) ? $addressInfo['area'] : '';
  226. $address = isset($addressInfo['address']) ? $addressInfo['address'] : '';
  227. if (empty($addressInfo) || empty($realname) || empty($mobile) || empty($area) || empty($address)) {
  228. RedisService::clear($cacheLockKey);
  229. $this->error = '收货地址信息错误,请核对后重试~';
  230. return false;
  231. }
  232. // 商家佣金
  233. $storeInfo = StoreModel::where(['id' => $storeId])->first();
  234. $bonusRate = isset($storeInfo['bonus_rate']) ? floatval($storeInfo['bonus_rate']) : 0;
  235. $storeBonusRate = ConfigService::make()->getConfigByCode('store_bonus_rate', 0);
  236. $storeBonusRate = $storeBonusRate > 0 && $storeBonusRate <= 100 ? $storeBonusRate : 0;
  237. $bonusRate = $bonusRate > 0 && $bonusRate <= 100 ? $bonusRate : $storeBonusRate;
  238. $bonus = moneyFormat($orderTotal * $bonusRate / 100, 2);
  239. $total = $orderTotal;
  240. if (env('PAY_DEBUG')) {
  241. $orderTotal = 0.1;
  242. }
  243. // 订单数据
  244. $order = [
  245. 'order_no' => $orderNo,
  246. 'user_id' => $userId,
  247. 'store_id' => $storeId,
  248. 'total' => $total,
  249. 'num' => $orderCount,
  250. 'pay_total' => $orderTotal,
  251. 'receiver_name' => $realname,
  252. 'receiver_mobile' => $mobile,
  253. 'receiver_area' => $area,
  254. 'receiver_address' => $address,
  255. 'bonus' => $bonus,
  256. 'create_time' => time(),
  257. 'update_time' => time(),
  258. 'status' => 1,
  259. 'mark' => 1,
  260. ];
  261. // 订单处理
  262. DB::beginTransaction();
  263. if (!$orderId = $this->model->insertGetId($order)) {
  264. DB::rollBack();
  265. $this->error = '创建订单失败';
  266. RedisService::clear($cacheLockKey);
  267. return false;
  268. }
  269. // 订单商品
  270. if ($orderGoods && !OrderGoodsModel::insert($orderGoods)) {
  271. DB::rollBack();
  272. $this->error = '处理订单商品错误';
  273. RedisService::clear($cacheLockKey);
  274. return false;
  275. }
  276. // 获取支付参数
  277. /* TODO 支付处理 */
  278. $payOrder = [
  279. 'type' => 1,
  280. 'order_no' => $orderNo,
  281. 'pay_money' => $orderTotal,
  282. 'body' => '购物消费',
  283. 'openid' => $openid
  284. ];
  285. // 调起支付
  286. $payment = PaymentService::make()->minPay($userInfo, $payOrder, 'store');
  287. if (empty($payment)) {
  288. DB::rollBack();
  289. RedisService::clear($cacheLockKey);
  290. $this->error = PaymentService::make()->getError();
  291. return false;
  292. }
  293. // 用户操作记录
  294. DB::commit();
  295. $this->error = '订单创建成功,请前往支付~';
  296. RedisService::clear($cacheLockKey);
  297. return [
  298. 'order_id' => $orderId,
  299. 'payment' => $payment,
  300. 'total' => $payOrder['pay_money'],
  301. 'pay_type' => 10,
  302. ];
  303. }
  304. /**
  305. * 订单支付
  306. * @param $userId
  307. * @param $id
  308. * @return array|false
  309. */
  310. public function pay($userId, $id)
  311. {
  312. if ($id <= 0) {
  313. $this->error = '请选择支付订单';
  314. return false;
  315. }
  316. // 缓存锁
  317. $cacheLockKey = "caches:orders:pay_lock:{$userId}_{$id}";
  318. if (RedisService::get($cacheLockKey)) {
  319. $this->error = '订单处理中~';
  320. return false;
  321. }
  322. // 商品数据
  323. RedisService::set($cacheLockKey, ['order_id' => $id, 'user_id' => $userId], rand(3, 5));
  324. // 用户信息
  325. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  326. ->select(['id', 'openid', 'mobile', 'nickname', 'realname', 'balance', 'status'])
  327. ->first();
  328. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  329. $openid = isset($userInfo['openid']) ? $userInfo['openid'] : '';
  330. if (empty($userInfo) || $status != 1) {
  331. $this->error = 1045;
  332. RedisService::clear($cacheLockKey);
  333. return false;
  334. }
  335. if (empty($openid)) {
  336. $this->error = '用户微信未授权,请重新授权登录';
  337. RedisService::clear($cacheLockKey);
  338. return false;
  339. }
  340. // 订单信息
  341. $info = $this->model->where(['id' => $id, 'mark' => 1])
  342. ->select(['id', 'order_no', 'pay_total', 'status'])
  343. ->first();
  344. $orderTotal = isset($info['pay_total']) ? $info['pay_total'] : 0;
  345. $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
  346. $status = isset($info['status']) ? $info['status'] : 0;
  347. if (empty($info) || empty($orderNo)) {
  348. $this->error = '订单信息不存在';
  349. RedisService::clear($cacheLockKey);
  350. return false;
  351. }
  352. if ($status != 1) {
  353. $this->error = '订单已支付';
  354. RedisService::clear($cacheLockKey);
  355. return false;
  356. }
  357. // 获取支付参数
  358. /* TODO 支付处理 */
  359. $payOrder = [
  360. 'type' => 1,
  361. 'order_no' => $orderNo,
  362. 'pay_money' => $orderTotal,
  363. 'body' => '购物消费',
  364. 'openid' => $openid
  365. ];
  366. // 调起支付
  367. $payment = PaymentService::make()->minPay($userInfo, $payOrder, 'store');
  368. if (empty($payment)) {
  369. DB::rollBack();
  370. RedisService::clear($cacheLockKey);
  371. $this->error = PaymentService::make()->getError();
  372. return false;
  373. }
  374. // 用户操作记录
  375. DB::commit();
  376. $this->error = '支付请求成功,请前往支付~';
  377. RedisService::clear($cacheLockKey);
  378. return [
  379. 'order_id' => $id,
  380. 'payment' => $payment,
  381. 'total' => $payOrder['pay_money'],
  382. 'pay_type' => 10,
  383. ];
  384. }
  385. /**
  386. * 订单取消
  387. * @param $userId
  388. * @param $id
  389. * @return array|false
  390. */
  391. public function cancel($userId, $id)
  392. {
  393. if ($id <= 0) {
  394. $this->error = '请选择订单';
  395. return false;
  396. }
  397. // 缓存锁
  398. $cacheLockKey = "caches:orders:cancel_lock:{$userId}_{$id}";
  399. if (RedisService::get($cacheLockKey)) {
  400. $this->error = '订单处理中~';
  401. return false;
  402. }
  403. // 商品数据
  404. RedisService::set($cacheLockKey, ['order_id' => $id, 'user_id' => $userId], rand(3, 5));
  405. // 用户信息
  406. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  407. ->select(['id', 'openid', 'mobile', 'nickname', 'realname', 'balance', 'status'])
  408. ->first();
  409. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  410. if (empty($userInfo) || $status != 1) {
  411. $this->error = 1045;
  412. RedisService::clear($cacheLockKey);
  413. return false;
  414. }
  415. // 订单信息
  416. $info = $this->model->where(['id' => $id, 'mark' => 1])
  417. ->select(['id', 'order_no', 'pay_total', 'status'])
  418. ->first();
  419. $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
  420. $status = isset($info['status']) ? $info['status'] : 0;
  421. if (empty($info) || empty($orderNo)) {
  422. $this->error = '订单信息不存在';
  423. RedisService::clear($cacheLockKey);
  424. return false;
  425. }
  426. if ($status != 1) {
  427. $this->error = '订单已支付';
  428. RedisService::clear($cacheLockKey);
  429. return false;
  430. }
  431. $this->error = '取消订单成功';
  432. $this->model->where(['user_id' => $userId, 'mark' => 0])->where('update_time', '<=', time() - 300)->delete();
  433. $this->model->where(['id' => $id])->update(['mark' => 0, 'update_time' => time()]);
  434. return ['id' => $id];
  435. }
  436. /**
  437. * 订单完成
  438. * @param $userId 订单用户ID
  439. * @param $id 订单ID
  440. * @return array|false
  441. */
  442. public function complete($userId, $id)
  443. {
  444. if ($id <= 0) {
  445. $this->error = '请选择订单';
  446. return false;
  447. }
  448. // 缓存锁
  449. $cacheLockKey = "caches:orders:complete_lock:{$userId}_{$id}";
  450. if (RedisService::get($cacheLockKey)) {
  451. $this->error = '订单处理中~';
  452. return false;
  453. }
  454. // 商品数据
  455. RedisService::set($cacheLockKey, ['order_id' => $id, 'user_id' => $userId], rand(3, 5));
  456. // 用户信息
  457. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  458. ->select(['id', 'openid', 'mobile', 'parent_id', 'nickname', 'realname', 'balance', 'status'])
  459. ->first();
  460. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  461. $parentId = isset($userInfo['parent_id']) ? $userInfo['parent_id'] : 0;
  462. if (empty($userInfo) || $status != 1) {
  463. $this->error = 1045;
  464. RedisService::clear($cacheLockKey);
  465. return false;
  466. }
  467. // 订单信息
  468. $info = $this->model->with(['orderGoods'])->where(['id' => $id, 'mark' => 1])
  469. ->select(['id', 'order_no', 'store_id', 'pay_total', 'bonus', 'delivery_no', 'delivery_company', 'delivery_code', 'status'])
  470. ->first();
  471. $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
  472. $deliveryNo = isset($info['delivery_no']) ? $info['delivery_no'] : '';
  473. $deliverCompany = isset($info['delivery_company']) ? $info['delivery_company'] : '';
  474. $deliverCode = isset($info['delivery_code']) ? $info['delivery_code'] : '';
  475. $storeId = isset($info['store_id']) ? $info['store_id'] : 0;
  476. $orderTotal = isset($info['pay_total']) ? $info['pay_total'] : 0;
  477. $bonus = isset($info['bonus']) ? $info['bonus'] : 0;
  478. $status = isset($info['status']) ? $info['status'] : 0;
  479. $orderGoods = isset($info['order_goods']) ? $info['order_goods'] : [];
  480. if (empty($info) || empty($orderNo)) {
  481. $this->error = '订单信息不存在';
  482. RedisService::clear($cacheLockKey);
  483. return false;
  484. }
  485. if ($status != 3) {
  486. $this->error = '订单未发货';
  487. RedisService::clear($cacheLockKey);
  488. return false;
  489. }
  490. if ((empty($deliveryNo) || empty($deliverCompany) || empty($deliverCode))) {
  491. $this->error = '订单发货信息错误';
  492. RedisService::clear($cacheLockKey);
  493. return false;
  494. }
  495. if (empty($deliveryNo) || empty($deliverCompany) || empty($deliverCode)) {
  496. $this->error = '订单发货信息错误,请联系客服';
  497. RedisService::clear($cacheLockKey);
  498. return false;
  499. }
  500. DB::beginTransaction();
  501. $this->model->where(['id' => $id])->update(['status' => '4', 'update_time' => time()]);
  502. // 商家订单数据统计
  503. $updateData = ['order_count' => DB::raw('order_count+1'), 'order_total' => DB::raw("order_total + {$orderTotal}")];
  504. StoreModel::where(['id' => $storeId])->update($updateData);
  505. // 商品销量数据
  506. if ($orderGoods) {
  507. $counts = [];
  508. foreach ($orderGoods as $item) {
  509. $counts[$item['goods_id']] = isset($counts[$item['goods_id']]) ? $counts[$item['goods_id']] : 0;
  510. $counts[$item['goods_id']] += $item['num'];
  511. }
  512. if ($counts) {
  513. foreach ($counts as $id => $v) {
  514. GoodsModel::where(['id' => $id])->update(['sales' => DB::raw("sales + {$v}"), 'update_time' => time()]);
  515. }
  516. }
  517. }
  518. // 结算商家收益
  519. if (SettleService::make()->storeBonus($storeId, $bonus, $info) < 0) {
  520. DB::rollBack();
  521. $this->error = SettleService::make()->getError();
  522. RedisService::clear($cacheLockKey);
  523. return false;
  524. }
  525. // 代理佣金结算
  526. if (SettleService::make()->agentBonus($userId, $orderTotal, $info, $parentId) < 0) {
  527. DB::rollBack();
  528. $this->error = SettleService::make()->getError();
  529. RedisService::clear($cacheLockKey);
  530. return false;
  531. }
  532. DB::commit();
  533. $this->error = '确认收货成功';
  534. return ['id' => $id];
  535. }
  536. /**
  537. * 售后或退款
  538. * @param $userId
  539. * @param $params
  540. * @return array|false
  541. */
  542. public function after($userId, $params)
  543. {
  544. $id = isset($params['id']) ? $params['id'] : 0;
  545. $afterType = isset($params['after_type']) ? $params['after_type'] : 1;
  546. if ($id <= 0) {
  547. $this->error = '请选择订单';
  548. return false;
  549. }
  550. // 缓存锁
  551. $cacheLockKey = "caches:orders:after_lock:{$userId}_{$id}";
  552. if (RedisService::get($cacheLockKey)) {
  553. $this->error = '订单处理中~';
  554. return false;
  555. }
  556. // 商品数据
  557. RedisService::set($cacheLockKey, ['params' => $params, 'user_id' => $userId], rand(3, 5));
  558. // 用户信息
  559. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  560. ->select(['id', 'openid', 'mobile', 'nickname', 'realname', 'balance', 'status'])
  561. ->first();
  562. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  563. if (empty($userInfo) || $status != 1) {
  564. $this->error = 1045;
  565. RedisService::clear($cacheLockKey);
  566. return false;
  567. }
  568. // 订单信息
  569. $info = $this->model->where(['id' => $id, 'mark' => 1])
  570. ->select(['id', 'order_no', 'after_type', 'refund_status', 'pay_total', 'status'])
  571. ->first();
  572. $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
  573. $status = isset($info['status']) ? $info['status'] : 0;
  574. $refundStatus = isset($info['refund_status']) ? $info['refund_status'] : 0;
  575. if (empty($info) || empty($orderNo)) {
  576. $this->error = '订单信息不存在';
  577. RedisService::clear($cacheLockKey);
  578. return false;
  579. }
  580. if ($status == 1) {
  581. $this->error = '订单未支付';
  582. RedisService::clear($cacheLockKey);
  583. return false;
  584. }
  585. if ($status == 4 && $afterType==2) {
  586. $this->error = '订单已完成';
  587. RedisService::clear($cacheLockKey);
  588. return false;
  589. }
  590. if ($refundStatus > 0 && $refundStatus != 4) {
  591. $this->error = '订单售后处理中';
  592. RedisService::clear($cacheLockKey);
  593. return false;
  594. }
  595. $afterRealname = isset($params['after_realname']) ? $params['after_realname'] : '';
  596. $afterPhone = isset($params['after_phone']) ? $params['after_phone'] : '';
  597. $afterRemark = isset($params['after_remark']) ? $params['after_remark'] : '';
  598. if ($afterType == 1) {
  599. if (empty($afterRealname) || empty($afterPhone) || empty($afterRemark)) {
  600. $this->error = '请填写售后信息';
  601. RedisService::clear($cacheLockKey);
  602. return false;
  603. }
  604. }
  605. $data = [
  606. 'after_type' => $afterType,
  607. 'after_realname' => $afterRealname,
  608. 'after_phone' => $afterPhone,
  609. 'after_remark' => $afterRemark,
  610. 'refund_remark' => isset($params['refund_remark']) ? $params['refund_remark'] : '',
  611. 'refund_status' => 3,
  612. 'update_time' => time()
  613. ];
  614. $this->model->where(['id' => $id])->update($data);
  615. $this->error = '订单申请售后成功';
  616. return ['id' => $id];
  617. }
  618. /**
  619. * 今日数量统计数据
  620. * @param $userId
  621. * @param int $type
  622. * @return array|mixed
  623. */
  624. public function getCountByDay($userId, $status = 3)
  625. {
  626. $cacheKey = "caches:orders:count_day_{$userId}_{$status}";
  627. $data = RedisService::get($cacheKey);
  628. if ($data) {
  629. return $data;
  630. }
  631. $data = $this->model->where(['user_id' => $userId, 'status' => $status, 'mark' => 1])
  632. ->where('create_time', '>=', strtotime(date('Y-m-d')))
  633. ->count('id');
  634. if ($data) {
  635. RedisService::set($cacheKey, $data, rand(5, 10));
  636. }
  637. return $data;
  638. }
  639. /**
  640. * 订单数
  641. * @param $userId
  642. * @param int $status
  643. * @return array|mixed
  644. */
  645. public function getCountByStatus($userId, $status = 3)
  646. {
  647. $cacheKey = "caches:orders:count_status_{$userId}_{$status}";
  648. $data = RedisService::get($cacheKey);
  649. if ($data) {
  650. return $data;
  651. }
  652. $data = $this->model->where(['user_id' => $userId, 'status' => $status, 'mark' => 1])
  653. ->count('id');
  654. if ($data) {
  655. RedisService::set($cacheKey, $data, rand(5, 10));
  656. }
  657. return $data;
  658. }
  659. /**
  660. * 获取订单审核状态
  661. * @param $userId
  662. * @return array|mixed
  663. */
  664. public function checkOrderStatus($userId)
  665. {
  666. $cacheKey = "caches:orders:checkOrder:{$userId}";
  667. $data = RedisService::get($cacheKey);
  668. if ($data) {
  669. return $data;
  670. }
  671. $data = $this->model->with(['confirm'])->where(function ($query) {
  672. $query->where(function ($query) {
  673. $query->where('status', 2)->where('confirm_at', '>=', date('Y-m-d H:i:s', time() - 3600));
  674. })->orWhere(function ($query) {
  675. $query->where('status', 9)->where('confirm_at', '>=', date('Y-m-d H:i:s', time() - 600));
  676. })->orWhere(function ($query) {
  677. $query->where('status', 3)->where('confirm_at', '>=', date('Y-m-d H:i:s', time() - 600));
  678. })->orWhere('status', 1);
  679. })
  680. ->where(['user_id' => $userId, 'mark' => 1])
  681. ->select(['id', 'order_no', 'user_id', 'goods_id', 'status'])
  682. ->orderBy('id', 'desc')
  683. ->first();
  684. $data = $data ? $data->toArray() : [];
  685. $result = [];
  686. if ($data) {
  687. $orderId = isset($data['id']) ? $data['id'] : 0;
  688. $status = isset($data['status']) ? $data['status'] : 0;
  689. $goodsId = isset($data['goods_id']) ? $data['goods_id'] : 0;
  690. $confirmOrder = isset($data['confirm']) ? $data['confirm'] : [];
  691. $confirmOrderId = isset($confirmOrder['id']) ? $confirmOrder['id'] : 0;
  692. $confirmOrderUser = isset($confirmOrder['user']) ? $confirmOrder['user'] : [];
  693. $realname = isset($confirmOrderUser['realname']) ? $confirmOrderUser['realname'] : '';
  694. if ($status == 9) {
  695. $message = "抱歉,订单已被其他师傅接走";
  696. } else if ($status == 2) {
  697. $message = "恭喜您,抢单成功,请前往完成订单!";
  698. } else if ($status == 3) {
  699. $message = "恭喜您,订单已经完成!";
  700. } else {
  701. $pickerCount = $this->model->where(['goods_id' => $goodsId, 'mark' => 1])->whereIn('status', [1, 2])->count('id');
  702. $pickerCount = $pickerCount <= 3 ? 3 : $pickerCount;
  703. $message = "<p style='padding: 5px 0;'>正在抢单</p><p>({$pickerCount}位师傅正在抢单中)</p>";
  704. }
  705. $result = ['order_id' => $orderId, 'goods_id' => $goodsId, 'confirm_order_id' => $confirmOrderId, 'status' => $status, 'message' => $message];
  706. RedisService::set($cacheKey, $result, rand(10, 20));
  707. }
  708. return $result;
  709. }
  710. /**
  711. * 物流查询
  712. * @param $id
  713. * @return array|false|mixed
  714. */
  715. public function getDelivery($id)
  716. {
  717. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  718. $deliveryNo = isset($info['delivery_no']) ? $info['delivery_no'] : '';
  719. $deliveryCode = isset($info['delivery_code']) ? $info['delivery_code'] : '';
  720. $mobile = isset($info['receiver_mobile']) ? $info['receiver_mobile'] : '';
  721. if (empty($info)) {
  722. $this->error = '请选择订单';
  723. return false;
  724. }
  725. $cacheKey = "caches:kd100_{$id}";
  726. $data = RedisService::get($cacheKey);
  727. $data = [
  728. [
  729. "time" => "2025-12-24 11:29:23",
  730. "context" => "您的快件已投递,收件人在[沙岗镇绕城路天猫1号店妈妈驿站]取件(凭取件码签收),如有疑问请联系站点:13086796129,快递员电话:13086796129,投诉电话:15278929512。感谢使用圆通速递,期待再次为您服务!",
  731. "ftime" => "2025-12-24 11:29:23",
  732. "areaCode" => "CN450521000000",
  733. "areaName" => "广西,北海市,合浦县",
  734. "status" => "签收",
  735. ],
  736. [
  737. "time" => "2025-12-22 17:29:40",
  738. "context" => "您的快件已到达[妈妈驿站]沙岗镇绕城路1号天猫店,请您及时取件,如有取件码问题或找不到包裹等问题,请联系站点:13086796129,快递员电话:13086796129,投诉电话:15278929512。感谢使用圆通速递,期待再次为您服务!",
  739. "ftime" => "2025-12-22 17:29:40",
  740. "areaCode" => "CN450521000000",
  741. "areaName" => "广西,北海市,合浦县",
  742. "status" => "派件",
  743. ],[
  744. "time" => "2025-12-22 17:28:40",
  745. "context" => "【广西北海市合浦县沙岗镇】的莫业琨(13086796129)正在派件,(有事先呼我,勿找平台,少一次投诉,多一份感恩)!如有疑问请联系网点:15278929512,投诉电话:15278929512。[95161和18521号段的上海号码为圆通快递员专属号码,请放心接听]",
  746. "ftime" => "2025-12-22 17:28:40",
  747. "areaCode" => "CN450521100000",
  748. "areaName" => "广西,北海市,合浦县,廉州镇",
  749. "status" => "派件",
  750. ],[
  751. "time" => "2025-12-22 02:44:12",
  752. "context" => "您的快件离开【南宁转运中心】,已发往【广西北海市合浦】",
  753. "ftime" => "2025-12-22 02:44:12",
  754. "areaCode" => "CN450108000000",
  755. "areaName" => "广西,南宁市,良庆区",
  756. "status" => "在途",
  757. ],[
  758. "time" => "2025-12-22 02:21:21",
  759. "context" => "您的快件已经到达【南宁转运中心】【物流问题无需找商家或平台,请致电(专属热线:95554)更快解决】",
  760. "ftime" => "2025-12-22 02:21:21",
  761. "areaCode" => "CN450108000000",
  762. "areaName" => "广西,南宁市,良庆区",
  763. "status" => "在途",
  764. ],[
  765. "time" => "2025-12-20 23:47:58",
  766. "context" => "您的快件离开【临海转运中心】,已发往【南宁转运中心】。预计【12月22日】到达【南宁市】,因运输距离较远,预计将在【22日晚上】为您更新快件状态,请您放心!",
  767. "ftime" => "2025-12-20 23:47:58",
  768. "areaCode" => "CN331082000000",
  769. "areaName" => "浙江,台州市,临海市",
  770. "status" => "在途",
  771. ],[
  772. "time" => "2025-12-20 23:45:58",
  773. "context" => "您的快件已经到达【临海转运中心】【物流问题无需找商家或平台,请致电(专属热线:95554)更快解决】",
  774. "ftime" => "2025-12-20 23:45:58",
  775. "areaCode" => "CN331082000000",
  776. "areaName" => "浙江,台州市,临海市",
  777. "status" => "在途",
  778. ]
  779. ];
  780. if ($data) {
  781. return $data;
  782. }
  783. $result = Kd100Service::make()->query($deliveryNo, $mobile, $deliveryCode);
  784. $status = isset($result['status'])?$result['status']:0;
  785. $data = isset($result['data'])?$result['data']:[];
  786. if ($data && $status==200) {
  787. RedisService::set($cacheKey, $data, 300);
  788. }
  789. return $data;
  790. }
  791. }