OwnerService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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::count();
  33. if (count($basCustomers) == $ownerCount) return null;
  34. foreach ($basCustomers as $basCustomer) {
  35. $owner = Owner::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. $owners = Owner::query()->select('id', 'name')->get();
  44. return $owners;
  45. }
  46. public function first(array $params){
  47. $owner = Owner::query();
  48. foreach ($params as $column => $value){
  49. $owner->where($column, $value);
  50. }
  51. return $owner->first();
  52. }
  53. public function create(array $params){
  54. $owner = Owner::query()->create($params);
  55. return $owner;
  56. }
  57. public function firstOrCreate(array $params, array $values = null){
  58. if (!$values) return Owner::query()->firstOrCreate($params);
  59. return Owner::query()->firstOrCreate($params,$values);
  60. }
  61. // XXX 代码逻辑有待纠正
  62. public function 获取WMS订单列中对应的WAS货主列($WMSOrderHeaders)
  63. {
  64. $customerIds = array_unique(data_get($WMSOrderHeaders, '*.customerid'));
  65. $owners = Owner::query()->whereIn('code', $customerIds)->get();
  66. $codes = data_get($owners, '*.code');
  67. $codeDiff = array_diff($customerIds, $codes);
  68. $logs = [];
  69. $errs = [];
  70. if (count($codes) > count($customerIds)) {
  71. $basCustomer = OracleBasCustomer::query()->whereIn('customerid', $codeDiff)->get();
  72. foreach ($basCustomer as $customer) {
  73. if($customer['descr_c'] ?? false && $customer['customerid'] && false){
  74. try {
  75. $owner = Owner::query()->create(['name' => $customer['descr_c'], 'code' => $customer['customerid']]);
  76. $owners->push($owner);
  77. array_push($logs, ['info' => '创建「' . json_encode($owner) . '」']);
  78. } catch (\Exception $e) {
  79. array_push($errs, ['info' => '创建「' . json_encode($errs) . '」']);
  80. }
  81. }
  82. }
  83. }
  84. if(count($logs)>0)
  85. LogService::log(__METHOD__, __FUNCTION__, '批量创建客户「:' . json_encode($logs) . '」', Auth::user()['id']);
  86. if(count($errs)>0)
  87. LogService::log(__METHOD__, __FUNCTION__, '批量创建客户失败「:' . json_encode($errs) . '」', Auth::user()['id']);
  88. return $owners;
  89. }
  90. public function 获取订单跟踪的货主(){
  91. return Owner::query()->with('orderTrackingOwner')->whereHas('orderTrackingOwner',function($query){
  92. $query->where('status','启用');
  93. })->get();
  94. }
  95. public function getByWmsOrders($orderHeaders){
  96. $customerIds = array_unique(data_get($orderHeaders,'*.customerid'));
  97. $customerIds = array_diff($customerIds,[null,'','*']);
  98. $owners = Owner::query()->whereIn('code',data_get($orderHeaders,'*.customerid'))->get();
  99. if($owners->count() < $customerIds){
  100. $customerIds = array_diff($customerIds,data_get($owners,'*.code'));
  101. $owner_list = $this->createByWmsCustomerIds($customerIds);
  102. $owners->concat($owner_list);
  103. }
  104. return $owners;
  105. }
  106. public function createByWmsCustomerIds($codes){
  107. if(!$codes) {return [];}
  108. $basCustomer = OracleBasCustomer::query()
  109. ->where('Customer_Type','OW')
  110. ->whereIn('CustomerID', $codes)
  111. ->get();
  112. $insert_params = [];
  113. $created_at = Carbon::now()->format('Y-m-d H:i:s');
  114. foreach ($basCustomer as $item) {
  115. $insert_params[] = [
  116. 'code' => $item->customerid,
  117. 'name' => $item->descr_c,
  118. 'created_at' => $created_at,
  119. ];
  120. }
  121. try {
  122. if (count($insert_params) > 0) {
  123. $this->insert($insert_params);
  124. LogService::log(__METHOD__, __FUNCTION__, '批量创建 owner error' . count($insert_params) . json_encode($insert_params) );
  125. }
  126. } catch (\Exception $e) {
  127. LogService::log(__METHOD__, __FUNCTION__, '批量创建 owner error' . json_encode($insert_params) . '||' . $e->getMessage() . '||' . $e->getTraceAsString());
  128. } finally {
  129. return Owner::query()->whereIn('code', $codes)->get();
  130. }
  131. }
  132. public function insert($fillables){
  133. return Owner::query()->insert($fillables);
  134. }
  135. public function getAuthorizedOwners(){
  136. $user = Auth::user();
  137. return Owner::query()->whereIn('id',$user->getPermittingOwnerIdsAttribute()??[])->get();
  138. }
  139. }