OwnerService.php 9.0 KB

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