OrderPackageCommoditiesService.php 33 KB

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