OrderService.php 33 KB

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