diff --git a/backend/app/Command/AppCompanySync.php b/backend/app/Command/AppCompanySync.php index 56d185a..7c2e879 100644 --- a/backend/app/Command/AppCompanySync.php +++ b/backend/app/Command/AppCompanySync.php @@ -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("公司数据同步失败,数据更新已撤销"); + + } } }