| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- <?php
- /** Additive checkpoint: php scripts/migrate_water_agents.php --apply. Old tables remain intact. */
- require dirname(__DIR__) . '/vendor/autoload.php';
- \Dotenv\Dotenv::createUnsafeMutable(dirname(__DIR__))->load();
- \support\App::loadAllConfig(['route']);
- \Webman\ThinkOrm\ThinkOrm::start(null);
- use support\think\Db;
- if (!in_array('--apply', $argv, true)) {
- echo "Plan: create water agent directory and copy explicit legacy profiles; no deletion or permission changes.\n";
- exit(0);
- }
- Db::execute(file_get_contents(dirname(__DIR__) . '/plugin/saiadmin/db/20260909_water_agent.sql'));
- $tables = array_map(fn($row) => array_values($row)[0], Db::query('SHOW TABLES'));
- $imported = 0;
- if (in_array('sa_agent_profile', $tables, true)) {
- Db::transaction(function () use (&$imported, $tables) {
- foreach (Db::table('sa_agent_profile')->select()->toArray() as $profile) {
- $userId = (int)$profile['agent_id'];
- if (Db::table('sa_water_agent')->where('user_id', $userId)->find()) continue;
- $user = Db::table('sa_system_user')->where('id', $userId)->find();
- if (!$user) throw new RuntimeException('Legacy agent has no identity record; review before cleanup');
- $contract = in_array('sa_agent_contract', $tables, true)
- ? Db::table('sa_agent_contract')->where('agent_id', $userId)->find() : null;
- $now = date('Y-m-d H:i:s');
- Db::table('sa_water_agent')->insert([
- 'user_id'=>$userId, 'name'=>$user['realname'] ?: $user['username'],
- 'contact'=>$user['realname'] ?: $user['username'], 'phone'=>$user['phone'] ?? '',
- 'region'=>implode(' ', array_filter([$profile['province'], $profile['city'], $profile['district']])),
- 'address'=>$profile['company_address'] ?? '',
- 'status'=>empty($user['delete_time']) && (int)$user['status'] === 1 ? 1 : 2,
- 'contract_urls'=>$contract['contract_urls'] ?? null,
- 'identity_urls'=>$contract['identity_urls'] ?? null,
- 'extra_urls'=>$contract['extra_urls'] ?? null,
- 'created_at'=>$profile['create_time'] ?: $now, 'updated_at'=>$now,
- ]);
- $imported++;
- }
- });
- }
- echo "Water agent migration complete: imported {$imported}; total ".Db::table('sa_water_agent')->count().". No legacy data deleted.\n";
|