OwnerService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. <?php
  2. namespace App\Services;
  3. use App\Authority;
  4. use App\Interfaces\UserFilter;
  5. use App\OracleBasCustomer;
  6. use App\Owner;
  7. use App\OwnerPriceDirectLogistic;
  8. use App\OwnerPriceExpress;
  9. use App\OwnerPriceLogistic;
  10. use App\OwnerPriceOperation;
  11. use App\OwnerPriceSystem;
  12. use App\OwnerStoragePriceModel;
  13. use App\Services\common\BatchUpdateService;
  14. use App\User;
  15. use Carbon\Carbon;
  16. use Doctrine\DBAL\Exception\DatabaseObjectExistsException;
  17. use Illuminate\Database\Eloquent\Builder;
  18. use Illuminate\Database\Eloquent\Model;
  19. use Illuminate\Support\Collection;
  20. use Illuminate\Support\Facades\Auth;
  21. use Illuminate\Support\Facades\Cache;
  22. use Illuminate\Support\Facades\DB;
  23. use App\Traits\ServiceAppAop;
  24. class OwnerService implements UserFilter
  25. {
  26. use ServiceAppAop;
  27. protected $modelClass=Owner::class;
  28. /** @var CacheService $cacheService */
  29. private $cacheService;
  30. function __construct(){
  31. $this->instant($this->cacheService,'CacheService');
  32. }
  33. /*
  34. * array | string $column
  35. * 默认一些select字段,可传递string 或 array来指定select字段
  36. */
  37. public function getIntersectPermitting(array $column = ['id', 'name'])
  38. {
  39. $ownerIds=app('UserService')->getPermittingOwnerIds(Auth::user());
  40. return $this->cacheService->getOrExecute('OwnersAll_IdName'.md5(json_encode($column).json_encode($ownerIds)),function()use($column,$ownerIds){
  41. if(empty($ownerIds))return new Collection();
  42. return Owner::query()->select($column)->whereIn('id', $ownerIds)->whereNull('deleted_at')->get();
  43. },config('cache.expirations.owners'));
  44. }
  45. public function getSelection($column = ['id'])
  46. {
  47. return $this->cacheService->getOrExecute('OwnersAll_'.md5(json_encode($column)),function()use($column){
  48. return Owner::filterAuthorities()->select($column)->get();
  49. },config('cache.expirations.owners'));
  50. }
  51. /**
  52. *同步WMS全部货主至WAS
  53. */
  54. public function syncOwnersData()
  55. {
  56. $basCustomers = OracleBasCustomer::query()
  57. ->select('CUSTOMERID', 'DESCR_C')
  58. ->where('DESCR_C', 'not like', '%换ERP%')
  59. ->where('DESCR_C', 'not like', '%退仓%')
  60. ->where('CUSTOMER_TYPE', 'OW')
  61. ->get();
  62. $ownerCount = Owner::query()->count();
  63. if (count($basCustomers) == $ownerCount) return null;
  64. foreach ($basCustomers as $basCustomer) {
  65. $owner = Owner::query()->where('code', $basCustomer['customerid'])->first();
  66. if (!isset($owner)){
  67. Owner::query()->create([
  68. 'code' => $basCustomer['customerid'],
  69. 'name' => $basCustomer['descr_c'],
  70. 'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
  71. ]);
  72. continue;
  73. }
  74. if ($owner['name']!=$basCustomer['descr_c']){
  75. $owner->update([
  76. 'code' => $basCustomer['customerid'],
  77. 'name' => $basCustomer['descr_c'],
  78. ]);
  79. }
  80. }
  81. $owners = Owner::query()->select('id', 'name')->get();
  82. return $owners;
  83. }
  84. public function first(array $params, array $rules =[]){
  85. return $this->cacheService->getOrExecute('OwnersFirst'.md5(json_encode($params),json_encode($rules)),function()use($params,$rules){
  86. $owner = Owner::query();
  87. foreach ($params as $column => $value){
  88. if (!isset($rules[$column]))$owner->where($column, $value);
  89. else{
  90. switch ($rules[$column]){
  91. case "or":
  92. $owner->orWhere($column, $value);
  93. break;
  94. }
  95. }
  96. }
  97. return $owner->first();
  98. },config('cache.expirations.rarelyChange'));
  99. }
  100. public function find($id, $with = [])
  101. {
  102. return Owner::query()->with($with)->find($id);
  103. }
  104. public function update(Owner $owner, array $values, array $related = [])
  105. {
  106. if ($related["ownerStoragePriceModels"] ?? false)$owner->ownerStoragePriceModels()->sync($related["ownerStoragePriceModels"]);
  107. return $owner->update($values);
  108. }
  109. public function create(array $params, array $related = []){
  110. /** @var Owner $owner */
  111. $owner = Owner::query()->create($params);
  112. if ($related["ownerStoragePriceModels"] ?? false)$owner->ownerStoragePriceModels()->syncWithoutDetaching($related["ownerStoragePriceModels"]);
  113. return $owner;
  114. }
  115. public function firstOrCreate(array $params, array $values = null){
  116. if (!$values) return Owner::query()->whereNull("deleted_at")->firstOrCreate($params);
  117. return Owner::query()->whereNull("deleted_at")->firstOrCreate($params,$values);
  118. }
  119. public function 获取订单跟踪的货主(){
  120. return Owner::query()->with('orderTrackingOwner')->whereHas('orderTrackingOwner',function($query){
  121. $query->where('status','启用');
  122. })->get();
  123. }
  124. public function getByWmsOrders($orderHeaders){
  125. $customerIds = array_unique(data_get($orderHeaders,'*.customerid'));
  126. $customerIds = array_diff($customerIds,[null,'','*']);
  127. $owners = Owner::query()->whereIn('code',$customerIds)->get();
  128. if($owners->count() < count($customerIds)){
  129. $customerIds = array_diff($customerIds,data_get($owners,'*.code'));
  130. $owner_list = $this->createByWmsCustomerIds($customerIds);
  131. $owners=$owners->concat($owner_list);
  132. }
  133. return $owners;
  134. }
  135. public function createByWmsCustomerIds($codes){
  136. if(!$codes) {return [];}
  137. $basCustomer = OracleBasCustomer::query()
  138. ->where('Customer_Type','OW')
  139. ->whereIn('CustomerID', $codes)
  140. ->get();
  141. $insert_params = [];
  142. $created_at = Carbon::now()->format('Y-m-d H:i:s');
  143. foreach ($basCustomer as $item) {
  144. $insert_params[] = [
  145. 'code' => $item->customerid,
  146. 'name' => $item->descr_c,
  147. 'created_at' => $created_at,
  148. ];
  149. }
  150. try {
  151. if (count($insert_params) > 0) {
  152. $this->insert($insert_params);
  153. app('LogService')->log(__METHOD__, __FUNCTION__, '批量创建 owner ' . count($insert_params) . json_encode($insert_params) );
  154. }
  155. } catch (\Exception $e) {
  156. app('LogService')->log(__METHOD__, __FUNCTION__, '批量创建 owner error' . json_encode($insert_params) . '||' . $e->getMessage() . '||' . $e->getTraceAsString());
  157. } finally {
  158. return Owner::query()->whereIn('code', $codes)->get();
  159. }
  160. }
  161. public function insert($fillables){
  162. return Owner::query()->insert($fillables);
  163. }
  164. public function getAuthorizedOwners(){
  165. $user = Auth::user();
  166. return Owner::query()->whereIn('id',app('UserService')->getPermittingOwnerIds($user)??[])->get();
  167. }
  168. public function get(array $params, array $withs = null, bool $authority = true, bool $notShowSoftDelete = false, $user = null)
  169. {
  170. /** @var User $user */
  171. if ($user==null)$user = Auth::user();
  172. return Cache::remember(
  173. 'owner_'.md5(json_encode($params).json_encode($withs).$authority.$notShowSoftDelete.json_encode($user))
  174. ,config('cache.expirations.rarelyChange')
  175. ,function()use($params,$withs,$authority,$notShowSoftDelete,$user){
  176. $query = Owner::query();
  177. if ($withs)$query->with($withs);
  178. if ($authority&&$user){
  179. $ids = $user->getPermittingOwnerIdsAttribute();
  180. $query->whereIn("id",$ids);
  181. }
  182. if ($notShowSoftDelete) $query->whereNull('deleted_at');
  183. $query = $this->query($query,$params);
  184. return $query->get();
  185. });
  186. }
  187. public function paginate(array $params, array $withs = null, bool $authority = true, bool $notShowSoftDelete = false)
  188. {
  189. /** @var User $user */
  190. $user = Auth::user();
  191. $query = Owner::query();
  192. if ($withs)$query->with($withs);
  193. if ($authority){
  194. $ids = $user->getPermittingOwnerIdsAttribute();
  195. $query->whereIn("id",$ids);
  196. }
  197. if ($notShowSoftDelete) $query->whereNull('deleted_at');
  198. $query = $this->query($query,$params)->orderByDesc("id");
  199. return $query->paginate($params["paginate"] ?? 50);
  200. }
  201. private function query(Builder $builder, array $params)
  202. {
  203. foreach ($params as $column => $param){
  204. if ($column == 'paginate' || $column == 'page' || !$param)continue;
  205. if ($param === true){
  206. $builder->whereNotNull($column);
  207. continue;
  208. }
  209. if ($param === false){
  210. $builder->whereNull($column);
  211. continue;
  212. }
  213. if ($column == 'created_at_start'){
  214. $builder->where("created_at",">=",$param.":00");
  215. continue;
  216. }
  217. if ($column == 'created_at_end'){
  218. $builder->where("created_at","<=",$param.":59");
  219. continue;
  220. }
  221. if ($column == 'contract_number'){
  222. $builder->whereHas("contracts",function ($query)use($param){
  223. /** @var Builder $query */
  224. $query->where("contract_number","like",$param."%");
  225. });
  226. continue;
  227. }
  228. if ($column == 'using_type'){
  229. $builder->whereHas("ownerStoragePriceModels",function ($query)use($param){
  230. /** @var Builder $query */
  231. $query->where("using_type",$param);
  232. });
  233. continue;
  234. }
  235. if ($column == 'customers'){
  236. if (is_array($param))$builder->whereIn('customer_id',$param);
  237. else $builder->where('customer_id',$param);
  238. continue;
  239. }
  240. if ($column == 'ids'){
  241. if (is_array($param))$builder->whereIn('id',$param);
  242. else $builder->where('id',$param);
  243. continue;
  244. }
  245. if ($column == 'owners'){
  246. if (is_array($param))$builder->whereIn('owner_id',$param);
  247. else $builder->where('owner_id',$param);
  248. continue;
  249. }
  250. if ($column == 'user_work_group'){
  251. $builder->where("user_workgroup_id",$param);
  252. continue;
  253. }
  254. if (is_array($param))$builder->whereIn($column,$param);
  255. else $builder->where($column,$param);
  256. }
  257. return $builder;
  258. }
  259. public function getOwnerByCodes($codes)
  260. {
  261. $collect = collect();
  262. if(count($codes) == 0)return $collect;
  263. foreach ($codes as $code) {
  264. $collect = $collect->push($this->getOwnerByCode($code));
  265. }
  266. return $collect;
  267. }
  268. public function getOwnerByCode($code){
  269. return Cache::remember("getOwnerByCode_{$code}", config('cache.expirations.owners'), function ()use($code){
  270. $owner = Owner::query()->where('code',$code)->first();
  271. if($owner) return $owner;
  272. $basCustomer = app('OracleBasCustomerService')->first(['Customer_Type'=>'OW','CustomerID'=>$code]);
  273. if(!$basCustomer)return null;
  274. if($basCustomer && $basCustomer['active_flag']=='Y') return Owner::query()
  275. ->create(['name'=>$basCustomer['descr_c'],'code'=>$basCustomer['customerid']]);
  276. $deleted_at=Carbon::now()->toDateTimeString();
  277. if($basCustomer && $basCustomer['active_flag']=='N') return Owner::query()
  278. ->create(['name'=>$basCustomer['descr_c'],'code'=>$basCustomer['customerid'],'deleted_at'=>$deleted_at]);
  279. });
  280. }
  281. public function codeGetOwner($code)
  282. {
  283. return app(CacheService::class)->getOrExecute("owner_".$code,function ()use($code){
  284. return Owner::query()->firstOrCreate(["code"=>$code],["code"=>$code,"name"=>$code]);
  285. });
  286. }
  287. /**
  288. * 向FLUX同步推送WAS本地录入信息
  289. *
  290. * @param array|Owner|integer $owner
  291. * @return bool
  292. */
  293. public function syncPush($owner)
  294. {
  295. if (is_array($owner)){
  296. $owner = new Owner();
  297. foreach ($owner as $column=>$value){
  298. $owner[$column] = $value;
  299. }
  300. }
  301. if (is_numeric($owner)){
  302. $owner = Owner::query()->find($owner);
  303. if (!$owner)return false;
  304. }
  305. $wms = DB::connection("oracle")->selectOne(DB::raw("SELECT CUSTOMERID FROM BAS_CUSTOMER WHERE CUSTOMER_TYPE = ? AND CUSTOMERID = ?"),["OW",$owner->code]);
  306. if (!$wms && $owner->code){
  307. $query = DB::raw(<<<sql
  308. INSERT INTO BAS_CUSTOMER(CUSTOMERID,CUSTOMER_TYPE,DESCR_C,ADDTIME,EDITTIME,ADDWHO)
  309. VALUES(?,?,?,TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),?)
  310. sql
  311. );
  312. $date = date('Y-m-d H:i:s');
  313. DB::connection("oracle")->insert($query,[$owner->code,'OW',$owner->name,$date,$date,'WAS-'.(Auth::user() ? Auth::user()['name'] : 'SYSTEM')]);
  314. }
  315. return true;
  316. }
  317. public function syncUpdate($owner)
  318. {
  319. if (is_array($owner)){
  320. $owner = new Owner();
  321. foreach ($owner as $column=>$value){
  322. $owner[$column] = $value;
  323. }
  324. }
  325. if (is_numeric($owner)){
  326. $owner = Owner::query()->find($owner);
  327. if (!$owner)return false;
  328. }
  329. $sql = DB::raw(<<<sql
  330. update BAS_CUSTOMER set ACTIVE_FLAG = ?,EDITTIME = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),EDITWHO = ? where CUSTOMERID = ? and CUSTOMER_TYPE = ?
  331. sql
  332. );
  333. $date = date('Y-m-d H:i:s');
  334. if ($owner && $owner->deleted_at){
  335. DB::connection("oracle")->update($sql,['N',$date,'WAS-'.(Auth::user() ? Auth::user()['name'] : 'SYSTEM'),$owner->code,'OW']);
  336. }
  337. if ($owner && $owner->deleted_at==null) {
  338. DB::connection("oracle")->update($sql,['Y',$date,'WAS-'.(Auth::user() ? Auth::user()['name'] : 'SYSTEM'),$owner->code,'OW']);
  339. }
  340. return true;
  341. }
  342. /**
  343. * 同步货主时创建权限
  344. *
  345. * @param array|Owner $owner
  346. */
  347. public function createAuthority($owner)
  348. {
  349. Authority::query()->create([
  350. 'name' => "_{$owner['id']}",
  351. 'alias_name' => "(货主:{$owner['name']})",
  352. 'remark' => "(key: _{$owner['id']})",
  353. ]);
  354. }
  355. /**
  356. * 停用货主时删除权限
  357. *
  358. * @param array|Owner $owner
  359. */
  360. public function deleteAuthority($owner)
  361. {
  362. $authorities = Authority::query()->where('name',"_{$owner['id']}")
  363. ->where("alias_name","like","(货主%")
  364. ->get(["id"]);
  365. $ids = array_column($authorities->toArray(),"id");
  366. DB::table("authority_role")->whereIn("id_authority",$ids)->delete();
  367. Authority::destroy($ids);
  368. }
  369. /**
  370. * 计费模型变动时更新货主中关联属性
  371. *
  372. * @param integer $ownerId
  373. *
  374. */
  375. public function refreshRelevance($ownerId)
  376. {
  377. $relevance = [];
  378. $sql = <<<sql
  379. SELECT 1 FROM owner_storage_price_models a
  380. LEFT JOIN owner_storage_price_model_owner b ON a.id = b.owner_storage_price_model_id
  381. LEFT JOIN owners c ON b.owner_id = c.id
  382. WHERE (a.operation IS NULL OR a.operation = '') AND c.id = ? LIMIT 1
  383. sql;
  384. if (DB::selectOne(DB::raw($sql),[$ownerId]))$relevance[] = 0;
  385. $sql = <<<sql
  386. SELECT 1 FROM owner_price_operations a
  387. LEFT JOIN owner_price_operation_owner b ON a.id = b.owner_price_operation_id
  388. LEFT JOIN owners c ON b.owner_id = c.id
  389. WHERE (a.operation IS NULL OR a.operation = '') AND c.id = ? LIMIT 1
  390. sql;
  391. if (DB::selectOne(DB::raw($sql),[$ownerId]))$relevance[] = 1;
  392. $sql = <<<sql
  393. SELECT 1 FROM owner_price_expresses a
  394. LEFT JOIN owner_price_express_owner b ON a.id = b.owner_price_express_id
  395. LEFT JOIN owners c ON b.owner_id = c.id
  396. WHERE (a.operation IS NULL OR a.operation = '') AND c.id = ? LIMIT 1
  397. sql;
  398. if (DB::selectOne(DB::raw($sql),[$ownerId]))$relevance[] = 2;
  399. $sql = <<<sql
  400. SELECT 1 FROM owner_price_logistics a
  401. LEFT JOIN owner_price_logistic_owner b ON a.id = b.owner_price_logistic_id
  402. LEFT JOIN owners c ON b.owner_id = c.id
  403. WHERE (a.operation IS NULL OR a.operation = '') AND c.id = ? LIMIT 1
  404. sql;
  405. if (DB::selectOne(DB::raw($sql),[$ownerId]))$relevance[] = 3;
  406. $sql = <<<sql
  407. SELECT 1 FROM owner_price_direct_logistics a
  408. LEFT JOIN owner_price_direct_logistic_owner b ON a.id = b.owner_price_direct_logistic_id
  409. LEFT JOIN owners c ON b.owner_id = c.id
  410. WHERE (a.operation IS NULL OR a.operation = '') AND c.id = ? LIMIT 1
  411. sql;
  412. if (DB::selectOne(DB::raw($sql),[$ownerId]))$relevance[] = 4;
  413. $sql = <<<sql
  414. SELECT 1 FROM owner_price_systems a LEFT JOIN owners b ON a.owner_id = b.id
  415. WHERE b.id = ? LIMIT 1
  416. sql;
  417. if (DB::selectOne(DB::raw($sql),[$ownerId]))$relevance[] = 5;
  418. Owner::query()->where("id",$ownerId)->update(["relevance"=>$relevance]);
  419. }
  420. /**
  421. * 税率变更时附加税率
  422. *
  423. * @param Owner|\stdClass $owner
  424. */
  425. public function attachTaxRate(Owner $owner)
  426. {
  427. OwnerStoragePriceModel::query()->whereHas("owners",function (Builder $query)use($owner){
  428. $query->where("id",$owner->id);
  429. })->whereNull("tax_rate_id")->update(["tax_rate_id"=>$owner->tax_rate_id]);
  430. OwnerPriceOperation::query()->whereHas("owners",function (Builder $query)use($owner){
  431. $query->where("id",$owner->id);
  432. })->whereNull("tax_rate_id")->update(["tax_rate_id"=>$owner->tax_rate_id]);
  433. OwnerPriceExpress::query()->whereHas("owners",function (Builder $query)use($owner){
  434. $query->where("id",$owner->id);
  435. })->whereNull("tax_rate_id")->update(["tax_rate_id"=>$owner->tax_rate_id]);
  436. OwnerPriceLogistic::query()->whereHas("owners",function (Builder $query)use($owner){
  437. $query->where("id",$owner->id);
  438. })->whereNull("tax_rate_id")->update(["tax_rate_id"=>$owner->tax_rate_id]);
  439. OwnerPriceDirectLogistic::query()->whereHas("owners",function (Builder $query)use($owner){
  440. $query->where("id",$owner->id);
  441. })->whereNull("tax_rate_id")->update(["tax_rate_id"=>$owner->tax_rate_id]);
  442. OwnerPriceSystem::query()->where("owner_id",$owner->id)->whereNull("tax_rate_id")
  443. ->update(["tax_rate_id"=>$owner->tax_rate_id]);
  444. }
  445. /**
  446. * 税率变更时取消税率
  447. *
  448. * @param Owner|\stdClass $owner
  449. */
  450. public function removeTaxRate(Owner $owner)
  451. {
  452. OwnerStoragePriceModel::query()->whereHas("owners",function (Builder $query)use($owner){
  453. $query->where("id",$owner->id);
  454. })->update(["tax_rate_id"=>null]);
  455. OwnerPriceOperation::query()->whereHas("owners",function (Builder $query)use($owner){
  456. $query->where("id",$owner->id);
  457. })->update(["tax_rate_id"=>null]);
  458. OwnerPriceExpress::query()->whereHas("owners",function (Builder $query)use($owner){
  459. $query->where("id",$owner->id);
  460. })->update(["tax_rate_id"=>null]);
  461. OwnerPriceLogistic::query()->whereHas("owners",function (Builder $query)use($owner){
  462. $query->where("id",$owner->id);
  463. })->update(["tax_rate_id"=>null]);
  464. OwnerPriceDirectLogistic::query()->whereHas("owners",function (Builder $query)use($owner){
  465. $query->where("id",$owner->id);
  466. })->update(["tax_rate_id"=>null]);
  467. OwnerPriceSystem::query()->where("owner_id",$owner->id)
  468. ->update(["tax_rate_id"=>null]);
  469. }
  470. /**
  471. * 获取税率 或 税费
  472. *
  473. * @param Model|\stdClass $model
  474. * @param int $ownerId
  475. * @param float|null $money
  476. *
  477. * @return float|null
  478. */
  479. public function getTaxRateFee(Model $model, int $ownerId, ?float $money = null):?float
  480. {
  481. $taxRate = null;
  482. if ($model->tax_rate_id){
  483. $model->loadMissing("taxRate");
  484. $taxRate = $model->taxRate;
  485. }
  486. if (!$taxRate){
  487. /** @var Model|\stdClass $owner */
  488. $owner = new Owner();
  489. $owner->id = $ownerId;
  490. $owner->load("taxRate");
  491. $taxRate = $owner->taxRate;
  492. }
  493. if (!$taxRate)return null;
  494. if ($money===null)return $taxRate->value;
  495. return $money*($taxRate->value/100);
  496. }
  497. public function changeManualBackStatus($id,$isManual)
  498. {
  499. $owner=Owner::query()->find($id);
  500. if ($isManual==0)$owner->update(['is_manual_back'=>1]);
  501. else $owner->update(['is_manual_back'=>0]);
  502. return $owner;
  503. }
  504. public function changeIntervalTime($id,$intervalTime)
  505. {
  506. $owner=Owner::query()->find($id);
  507. $owner->update(['interval_time'=>$intervalTime]);
  508. return $owner;
  509. }
  510. function getIdArr(?int $userId = null): array
  511. {
  512. if (!$userId)$userId = Auth::id();
  513. return array_column($this->getQuery($userId)->get()->toArray(),"id");
  514. }
  515. function getQuery(?int $userId = null): Builder
  516. {
  517. if (!$userId)$userId = Auth::id();
  518. $query = Owner::query()->select("owners.id");
  519. if (!app("UserService")->checkAdminIdentity($userId)){
  520. $query->whereHas("roles",function ($query)use($userId){
  521. $query->whereHas("users",function ($query)use($userId){
  522. $query->where("users.id",$userId);
  523. });
  524. });
  525. }
  526. return $query->whereNull("deleted_at");
  527. }
  528. }