OrderService.php 32 KB

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