OwnerService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. use Illuminate\Support\Facades\DB;
  12. Class OwnerService
  13. {
  14. /** @var CacheService $cacheService */
  15. private $cacheService;
  16. function __construct(){
  17. $this->cacheService=app('CacheService');
  18. }
  19. /*
  20. * array | string $column
  21. * 默认一些select字段,可传递string 或 array来指定select字段
  22. */
  23. public function getIntersectPermitting(array $column = ['id', 'name'])
  24. {
  25. $ownerIds=app('UserService')->getPermittingOwnerIds(Auth::user());
  26. return $this->cacheService->getOrExecute('OwnersAll_IdName'.md5(json_encode($column).json_encode($ownerIds)),function()use($column,$ownerIds){
  27. if(empty($ownerIds))return new Collection();
  28. return Owner::query()->select($column)->whereIn('id', $ownerIds)->get();
  29. },config('cache.expirations.owners'));
  30. }
  31. public function getSelection($column = ['id'])
  32. {
  33. return $this->cacheService->getOrExecute('OwnersAll_'.md5(json_encode($column)),function()use($column){
  34. return Owner::filterAuthorities()->select($column)->get();
  35. },config('cache.expirations.owners'));
  36. }
  37. /**
  38. *同步WMS全部货主至WAS
  39. */
  40. public function syncOwnersData()
  41. {
  42. $basCustomers = OracleBasCustomer::query()
  43. ->select('CUSTOMERID', 'DESCR_C')
  44. ->where('DESCR_C', 'not like', '%换ERP%')
  45. ->where('DESCR_C', 'not like', '%退仓%')
  46. ->where('CUSTOMER_TYPE', 'OW')
  47. ->get();
  48. $ownerCount = Owner::query()->count();
  49. if (count($basCustomers) == $ownerCount) return null;
  50. foreach ($basCustomers as $basCustomer) {
  51. $owner = Owner::query()->where('code', $basCustomer['customerid'])->first();
  52. if (!isset($owner)){
  53. Owner::query()->create([
  54. 'code' => $basCustomer['customerid'],
  55. 'name' => $basCustomer['descr_c'],
  56. 'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
  57. ]);
  58. continue;
  59. }
  60. if ($owner['name']!=$basCustomer['descr_c']){
  61. $owner->update([
  62. 'code' => $basCustomer['customerid'],
  63. 'name' => $basCustomer['descr_c'],
  64. ]);
  65. }
  66. }
  67. $owners = Owner::query()->select('id', 'name')->get();
  68. return $owners;
  69. }
  70. public function first(array $params, array $rules =[]){
  71. return $this->cacheService->getOrExecute('OwnersFirst'.md5(json_encode($params),json_encode($rules)),function()use($params,$rules){
  72. $owner = Owner::query();
  73. foreach ($params as $column => $value){
  74. if (!isset($rules[$column]))$owner->where($column, $value);
  75. else{
  76. switch ($rules[$column]){
  77. case "or":
  78. $owner->orWhere($column, $value);
  79. break;
  80. }
  81. }
  82. }
  83. return $owner->first();
  84. },config('cache.expirations.rarelyChange'));
  85. }
  86. public function find($id)
  87. {
  88. return Owner::query()->find($id);
  89. }
  90. public function update(Owner $owner, array $values, array $related = [])
  91. {
  92. if ($related["ownerStoragePriceModels"] ?? false)$owner->ownerStoragePriceModels()->sync($related["ownerStoragePriceModels"]);
  93. return $owner->update($values);
  94. }
  95. public function create(array $params, array $related = []){
  96. /** @var Owner $owner */
  97. $owner = Owner::query()->create($params);
  98. if ($related["ownerStoragePriceModels"] ?? false)$owner->ownerStoragePriceModels()->syncWithoutDetaching($related["ownerStoragePriceModels"]);
  99. return $owner;
  100. }
  101. public function firstOrCreate(array $params, array $values = null){
  102. if (!$values) return Owner::query()->firstOrCreate($params);
  103. return Owner::query()->firstOrCreate($params,$values);
  104. }
  105. public function 获取订单跟踪的货主(){
  106. return Owner::query()->with('orderTrackingOwner')->whereHas('orderTrackingOwner',function($query){
  107. $query->where('status','启用');
  108. })->get();
  109. }
  110. public function getByWmsOrders($orderHeaders){
  111. $customerIds = array_unique(data_get($orderHeaders,'*.customerid'));
  112. $customerIds = array_diff($customerIds,[null,'','*']);
  113. $owners = Owner::query()->whereIn('code',$customerIds)->get();
  114. if($owners->count() < count($customerIds)){
  115. $customerIds = array_diff($customerIds,data_get($owners,'*.code'));
  116. $owner_list = $this->createByWmsCustomerIds($customerIds);
  117. $owners=$owners->concat($owner_list);
  118. }
  119. return $owners;
  120. }
  121. public function createByWmsCustomerIds($codes){
  122. if(!$codes) {return [];}
  123. $basCustomer = OracleBasCustomer::query()
  124. ->where('Customer_Type','OW')
  125. ->whereIn('CustomerID', $codes)
  126. ->get();
  127. $insert_params = [];
  128. $created_at = Carbon::now()->format('Y-m-d H:i:s');
  129. foreach ($basCustomer as $item) {
  130. $insert_params[] = [
  131. 'code' => $item->customerid,
  132. 'name' => $item->descr_c,
  133. 'created_at' => $created_at,
  134. ];
  135. }
  136. try {
  137. if (count($insert_params) > 0) {
  138. $this->insert($insert_params);
  139. app('LogService')->log(__METHOD__, __FUNCTION__, '批量创建 owner ' . count($insert_params) . json_encode($insert_params) );
  140. }
  141. } catch (\Exception $e) {
  142. app('LogService')->log(__METHOD__, __FUNCTION__, '批量创建 owner error' . json_encode($insert_params) . '||' . $e->getMessage() . '||' . $e->getTraceAsString());
  143. } finally {
  144. return Owner::query()->whereIn('code', $codes)->get();
  145. }
  146. }
  147. public function insert($fillables){
  148. return Owner::query()->insert($fillables);
  149. }
  150. public function getAuthorizedOwners(){
  151. $user = Auth::user();
  152. return Owner::query()->whereIn('id',app('UserService')->getPermittingOwnerIds($user)??[])->get();
  153. }
  154. public function get(array $params, array $withs = null, bool $authority = true, bool $notShowSoftDelete = false, $user = null)
  155. {
  156. /** @var User $user */
  157. if ($user==null) {
  158. $user = Auth::user();
  159. }
  160. $query = Owner::query();
  161. if ($withs)$query->with($withs);
  162. if ($authority){
  163. $ids = $user->getPermittingOwnerIdsAttribute();
  164. $query->whereIn("id",$ids);
  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. $query->whereIn("id",$ids);
  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 getOwnerByCodes($codes)
  200. {
  201. $collect = collect();
  202. if(count($codes) == 0)return $collect;
  203. foreach ($codes as $code) {
  204. $collect->push($this->getOwnerByCode($code));
  205. }
  206. return $collect;
  207. }
  208. public function getOwnerByCode($code){
  209. return Cache::remember("getOwnerByCode_{$code}", config('cache.expirations.owners'), function ()use($code){
  210. $owner = Owner::query()->where('code',$code)->first();
  211. if($owner) return $owner;
  212. $basCustomer = app('OracleBasCustomerService')->first(['Customer_Type'=>'OW','CustomerID'=>$code]);
  213. if(!$basCustomer)return null;
  214. return Owner::query()->create(['name'=>$basCustomer['descr_c'],'code'=>$basCustomer['customerid']]);
  215. });
  216. }
  217. public function codeGetOwner($code)
  218. {
  219. return app(CacheService::class)->getOrExecute("owner_".$code,function ()use($code){
  220. return Owner::query()->firstOrCreate(["code"=>$code],["code"=>$code,"name"=>$code]);
  221. });
  222. }
  223. /**
  224. * 向FLUX同步推送WAS本地录入信息
  225. *
  226. * @param array|Owner|integer $owner
  227. * @return bool
  228. */
  229. public function syncPush($owner)
  230. {
  231. if (is_array($owner)){
  232. $owner = new Owner();
  233. foreach ($owner as $column=>$value){
  234. $owner[$column] = $value;
  235. }
  236. }
  237. if (is_numeric($owner)){
  238. $owner = Owner::query()->find($owner);
  239. if (!$owner)return false;
  240. }
  241. $wms = DB::connection("oracle")->selectOne(DB::raw("SELECT CUSTOMERID FROM BAS_CUSTOMER WHERE CUSTOMER_TYPE = ? AND CUSTOMERID = ?"),["OW",$owner->code]);
  242. if (!$wms && $owner->code){
  243. $query = DB::raw(<<<sql
  244. INSERT INTO BAS_CUSTOMER(CUSTOMERID,CUSTOMER_TYPE,DESCR_C,ADDTIME,EDITTIME)
  245. VALUES(?,?,?,TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'))
  246. sql
  247. );
  248. $date = date('Y-m-d H:i:s');
  249. DB::connection("oracle")->insert($query,[$owner->code,'OW',$owner->name,$date,$date]);
  250. }
  251. return true;
  252. }
  253. }