OwnerService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. /** @var CacheService $cacheService */
  10. private $cacheService;
  11. function __construct(){
  12. $this->cacheService=app('CacheService');
  13. }
  14. /*
  15. * array | string $column
  16. * 默认一些select字段,可传递string 或 array来指定select字段
  17. */
  18. public function getSelection($column = ['id', 'name'])
  19. {
  20. return $this->cacheService->getOrExecute('OwnersAll_IdName',function()use($column){
  21. return Owner::filterAuthorities()->select($column)->get();
  22. },config('cache.expirations.owners'));
  23. }
  24. public function getSelectionId($column = ['id'])
  25. {
  26. return $this->cacheService->getOrExecute('OwnersAll_Id',function()use($column){
  27. return Owner::filterAuthorities()->select($column)->get();
  28. },config('cache.expirations.owners'));
  29. }
  30. /**
  31. *同步WMS全部货主至WAS
  32. */
  33. public function syncOwnersData()
  34. {
  35. $basCustomers = OracleBasCustomer::query()
  36. ->select('CUSTOMERID', 'DESCR_C')
  37. ->where('DESCR_C', 'not like', '%换ERP%')
  38. ->where('DESCR_C', 'not like', '%退仓%')
  39. ->where('CUSTOMER_TYPE', 'OW')
  40. ->get();
  41. $ownerCount = Owner::query()->count();
  42. if (count($basCustomers) == $ownerCount) return null;
  43. foreach ($basCustomers as $basCustomer) {
  44. $owner = Owner::query()->where('code', $basCustomer['customerid'])->first();
  45. if (!isset($owner)){
  46. Owner::query()->create([
  47. 'code' => $basCustomer['customerid'],
  48. 'name' => $basCustomer['descr_c'],
  49. 'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
  50. ]);
  51. }
  52. if ($owner['name']!=$basCustomer['descr_c']){
  53. $owner->update([
  54. 'code' => $basCustomer['customerid'],
  55. 'name' => $basCustomer['descr_c'],
  56. ]);
  57. }
  58. }
  59. $owners = Owner::query()->select('id', 'name')->get();
  60. return $owners;
  61. }
  62. public function first(array $params, array $rules =[]){
  63. return $this->cacheService->getOrExecute('OwnersFirst'.md5(json_encode($params),json_encode($rules)),function()use($params,$rules){
  64. $owner = Owner::query();
  65. foreach ($params as $column => $value){
  66. if (!isset($rules[$column]))$owner->where($column, $value);
  67. else{
  68. switch ($rules[$column]){
  69. case "or":
  70. $owner->orWhere($column, $value);
  71. break;
  72. }
  73. }
  74. }
  75. return $owner->first();
  76. },config('cache.expirations.rarelyChange'));
  77. }
  78. public function create(array $params){
  79. $owner = Owner::query()->create($params);
  80. return $owner;
  81. }
  82. public function firstOrCreate(array $params, array $values = null){
  83. if (!$values) return Owner::query()->firstOrCreate($params);
  84. return Owner::query()->firstOrCreate($params,$values);
  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() < 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',app('UserService')->getPermittingOwnerIds($user)??[])->get();
  134. }
  135. }