OwnerService.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 getIntersectPermitting(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 find($id)
  84. {
  85. return Owner::query()->find($id);
  86. }
  87. public function update(Owner $owner, array $values, array $related = [])
  88. {
  89. if ($related["ownerStoragePriceModels"] ?? false)$owner->ownerStoragePriceModels()->sync($related["ownerStoragePriceModels"]);
  90. return $owner->update($values);
  91. }
  92. public function create(array $params, array $related = []){
  93. /** @var Owner $owner */
  94. $owner = Owner::query()->create($params);
  95. if ($related["ownerStoragePriceModels"] ?? false)$owner->ownerStoragePriceModels()->syncWithoutDetaching($related["ownerStoragePriceModels"]);
  96. return $owner;
  97. }
  98. public function firstOrCreate(array $params, array $values = null){
  99. if (!$values) return Owner::query()->firstOrCreate($params);
  100. return Owner::query()->firstOrCreate($params,$values);
  101. }
  102. public function 获取订单跟踪的货主(){
  103. return Owner::query()->with('orderTrackingOwner')->whereHas('orderTrackingOwner',function($query){
  104. $query->where('status','启用');
  105. })->get();
  106. }
  107. public function getByWmsOrders($orderHeaders){
  108. $customerIds = array_unique(data_get($orderHeaders,'*.customerid'));
  109. $customerIds = array_diff($customerIds,[null,'','*']);
  110. $owners = Owner::query()->whereIn('code',$customerIds)->get();
  111. if($owners->count() < count($customerIds)){
  112. $customerIds = array_diff($customerIds,data_get($owners,'*.code'));
  113. $owner_list = $this->createByWmsCustomerIds($customerIds);
  114. $owners=$owners->concat($owner_list);
  115. }
  116. return $owners;
  117. }
  118. public function createByWmsCustomerIds($codes){
  119. if(!$codes) {return [];}
  120. $basCustomer = OracleBasCustomer::query()
  121. ->where('Customer_Type','OW')
  122. ->whereIn('CustomerID', $codes)
  123. ->get();
  124. $insert_params = [];
  125. $created_at = Carbon::now()->format('Y-m-d H:i:s');
  126. foreach ($basCustomer as $item) {
  127. $insert_params[] = [
  128. 'code' => $item->customerid,
  129. 'name' => $item->descr_c,
  130. 'created_at' => $created_at,
  131. ];
  132. }
  133. try {
  134. if (count($insert_params) > 0) {
  135. $this->insert($insert_params);
  136. app('LogService')->log(__METHOD__, __FUNCTION__, '批量创建 owner ' . count($insert_params) . json_encode($insert_params) );
  137. }
  138. } catch (\Exception $e) {
  139. app('LogService')->log(__METHOD__, __FUNCTION__, '批量创建 owner error' . json_encode($insert_params) . '||' . $e->getMessage() . '||' . $e->getTraceAsString());
  140. } finally {
  141. return Owner::query()->whereIn('code', $codes)->get();
  142. }
  143. }
  144. public function insert($fillables){
  145. return Owner::query()->insert($fillables);
  146. }
  147. public function getAuthorizedOwners(){
  148. $user = Auth::user();
  149. return Owner::query()->whereIn('id',app('UserService')->getPermittingOwnerIds($user)??[])->get();
  150. }
  151. public function get(array $params, array $withs = null, bool $authority = true, bool $notShowSoftDelete = false, $user = null)
  152. {
  153. /** @var User $user */
  154. if ($user==null) {
  155. $user = Auth::user();
  156. }
  157. $query = Owner::query();
  158. if ($withs)$query->with($withs);
  159. if ($authority){
  160. $ids = $user->getPermittingOwnerIdsAttribute();
  161. if ($ids) $query->whereIn("id",$ids);
  162. else return null;
  163. }
  164. if ($notShowSoftDelete) $query->whereNull('deleted_at');
  165. $query = $this->query($query,$params);
  166. return $query->get();
  167. }
  168. public function paginate(array $params, array $withs = null, bool $authority = true, bool $notShowSoftDelete = false)
  169. {
  170. /** @var User $user */
  171. $user = Auth::user();
  172. $query = Owner::query();
  173. if ($withs)$query->with($withs);
  174. if ($authority){
  175. $ids = $user->getPermittingOwnerIdsAttribute();
  176. if ($ids) $query->whereIn("id",$ids);
  177. else return null;
  178. }
  179. if ($notShowSoftDelete) $query->whereNull('deleted_at');
  180. $query = $this->query($query,$params);
  181. return $query->paginate($params["paginate"] ?? 50);
  182. }
  183. private function query(Builder $builder, array $params)
  184. {
  185. foreach ($params as $column => $param){
  186. if ($param === true){
  187. $builder->whereNotNull($column);
  188. continue;
  189. }
  190. if ($param === false){
  191. $builder->whereNull($column);
  192. continue;
  193. }
  194. $builder->where($column,$params);
  195. }
  196. return $builder;
  197. }
  198. }