OwnerService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 getByWmsOrders($orderHeaders){
  92. $customerIds = array_unique(data_get($orderHeaders,'*.customerid'));
  93. $customerIds = array_diff($customerIds,[null,'','*']);
  94. $owners = Owner::query()->whereIn('code',data_get($orderHeaders,'*.customerid'))->get();
  95. if($owners->count() < $customerIds){
  96. $customerIds = array_diff($customerIds,data_get($owners,'*.code'));
  97. $owner_list = $this->createByWmsCustomerIds($customerIds);
  98. $owners->concat($owner_list);
  99. }
  100. return $owners;
  101. }
  102. public function createByWmsCustomerIds($codes){
  103. if(!$codes) {return [];}
  104. $basCustomer = OracleBasCustomer::query()
  105. ->where('Customer_Type','OW')
  106. ->whereIn('CustomerID', $codes)
  107. ->get();
  108. $insert_params = [];
  109. $created_at = Carbon::now()->format('Y-m-d H:i:s');
  110. foreach ($basCustomer as $item) {
  111. $insert_params[] = [
  112. 'code' => $item->customerid,
  113. 'name' => $item->descr_c,
  114. 'created_at' => $created_at,
  115. ];
  116. }
  117. try {
  118. if (count($insert_params) > 0) {
  119. $this->insert($insert_params);
  120. LogService::log(__METHOD__, __FUNCTION__, '批量创建 owner error' . count($insert_params) . json_encode($insert_params) );
  121. }
  122. } catch (\Exception $e) {
  123. LogService::log(__METHOD__, __FUNCTION__, '批量创建 owner error' . json_encode($insert_params) . '||' . $e->getMessage() . '||' . $e->getTraceAsString());
  124. } finally {
  125. return Owner::query()->whereIn('code', $codes)->get();
  126. }
  127. }
  128. public function insert($fillables){
  129. return Owner::query()->insert($fillables);
  130. }
  131. public function getAuthorizedOwners(){
  132. $user = Auth::user();
  133. return Owner::query()->whereIn('id',$user->getPermittingOwnerIdsAttribute()??[])->get();
  134. }
  135. }