OwnerService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. public function 获取订单跟踪的货主(){
  62. return Owner::query()->with('orderTrackingOwner')->whereHas('orderTrackingOwner',function($query){
  63. $query->where('status','启用');
  64. })->get();
  65. }
  66. public function getByWmsOrders($orderHeaders){
  67. $customerIds = array_unique(data_get($orderHeaders,'*.customerid'));
  68. $customerIds = array_diff($customerIds,[null,'','*']);
  69. $owners = Owner::query()->whereIn('code',data_get($orderHeaders,'*.customerid'))->get();
  70. if($owners->count() < $customerIds){
  71. $customerIds = array_diff($customerIds,data_get($owners,'*.code'));
  72. $owner_list = $this->createByWmsCustomerIds($customerIds);
  73. $owners->concat($owner_list);
  74. }
  75. return $owners;
  76. }
  77. public function createByWmsCustomerIds($codes){
  78. if(!$codes) {return [];}
  79. $basCustomer = OracleBasCustomer::query()
  80. ->where('Customer_Type','OW')
  81. ->whereIn('CustomerID', $codes)
  82. ->get();
  83. $insert_params = [];
  84. $created_at = Carbon::now()->format('Y-m-d H:i:s');
  85. foreach ($basCustomer as $item) {
  86. $insert_params[] = [
  87. 'code' => $item->customerid,
  88. 'name' => $item->descr_c,
  89. 'created_at' => $created_at,
  90. ];
  91. }
  92. try {
  93. if (count($insert_params) > 0) {
  94. $this->insert($insert_params);
  95. LogService::log(__METHOD__, __FUNCTION__, '批量创建 owner error' . count($insert_params) . json_encode($insert_params) );
  96. }
  97. } catch (\Exception $e) {
  98. LogService::log(__METHOD__, __FUNCTION__, '批量创建 owner error' . json_encode($insert_params) . '||' . $e->getMessage() . '||' . $e->getTraceAsString());
  99. } finally {
  100. return Owner::query()->whereIn('code', $codes)->get();
  101. }
  102. }
  103. public function insert($fillables){
  104. return Owner::query()->insert($fillables);
  105. }
  106. public function getAuthorizedOwners(){
  107. $user = Auth::user();
  108. return Owner::query()->whereIn('id',$user->getPermittingOwnerIdsAttribute()??[])->get();
  109. }
  110. }