OwnerService.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. continue;
  57. }
  58. if ($owner['name']!=$basCustomer['descr_c']){
  59. $owner->update([
  60. 'code' => $basCustomer['customerid'],
  61. 'name' => $basCustomer['descr_c'],
  62. ]);
  63. }
  64. }
  65. $owners = Owner::query()->select('id', 'name')->get();
  66. return $owners;
  67. }
  68. public function first(array $params, array $rules =[]){
  69. return $this->cacheService->getOrExecute('OwnersFirst'.md5(json_encode($params),json_encode($rules)),function()use($params,$rules){
  70. $owner = Owner::query();
  71. foreach ($params as $column => $value){
  72. if (!isset($rules[$column]))$owner->where($column, $value);
  73. else{
  74. switch ($rules[$column]){
  75. case "or":
  76. $owner->orWhere($column, $value);
  77. break;
  78. }
  79. }
  80. }
  81. return $owner->first();
  82. },config('cache.expirations.rarelyChange'));
  83. }
  84. public function find($id)
  85. {
  86. return Owner::query()->find($id);
  87. }
  88. public function update(Owner $owner, array $values, array $related = [])
  89. {
  90. if ($related["ownerStoragePriceModels"] ?? false)$owner->ownerStoragePriceModels()->sync($related["ownerStoragePriceModels"]);
  91. return $owner->update($values);
  92. }
  93. public function create(array $params, array $related = []){
  94. /** @var Owner $owner */
  95. $owner = Owner::query()->create($params);
  96. if ($related["ownerStoragePriceModels"] ?? false)$owner->ownerStoragePriceModels()->syncWithoutDetaching($related["ownerStoragePriceModels"]);
  97. return $owner;
  98. }
  99. public function firstOrCreate(array $params, array $values = null){
  100. if (!$values) return Owner::query()->firstOrCreate($params);
  101. return Owner::query()->firstOrCreate($params,$values);
  102. }
  103. public function 获取订单跟踪的货主(){
  104. return Owner::query()->with('orderTrackingOwner')->whereHas('orderTrackingOwner',function($query){
  105. $query->where('status','启用');
  106. })->get();
  107. }
  108. public function getByWmsOrders($orderHeaders){
  109. $customerIds = array_unique(data_get($orderHeaders,'*.customerid'));
  110. $customerIds = array_diff($customerIds,[null,'','*']);
  111. $owners = Owner::query()->whereIn('code',$customerIds)->get();
  112. if($owners->count() < count($customerIds)){
  113. $customerIds = array_diff($customerIds,data_get($owners,'*.code'));
  114. $owner_list = $this->createByWmsCustomerIds($customerIds);
  115. $owners=$owners->concat($owner_list);
  116. }
  117. return $owners;
  118. }
  119. public function createByWmsCustomerIds($codes){
  120. if(!$codes) {return [];}
  121. $basCustomer = OracleBasCustomer::query()
  122. ->where('Customer_Type','OW')
  123. ->whereIn('CustomerID', $codes)
  124. ->get();
  125. $insert_params = [];
  126. $created_at = Carbon::now()->format('Y-m-d H:i:s');
  127. foreach ($basCustomer as $item) {
  128. $insert_params[] = [
  129. 'code' => $item->customerid,
  130. 'name' => $item->descr_c,
  131. 'created_at' => $created_at,
  132. ];
  133. }
  134. try {
  135. if (count($insert_params) > 0) {
  136. $this->insert($insert_params);
  137. app('LogService')->log(__METHOD__, __FUNCTION__, '批量创建 owner ' . count($insert_params) . json_encode($insert_params) );
  138. }
  139. } catch (\Exception $e) {
  140. app('LogService')->log(__METHOD__, __FUNCTION__, '批量创建 owner error' . json_encode($insert_params) . '||' . $e->getMessage() . '||' . $e->getTraceAsString());
  141. } finally {
  142. return Owner::query()->whereIn('code', $codes)->get();
  143. }
  144. }
  145. public function insert($fillables){
  146. return Owner::query()->insert($fillables);
  147. }
  148. public function getAuthorizedOwners(){
  149. $user = Auth::user();
  150. return Owner::query()->whereIn('id',app('UserService')->getPermittingOwnerIds($user)??[])->get();
  151. }
  152. public function get(array $params, array $withs = null, bool $authority = true, bool $notShowSoftDelete = false, $user = null)
  153. {
  154. /** @var User $user */
  155. if ($user==null) {
  156. $user = Auth::user();
  157. }
  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->get();
  168. }
  169. public function paginate(array $params, array $withs = null, bool $authority = true, bool $notShowSoftDelete = false)
  170. {
  171. /** @var User $user */
  172. $user = Auth::user();
  173. $query = Owner::query();
  174. if ($withs)$query->with($withs);
  175. if ($authority){
  176. $ids = $user->getPermittingOwnerIdsAttribute();
  177. if ($ids) $query->whereIn("id",$ids);
  178. else return null;
  179. }
  180. if ($notShowSoftDelete) $query->whereNull('deleted_at');
  181. $query = $this->query($query,$params);
  182. return $query->paginate($params["paginate"] ?? 50);
  183. }
  184. private function query(Builder $builder, array $params)
  185. {
  186. foreach ($params as $column => $param){
  187. if ($param === true){
  188. $builder->whereNotNull($column);
  189. continue;
  190. }
  191. if ($param === false){
  192. $builder->whereNull($column);
  193. continue;
  194. }
  195. $builder->where($column,$params);
  196. }
  197. return $builder;
  198. }
  199. public function codeGetOwner($code)
  200. {
  201. return app(CacheService::class)->getOrExecute("owner_".$code,function ()use($code){
  202. return Owner::query()->firstOrCreate(["code"=>$code],["code"=>$code,"name"=>$code]);
  203. });
  204. }
  205. }