vd/app/Domains/Real/Commands/Sync/CompanySync.php
2018-11-28 19:09:47 +08:00

42 lines
1.3 KiB
PHP

<?php
namespace App\Domains\Real\Commands\Sync;
use Carbon\Carbon;
use App\Models\Real\Company;
use Illuminate\Support\Facades\DB;
use App\Domains\Real\Services\CommonService;
use App\Domains\Real\Repositories\CompanyRepository;
class CompanySync extends Command
{
protected $name = 'real:sync-company';
protected $description = '同步RD企业数据';
public function handle()
{
$datetime = $this->getDateTime();
$sql = "SELECT c.custom_no AS id,c.name,c.create_time AS created_at,c.del FROM jxc_user a
INNER JOIN jxc_user_custom_relation b ON a.id=b.uid
INNER JOIN jxc_custom c ON b.custom_no=c.custom_no
WHERE a.parent_user IN (SELECT id FROM jxc_user WHERE parent_user=3 AND role_tag='normal')";
$data = DB::connection('real')->select($sql);
foreach ($data as &$item) {
$item = (array)$item;
$item['id'] = CommonService::parseCompanyId($item['id']);
$item['created_at'] = Carbon::parse($item['created_at']);
$item['updated_at'] = date('Y-m-d H:i:s');
$item['deleted_at'] = $item['del'] ? date('Y-m-d H:i:s') : null;
unset($item['del']);
}
Company::upsert($data, 'id');
app(CompanyRepository::class)->forgetCached();
}
}