vd/app/Domains/Real/Commands/Sync/CompanySync.php
2019-04-12 12:06:07 +08:00

54 lines
2.0 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\Repositories\CompanyRepository;
class CompanySync extends Command
{
protected $name = 'real:sync-company';
protected $description = '同步RD企业数据';
protected $ancestors = 'No00000000768';
public function handle()
{
// $sql = "SELECT c.id,c.custom_no AS sn,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')";
$descendants = DB::connection('real')->select("call proc_child('{$this->ancestors}')");
$descendants = array_pluck($descendants, 'custom_no');
$select = ['id', 'custom_no as sn', 'name', 'create_time as created_at', 'update_time as updated_at', 'del'];
$data = DB::connection('real')->table('jxc_custom')->select($select)
->where(function ($query) use ($descendants) {
$query->whereIn('custom_no', $descendants)->where('cLevel', 1);
})->orWhere('custom_no', 'No00000000836')
->get();
$array = [];
foreach ($data as $item) {
$item = (array)$item;
$item['name'] = trim($item['name']);
$item['created_at'] = is_numeric($item['created_at']) ? Carbon::createFromTimestamp($item['created_at']) : Carbon::parse($item['created_at']);
$item['updated_at'] = is_numeric($item['updated_at']) ? Carbon::createFromTimestamp($item['updated_at']) : Carbon::parse($item['updated_at']);
$item['deleted_at'] = $item['del'] ? $item['updated_at'] : null;
unset($item['del']);
$array[] = $item;
}
Company::upsert($array, 'id');
app(CompanyRepository::class)->forgetCached();
}
}