'充电底座', 100 => 'K霸宝', 101 => '充电宝', 102 => 'K霸宝 Blue', 103 => '密码线充', 104 => '蓝牙线充', 105 => '密码蓝牙线充', 107 => '车载充电器', ]; public function indexList(array $where): array { $page = max(1, (int) request()->input('page', 1)); $limit = min(100, max(1, (int) request()->input('limit', 20))); $result = $this->buildQuery($where) ->order('r.action_time', 'desc') ->order('r.id', 'desc') ->paginate([ 'page' => $page, 'list_rows' => $limit, ]) ->toArray(); $result['data'] = array_map(fn (array $row) => $this->formatRecord($row), $result['data'] ?? []); return $result; } public function export(array $where): Response { $rows = $this->buildQuery($where) ->order('r.action_time', 'desc') ->order('r.id', 'desc') ->select() ->toArray(); $data = []; foreach ($rows as $row) { $record = $this->formatRecord($row); $data[] = [ 'id' => $record['id'], 'operator_name' => $this->excelSafe($record['operator_name']), 'operator_phone' => $this->excelSafe($record['operator_phone']), 'action_name' => $record['action_name'], 'action_time' => $record['action_time'], 'merchant' => $this->excelSafe($record['merchant']), 'merchant_phone' => $this->excelSafe($record['merchant_phone']), 'qr_code' => $this->excelSafe($record['qr_code']), 'device_sn' => $this->excelSafe($record['device_sn']), 'type_name' => $record['type_name'], ]; } $fileName = '铺货记录_' . date('Ymd_His') . '.xlsx'; $writer = new OpenSpoutWriter($fileName); $writer->setWidth([10, 20, 16, 12, 20, 24, 16, 22, 24, 18]); $writer->setHeader([ '记录ID', '操作人', '操作人电话', '操作类型', '操作时间', '商户名称', '商户电话', '二维码/机器码', '设备SN码', '设备类型', ]); $writer->setData($data); return response()->download($writer->returnFile(), urlencode($fileName)); } private function buildQuery(array $where) { $query = Db::table('sa_merchant_device_binding_record')->alias('r') ->leftJoin('sa_device d', 'd.id = r.device_id') ->leftJoin('sa_merchant m', 'm.id = r.merchant_id') ->leftJoin('sa_system_user u', 'u.id = r.operator_id') ->field(implode(',', [ 'r.id', 'r.device_id', 'r.device_sn', 'r.depend_type', 'r.merchant_id', 'r.action_type', 'r.operator_id', 'r.action_time', 'r.create_time', 'd.goods_name', 'd.device_code', 'd.model', 'd.manage_agent_id', 'm.name AS merchant_name', 'm.phone AS merchant_phone', 'u.username AS operator_username', 'u.realname AS operator_realname', 'u.phone AS operator_phone', ])); $currentId = $this->currentAdminId(); if ($currentId !== 1) { $query->where('d.manage_agent_id', $currentId); } $actionType = trim((string) ($where['action_type'] ?? '')); if (in_array($actionType, ['1', '2'], true)) { $query->where('r.action_type', (int) $actionType); } $merchant = trim((string) ($where['merchant'] ?? '')); if ($merchant !== '') { $query->whereLike('m.name', '%' . $merchant . '%'); } $qrCode = trim((string) ($where['qr_code'] ?? '')); if ($qrCode !== '') { $query->whereLike('d.device_code', '%' . $qrCode . '%'); } $deviceSn = trim((string) ($where['device_sn'] ?? '')); if ($deviceSn !== '') { $query->whereLike('r.device_sn', '%' . $deviceSn . '%'); } if (($where['goods_type'] ?? '') !== '') { $query->where('r.depend_type', (int) $where['goods_type']); } return $query; } private function formatRecord(array $row): array { $type = (int) ($row['depend_type'] ?? 0); $actionType = (int) ($row['action_type'] ?? 0); $operatorId = (int) ($row['operator_id'] ?? 0); $operatorName = (string) (($row['operator_realname'] ?? '') ?: ($row['operator_username'] ?? '')); return [ 'id' => (int) $row['id'], 'device_id' => (int) ($row['device_id'] ?? 0), 'device_sn' => (string) ($row['device_sn'] ?? ''), 'qr_code' => (string) (($row['device_code'] ?? '') ?: ($row['device_sn'] ?? '')), 'goods_name' => (string) ($row['goods_name'] ?? ''), 'goods_type' => $type, 'type_name' => self::TYPE_NAMES[$type] ?? '其他设备', 'action_type' => $actionType, 'action_name' => $actionType === 1 ? '铺货' : ($actionType === 2 ? '解绑' : '未知'), 'action_time' => (string) ($row['action_time'] ?? ''), 'merchant_id' => (int) ($row['merchant_id'] ?? 0), 'merchant' => (string) (($row['merchant_name'] ?? '') ?: '--'), 'merchant_phone' => (string) ($row['merchant_phone'] ?? ''), 'operator_id' => $operatorId, 'operator_name' => $operatorName !== '' ? $operatorName : '用户#' . $operatorId, 'operator_username' => (string) ($row['operator_username'] ?? ''), 'operator_phone' => (string) ($row['operator_phone'] ?? ''), ]; } private function currentAdminId(): int { return (int) ($this->adminInfo['id'] ?? 0); } private function excelSafe(mixed $value): string { $value = (string) $value; return preg_match('/^[=+\-@]/u', $value) === 1 ? "'" . $value : $value; } }