OwnerService.php 11 KB

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