OrderService.php 32 KB

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