OrderService.php 33 KB

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