allocatr/application/admin/command/AreaPinyinCommand.php
2025-03-02 18:59:24 +08:00

41 lines
1.2 KiB
PHP

<?php
namespace app\admin\command;
use Overtrue\Pinyin\Converter;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use Overtrue\Pinyin\Pinyin;
class AreaPinyinCommand extends Command
{
protected function configure()
{
$this->setName('area:pinyin')
->setDescription('批量生成区域名称的拼音');
}
protected function execute(Input $input, Output $output)
{
$pinyin = new Pinyin();
// 读取 `area` 表所有数据
$areas = Db::name('areas')->where('id','>',0)->select();
foreach ($areas as $area) {
$fullPinyin = strtolower(str_replace(' ','',$pinyin->name($area['merge_name'],Converter::TONE_STYLE_NONE))); // 全拼
$abbrPinyin = strtolower(str_replace(' ','',$pinyin->abbr($area['merge_name']))); // 首字母
// 更新数据库
Db::name('areas')
->where('id', $area['id'])
->update(['pinyin' => $fullPinyin, 'abbr' => $abbrPinyin]);
$output->writeln("更新: {$area['merge_name']} -> {$fullPinyin} ({$abbrPinyin})");
}
$output->writeln("拼音转换完成!");
}
}