OrderPackageCommoditiesService.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. <?php
  2. namespace App\Services;
  3. use App\Commodity;
  4. use App\OracleActAllocationDetails;
  5. use App\OracleBasSKU;
  6. use App\OracleDOCOrderDetail;
  7. use App\OracleDOCOrderHeader;
  8. use App\Order;
  9. use App\OrderPackage;
  10. use App\OrderPackageCommodities;
  11. use App\OrderTracking;
  12. use App\Owner;
  13. use App\Services\common\BatchUpdateService;
  14. use App\Services\common\DataHandlerService;
  15. use Carbon\Carbon;
  16. use Illuminate\Support\Collection;
  17. class OrderPackageCommoditiesService
  18. {
  19. public function insert(array $params){
  20. return OrderPackageCommodities::query()->insert($params);
  21. }
  22. public function batchUpdate($params){
  23. return app(BatchUpdateService::class)->batchUpdate('order_package_commodities',$params);
  24. }
  25. //-------------------------
  26. public function basedOnOracleDetailsStore($orderNo, $orderPackage)
  27. {
  28. $details = OracleDOCOrderDetail::query()->where('orderNo', $orderNo)->get();
  29. $orderPackageCommodities = OrderPackageCommodities::query()->where('order_package_id',$orderPackage['id'])->get();
  30. $this->根据详情更新OrderPackage下的商品信息($orderPackage,$orderPackageCommodities,$details,'qtyordered');
  31. unset($details,$orderPackageCommodities);
  32. }
  33. public function basedOnActAllocationDetailsStoreByOrderNo($orderNo,$orderPackage){
  34. $details = OracleActAllocationDetails::query()->where('orderno', $orderNo)->get();
  35. $orderPackageCommodities = OrderPackageCommodities::query()->where('order_package_id',$orderPackage['id'])->get();
  36. $this->根据详情更新OrderPackage下的商品信息($orderPackage,$orderPackageCommodities,$details,'qty_each');
  37. unset($details,$orderPackageCommodities);
  38. }
  39. public function 根据详情更新OrderPackage下的商品信息($orderPackage,$orderPackageCommodities,$details,$key){
  40. $newItems = [];
  41. foreach ($details as $detail) {
  42. $owner = Owner::query()->where(['code' => $detail['customerid']])->first(); // 货主
  43. $sku = $detail->sku; // sku
  44. $owner_id = $owner->id; // 货主id
  45. $commodity = Commodity::query()->where(['sku' => $sku, 'owner_id' => $owner_id])->first(); // 商品
  46. if ($commodity == null) {
  47. $basSku = OracleBasSKU::query()->where(['sku' => $sku, 'customerid' => $detail->customerid])->first(); // 没有找到对应的商品信息
  48. $commodity = Commodity::query()->create(['sku' => $sku, 'owner_id' => $owner_id, 'name' => $basSku->descr_c]);
  49. }
  50. $data = ['order_package_id' => $orderPackage['id'], 'commodity_id' => $commodity['id'], 'amount' => $detail[$key]];
  51. array_push($newItems,$data);
  52. }
  53. $diffarr = [];
  54. foreach ($newItems as $newItem) {
  55. $packageCommodity = $orderPackageCommodities->where('order_package_id',$newItem['order_package_id'])->where('commodity_id',$newItem['commodity_id'])->first();
  56. if($packageCommodity!=null){
  57. if($packageCommodity['amount'] != $newItem['amount']){
  58. $packageCommodity->update(['amount'=>intval($newItem['amount'])]);
  59. }
  60. $orderPackageCommodities = $orderPackageCommodities->filter(function ($item) use ($packageCommodity){
  61. if($item['id'] == $packageCommodity['id']){
  62. return false;
  63. }
  64. return true;
  65. });
  66. array_push($diffarr,$newItem);
  67. }
  68. }
  69. if($orderPackageCommodities->count() > 0){
  70. foreach ($orderPackageCommodities as $orderPackageCommodity) {
  71. $id = $orderPackageCommodity['id'];
  72. OrderPackageCommodities::destroy($id);
  73. }
  74. }
  75. $newItems = array_filter($newItems,function($newItem) use ($diffarr){
  76. foreach ($diffarr as $item) {
  77. if($item['order_package_id'] == $newItem['order_package_id'] &&
  78. $item['commodity_id'] == $newItem['commodity_id'] &&
  79. $item['amount'] == $newItem['amount']){
  80. return false;
  81. }
  82. }
  83. return true;
  84. });
  85. try {
  86. if(count($newItems) > 0 ){
  87. OrderPackageCommodities::query()->insert($newItems);
  88. app('LogService')->log(__METHOD__,__FUNCTION__,'添加包裹商品信息'.json_encode($newItems));
  89. }
  90. } catch (\Exception $e) {
  91. app('LogService')->log(__METHOD__,__FUNCTION__,'添加包裹商品信息异常'.json_encode($newItems).$e->getMessage(),$e->getTraceAsString());
  92. } finally {
  93. unset($newItems,$orderPackageCommodities);
  94. }
  95. }
  96. public function basedOnActAllocationDetailsStore($orderPackage)
  97. {
  98. $details = OracleActAllocationDetails::query()->where('picktotraceid', $orderPackage->logistic_number)->get();
  99. $orderPackageCommodities = OrderPackageCommodities::query()->where('order_package_id',$orderPackage['id'])->get();
  100. $this->根据详情更新OrderPackage下的商品信息($orderPackage,$orderPackageCommodities,$details,'qty_each');
  101. unset($details,$orderPackageCommodities);
  102. }
  103. /* public function basedOnActAllocationDetail(Order $order, OracleDOCOrderHeader $header, array $details)
  104. {
  105. foreach ($details as $detail) {
  106. $logistic_number = $details['picktotraceid'];
  107. if ($logistic_number == '*' || $logistic_number == null || $logistic_number == '') {
  108. $logistic_number = $header['soreference5'];
  109. if ($logistic_number == '*' || $logistic_number == null || $logistic_number == '') {
  110. continue;
  111. }
  112. }
  113. $orderPackage = OrderPackage::query()->firstOrCreate(['order_id'=>$order['id'],'logistic_number',$details]);
  114. $this->createByActAllocationDetail($detail, $orderPackage);
  115. }
  116. }*/
  117. /* private function createByActAllocationDetail($detail,$orderPackage)
  118. {
  119. $owner = Owner::query()->where(['code' => $detail->customerid])->first(); // 货主
  120. $sku = $detail->sku;
  121. $owner_id = $owner['id'];
  122. $commodity = Commodity::query()->where(['sku' => $sku, 'owner_id' => $owner_id])->first();
  123. if ($commodity == null) {
  124. $basSku = OracleBasSKU::query()->where(['sku' => $sku, 'customerid' => $detail->customerid])->first();
  125. $commodity = Commodity::query()->create(['sku' => $sku, 'owner_id' => $owner_id, 'name' => $basSku->descr_c]);
  126. }
  127. try {
  128. $count = OrderPackageCommodities::query()->where(['order_package_id' => $orderPackage['id'], 'commodity_id' => $commodity['id'], 'amount' => $detail['qty_each']])->count();
  129. if($count == 0){
  130. $orderPackageCommodities = OrderPackageCommodities::query()->firstOrCreate(['order_package_id' => $orderPackage['id'], 'commodity_id' => $commodity['id'], 'amount' => $detail['qty_each']]);
  131. app('LogService')->log(__METHOD__,__FUNCTION__,'创建订单包裹详情'.json_encode($orderPackageCommodities));
  132. }
  133. } catch (\Exception $e) {
  134. app('LogService')->log(__METHOD__,__FUNCTION__,'创建订单包裹失败'.$e->getMessage().$e->getTraceAsString());
  135. } finally {
  136. return $orderPackageCommodities ?? null;
  137. }
  138. }*/
  139. public function createByWmsOrder($orderHeaders)
  140. {
  141. if(!$orderHeaders){ return [];}
  142. $this->更新OPC_根据WMS订单($orderHeaders);
  143. }
  144. // public function getParamsByActAllocationDetails($details,array $order_packages_logistic_number_map,array $owner_code_map,array $commodity_owner_id_sku_map,array $logistic_numbers){
  145. // /** @var DataHandlerService $dataHandlerService */
  146. // $dataHandlerService = app(DataHandlerService::class);
  147. // if(!$details)return [];
  148. // $params = [];
  149. // $create_at = Carbon::now()->format('Y-m-d H:i:s');
  150. // $updated_at = Carbon::now()->format('Y-m-d H:i:s');
  151. // foreach ($details as $actAllocationDetail) {
  152. // $pickToTraceID = $actAllocationDetail->picktotraceid;
  153. // if(in_array($pickToTraceID,$logistic_numbers)){
  154. // $orderPackage = $dataHandlerService->getKeyValue(['logistic_number'=>$pickToTraceID],$order_packages_logistic_number_map);
  155. // if(!$orderPackage)continue;
  156. // $owner = $dataHandlerService->getKeyValue(['code'=>$actAllocationDetail->customerid],$owner_code_map);
  157. // if(!$owner)continue;
  158. // $commodity = $dataHandlerService->getKeyValue(['owner_id'=>$owner->id,'sku'=>$actAllocationDetail->sku],$commodity_owner_id_sku_map);
  159. // if(!$commodity)continue;
  160. // $params[] = [
  161. // 'commodity_id' =>$commodity->id,
  162. // 'order_package_id' =>$orderPackage->id,
  163. // 'amount' => $actAllocationDetail->qty_each,
  164. // 'created_at' => $create_at
  165. // ];
  166. // }
  167. // }
  168. // unset($details,$order_packages_logistic_number_map,$owner_code_map,$commodity_owner_id_sku_map,$logistic_numbers);
  169. // return $params;
  170. // }
  171. public function create(array $params){
  172. if(!$params){return [];}
  173. try {
  174. $this->insert($params);
  175. app('LogService')->log(__METHOD__,__FUNCTION__,'批量创建 OrderPackageCommodities'.count($params).json_encode($params));
  176. } catch (\Exception $e) {
  177. app('LogService')->log(__METHOD__,__FUNCTION__,'批量创建 OrderPackageCommodities error'.json_encode($params)."||".$e->getMessage().'||'.$e->getTraceAsString());
  178. } finally {
  179. $order_package_ids = array_unique(data_get($params,'*.order_package_id'));
  180. unset($params);
  181. return OrderPackageCommodities::query()
  182. ->with('commodity')
  183. ->whereIn('order_package_id',$order_package_ids)
  184. ->get();
  185. }
  186. }
  187. public function updateByWmsOrder($orderHeaders){
  188. if(!$orderHeaders){return ;}
  189. $this->更新OPC_根据WMS订单($orderHeaders);
  190. }
  191. public function getByWmsOrder($orderHeaders){
  192. $order_no = data_get($orderHeaders,'*.orderno');
  193. return OrderPackageCommodities::query()
  194. ->with('package.order','commodity')
  195. ->whereIn('order_package_id',function($query) use ($order_no){
  196. $query->from('order_packages')->select('id')->whereIn('order_id',function($query)use($order_no){
  197. $query->from('orders')->select('id')->whereIn('code',$order_no);
  198. });
  199. })->get();
  200. }
  201. public function batchUpdateItself($column, array $params)
  202. {
  203. return app(BatchUpdateService::class)->batchUpdateItself('order_package_commodities', $column, $params);
  204. }
  205. /**
  206. * @param array $logistic_numbers
  207. * @return array|mixed
  208. */
  209. public function 删除包裹商品信息_根据快递单号($logistic_numbers)
  210. {
  211. $orderPackageCommodities = OrderPackageCommodities::query()
  212. ->with('package')
  213. ->whereIn('order_package_id',function($query)use($logistic_numbers){
  214. $query->from('order_packages')->select('id')->whereIn('logistic_number',$logistic_numbers);
  215. })->get();
  216. $ids = data_get($orderPackageCommodities, '*.id');
  217. if(count($ids) == 0){return [];}
  218. try {
  219. OrderPackageCommodities::query()->whereIn('id', $ids)->delete();
  220. app('LogService')->log(__METHOD__,__FUNCTION__,'删除多余OrderPackageCommodities '.$orderPackageCommodities->count().json_encode($orderPackageCommodities),null);
  221. } catch (\Exception $e) {
  222. app('LogService')->log(__METHOD__,__FUNCTION__,'删除多余OrderPackageCommodities error'.$orderPackageCommodities->count().json_encode($orderPackageCommodities).$e->getMessage().$e->getTraceAsString(),null);
  223. return [];
  224. }
  225. return $ids;
  226. }
  227. /**
  228. * @param array $orderNos
  229. * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
  230. */
  231. public function getByOrderNos($orderNos)
  232. {
  233. return OrderPackageCommodities::query()->with('package.order','commodity')
  234. ->whereIn('order_package_id',function($query) use ($orderNos){
  235. $query->from('order_packages')->select('id')->whereIn('order_id',function($query)use($orderNos){
  236. $query->from('orders')->select('id')->whereIn('code',$orderNos);
  237. });
  238. })->get();
  239. }
  240. /**
  241. * @param Collection $orders
  242. * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
  243. */
  244. public function getByOrders($orders)
  245. {
  246. return $this->getByOrderNos(data_get($orders,'*.code'));
  247. }
  248. /**
  249. * @param OracleDOCOrderHeader $orderHeader
  250. * @param Order $order
  251. * @return array|Collection|\Tightenco\Collect\Support\Collection
  252. */
  253. public function 返回创建数组($orderHeader)
  254. {
  255. if($orderHeader->sostatus == 90){return [];}
  256. $actAllocationDetails = $orderHeader->actAllocationDetails->collect();
  257. $innerParams = collect();
  258. $actAllocationDetails->each(function($detail)use(&$innerParams){
  259. $sku = $detail->sku;
  260. $amount = $detail->qty_each;
  261. $logistic_number = $detail->picktotraceid;
  262. $params = [
  263. 'ownerCode' => $detail->customerid,
  264. 'orderNo' => $detail->orderno,
  265. 'sku' => $sku,
  266. 'amount' => $amount,
  267. 'logistic_number' => $logistic_number,
  268. ];
  269. $innerParam = $innerParams->where('logistic_number',$logistic_number)->where('sku',$sku)->first();
  270. if($innerParam ?? false){
  271. $bool = false;
  272. $innerParams = $innerParams->map(function($param)use($innerParam,$amount,&$bool){
  273. if($innerParam['logistic_number'] == $param['logistic_number'] &&
  274. $innerParam['sku'] == $param['sku'] && !$bool){
  275. $param['amount'] += $amount;
  276. $bool = !$bool;
  277. }
  278. return $param;
  279. });
  280. }else{
  281. $innerParams->push($params);
  282. }
  283. });
  284. return $innerParams;
  285. }
  286. public function 返回创建数组_WMS订单($orderHeaders)
  287. {
  288. $innerParams = [];
  289. foreach ($orderHeaders as $orderHeader) {
  290. $innerParam = $this->返回创建数组($orderHeader);
  291. foreach ($innerParam as $param) {
  292. $innerParams[] = $param;
  293. }
  294. }
  295. return $innerParams;
  296. }
  297. /**
  298. * @param array $innerParams
  299. * @return array
  300. */
  301. public function 生成OrderPackageCommodities_基于创建数组($innerParams)
  302. {
  303. if(!$innerParams){return [];}
  304. if(is_array($innerParams) && count($innerParams) == 0){return [];}
  305. $commodity_map = $this->返回Commodity数组_根据数组中sku($innerParams);
  306. $orderPackages_map = app(OrderPackageService::class)->返回OrderPackage数组_根据数组中的快递单号($innerParams);
  307. $createParams = [];
  308. $dataTime = Carbon::now()->format('Y-m-d H:i:s');
  309. foreach ($innerParams as $innerParam) {
  310. $key = ' ownerCode='.($innerParam['ownerCode'] ?? '').' sku='.($innerParam['sku'] ?? '');
  311. $commodity = $commodity_map[$key] ?? null;
  312. $orderPackage = $orderPackages_map[$innerParam['logistic_number']] ?? '';
  313. $createParams[] = [
  314. 'order_package_id' => $orderPackage->id ?? '',
  315. 'commodity_id' => $commodity->id ?? null,
  316. 'amount' => $innerParam['amount'],
  317. 'created_at' => $dataTime,
  318. 'updated_at' => $dataTime,
  319. ];
  320. }
  321. return $createParams;
  322. }
  323. /**
  324. * @param $orderHeaders
  325. */
  326. public function 更新OPC_根据WMS订单($orderHeaders)
  327. {
  328. if(!$orderHeaders){return ;}
  329. if(count($orderHeaders) == 0){return ;}
  330. app(CommodityService::class)->getByWmsOrders($orderHeaders);
  331. $orderPackageCommodities = $this->getByWmsOrder($orderHeaders);
  332. $OPCCollects = $this->将orderPackageCommodity抽象成数组($orderPackageCommodities);
  333. $creatParams = $this->返回创建数组_WMS订单($orderHeaders);
  334. $orderNos = $this->数据重组($OPCCollects,$creatParams);
  335. $updateParams= collect();$deleteIds = [];$insertParams =[];
  336. foreach ($orderNos as $orderNo) {
  337. $OPCCollect = $OPCCollects[$orderNo] ?? null;
  338. $creatParam = $creatParams[$orderNo];
  339. $retain[] = $this->删选可以保留的OrderPackageCommodities($OPCCollect,$creatParam);
  340. $collect = $this->删选需要修改的OrderPackageCommodities($OPCCollect,$creatParam);
  341. $ids = $this->删选出删除的OrderPackageCommodities($OPCCollect,$creatParam);
  342. $insertParam = $this->删选出需要添加的OrderPackageCommodities($OPCCollect,$creatParam);
  343. $deleteIds = array_merge($deleteIds,$ids);
  344. foreach ($collect as $item) {
  345. $updateParams->push($item);
  346. }
  347. foreach ($insertParam as $item) {
  348. $insertParams[]= $item;
  349. }
  350. }
  351. $this->根据更新数组进行更新($updateParams);
  352. $this->删除OPC以及对应的追踪件($deleteIds);
  353. $creatParams = $this->生成OrderPackageCommodities_基于创建数组($insertParams);
  354. $this->create($creatParams);
  355. }
  356. /**
  357. * @param Collection $orderPackageCommodities
  358. * @return Collection|\Tightenco\Collect\Support\Collection
  359. */
  360. public function 将orderPackageCommodity抽象成数组($orderPackageCommodities)
  361. {
  362. /** @var DataHandlerService $dataHandlerService */
  363. $dataHandlerService = app(DataHandlerService::class);
  364. $collect = collect();
  365. $ownerIds = array_unique(data_get($orderPackageCommodities,'*.commodity.owner_id'));
  366. $owners = Owner::query()->whereIn('id',$ownerIds)->get();
  367. $owner_id_map = $dataHandlerService->dataHeader(['id'],$owners);
  368. foreach ($orderPackageCommodities as $orderPackageCommodity) {
  369. $owner = $dataHandlerService->getKeyValue(['id'=>$orderPackageCommodity->commodity->owner_id ?? ''],$owner_id_map);
  370. $params = [
  371. 'id' => $orderPackageCommodity->id,
  372. 'ownerCode' => $owner->code,
  373. 'orderNo' => $orderPackageCommodity->package->order->code,
  374. 'sku' => $orderPackageCommodity->commodity->sku,
  375. 'amount' => $orderPackageCommodity->amount,
  376. 'logistic_number' => $orderPackageCommodity->package->logistic_number,
  377. ];
  378. $collect->push($params);
  379. }
  380. return $collect;
  381. }
  382. public function 数据重组(&$OPCCollects,&$params)
  383. {
  384. $collect_map = [];$params_map = [];$order_no_map = [];
  385. foreach ($OPCCollects as $item) {
  386. $orderNo = $item['orderNo'];
  387. if(!isset($collect_map[$orderNo])){$collect_map[$orderNo] = collect();}
  388. $collect_map[$orderNo]->push($item);
  389. }
  390. foreach ($params as $param) {
  391. $orderNo = $param['orderNo'];
  392. if(!isset($params_map[$orderNo])){$params_map[$orderNo] = collect();}
  393. $order_no_map[] = $orderNo;
  394. $params_map[$orderNo]->push($param);
  395. }
  396. $order_no_map = array_unique($order_no_map);
  397. $OPCCollects = $collect_map;
  398. $params = $params_map;
  399. return $order_no_map;
  400. }
  401. public function 删选可以保留的OrderPackageCommodities(&$OPCCollect,&$params)
  402. {
  403. $retain = collect();
  404. if($OPCCollect == null){return $retain;}
  405. $map = [];
  406. foreach ($params as $param) {
  407. $key = ' logistic_number='.$param['logistic_number'].' sku='.$param['sku'].' amount='.$param['amount'];
  408. if(! isset($map[$key])){
  409. $map[$key] = [];
  410. }
  411. $map[$key][] = $param;
  412. }
  413. foreach ($OPCCollect as $item) {
  414. $key = ' logistic_number='.$item['logistic_number'].' sku='.$item['sku'].' amount='.$item['amount'];
  415. if(isset($map[$key]) && count($map[$key]) > 0){
  416. $value = $map[$key][0];
  417. array_shift($map[$key]);
  418. $params->filter(function($param)use($value){
  419. return count(array_diff_assoc($param,$value)) != 0 ;
  420. });
  421. $retain->push($value);
  422. }
  423. }
  424. return $retain;
  425. }
  426. public function 删选出需要添加的OrderPackageCommodities(&$OPCCollect,&$params)
  427. {
  428. $innerParam = [];
  429. $OPCmap = [];
  430. if($OPCCollect == null){return $params;}
  431. foreach ($OPCCollect as $item) {
  432. $key = ' logistic_number='.$item['logistic_number'].' sku='.$item['sku'].' amount='.$item['amount'];
  433. if(!isset($OPCmap[$key])){
  434. $OPCmap[$key][] = [];
  435. }
  436. $OPCmap[$key][] = $item;
  437. }
  438. $paramsMap = [];
  439. foreach ($params as $param) {
  440. $key = ' logistic_number='.$param['logistic_number'].' sku='.$param['sku'].' amount='.$param['amount'];
  441. if(!isset($OPCmap[$key])){
  442. $paramsMap[$key][] = [];
  443. }
  444. $paramsMap[$key][] = $param;
  445. }
  446. $paramsCopy = $params->collect();
  447. foreach ($paramsCopy as $param) {
  448. $key = ' logistic_number='.$param['logistic_number'].' sku='.$param['sku'].' amount='.$param['amount'];
  449. if(!(isset($OPCmap[$key]) && count($OPCmap[$key]) == 0)){
  450. $innerParam[] = $param;
  451. $OPCCollect->filter(function($item)use($param){
  452. return !($item['logistic_number'] == $param['logistic_number'] &&$item['sku'] == $param['sku'] &&$item['amount'] == $param['amount']) ;
  453. });
  454. }
  455. }
  456. return $innerParam;
  457. }
  458. public function 删选需要修改的OrderPackageCommodities(&$OPCCollect,&$params)
  459. {
  460. $update = collect();
  461. $map = [];
  462. if($OPCCollect == null){return $update;}
  463. foreach ($OPCCollect as $item) {
  464. $key = ' logistic_number='.$item['logistic_number'].' sku='.$item['sku'];
  465. if(!isset($map[$key])){$map[$key] = [];}
  466. $map[$key][] = $item;
  467. }
  468. $paramsCopy = $params->collect();
  469. foreach ($paramsCopy as $param) {
  470. $key = ' logistic_number='.$param['logistic_number'].' sku='.$param['sku'];
  471. if(!isset($map[$key])){continue;}
  472. $item = array_shift($map[$key]);
  473. $params = $params->filter(function($value)use($item){
  474. return !($value['logistic_number'] == $item['logistic_number'] && $value['sku'] == $item['sku']) ;
  475. });
  476. $OPCCollect = $OPCCollect->filter(function($opc)use($item){
  477. return !($opc['logistic_number'] == $item['logistic_number'] && $opc['sku'] == $item['sku'] && $opc['amount'] == $item['amount'] && $item['id'] == $opc['id']);
  478. });
  479. if($item['amount']!= $param['amount']){
  480. $param['id'] = $item['id'];
  481. $update->push($param);
  482. }
  483. }
  484. return $update;
  485. }
  486. public function 删选出删除的OrderPackageCommodities(&$OPCCollect,&$params)
  487. {
  488. $deleteIds = [];
  489. $map = [];
  490. if($OPCCollect == null){return $deleteIds;}
  491. foreach ($params as $item) {
  492. $key = ' logistic_number='.$item['logistic_number'].' sku='.$item['sku'].' amount='.$item['amount'];
  493. if(!isset($map[$key])){
  494. $map[$key] = [];
  495. }
  496. $map[$key][] = $item;
  497. }
  498. foreach ($OPCCollect as $opc) {
  499. $key = ' logistic_number='.$opc['logistic_number'].' sku='.$opc['sku'].' amount='.$opc['amount'];
  500. if(!isset($map[$key]) || count($map[$key])==0){
  501. $deleteIds[] = $opc['id'];
  502. }
  503. }
  504. return $deleteIds;
  505. }
  506. public function 删除OPC以及对应的追踪件($ids)
  507. {
  508. if(!$ids){return;}
  509. $OPCs = OrderPackageCommodities::query()->whereIn('id', $ids)->get();
  510. if($OPCs->count() == 0){return;}
  511. try {
  512. OrderPackageCommodities::query()->whereIn('id', $ids)->delete();
  513. app('LogService')->log(__METHOD__, __FUNCTION__, '删除OrderPackageCommodities ' . count($ids) . json_encode($OPCs));
  514. $orderTracking = OrderTracking::query()->whereIn('order_package_commodity_id',$ids)->get();
  515. app(OrderTrackingService::class)->deleteOrderTracings($orderTracking);
  516. } catch (\Exception $e) {
  517. app('LogService')->log(__METHOD__, __FUNCTION__, '删除OrderPackageCommodities ' . count($ids) . json_encode($OPCs).$e->getMessage().$e->getTraceAsString());
  518. }
  519. }
  520. public function 根据更新数组进行更新($updateParams)
  521. {
  522. if(!isset($updateParams)){return;}
  523. if(is_array($updateParams) && count($updateParams)==0){return;}
  524. $commodity_map = $this->返回Commodity数组_根据数组中sku($updateParams);
  525. $orderPackages_map = app(OrderPackageService::class)->返回OrderPackage数组_根据数组中的快递单号($updateParams);
  526. $dataTime = Carbon::now()->format('Y-m-d H:i:s');
  527. $update_params = [['id','commodity_id','order_package_id','amount','updated_at']];
  528. foreach ($updateParams as $updateParam) {
  529. $commodity_key = ' ownerCode='.($updateParam['ownerCode'] ?? '').' sku='.($updateParam['sku'] ?? '');
  530. $commodity = $commodity_map[$commodity_key] ?? '';
  531. $orderPackage = $orderPackages_map[$updateParam['logistic_number']] ?? '';
  532. $update_params[] = [
  533. 'id' => $updateParam['id'],
  534. 'commodity_id' => $commodity->id ?? '',
  535. 'order_package_id' => $orderPackage->id ?? '',
  536. 'amount' => $updateParam['amount'],
  537. 'updated_at' => $dataTime
  538. ];
  539. }
  540. $this->batchUpdate($update_params);
  541. }
  542. private function 返回Commodity数组_根据数组中sku($params)
  543. {
  544. $skus = array_unique(data_get($params,'*.sku'));
  545. $commodities = Commodity::query()->with('owner')->whereIn('sku',$skus)->get();
  546. $commodity_map = [];
  547. foreach ($commodities as $commodity) {
  548. $key = ' ownerCode='.($commodity->owner->code ?? '').' sku='.$commodity->sku;
  549. $commodity_map[$key] = $commodity;
  550. }
  551. return $commodity_map;
  552. }
  553. public function syncOrderPackageCommodities($orderHeaders)
  554. {
  555. $this->syncOrderPackageCommoditiesByOrderHeaders($orderHeaders);
  556. }
  557. // TODO
  558. public function deleteUnnecessaryOrderCommodities($ids)
  559. {
  560. if(!$ids)return;
  561. if(count($ids)==0)return;
  562. $items = OrderPackageCommodities::query()->whereIn('order_package_id',$ids)->get();
  563. OrderPackageCommodities::query()->whereIn('order_package_id',$ids)->delete();
  564. app('LogService')->log(__METHOD__,__FUNCTION__,'删除 OrderPackageCommodities'.json_encode($items));
  565. }
  566. // TODO
  567. public function syncOrderPackageCommoditiesByOrderHeaders(&$orderHeaders)
  568. {
  569. /**
  570. * @var DataHandlerService $dataHandService
  571. * @var CommodityService $commodityService
  572. * @var OrderPackageService $orderPackageService
  573. * @var OwnerService $ownerService
  574. */
  575. $dataHandService = app('DataHandlerService');
  576. $commodityService = app('CommodityService');
  577. $orderPackageService = app( 'OrderPackageService');
  578. $ownerService = app( 'OwnerService');
  579. if(!$orderHeaders)return ;
  580. $owner_codes = [];$sku_codes = [];$map = [];
  581. foreach ($orderHeaders as $orderHeader) {
  582. $Order_Details = $orderHeader->oracleDOCOrderDetails;
  583. $Order_Details->each(function($item)use(&$owner_codes,&$sku_codes,&$map){
  584. $owner_codes[$item['customerid']] = $item['customerid'];
  585. $sku_codes[$item['sku']] = $item['sku'];
  586. $value = [
  587. 'owner_code'=> $item['customerid'],
  588. 'sku' => $item['sku']
  589. ];
  590. $key = json_encode($value);
  591. $map[$key] = $value;
  592. });
  593. }
  594. if(count($owner_codes) ==0 || count($sku_codes) == 0)return ;
  595. $owners = $ownerService->getOwnerByCodes($owner_codes);
  596. // $commodities = $commodityService->get_(data_get($owners,'*.id'),$sku_codes,[],true); // 预先处理 3s
  597. $commodities = $commodityService->getCommoditiesByMaps($map); // 预先处理 3s
  598. $orderPackages = $orderPackageService->getByWmsOrders($orderHeaders);
  599. $orderCommodities = $this->getByWmsOrder($orderHeaders);
  600. $owner_id_map=[];
  601. foreach ($owners as $owner) {
  602. $owner_id_map[$owner['id']] = $owner;
  603. }
  604. $package_map = $dataHandService->dataHeader(['logistic_number'],$orderPackages);
  605. $orderCommodities = $this->regroupOrderCommodities($orderCommodities);
  606. $orderAllocationDetails = $this->regroupWmsOrderAllocationDetails($orderHeaders);
  607. $del_orderCommodities =[];
  608. $update_params =$this->filterUpdateParams($orderAllocationDetails,$orderCommodities,$del_orderCommodities); // 修改
  609. $inner_params = $this->filterInnerParams($orderAllocationDetails,$orderCommodities); // 创建
  610. $delete_params = $this->filterDeleteParams($orderAllocationDetails,$orderCommodities); // 删除
  611. unset($orderAllocationDetails,$orderCommodities);
  612. if(count($inner_params)>0)
  613. $this->createOrderCommodities($inner_params,$package_map,$commodities,$owner_id_map); // 创建 3s
  614. unset($inner_params,$package_map,$commodities,$owner_id_map);
  615. if(count($update_params)>0)
  616. $this->updateOrderCommodities($update_params); // 更新
  617. unset($update_params);
  618. if(count($delete_params)>0)
  619. $this->deleteOrderCommodities($delete_params); // 删除
  620. unset($delete_params);
  621. if(count($del_orderCommodities)>0)
  622. $this->deleteOrderCommodities($del_orderCommodities); // 删除
  623. unset($del_orderCommodities);
  624. }
  625. // TODO 重组已有的OrderCommodities
  626. public function regroupOrderCommodities(&$orderCommodities)
  627. {
  628. $params = [];
  629. $orderCommodities->each(function($orderCommodity)use(&$params){
  630. $order_no = $orderCommodity->package->order->code ?? '';
  631. $logistic_number = $orderCommodity->package->logistic_number ?? '';
  632. $sku = $orderCommodity->commodity->sku ?? '';
  633. $key = ' orderno='.$order_no.' logsitic_number='.$logistic_number.' sku='.$sku.' ';
  634. $params[$key][] = [
  635. 'id' =>$orderCommodity->id,
  636. 'orderno'=>$order_no,
  637. 'logistic_number'=>$logistic_number,
  638. 'sku' => $sku,
  639. 'amount' => $orderCommodity->amount,
  640. 'order_package_id'=>$orderCommodity->package->id ?? '',
  641. 'commodity_id'=>$orderCommodity->commodity->id ?? '',
  642. ];
  643. });
  644. return $params;
  645. }
  646. // TODO 重组OrderAllocationDetails
  647. public function regroupWmsOrderAllocationDetails(&$orderHeaders)
  648. {
  649. /**
  650. * @var OracleDOCOrderHeader $orderHeader
  651. */
  652. $params =[];
  653. foreach ($orderHeaders as $orderHeader) {
  654. $order_no = $orderHeader->orderno;
  655. $actAllocationDetails = $orderHeader->actAllocationDetails ?? [];
  656. $actAllocationDetails->each(function($item)use(&$params,$order_no){
  657. $logistic_number = $item->picktotraceid;
  658. $sku = $item->sku;
  659. $key = ' orderno='.$order_no.' logsitic_number='.$logistic_number.' sku='.$sku.' ';
  660. if(isset($params[$key])){
  661. $params[$key]['amount']+= $item->qty_each;
  662. }else{
  663. $params[$key] = [
  664. 'orderno'=>$item->orderno,
  665. 'logistic_number'=>$item->picktotraceid,
  666. 'sku' => $sku,
  667. 'amount' => $item->qty_each,
  668. 'owner_code' => $item->customerid
  669. ];
  670. }
  671. });
  672. }
  673. return $params;
  674. }
  675. // TODO 过滤已有的
  676. public function filterUpdateParams(&$orderAllocationDetails,&$orderCommodities,&$del_orderCommodities)
  677. {
  678. $update_params = [];
  679. foreach ($orderCommodities as $key=>$orderCommodity) {
  680. if(isset($orderAllocationDetails[$key])){
  681. if(count($orderCommodity) == 1 && $orderAllocationDetails[$key]['amount'] != $orderCommodity[0]['amount']){
  682. $update_params[$key] = $orderCommodity[0];
  683. $update_params[$key]['amount'] = $orderAllocationDetails[$key]['amount'];
  684. }elseif(count($orderCommodity)>1){
  685. $update_params[$key] = array_shift($orderCommodity);
  686. $update_params[$key][0]['amount'] = $orderAllocationDetails[$key]['amount'];
  687. collect($orderCommodity)->each(function($item)use(&$del_orderCommodities){
  688. $del_orderCommodities[] = $item;
  689. });
  690. }
  691. unset($orderCommodities[$key],$orderAllocationDetails[$key]);
  692. }
  693. }
  694. return $update_params;
  695. }
  696. // TODO 创建
  697. public function filterInnerParams(&$orderAllocationDetails,&$orderCommodities)
  698. {
  699. $inner_params = [];
  700. foreach ($orderAllocationDetails as $key=>$orderAllocationDetail) {
  701. if(!($orderCommodities[$key] ?? false)){
  702. $inner_params[] = $orderAllocationDetail;
  703. unset($orderCommodities[$key],$orderAllocationDetail);
  704. }
  705. }
  706. return $inner_params;
  707. }
  708. // TODO 更新
  709. // public function filterUpdateParams(&$orderAllocationDetails,&$orderCommodities)
  710. // {
  711. // $update_params = [];
  712. // foreach ($orderCommodities as $key => $orderCommodity) {
  713. // if(isset($orderAllocationDetails[$key])){
  714. // if($orderCommodity->amount != $orderAllocationDetails[$key]->amount){
  715. // $update_params[$key] = $orderCommodity[$key]->amount = $orderAllocationDetails[$key]->amount;
  716. // unset($orderCommodity,$orderAllocationDetails[$key]);
  717. // }
  718. // }
  719. // }
  720. // return $update_params;
  721. // }
  722. // TODO
  723. public function filterDeleteParams(&$orderAllocationDetails,&$orderCommodities)
  724. {
  725. $del_params = [];
  726. foreach ($orderCommodities as $key => $orderCommodity) {
  727. if(!isset($orderAllocationDetails[$key])){
  728. collect($orderCommodity)->each(function ($item)use(&$del_params){
  729. $del_params[] = $item;
  730. });
  731. unset($orderCommodities[$key]);
  732. }
  733. }
  734. return $del_params;
  735. }
  736. // TODO 根据数据创建
  737. public function createOrderCommodities(&$inner_params,&$package_map,&$commodities,&$owner_id_map)
  738. {
  739. /** @var DataHandlerService $dataHandlerService */
  740. $dataHandlerService = app('DataHandlerService');
  741. $commodity_map = (function()use(&$commodities,&$owner_id_map){
  742. $map = [];
  743. foreach ($commodities as $commodity) {
  744. $owner = $owner_id_map[$commodity['owner_id']] ;
  745. $key = ' owner='.$owner->code.' sku='.$commodity->sku;
  746. $map[$key] = $commodity;
  747. }
  748. return $map;
  749. })();
  750. $create_params =[];
  751. $date = Carbon::now();
  752. foreach ($inner_params as $inner_param) {
  753. if($inner_param['logistic_number'] === '*' || $inner_param['logistic_number'] === ' ')continue;
  754. $package = $dataHandlerService->getKeyValue(['logistic_number'=>$inner_param['logistic_number']],$package_map);
  755. if(!$package)continue;
  756. $commodity = $commodity_map[' owner='.$inner_param['owner_code'].' sku='.$inner_param['sku']] ?? null;
  757. $create_params[] = [
  758. 'order_package_id'=>$package->id,
  759. 'commodity_id'=>$commodity->id ?? null,
  760. 'amount' => $inner_param['amount'],
  761. 'created_at' => $date,
  762. 'updated_at' => $date
  763. ];
  764. }
  765. if(count($create_params)>0){
  766. $inner_array = array_chunk($create_params,5000);
  767. foreach ($inner_array as $item) {
  768. try {
  769. $bool = $this->insert($item);
  770. LogService::log(__METHOD__, __FUNCTION__, '批量添加 orderCommodity ' . ($bool > 0 ? 'SUCCESS' : 'FAULT') . " || " . count($item) . ' || ' . json_encode($create_params));
  771. } catch (\Exception $e) {
  772. LogService::log(__METHOD__,__FUNCTION__.'error','批量添加 orderCommodity ERROR'." || ".count($item).' || '.json_encode($item). ' || '.json_encode($e->getMessage()). ' || '.json_encode($e->getTraceAsString()));
  773. }
  774. }
  775. }
  776. unset($create_params,$commodity_map);
  777. }
  778. // TODO
  779. public function updateOrderCommodities(&$orderCommodities)
  780. {
  781. if(!$orderCommodities)return ;
  782. $updated_at = Carbon::now();
  783. $update_params = [['id','order_package_id','commodity_id','amount','updated_at']];
  784. foreach ($orderCommodities as $orderCommodity) {
  785. $update_params[] = [
  786. 'id' => $orderCommodity['id'] ?? '',
  787. 'order_package_id'=>$orderCommodity['order_package_id'] ?? '',
  788. 'commodity_id'=>$orderCommodity['commodity_id'] ?? '',
  789. 'amount' => $orderCommodity['amount'],
  790. 'updated_at' => $updated_at
  791. ];
  792. }
  793. if(count($update_params)>1)
  794. $this->batchUpdate($update_params);
  795. unset($update_params);
  796. }
  797. // TODO 删除
  798. public function deleteOrderCommodities(&$orderCommodities){
  799. if(!$orderCommodities)return;
  800. if(count($orderCommodities)==0)return;
  801. try {
  802. $ids = data_get($orderCommodities, '*.id');
  803. if(count($ids) >0){
  804. $bool = OrderPackageCommodities::query()->whereIn('id', data_get($orderCommodities, '*.id'))->delete();
  805. LogService::log(__METHOD__, __FUNCTION__, '批量删除 OrderCommodity ' . ($bool > 0 ? 'SUCCESS' : 'FAULT') .'||' .count($orderCommodities) . ' || ' . json_encode($orderCommodities));
  806. }
  807. } catch (\Exception $e) {
  808. LogService::log(__METHOD__, __FUNCTION__.' error', '批量删除 OrderCommodity Error' . ' || ' . json_encode($e->getMessage()) . ' || ' . json_encode($e->getTraceAsString()));
  809. }
  810. unset($orderCommodities);
  811. }
  812. }