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; } protected function formatListItem(array $row) { $typeMap = [ 'shipping' => '发货同步', 'confirm' => '确认收货同步', ]; $row['sync_type_text'] = $typeMap[$row['sync_type'] ?? ''] ?? ($row['sync_type'] ?? '-'); $status = $row['status'] ?? ''; $retryCount = intval($row['retry_count'] ?? 0); if ($status === 'success') { $row['status_text'] = '成功'; } elseif ($status === 'failed') { $row['status_text'] = '失败'; } elseif ($status === 'pending') { $row['status_text'] = $retryCount > 0 ? '待重试' : '处理中'; } else { $row['status_text'] = $status ?: '-'; } if (($row['sync_type'] ?? '') !== 'shipping') { $row['delivery_mode_text'] = '-'; } else { $modeMap = [1 => '统一发货', 2 => '分拆发货']; $row['delivery_mode_text'] = $modeMap[intval($row['delivery_mode'] ?? 0)] ?? ('模式' . $row['delivery_mode']); } if ($status === 'success') { $row['error_text'] = '-'; } else { $parts = []; if ($row['errcode'] !== null && $row['errcode'] !== '') { $parts[] = '[' . $row['errcode'] . ']'; } if (!empty($row['errmsg'])) { $parts[] = $row['errmsg']; } $row['error_text'] = $parts ? implode(' ', $parts) : '-'; } return $row; } 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(); foreach ($list as $key => $row) { $list[$key] = $this->formatListItem($row); } 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, $this->formatListItem($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()); } } }