OwnerService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Services;
  3. use App\OracleBasCustomer;
  4. use App\Owner;
  5. use Carbon\Carbon;
  6. use Illuminate\Support\Facades\Auth;
  7. Class OwnerService
  8. {
  9. /*
  10. * array | string $column
  11. * 默认一些select字段,可传递string 或 array来指定select字段
  12. */
  13. public function getSelection($column = ['id', 'name'])
  14. {
  15. return Owner::filterAuthorities()->select($column)->get();
  16. }
  17. /**
  18. *同步WMS全部货主至WAS
  19. */
  20. public function syncOwnersData()
  21. {
  22. $basCustomers = OracleBasCustomer::query()
  23. ->select('CUSTOMERID', 'DESCR_C')
  24. ->where('DESCR_C', 'not like', '%换ERP%')
  25. ->where('DESCR_C', 'not like', '%退仓%')
  26. ->where('CUSTOMER_TYPE', 'OW')
  27. ->get();
  28. $ownerCount = Owner::count();
  29. if (count($basCustomers) == $ownerCount) return null;
  30. foreach ($basCustomers as $basCustomer) {
  31. $owner = Owner::where('code', $basCustomer['customerid'])->first();
  32. if (!isset($owner))
  33. Owner::query()->create([
  34. 'code' => $basCustomer['customerid'],
  35. 'name' => $basCustomer['descr_c'],
  36. 'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
  37. ]);
  38. }
  39. $owners = Owner::query()->select('id', 'name')->get();
  40. return $owners;
  41. }
  42. public function first(array $params){
  43. $owner = Owner::query();
  44. foreach ($params as $column => $value){
  45. $owner->where($column, $value);
  46. }
  47. return $owner->first();
  48. }
  49. public function create(array $params){
  50. $owner = Owner::query()->create($params);
  51. return $owner;
  52. }
  53. public function firstOrCreate(array $params, array $values = null){
  54. if (!$values) return Owner::query()->firstOrCreate($params);
  55. return Owner::query()->firstOrCreate($params,$values);
  56. }
  57. // XXX 代码逻辑有待纠正
  58. public function 获取WMS订单列中对应的WAS货主列($WMSOrderHeaders)
  59. {
  60. $customerIds = array_unique(data_get($WMSOrderHeaders, '*.customerid'));
  61. $owners = Owner::query()->whereIn('code', $customerIds)->get();
  62. $codes = data_get($owners, '*.code');
  63. $codeDiff = array_diff($customerIds, $codes);
  64. $logs = [];
  65. $errs = [];
  66. if (count($codes) > count($customerIds)) {
  67. $basCustomer = OracleBasCustomer::query()->whereIn('customerid', $codeDiff)->get();
  68. foreach ($basCustomer as $customer) {
  69. if($customer['descr_c'] ?? false && $customer['customerid'] && false){
  70. try {
  71. $owner = Owner::query()->create(['name' => $customer['descr_c'], 'code' => $customer['customerid']]);
  72. $owners->push($owner);
  73. array_push($logs, ['info' => '创建「' . json_encode($owner) . '」']);
  74. } catch (\Exception $e) {
  75. array_push($errs, ['info' => '创建「' . json_encode($errs) . '」']);
  76. }
  77. }
  78. }
  79. }
  80. if(count($logs)>0)
  81. LogService::log(__METHOD__, __FUNCTION__, '批量创建客户「:' . json_encode($logs) . '」', Auth::user()['id']);
  82. if(count($errs)>0)
  83. LogService::log(__METHOD__, __FUNCTION__, '批量创建客户失败「:' . json_encode($errs) . '」', Auth::user()['id']);
  84. return $owners;
  85. }
  86. public function 获取订单跟踪的货主(){
  87. return Owner::query()->with('orderTrackingOwner')->whereHas('orderTrackingOwner',function($query){
  88. $query->where('status','启用');
  89. })->get();
  90. }
  91. public function getAuthorizedOwners(){
  92. $user = Auth::user();
  93. return Owner::query()->whereIn('id',$user->getPermittingOwnerIdsAttribute()??[])->get();
  94. }
  95. }