request->param(); $query = Db::name('qyt_invoice'); !empty($param['qyt_enterprise_id']) && $query->where('qyt_enterprise_id', (int) $param['qyt_enterprise_id']); !empty($param['month']) && $query->where('month', (string) $param['month']); !empty($param['mobile']) && $query->where('mobile', trim((string) $param['mobile'])); if (isset($param['has_invoice']) && $param['has_invoice'] !== '' && (int) $param['has_invoice'] >= 0) { (int) $param['has_invoice'] === 1 ? $query->where('invoice_count', '>', 0) : $query->where('invoice_count', 0); } $list = $query->order(['month' => 'desc', 'invoice_id' => 'desc'])->paginate(15)->toArray(); $names = EnterpriseModel::column('company_name', 'enterprise_id'); foreach ($list['data'] as &$row) { $row = QytInvoice::format($row); $row['company_name'] = $names[$row['qyt_enterprise_id']] ?? '(企业已删除)'; } return $this->renderSuccess(compact('list')); } public function sync(): Json { $month = trim((string) $this->request->param('month')); $mobile = trim((string) $this->request->param('mobile', '')); $enterpriseId = (int) $this->request->param('enterpriseId', 0); if ($enterpriseId > 0) { $enterprise = EnterpriseModel::detail($enterpriseId); if (!$enterprise) { return $this->renderError('企业不存在'); } $res = QytInvoice::sync($enterprise, $month, $mobile); return $res['ok'] ? $this->renderSuccess($res['row'], $res['message']) : $this->renderError($res['message']); } // 批量:所有启用企业的该月全量 $ok = $fail = 0; $errors = []; foreach (EnterpriseModel::where('is_enable', 1)->select() as $enterprise) { $res = QytInvoice::sync($enterprise, $month, ''); if ($res['ok']) { $ok++; } else { $fail++; $errors[] = $enterprise['company_name'] . ':' . $res['message']; } } $message = "批量同步完成:成功 {$ok} 家,失败 {$fail} 家" . ($errors ? '(' . implode(';', array_slice($errors, 0, 3)) . ')' : ''); return $fail && !$ok ? $this->renderError($message) : $this->renderSuccess([], $message); } }