OwnerService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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\Database\Eloquent\Builder;
  8. use Illuminate\Support\Facades\Auth;
  9. Class OwnerService
  10. {
  11. /** @var CacheService $cacheService */
  12. private $cacheService;
  13. function __construct(){
  14. $this->cacheService=app('CacheService');
  15. }
  16. /*
  17. * array | string $column
  18. * 默认一些select字段,可传递string 或 array来指定select字段
  19. */
  20. public function getSelection(array $column = ['id', 'name'])
  21. {
  22. return $this->cacheService->getOrExecute('OwnersAll_IdName'.md5(json_encode($column)),function()use($column){
  23. return Owner::filterAuthorities()->select($column)->get();
  24. },config('cache.expirations.owners'));
  25. }
  26. public function getSelectionId($column = ['id'])
  27. {
  28. return $this->cacheService->getOrExecute('OwnersAll_Id',function()use($column){
  29. return Owner::filterAuthorities()->select($column)->get();
  30. },config('cache.expirations.owners'));
  31. }
  32. /**
  33. *同步WMS全部货主至WAS
  34. */
  35. public function syncOwnersData()
  36. {
  37. $basCustomers = OracleBasCustomer::query()
  38. ->select('CUSTOMERID', 'DESCR_C')
  39. ->where('DESCR_C', 'not like', '%换ERP%')
  40. ->where('DESCR_C', 'not like', '%退仓%')
  41. ->where('CUSTOMER_TYPE', 'OW')
  42. ->get();
  43. $ownerCount = Owner::query()->count();
  44. if (count($basCustomers) == $ownerCount) return null;
  45. foreach ($basCustomers as $basCustomer) {
  46. $owner = Owner::query()->where('code', $basCustomer['customerid'])->first();
  47. if (!isset($owner)){
  48. Owner::query()->create([
  49. 'code' => $basCustomer['customerid'],
  50. 'name' => $basCustomer['descr_c'],
  51. 'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
  52. ]);
  53. }
  54. if ($owner['name']!=$basCustomer['descr_c']){
  55. $owner->update([
  56. 'code' => $basCustomer['customerid'],
  57. 'name' => $basCustomer['descr_c'],
  58. ]);
  59. }
  60. }
  61. $owners = Owner::query()->select('id', 'name')->get();
  62. return $owners;
  63. }
  64. public function first(array $params, array $rules =[]){
  65. return $this->cacheService->getOrExecute('OwnersFirst'.md5(json_encode($params),json_encode($rules)),function()use($params,$rules){
  66. $owner = Owner::query();
  67. foreach ($params as $column => $value){
  68. if (!isset($rules[$column]))$owner->where($column, $value);
  69. else{
  70. switch ($rules[$column]){
  71. case "or":
  72. $owner->orWhere($column, $value);
  73. break;
  74. }
  75. }
  76. }
  77. return $owner->first();
  78. },config('cache.expirations.rarelyChange'));
  79. }
  80. public function create(array $params){
  81. $owner = Owner::query()->create($params);
  82. return $owner;
  83. }
  84. public function firstOrCreate(array $params, array $values = null){
  85. if (!$values) return Owner::query()->firstOrCreate($params);
  86. return Owner::query()->firstOrCreate($params,$values);
  87. }
  88. public function 获取订单跟踪的货主(){
  89. return Owner::query()->with('orderTrackingOwner')->whereHas('orderTrackingOwner',function($query){
  90. $query->where('status','启用');
  91. })->get();
  92. }
  93. public function getByWmsOrders($orderHeaders){
  94. $customerIds = array_unique(data_get($orderHeaders,'*.customerid'));
  95. $customerIds = array_diff($customerIds,[null,'','*']);
  96. $owners = Owner::query()->whereIn('code',data_get($orderHeaders,'*.customerid'))->get();
  97. if($owners->count() < count($customerIds)){
  98. $customerIds = array_diff($customerIds,data_get($owners,'*.code'));
  99. $owner_list = $this->createByWmsCustomerIds($customerIds);
  100. $owners=$owners->concat($owner_list);
  101. }
  102. return $owners;
  103. }
  104. public function createByWmsCustomerIds($codes){
  105. if(!$codes) {return [];}
  106. $basCustomer = OracleBasCustomer::query()
  107. ->where('Customer_Type','OW')
  108. ->whereIn('CustomerID', $codes)
  109. ->get();
  110. $insert_params = [];
  111. $created_at = Carbon::now()->format('Y-m-d H:i:s');
  112. foreach ($basCustomer as $item) {
  113. $insert_params[] = [
  114. 'code' => $item->customerid,
  115. 'name' => $item->descr_c,
  116. 'created_at' => $created_at,
  117. ];
  118. }
  119. try {
  120. if (count($insert_params) > 0) {
  121. $this->insert($insert_params);
  122. app('LogService')->log(__METHOD__, __FUNCTION__, '批量创建 owner error' . count($insert_params) . json_encode($insert_params) );
  123. }
  124. } catch (\Exception $e) {
  125. app('LogService')->log(__METHOD__, __FUNCTION__, '批量创建 owner error' . json_encode($insert_params) . '||' . $e->getMessage() . '||' . $e->getTraceAsString());
  126. } finally {
  127. return Owner::query()->whereIn('code', $codes)->get();
  128. }
  129. }
  130. public function insert($fillables){
  131. return Owner::query()->insert($fillables);
  132. }
  133. public function getAuthorizedOwners(){
  134. $user = Auth::user();
  135. return Owner::query()->whereIn('id',app('UserService')->getPermittingOwnerIds($user)??[])->get();
  136. }
  137. public function get(array $params, array $withs = null, bool $authority = true, bool $notShowSoftDelete = false)
  138. {
  139. /** @var User $user */
  140. $user = Auth::user();
  141. $query = Owner::query();
  142. if ($authority){
  143. $ids = $user->getPermittingOwnerIdsAttribute();
  144. if ($ids) $query->whereIn("id",$ids);
  145. else return null;
  146. }
  147. if ($notShowSoftDelete) $query->whereNull('deleted_at');
  148. $query = $this->query($query,$params);
  149. return $query->get();
  150. }
  151. private function query(Builder $builder, array $params)
  152. {
  153. foreach ($params as $column => $param){
  154. if ($param === true){
  155. $builder->whereNotNull($column);
  156. continue;
  157. }
  158. if ($param === false){
  159. $builder->whereNull($column);
  160. continue;
  161. }
  162. $builder->where($column,$params);
  163. }
  164. return $builder;
  165. }
  166. }