add company sync
This commit is contained in:
@@ -8,6 +8,10 @@ use Hyperf\Command\Command as HyperfCommand;
|
|||||||
use Hyperf\Command\Annotation\Command;
|
use Hyperf\Command\Annotation\Command;
|
||||||
use Psr\Container\ContainerInterface;
|
use Psr\Container\ContainerInterface;
|
||||||
use App\Platform\Tools\Request\CompanyRequest;
|
use App\Platform\Tools\Request\CompanyRequest;
|
||||||
|
use App\Model\Company;
|
||||||
|
use Hyperf\DbConnection\Db;
|
||||||
|
use App\Utils\Log;
|
||||||
|
use Exception;
|
||||||
|
|
||||||
#[Command]
|
#[Command]
|
||||||
class AppCompanySync extends HyperfCommand
|
class AppCompanySync extends HyperfCommand
|
||||||
@@ -27,8 +31,40 @@ class AppCompanySync extends HyperfCommand
|
|||||||
{
|
{
|
||||||
$companies = CompanyRequest::all();
|
$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("公司数据同步失败,数据更新已撤销");
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user