OwnerService.php 3.7 KB

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