OwnerService.php 6.7 KB

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