2025-11-10 16:49:05 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Command;
|
|
|
|
|
|
|
|
|
|
use Hyperf\Command\Command as HyperfCommand;
|
|
|
|
|
use Hyperf\Command\Annotation\Command;
|
|
|
|
|
use Psr\Container\ContainerInterface;
|
2025-11-10 16:57:45 +08:00
|
|
|
use App\Platform\Tools\Request\CompanyRequest;
|
2025-11-11 09:26:23 +08:00
|
|
|
use App\Model\Company;
|
|
|
|
|
use Hyperf\DbConnection\Db;
|
|
|
|
|
use App\Utils\Log;
|
|
|
|
|
use Exception;
|
2025-11-10 16:49:05 +08:00
|
|
|
|
|
|
|
|
#[Command]
|
|
|
|
|
class AppCompanySync extends HyperfCommand
|
|
|
|
|
{
|
|
|
|
|
public function __construct(protected ContainerInterface $container)
|
|
|
|
|
{
|
|
|
|
|
parent::__construct('app:company:sync');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function configure()
|
|
|
|
|
{
|
|
|
|
|
parent::configure();
|
|
|
|
|
$this->setDescription('Sync companies from Tools Dashboard');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
2025-11-10 16:57:45 +08:00
|
|
|
$companies = CompanyRequest::all();
|
|
|
|
|
|
2025-11-11 09:26:23 +08:00
|
|
|
try{
|
|
|
|
|
Db::beginTransaction();
|
2025-11-10 16:49:05 +08:00
|
|
|
|
2025-11-11 09:26:23 +08:00
|
|
|
$companies->each(function($el){
|
|
|
|
|
$exist_company = Company::find($el['id']);
|
|
|
|
|
if($exist_company){
|
2025-11-12 09:35:34 +08:00
|
|
|
# @attention update 方法不会进行 cast 类型转换,执行时需要注意, tools 数据中 isEnabled 存在值为 空字符串的情况
|
2025-11-11 09:26:23 +08:00
|
|
|
$exist_company->update([
|
|
|
|
|
'name' => $el['name'],
|
2025-11-12 09:35:34 +08:00
|
|
|
'enabled' => \boolval($el['isEnabled']) === true ? true : false,
|
2025-11-11 09:26:23 +08:00
|
|
|
] );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Company::create([
|
|
|
|
|
'id' => $el['id'],
|
|
|
|
|
'name' => $el['name'],
|
|
|
|
|
'enabled' => $el['isEnabled']
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Db::commit();
|
|
|
|
|
|
|
|
|
|
$this->info("公司数据同步已完成");
|
|
|
|
|
|
|
|
|
|
} catch(Exception $e){
|
|
|
|
|
|
|
|
|
|
Db::rollBack();
|
|
|
|
|
dump($e->getMessage());
|
|
|
|
|
Log::error($e->getMessage());
|
|
|
|
|
$this->error($e->getMessage());
|
|
|
|
|
$this->info("公司数据同步失败,数据更新已撤销");
|
|
|
|
|
|
|
|
|
|
}
|
2025-11-12 09:35:34 +08:00
|
|
|
|
2025-11-10 16:49:05 +08:00
|
|
|
}
|
|
|
|
|
}
|