| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- // +----------------------------------------------------------------------
- // | saiadmin [ saiadmin快速开发框架 ]
- // +----------------------------------------------------------------------
- namespace plugin\saiadmin\app\logic\device;
- use plugin\saiadmin\basic\think\BaseLogic;
- use plugin\saiadmin\service\OpenSpoutWriter;
- use support\Response;
- use support\think\Db;
- /**
- * 设备铺货记录列表、搜索与导出。
- *
- * 业务语义迁移自旧项目 agentapi/merchant/bind_device_records:
- * action_type 1 为铺货,2 为解绑;查询范围按设备当前管理代理隔离。
- */
- class DistributionRecordLogic extends BaseLogic
- {
- private const TYPE_NAMES = [
- 1 => '充电底座',
- 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;
- }
- }
|