OwnerService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace App\Services;
  3. use App\OracleBasCustomer;
  4. use App\Owner;
  5. use App\User;
  6. use Carbon\Carbon;
  7. use Illuminate\Support\Facades\Auth;
  8. Class OwnerService
  9. {
  10. /*
  11. * array | string $column
  12. * 默认一些select字段,可传递string 或 array来指定select字段
  13. */
  14. public function getSelection($column = ['id', 'name'])
  15. {
  16. return Owner::filterAuthorities()->select($column)->get();
  17. }
  18. public function getSelectionId($column = ['id'])
  19. {
  20. return Owner::filterAuthorities()->select($column)->get();
  21. }
  22. /**
  23. *同步WMS全部货主至WAS
  24. */
  25. public function syncOwnersData()
  26. {
  27. $basCustomers = OracleBasCustomer::query()
  28. ->select('CUSTOMERID', 'DESCR_C')
  29. ->where('DESCR_C', 'not like', '%换ERP%')
  30. ->where('DESCR_C', 'not like', '%退仓%')
  31. ->where('CUSTOMER_TYPE', 'OW')
  32. ->get();
  33. $ownerCount = Owner::query()->count();
  34. if (count($basCustomers) == $ownerCount) return null;
  35. foreach ($basCustomers as $basCustomer) {
  36. $owner = Owner::query()->where('code', $basCustomer['customerid'])->first();
  37. if (!isset($owner)){
  38. Owner::query()->create([
  39. 'code' => $basCustomer['customerid'],
  40. 'name' => $basCustomer['descr_c'],
  41. 'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
  42. ]);
  43. }
  44. if ($owner['name']!=$basCustomer['descr_c']){
  45. $owner->update([
  46. 'code' => $basCustomer['customerid'],
  47. 'name' => $basCustomer['descr_c'],
  48. ]);
  49. }
  50. }
  51. $owners = Owner::query()->select('id', 'name')->get();
  52. return $owners;
  53. }
  54. public function first(array $params, array $rules =[]){
  55. $owner = Owner::query();
  56. foreach ($params as $column => $value){
  57. if (!isset($rules[$column]))$owner->where($column, $value);
  58. else{
  59. switch ($rules[$column]){
  60. case "or":
  61. $owner->orWhere($column, $value);
  62. break;
  63. }
  64. }
  65. }
  66. return $owner->first();
  67. }
  68. public function create(array $params){
  69. $owner = Owner::query()->create($params);
  70. return $owner;
  71. }
  72. public function firstOrCreate(array $params, array $values = null){
  73. if (!$values) return Owner::query()->firstOrCreate($params);
  74. return Owner::query()->firstOrCreate($params,$values);
  75. }
  76. public function 获取订单跟踪的货主(){
  77. return Owner::query()->with('orderTrackingOwner')->whereHas('orderTrackingOwner',function($query){
  78. $query->where('status','启用');
  79. })->get();
  80. }
  81. public function getByWmsOrders($orderHeaders){
  82. $customerIds = array_unique(data_get($orderHeaders,'*.customerid'));
  83. $customerIds = array_diff($customerIds,[null,'','*']);
  84. $owners = Owner::query()->whereIn('code',data_get($orderHeaders,'*.customerid'))->get();
  85. if($owners->count() < $customerIds){
  86. $customerIds = array_diff($customerIds,data_get($owners,'*.code'));
  87. $owner_list = $this->createByWmsCustomerIds($customerIds);
  88. $owners->concat($owner_list);
  89. }
  90. return $owners;
  91. }
  92. public function createByWmsCustomerIds($codes){
  93. if(!$codes) {return [];}
  94. $basCustomer = OracleBasCustomer::query()
  95. ->where('Customer_Type','OW')
  96. ->whereIn('CustomerID', $codes)
  97. ->get();
  98. $insert_params = [];
  99. $created_at = Carbon::now()->format('Y-m-d H:i:s');
  100. foreach ($basCustomer as $item) {
  101. $insert_params[] = [
  102. 'code' => $item->customerid,
  103. 'name' => $item->descr_c,
  104. 'created_at' => $created_at,
  105. ];
  106. }
  107. try {
  108. if (count($insert_params) > 0) {
  109. $this->insert($insert_params);
  110. LogService::log(__METHOD__, __FUNCTION__, '批量创建 owner error' . count($insert_params) . json_encode($insert_params) );
  111. }
  112. } catch (\Exception $e) {
  113. LogService::log(__METHOD__, __FUNCTION__, '批量创建 owner error' . json_encode($insert_params) . '||' . $e->getMessage() . '||' . $e->getTraceAsString());
  114. } finally {
  115. return Owner::query()->whereIn('code', $codes)->get();
  116. }
  117. }
  118. public function insert($fillables){
  119. return Owner::query()->insert($fillables);
  120. }
  121. public function getAuthorizedOwners(){
  122. $user = Auth::user();
  123. return Owner::query()->whereIn('id',$user->getPermittingOwnerIdsAttribute()??[])->get();
  124. }
  125. public function get()
  126. {
  127. /** @var User $user */
  128. $user = Auth::user();
  129. $ids = $user->getPermittingOwnerIdsAttribute();
  130. return Owner::query();
  131. }
  132. }