OrderService.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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\Common;
  12. use App\Models\AccountLogModel;
  13. use App\Models\ActionLogModel;
  14. use App\Models\GoodsModel;
  15. use App\Models\MemberModel;
  16. use App\Models\MessageModel;
  17. use App\Models\OrderModel;
  18. use App\Models\OrderGoodsModel;
  19. use App\Models\StoreModel;
  20. use App\Services\BaseService;
  21. use App\Services\RedisService;
  22. use Illuminate\Support\Facades\DB;
  23. /**
  24. * 订单管理-服务类
  25. * @author laravel开发员
  26. * @since 2020/11/11
  27. * Class OrderService
  28. * @package App\Services\Common
  29. */
  30. class OrderService extends BaseService
  31. {
  32. // 静态对象
  33. protected static $instance = null;
  34. /**
  35. * 构造函数
  36. * @author laravel开发员
  37. * @since 2020/11/11
  38. * OrderService constructor.
  39. */
  40. public function __construct()
  41. {
  42. $this->model = new OrderModel();
  43. }
  44. /**
  45. * 静态入口
  46. * @return static|null
  47. */
  48. public static function make()
  49. {
  50. if (!self::$instance) {
  51. self::$instance = (new static());
  52. }
  53. return self::$instance;
  54. }
  55. /**
  56. * @param $params
  57. * @param int $pageSize
  58. * @return array
  59. */
  60. public function getDataList($params, $pageSize = 15)
  61. {
  62. $query = $this->model->where('mark', 1);
  63. // 店铺筛选
  64. if (isset($params['store_id']) && $params['store_id'] > 0) {
  65. $query->where('store_id', $params['store_id']);
  66. }
  67. // 用户筛选
  68. if (isset($params['user_id']) && $params['user_id'] > 0) {
  69. $query->where('user_id', $params['user_id']);
  70. }
  71. // 状态筛选
  72. if (isset($params['status']) && $params['status'] > 0) {
  73. $query->where('status', $params['status']);
  74. }
  75. // 售后类型筛选(1-售后,2-退款)
  76. if (isset($params['after_type']) && $params['after_type'] > 0) {
  77. $query->where('after_type', $params['after_type']);
  78. }
  79. // 退款状态筛选
  80. if (isset($params['refund_status']) && $params['refund_status'] > 0) {
  81. $query->where('refund_status', $params['refund_status']);
  82. }
  83. // 关键词搜索(订单号、商品名称、收货人手机)
  84. if (isset($params['keyword']) && $params['keyword']) {
  85. $keyword = $params['keyword'];
  86. $query->where(function ($q) use ($keyword) {
  87. $q->where('order_no', 'like', '%' . $keyword . '%')
  88. ->orWhere('receiver_name', 'like', '%' . $keyword . '%')
  89. ->orWhere('receiver_mobile', 'like', '%' . $keyword . '%')
  90. ->orWhereHas('orderGoods', function ($q2) use ($keyword) {
  91. $q2->where('goods_name', 'like', '%' . $keyword . '%');
  92. });
  93. });
  94. }
  95. $list = $query->with(['user', 'orderGoods', 'store'])
  96. ->orderBy('create_time', 'desc')
  97. ->orderBy('id', 'desc')
  98. ->paginate($pageSize);
  99. $list = $list ? $list->toArray() : [];
  100. if ($list && isset($list['data'])) {
  101. foreach ($list['data'] as &$item) {
  102. $item['create_time'] = $item['create_time'] ? date('Y-m-d H:i:s', strtotime($item['create_time'])) : '';
  103. $item['update_time'] = $item['update_time'] ? date('Y-m-d H:i:s', strtotime($item['update_time'])) : '';
  104. $item['user'] = $item['user'] ?? [];
  105. $item['store'] = $item['store'] ?? [];
  106. // 获取第一个商品信息(thumb已通过Model访问器处理)
  107. $item['goods'] = isset($item['order_goods'][0]) ? $item['order_goods'][0] : null;
  108. }
  109. }
  110. return [
  111. 'msg' => '操作成功',
  112. 'code' => 0,
  113. 'data' => $list['data'] ?? [],
  114. 'count' => $list['total'] ?? 0,
  115. ];
  116. }
  117. /**
  118. * 获取订单详情
  119. */
  120. public function getInfo($id)
  121. {
  122. $info = $this->model->where('id', $id)->where('mark', 1)
  123. ->with(['user', 'orderGoods', 'store'])
  124. ->first();
  125. if (!$info) {
  126. return ['code' => 1, 'msg' => '订单不存在'];
  127. }
  128. $info = $info->toArray();
  129. $info['create_time'] = $info['create_time'] ? date('Y-m-d H:i:s', strtotime($info['create_time'])) : '';
  130. $info['update_time'] = $info['update_time'] ? date('Y-m-d H:i:s', strtotime($info['update_time'])) : '';
  131. if (isset($info['order_goods'])) {
  132. foreach ($info['order_goods'] as &$goods) {
  133. $goods['thumb'] = $goods['thumb'] ? get_image_url($goods['thumb']) : '';
  134. $goods['create_time'] = $goods['create_time'] ? date('Y-m-d H:i:s', strtotime($goods['create_time'])) : '';
  135. $goods['update_time'] = $goods['update_time'] ? date('Y-m-d H:i:s', strtotime($goods['update_time'])) : '';
  136. }
  137. }
  138. return ['code' => 0, 'msg' => '操作成功', 'data' => $info];
  139. }
  140. /**
  141. * 查询
  142. * @param $params
  143. * @return \Illuminate\Database\Eloquent\Builder
  144. */
  145. public function getQuery($params)
  146. {
  147. $where = ['a.mark' => 1];
  148. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  149. return $this->model->with(['user', 'goods'])->from('orders as a')
  150. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  151. ->leftJoin('goods as c', 'c.id', '=', 'a.goods_id')
  152. ->where($where)
  153. ->where(function ($query) use ($params) {
  154. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  155. if ($keyword) {
  156. $query->where('a.order_no', 'like', "%{$keyword}%");
  157. }
  158. // 接单人
  159. $account = isset($params['account']) ? $params['account'] : '';
  160. if ($account) {
  161. $query->where(function ($query) use ($account) {
  162. $query->where('b.nickname', 'like', "%{$account}%")->orWhere('b.mobile', 'like', "%{$account}%");
  163. });
  164. }
  165. // 商品
  166. $goodsId = isset($params['goods_id']) ? intval($params['goods_id']) : 0;
  167. $goods = isset($params['goods']) ? trim($params['goods']) : '';
  168. if ($goods) {
  169. $query->where(function ($query) use ($goods) {
  170. $query->where('c.goods_name', 'like', "%{$goods}%");
  171. if (preg_match("/^(1[0-9]+|[1-9]+)$/", $goods)) {
  172. $query->where('a.goods_id', intval($goods));
  173. } else {
  174. $query->where('c.goods_name', 'like', "%{$goods}%");
  175. }
  176. });
  177. }
  178. if ($goodsId > 0) {
  179. $query->where('a.goods_id', intval($goodsId));
  180. }
  181. })
  182. ->where(function ($query) use ($params) {
  183. $status = isset($params['status']) ? $params['status'] : 0;
  184. if ($status == 0) {
  185. $query->whereIn('a.status', [2, 3]);
  186. } else if ($status) {
  187. $query->where('a.status', $status);
  188. }
  189. })
  190. ->where(function ($query) use ($userId) {
  191. if ($userId) {
  192. $query->where('a.user_id', '=', $userId);
  193. }
  194. });
  195. }
  196. /**
  197. * 按日期统计订单数
  198. * @param string $beginAt 开始时间
  199. * @param string $endAt 结束时间
  200. * @param int[] $status 状态:数组或数值
  201. * @return mixed
  202. */
  203. public function getCountByTime($beginAt = '', $endAt = '', $status = 3)
  204. {
  205. $cacheKey = "caches:orders:count_{$status}_{$beginAt}_{$endAt}";
  206. $data = RedisService::get($cacheKey);
  207. if ($data) {
  208. return $data;
  209. }
  210. $where = ['mark' => 1];
  211. $data = $this->model->where($where)->where(function ($query) use ($beginAt, $endAt, $status) {
  212. if ($beginAt && $endAt) {
  213. $query->whereBetween('create_time', [strtotime($beginAt), strtotime($endAt)]);
  214. } else if ($beginAt) {
  215. $query->where('create_time', '>=', strtotime($beginAt));
  216. }
  217. if ($status && is_array($status)) {
  218. $query->whereIn('status', $status);
  219. } else if ($status) {
  220. $query->where('status', $status);
  221. }
  222. })->count('id');
  223. if ($data) {
  224. RedisService::set($cacheKey, $data, rand(300, 600));
  225. }
  226. return $data;
  227. }
  228. /**
  229. * 按日期统计订单金额
  230. * @param string $beginAt 开始时间
  231. * @param string $endAt 结束时间
  232. * @param int[] $status 状态:数组或数值
  233. * @return mixed
  234. */
  235. public function getTotalByTime($beginAt = '', $endAt = '', $status = 3)
  236. {
  237. $cacheKey = "caches:orders:total_{$status}_{$beginAt}_{$endAt}";
  238. $data = RedisService::get($cacheKey);
  239. if ($data) {
  240. return $data;
  241. }
  242. $where = ['mark' => 1];
  243. $data = $this->model->where($where)->where(function ($query) use ($beginAt, $endAt, $status) {
  244. if ($beginAt && $endAt) {
  245. $query->whereBetween('create_time', [strtotime($beginAt), strtotime($endAt)]);
  246. } else if ($beginAt) {
  247. $query->where('create_time', '>=', strtotime($beginAt));
  248. }
  249. if ($status && is_array($status)) {
  250. $query->whereIn('status', $status);
  251. } else if ($status) {
  252. $query->where('status', $status);
  253. }
  254. })->sum('total');
  255. if ($data) {
  256. RedisService::set($cacheKey, $data, rand(300, 600));
  257. }
  258. return $data;
  259. }
  260. /**
  261. * 添加或编辑
  262. * @return array
  263. * @since 2020/11/11
  264. * @author laravel开发员
  265. */
  266. public function edit()
  267. {
  268. $params = request()->post();
  269. return parent::edit($params); // TODO: Change the autogenerated stub
  270. }
  271. /**
  272. * 完成支付
  273. */
  274. public function completePay()
  275. {
  276. $id = request()->post('id');
  277. $transactionId = request()->post('transaction_id', '');
  278. if (!$id) {
  279. return ['code' => 1, 'msg' => '参数错误'];
  280. }
  281. $order = $this->model->find($id);
  282. if (!$order) {
  283. return ['code' => 1, 'msg' => '订单不存在'];
  284. }
  285. if ($order->status != 1) {
  286. return ['code' => 1, 'msg' => '订单状态不正确'];
  287. }
  288. $updateData = [
  289. 'status' => 2, // 已付款
  290. 'transaction_id' => $transactionId ?: 'PAY' . time() . rand(1000, 9999),
  291. 'pay_at'=> date('Y-m-d H:i:s'),
  292. 'remark'=> '人工审核支付',
  293. 'update_time' => time()
  294. ];
  295. $result = $this->model->where('id', $id)->update($updateData);
  296. if ($result) {
  297. ActionLogModel::setTitle("订单完成支付");
  298. ActionLogModel::record();
  299. RedisService::keyDel("caches:orders:*");
  300. return ['code' => 0, 'msg' => '支付完成'];
  301. }
  302. return ['code' => 1, 'msg' => '操作失败'];
  303. }
  304. /**
  305. * 订单发货
  306. */
  307. public function deliverOrder()
  308. {
  309. $id = request()->post('id');
  310. $deliveryCompany = request()->post('delivery_company', '');
  311. $deliveryNo = request()->post('delivery_no', '');
  312. $deliveryCode = request()->post('delivery_code', '');
  313. if (!$id) {
  314. return ['code' => 1, 'msg' => '参数错误'];
  315. }
  316. $order = $this->model->find($id);
  317. if (!$order) {
  318. return ['code' => 1, 'msg' => '订单不存在'];
  319. }
  320. if ($order->status != 2) {
  321. return ['code' => 1, 'msg' => '订单状态不正确,只有已付款订单可以发货'];
  322. }
  323. if (!$deliveryNo) {
  324. return ['code' => 1, 'msg' => '请填写快递单号'];
  325. }
  326. $updateData = [
  327. 'status' => 3, // 已发货
  328. 'delivery_company' => $deliveryCompany,
  329. 'delivery_no' => $deliveryNo,
  330. 'delivery_code' => $deliveryCode,
  331. 'update_time' => time()
  332. ];
  333. $result = $this->model->where('id', $id)->update($updateData);
  334. if ($result) {
  335. ActionLogModel::setTitle("订单发货");
  336. ActionLogModel::record();
  337. RedisService::keyDel("caches:orders:*");
  338. return ['code' => 0, 'msg' => '发货成功'];
  339. }
  340. return ['code' => 1, 'msg' => '操作失败'];
  341. }
  342. /**
  343. * 订单完成(管理后台)
  344. */
  345. public function completeOrder()
  346. {
  347. $id = request()->post('id');
  348. if (!$id) {
  349. return ['code' => 1, 'msg' => '参数错误'];
  350. }
  351. $order = $this->model->find($id);
  352. if (!$order) {
  353. return ['code' => 1, 'msg' => '订单不存在'];
  354. }
  355. if ($order->status != 3) {
  356. return ['code' => 1, 'msg' => '订单状态不正确,只有已发货订单可以完成'];
  357. }
  358. // 调用用户端的订单完成方法,触发收益结算等业务逻辑
  359. $apiOrderService = \App\Services\Api\OrderService::make();
  360. $result = $apiOrderService->complete($order->user_id, $id);
  361. if ($result) {
  362. ActionLogModel::setTitle("订单完成");
  363. ActionLogModel::record();
  364. RedisService::keyDel("caches:orders:*");
  365. return ['code' => 0, 'msg' => '确认收货成功'];
  366. }
  367. // 获取错误信息
  368. $error = $apiOrderService->getError();
  369. return ['code' => 1, 'msg' => $error ?: '操作失败', 'error' => $error];
  370. }
  371. /**
  372. * 取消订单
  373. */
  374. public function cancelOrder()
  375. {
  376. $id = request()->post('id');
  377. $cancelReason = request()->post('cancel_reason', '');
  378. if (!$id) {
  379. return ['code' => 1, 'msg' => '参数错误'];
  380. }
  381. $order = $this->model->find($id);
  382. if (!$order) {
  383. return ['code' => 1, 'msg' => '订单不存在'];
  384. }
  385. if ($order->status != 1) {
  386. return ['code' => 1, 'msg' => '只有待付款订单可以取消'];
  387. }
  388. $updateData = [
  389. 'mark' => 0, // 标记为删除
  390. 'update_time' => time()
  391. ];
  392. $result = $this->model->where('id', $id)->update($updateData);
  393. if ($result) {
  394. ActionLogModel::setTitle("取消订单");
  395. ActionLogModel::record();
  396. RedisService::keyDel("caches:orders:*");
  397. return ['code' => 0, 'msg' => '订单已取消'];
  398. }
  399. return ['code' => 1, 'msg' => '操作失败'];
  400. }
  401. /**
  402. * 申请退款
  403. */
  404. public function applyRefund()
  405. {
  406. $id = request()->post('id');
  407. $afterType = request()->post('after_type', 2); // 默认退款
  408. $afterRealname = request()->post('after_realname', '');
  409. $afterPhone = request()->post('after_phone', '');
  410. $afterRemark = request()->post('after_remark', '');
  411. if (!$id) {
  412. return ['code' => 1, 'msg' => '参数错误'];
  413. }
  414. if (!$afterRealname) {
  415. return ['code' => 1, 'msg' => '请填写联系人姓名'];
  416. }
  417. if (!$afterPhone) {
  418. return ['code' => 1, 'msg' => '请填写联系电话'];
  419. }
  420. if (!$afterRemark) {
  421. return ['code' => 1, 'msg' => '请填写退款原因'];
  422. }
  423. $order = $this->model->find($id);
  424. if (!$order) {
  425. return ['code' => 1, 'msg' => '订单不存在'];
  426. }
  427. // 只有已付款、已发货、已完成的订单可以申请退款
  428. if (!in_array($order->status, [2, 3, 4])) {
  429. return ['code' => 1, 'msg' => '该订单状态不允许申请退款'];
  430. }
  431. if ($order->refund_status != 0) {
  432. return ['code' => 1, 'msg' => '该订单已申请过退款'];
  433. }
  434. $updateData = [
  435. 'refund_status' => 3, // 待审核
  436. 'after_type' => $afterType, // 1-售后,2-退款
  437. 'after_realname' => $afterRealname,
  438. 'after_phone' => $afterPhone,
  439. 'after_remark' => $afterRemark,
  440. 'update_time' => time()
  441. ];
  442. $result = $this->model->where('id', $id)->update($updateData);
  443. if ($result) {
  444. $typeText = $afterType == 1 ? '售后' : '退款';
  445. ActionLogModel::setTitle("申请{$typeText}");
  446. ActionLogModel::record();
  447. RedisService::keyDel("caches:orders:*");
  448. return ['code' => 0, 'msg' => "{$typeText}申请已提交"];
  449. }
  450. return ['code' => 1, 'msg' => '操作失败'];
  451. }
  452. /**
  453. * 同意退款(审核通过,状态变为已审核)
  454. */
  455. public function agreeRefund()
  456. {
  457. $id = request()->post('id');
  458. $refundRemark = request()->post('refund_remark', '');
  459. if (!$id) {
  460. return ['code' => 1, 'msg' => '参数错误'];
  461. }
  462. $order = $this->model->find($id);
  463. if (!$order) {
  464. return ['code' => 1, 'msg' => '订单不存在'];
  465. }
  466. if ($order->refund_status != 3) {
  467. return ['code' => 1, 'msg' => '该订单未申请退款或已处理'];
  468. }
  469. $updateData = [
  470. 'refund_status' => 2, // 已审核(待确认退款)
  471. 'refund_remark' => $refundRemark ?: '退款申请已通过,待确认退款',
  472. 'update_time' => time()
  473. ];
  474. $result = $this->model->where('id', $id)->update($updateData);
  475. if ($result) {
  476. ActionLogModel::setTitle("同意退款");
  477. ActionLogModel::record();
  478. RedisService::keyDel("caches:orders:*");
  479. return ['code' => 0, 'msg' => '已同意退款,请确认退款'];
  480. }
  481. return ['code' => 1, 'msg' => '操作失败'];
  482. }
  483. /**
  484. * 确认退款(最终完成退款)
  485. */
  486. public function confirmRefund()
  487. {
  488. $id = request()->post('id');
  489. $refundAmount = request()->post('refund_amount', 0);
  490. if (!$id) {
  491. return ['code' => 1, 'msg' => '参数错误'];
  492. }
  493. if ($refundAmount <= 0) {
  494. return ['code' => 1, 'msg' => '请输入退款金额'];
  495. }
  496. $order = $this->model->find($id);
  497. if (!$order) {
  498. return ['code' => 1, 'msg' => '订单不存在'];
  499. }
  500. // 允许待审核(3)和已审核(2)状态的订单进行退款
  501. if (!in_array($order->refund_status, [2, 3])) {
  502. return ['code' => 1, 'msg' => '该订单状态不允许退款'];
  503. }
  504. if ($refundAmount > $order->pay_total) {
  505. return ['code' => 1, 'msg' => '退款金额不能大于订单金额'];
  506. }
  507. // 使用事务
  508. DB::beginTransaction();
  509. try {
  510. // 调用支付服务退款
  511. $paymentService = \App\Services\PaymentService::make();
  512. $refundData = [
  513. 'money' => $refundAmount,
  514. 'pay_type' => $order->pay_type,
  515. 'order_no' => $order->order_no,
  516. 'out_trade_no' => $order->out_trade_no,
  517. 'transaction_id' => $order->transaction_id,
  518. 'remark' => '订单退款'
  519. ];
  520. $refundResult = $paymentService->refund($refundData, 'store');
  521. if (!$refundResult) {
  522. DB::rollBack();
  523. return ['code' => 1, 'msg' => '退款失败:' . $paymentService->getError() ?: '退款失败'];
  524. }
  525. // 更新订单状态
  526. $updateData = [
  527. 'refund_status' => 1, // 已退款
  528. 'refund_amount' => $refundAmount,
  529. 'update_time' => time()
  530. ];
  531. $result = $this->model->where('id', $id)->update($updateData);
  532. if ($result) {
  533. DB::commit();
  534. ActionLogModel::setTitle("确认退款");
  535. ActionLogModel::record();
  536. RedisService::keyDel("caches:orders:*");
  537. return ['code' => 0, 'msg' => '退款成功'];
  538. }
  539. DB::rollBack();
  540. return ['code' => 1, 'msg' => '更新订单状态失败'];
  541. } catch (\Exception $e) {
  542. DB::rollBack();
  543. return ['code' => 1, 'msg' => '退款失败:' . $e->getMessage()];
  544. }
  545. }
  546. /**
  547. * 拒绝退款
  548. */
  549. public function rejectRefund()
  550. {
  551. $id = request()->post('id');
  552. $refundRemark = request()->post('refund_remark', '');
  553. if (!$id) {
  554. return ['code' => 1, 'msg' => '参数错误'];
  555. }
  556. if (!$refundRemark) {
  557. return ['code' => 1, 'msg' => '请填写拒绝原因'];
  558. }
  559. $order = $this->model->find($id);
  560. if (!$order) {
  561. return ['code' => 1, 'msg' => '订单不存在'];
  562. }
  563. if ($order->refund_status != 3) {
  564. return ['code' => 1, 'msg' => '该订单未申请退款或已处理'];
  565. }
  566. $updateData = [
  567. 'refund_status' => 4, // 审核驳回
  568. 'refund_remark' => $refundRemark,
  569. 'update_time' => time()
  570. ];
  571. $result = $this->model->where('id', $id)->update($updateData);
  572. if ($result) {
  573. ActionLogModel::setTitle("拒绝退款");
  574. ActionLogModel::record();
  575. RedisService::keyDel("caches:orders:*");
  576. return ['code' => 0, 'msg' => '已拒绝退款'];
  577. }
  578. return ['code' => 1, 'msg' => '操作失败'];
  579. }
  580. /**
  581. * 订单统计
  582. * @param int $storeId 商户ID,0表示平台管理员查看全部数据
  583. */
  584. public function getStatistics($storeId = 0)
  585. {
  586. // 总订单数
  587. $total = $this->model->where('mark', 1)
  588. ->when($storeId > 0, function ($query) use ($storeId) {
  589. return $query->where('store_id', $storeId);
  590. })
  591. ->count();
  592. // 总交易额(已完成订单)
  593. $totalAmount = $this->model->where('mark', 1)
  594. ->where('status', 4)
  595. ->when($storeId > 0, function ($query) use ($storeId) {
  596. return $query->where('store_id', $storeId);
  597. })
  598. ->sum('pay_total');
  599. // 待处理订单(待付款 + 已付款)
  600. $pending = $this->model->where('mark', 1)
  601. ->whereIn('status', [1, 2])
  602. ->when($storeId > 0, function ($query) use ($storeId) {
  603. return $query->where('store_id', $storeId);
  604. })
  605. ->count();
  606. // 待退款订单
  607. $refunding = $this->model->where('mark', 1)
  608. ->where('refund_status', 3)
  609. ->when($storeId > 0, function ($query) use ($storeId) {
  610. return $query->where('store_id', $storeId);
  611. })
  612. ->count();
  613. return [
  614. 'code' => 0,
  615. 'msg' => '操作成功',
  616. 'data' => [
  617. 'total' => $total,
  618. 'totalAmount' => number_format($totalAmount, 2, '.', ''),
  619. 'pending' => $pending,
  620. 'refunding' => $refunding
  621. ]
  622. ];
  623. }
  624. }