where('status', $status); } if ($syncType !== '') { $query->where('sync_type', $syncType); } if ($orderSn !== '') { $query->where('order_sn', 'like', '%' . $orderSn . '%'); } return $query; } protected function getStats() { $rows = Db::name('shopro_wechat_trade_sync') ->field('status, COUNT(*) AS cnt') ->group('status') ->select(); $stats = ['success' => 0, 'pending' => 0, 'failed' => 0, 'total' => 0]; foreach ($rows as $row) { $key = $row['status'] ?? ''; if (isset($stats[$key])) { $stats[$key] = intval($row['cnt']); } $stats['total'] += intval($row['cnt']); } return $stats; } public function index() { if ($this->request->isAjax()) { $page = max(1, intval($this->request->get('page', 1))); $limit = max(1, min(100, intval($this->request->get('limit', 20)))); $status = trim($this->request->get('status', '')); $syncType = trim($this->request->get('sync_type', '')); $orderSn = trim($this->request->get('order_sn', '')); $query = $this->buildQuery($status, $syncType, $orderSn)->order('id', 'desc'); $total = (clone $query)->count(); $list = $query->page($page, $limit)->select(); return json(['total' => $total, 'rows' => $list]); } $this->view->assign('stats', $this->getStats()); return $this->view->fetch(); } public function detail() { $id = intval($this->request->get('id')); $row = Db::name('shopro_wechat_trade_sync')->where('id', $id)->find(); if (!$row) { $this->error('记录不存在'); } $this->success('', null, $row); } public function retry() { $id = intval($this->request->post('id')); $row = Db::name('shopro_wechat_trade_sync')->where('id', $id)->find(); if (!$row) { $this->error('记录不存在'); } if ($row['status'] === 'success') { $this->error('该记录已同步成功,无需重试'); } try { $service = new WxShippingService(); if ($row['sync_type'] === 'confirm') { $service->syncOrderConfirm($row['order_id']); } else { $service->syncOrderShipping($row['order_id'], $row['order_item_id']); } $this->success('重试成功'); } catch (\Exception $e) { $this->error('重试失败:' . $e->getMessage()); } } }