add company sync
This commit is contained in:
@@ -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();
|
||||
|
||||
$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("公司数据同步失败,数据更新已撤销");
|
||||
|
||||
$this->line('Hello Hyperf!', 'info');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user