add company sync

This commit is contained in:
2025-11-11 09:26:23 +08:00
parent 34415a3a7f
commit e46da76458
+38 -2
View File
@@ -8,6 +8,10 @@ use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;
use App\Platform\Tools\Request\CompanyRequest;
use App\Model\Company;
use Hyperf\DbConnection\Db;
use App\Utils\Log;
use Exception;
#[Command]
class AppCompanySync extends HyperfCommand
@@ -27,8 +31,40 @@ class AppCompanySync extends HyperfCommand
{
$companies = CompanyRequest::all();
dump($companies->all());
try{
Db::beginTransaction();
$this->line('Hello Hyperf!', 'info');
$companies->each(function($el){
$exist_company = Company::find($el['id']);
if($exist_company){
# @attention update 方法不会进行 cast 类型转换,执行时需要注意
$exist_company->update([
'name' => $el['name'],
'enabled' => $el['isEnabled']
] );
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("公司数据同步失败,数据更新已撤销");
}
}
}