OwnerService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. public function getSelectionId($column = ['id'])
  18. {
  19. return Owner::filterAuthorities()->select($column)->get();
  20. }
  21. /**
  22. *同步WMS全部货主至WAS
  23. */
  24. public function syncOwnersData()
  25. {
  26. $basCustomers = OracleBasCustomer::query()
  27. ->select('CUSTOMERID', 'DESCR_C')
  28. ->where('DESCR_C', 'not like', '%换ERP%')
  29. ->where('DESCR_C', 'not like', '%退仓%')
  30. ->where('CUSTOMER_TYPE', 'OW')
  31. ->get();
  32. $ownerCount = Owner::query()->count();
  33. if (count($basCustomers) == $ownerCount) return null;
  34. foreach ($basCustomers as $basCustomer) {
  35. $owner = Owner::query()->where('code', $basCustomer['customerid'])->first();
  36. if (!isset($owner)){
  37. Owner::query()->create([
  38. 'code' => $basCustomer['customerid'],
  39. 'name' => $basCustomer['descr_c'],
  40. 'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
  41. ]);
  42. }
  43. if ($owner['name']!=$basCustomer['descr_c']){
  44. $owner->update([
  45. 'code' => $basCustomer['customerid'],
  46. 'name' => $basCustomer['descr_c'],
  47. ]);
  48. }
  49. }
  50. $owners = Owner::query()->select('id', 'name')->get();
  51. return $owners;
  52. }
  53. public function first(array $params){
  54. $owner = Owner::query();
  55. foreach ($params as $column => $value){
  56. $owner->where($column, $value);
  57. }
  58. return $owner->first();
  59. }
  60. public function create(array $params){
  61. $owner = Owner::query()->create($params);
  62. return $owner;
  63. }
  64. public function firstOrCreate(array $params, array $values = null){
  65. if (!$values) return Owner::query()->firstOrCreate($params);
  66. return Owner::query()->firstOrCreate($params,$values);
  67. }
  68. public function 获取订单跟踪的货主(){
  69. return Owner::query()->with('orderTrackingOwner')->whereHas('orderTrackingOwner',function($query){
  70. $query->where('status','启用');
  71. })->get();
  72. }
  73. public function getByWmsOrders($orderHeaders){
  74. $customerIds = array_unique(data_get($orderHeaders,'*.customerid'));
  75. $customerIds = array_diff($customerIds,[null,'','*']);
  76. $owners = Owner::query()->whereIn('code',data_get($orderHeaders,'*.customerid'))->get();
  77. if($owners->count() < count($customerIds)){
  78. $customerIds = array_diff($customerIds,data_get($owners,'*.code'));
  79. $owner_list = $this->createByWmsCustomerIds($customerIds);
  80. $owners->concat($owner_list);
  81. }
  82. return $owners;
  83. }
  84. public function createByWmsCustomerIds($codes){
  85. if(!$codes) {return [];}
  86. $basCustomer = OracleBasCustomer::query()
  87. ->where('Customer_Type','OW')
  88. ->whereIn('CustomerID', $codes)
  89. ->get();
  90. $insert_params = [];
  91. $created_at = Carbon::now()->format('Y-m-d H:i:s');
  92. foreach ($basCustomer as $item) {
  93. $insert_params[] = [
  94. 'code' => $item->customerid,
  95. 'name' => $item->descr_c,
  96. 'created_at' => $created_at,
  97. ];
  98. }
  99. try {
  100. if (count($insert_params) > 0) {
  101. $this->insert($insert_params);
  102. LogService::log(__METHOD__, __FUNCTION__, '批量创建 owner error' . count($insert_params) . json_encode($insert_params) );
  103. }
  104. } catch (\Exception $e) {
  105. LogService::log(__METHOD__, __FUNCTION__, '批量创建 owner error' . json_encode($insert_params) . '||' . $e->getMessage() . '||' . $e->getTraceAsString());
  106. } finally {
  107. return Owner::query()->whereIn('code', $codes)->get();
  108. }
  109. }
  110. public function insert($fillables){
  111. return Owner::query()->insert($fillables);
  112. }
  113. public function getAuthorizedOwners(){
  114. $user = Auth::user();
  115. return Owner::query()->whereIn('id',$user->getPermittingOwnerIdsAttribute()??[])->get();
  116. }
  117. }