OwnerService.php 7.0 KB

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