OrderPackageCommoditiesService.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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. public function createByOrderHeader($orderHeaders)
  451. {
  452. $orderPackageCommodities = $this->getByWmsOrder($orderHeaders);
  453. $OPCCollects = $this->将orderPackageCommodity抽象成数组($orderPackageCommodities);
  454. $creatParams = $this->返回创建数组_WMS订单($orderHeaders);
  455. $this->数据重组($OPCCollects,$creatParams);
  456. $collect = $this->删选需要修改的OrderPackageCommodities($OPCCollect,$creatParam);
  457. $ids = $this->删选出删除的OrderPackageCommodities($OPCCollect,$creatParam);
  458. $insertParam = $this->删选出需要添加的OrderPackageCommodities($OPCCollect,$creatParam);
  459. $updateParams = [];
  460. foreach ($collect as $item) {
  461. $updateParams[] = $item;
  462. }
  463. $this->根据更新数组进行更新($updateParams);
  464. $this->删除OPC以及对应的追踪件($ids);
  465. $creatParams = $this->生成OrderPackageCommodities_基于创建数组($insertParam);
  466. $this->create($creatParams);
  467. }
  468. /**
  469. * @param $orderHeaders
  470. */
  471. public function 更新OPC_根据WMS订单($orderHeaders)
  472. {
  473. if(!$orderHeaders){return ;}
  474. if(count($orderHeaders) == 0){return ;}
  475. $orderPackageCommodities = $this->getByWmsOrder($orderHeaders);
  476. $OPCCollects = $this->将orderPackageCommodity抽象成数组($orderPackageCommodities);
  477. $creatParams = $this->返回创建数组_WMS订单($orderHeaders);
  478. $orderNos = $this->数据重组($OPCCollects,$creatParams);
  479. $updateParams= collect();$deleteIds = [];$insertParams =[];
  480. foreach ($orderNos as $orderNo) {
  481. $OPCCollect = $OPCCollects[$orderNo] ?? null;
  482. $creatParam = $creatParams[$orderNo];
  483. $retain[] = $this->删选可以保留的OrderPackageCommodities($OPCCollect,$creatParam);
  484. $collect = $this->删选需要修改的OrderPackageCommodities($OPCCollect,$creatParam);
  485. $ids = $this->删选出删除的OrderPackageCommodities($OPCCollect,$creatParam);
  486. $insertParam = $this->删选出需要添加的OrderPackageCommodities($OPCCollect,$creatParam);
  487. $deleteIds = array_merge($deleteIds,$ids);
  488. foreach ($collect as $item) {
  489. $updateParams->push($item);
  490. }
  491. foreach ($insertParam as $item) {
  492. $insertParams[]= $item;
  493. }
  494. }
  495. $this->根据更新数组进行更新($updateParams);
  496. $this->删除OPC以及对应的追踪件($deleteIds);
  497. $creatParams = $this->生成OrderPackageCommodities_基于创建数组($insertParams);
  498. $this->create($creatParams);
  499. }
  500. /**
  501. * @param Collection $orderPackageCommodities
  502. * @return Collection|\Tightenco\Collect\Support\Collection
  503. */
  504. public function 将orderPackageCommodity抽象成数组($orderPackageCommodities)
  505. {
  506. /** @var DataHandlerService $dataHandlerService */
  507. $dataHandlerService = app(DataHandlerService::class);
  508. $collect = collect();
  509. $ownerIds = array_unique(data_get($orderPackageCommodities,'*.commodity.owner_id'));
  510. $owners = Owner::query()->whereIn('id',$ownerIds)->get();
  511. $owner_id_map = $dataHandlerService->dataHeader(['id'],$owners);
  512. foreach ($orderPackageCommodities as $orderPackageCommodity) {
  513. $owner = $dataHandlerService->getKeyValue(['id'=>$orderPackageCommodity->commodity->owner_id],$owner_id_map);
  514. $params = [
  515. 'id' => $orderPackageCommodity->id,
  516. 'ownerCode' => $owner->code,
  517. 'orderNo' => $orderPackageCommodity->package->order->code,
  518. 'sku' => $orderPackageCommodity->commodity->sku,
  519. 'amount' => $orderPackageCommodity->amount,
  520. 'logistic_number' => $orderPackageCommodity->package->logistic_number,
  521. ];
  522. $collect->push($params);
  523. }
  524. return $collect;
  525. }
  526. public function 数据重组(&$OPCCollects,&$params)
  527. {
  528. $collect_map = [];$params_map = [];$order_no_map = [];
  529. foreach ($OPCCollects as $item) {
  530. $orderNo = $item['orderNo'];
  531. if(!isset($collect_map[$orderNo])){$collect_map[$orderNo] = collect();}
  532. $collect_map[$orderNo]->push($item);
  533. }
  534. foreach ($params as $param) {
  535. $orderNo = $param['orderNo'];
  536. if(!isset($params_map[$orderNo])){$params_map[$orderNo] = collect();}
  537. $order_no_map[] = $orderNo;
  538. $params_map[$orderNo]->push($param);
  539. }
  540. $order_no_map = array_unique($order_no_map);
  541. $OPCCollects = $collect_map;
  542. $params = $params_map;
  543. return $order_no_map;
  544. }
  545. public function 删选可以保留的OrderPackageCommodities(&$OPCCollect,&$params)
  546. {
  547. $retain = collect();
  548. if($OPCCollect == null){return $retain;}
  549. $map = [];
  550. foreach ($params as $param) {
  551. $key = ' logistic_number='.$param['logistic_number'].' sku='.$param['sku'].' amount='.$param['amount'];
  552. if(! isset($map[$key])){
  553. $map[$key] = [];
  554. }
  555. $map[$key][] = $param;
  556. }
  557. foreach ($OPCCollect as $item) {
  558. $key = ' logistic_number='.$item['logistic_number'].' sku='.$item['sku'].' amount='.$item['amount'];
  559. if(isset($map[$key]) && count($map[$key]) > 0){
  560. $value = $map[$key][0];
  561. array_shift($map[$key]);
  562. $params->filter(function($param)use($value){
  563. return count(array_diff_assoc($param,$value)) != 0 ;
  564. });
  565. $retain->push($value);
  566. }
  567. }
  568. return $retain;
  569. }
  570. public function 删选出需要添加的OrderPackageCommodities(&$OPCCollect,&$params)
  571. {
  572. $innerParam = [];
  573. $OPCmap = [];
  574. if($OPCCollect == null){return $params;}
  575. foreach ($OPCCollect as $item) {
  576. $key = ' logistic_number='.$item['logistic_number'].' sku='.$item['sku'].' amount='.$item['amount'];
  577. if(!isset($OPCmap[$key])){
  578. $OPCmap[$key][] = [];
  579. }
  580. $OPCmap[$key][] = $item;
  581. }
  582. $paramsMap = [];
  583. foreach ($params as $param) {
  584. $key = ' logistic_number='.$param['logistic_number'].' sku='.$param['sku'].' amount='.$param['amount'];
  585. if(!isset($OPCmap[$key])){
  586. $paramsMap[$key][] = [];
  587. }
  588. $paramsMap[$key][] = $param;
  589. }
  590. $paramsCopy = $params->collect();
  591. foreach ($paramsCopy as $param) {
  592. $key = ' logistic_number='.$param['logistic_number'].' sku='.$param['sku'].' amount='.$param['amount'];
  593. if(!(isset($OPCmap[$key]) && count($OPCmap[$key]) == 0)){
  594. $innerParam[] = $param;
  595. $OPCCollect->filter(function($item)use($param){
  596. return !($item['logistic_number'] == $param['logistic_number'] &&$item['sku'] == $param['sku'] &&$item['amount'] == $param['amount']) ;
  597. });
  598. }
  599. }
  600. return $innerParam;
  601. }
  602. public function 删选需要修改的OrderPackageCommodities(&$OPCCollect,&$params)
  603. {
  604. $update = collect();
  605. $map = [];
  606. if($OPCCollect == null){return $update;}
  607. foreach ($OPCCollect as $item) {
  608. $key = ' logistic_number='.$item['logistic_number'].' sku='.$item['sku'];
  609. if(!isset($map[$key])){$map[$key] = [];}
  610. $map[$key][] = $item;
  611. }
  612. $paramsCopy = $params->collect();
  613. foreach ($paramsCopy as $param) {
  614. $key = ' logistic_number='.$param['logistic_number'].' sku='.$param['sku'];
  615. if(!isset($map[$key])){continue;}
  616. $item = array_shift($map[$key]);
  617. $params = $params->filter(function($value)use($item){
  618. return !($value['logistic_number'] == $item['logistic_number'] && $value['sku'] == $item['sku']) ;
  619. });
  620. $OPCCollect = $OPCCollect->filter(function($opc)use($item){
  621. return !($opc['logistic_number'] == $item['logistic_number'] && $opc['sku'] == $item['sku'] && $opc['amount'] == $item['amount'] && $item['id'] == $opc['id']);
  622. });
  623. if($item['amount']!= $param['amount']){
  624. $param['id'] = $item['id'];
  625. $update->push($param);
  626. }
  627. }
  628. return $update;
  629. }
  630. public function 删选出删除的OrderPackageCommodities(&$OPCCollect,&$params)
  631. {
  632. $deleteIds = [];
  633. $map = [];
  634. if($OPCCollect == null){return $deleteIds;}
  635. foreach ($params as $item) {
  636. $key = ' logistic_number='.$item['logistic_number'].' sku='.$item['sku'].' amount='.$item['amount'];
  637. if(!isset($map[$key])){
  638. $map[$key] = [];
  639. }
  640. $map[$key][] = $item;
  641. }
  642. foreach ($OPCCollect as $opc) {
  643. $key = ' logistic_number='.$opc['logistic_number'].' sku='.$opc['sku'].' amount='.$opc['amount'];
  644. if(!isset($map[$key]) || count($map[$key])==0){
  645. $deleteIds[] = $opc['id'];
  646. }
  647. }
  648. return $deleteIds;
  649. }
  650. public function 删除OPC以及对应的追踪件($ids)
  651. {
  652. if(!$ids){return;}
  653. $OPCs = OrderPackageCommodities::query()->whereIn('id', $ids)->get();
  654. if($OPCs->count() == 0){return;}
  655. try {
  656. OrderPackageCommodities::query()->whereIn('id', $ids)->delete();
  657. LogService::log(__METHOD__, __FUNCTION__, '删除OrderPackageCommodities ' . count($ids) . json_encode($OPCs));
  658. $orderTracking = OrderTracking::query()->whereIn('order_package_commodity_id',$ids)->get();
  659. app(OrderTrackingService::class)->deleteOrderTracings($orderTracking);
  660. } catch (\Exception $e) {
  661. LogService::log(__METHOD__, __FUNCTION__, '删除OrderPackageCommodities ' . count($ids) . json_encode($OPCs).$e->getMessage().$e->getTraceAsString());
  662. }
  663. }
  664. public function 根据更新数组进行更新($updateParams)
  665. {
  666. if(!isset($updateParams)){return;}
  667. if(is_array($updateParams) && count($updateParams)==0){return;}
  668. $commodity_map = $this->返回Commodity数组_根据数组中sku($updateParams);
  669. $orderPackages_map = app(OrderPackageService::class)->返回OrderPackage数组_根据数组中的快递单号($updateParams);
  670. $dataTime = Carbon::now()->format('Y-m-d H:i:s');
  671. $update_params = [['id','commodity_id','order_package_id','amount','updated_at']];
  672. foreach ($updateParams as $updateParam) {
  673. $commodity_key = ' ownerCode='.($updateParam['ownerCode'] ?? '').' sku='.($updateParam['sku'] ?? '');
  674. $commodity = $commodity_map[$commodity_key] ?? '';
  675. $orderPackage = $orderPackages_map[$updateParam['logistic_number']] ?? '';
  676. $update_params[] = [
  677. 'id' => $updateParam['id'],
  678. 'commodity_id' => $commodity->id ?? '',
  679. 'order_package_id' => $orderPackage->id ?? '',
  680. 'amount' => $updateParam['amount'],
  681. 'updated_at' => $dataTime
  682. ];
  683. }
  684. $this->batchUpdate($update_params);
  685. }
  686. private function 返回Commodity数组_根据数组中sku($params)
  687. {
  688. $skus = array_unique(data_get($params,'*.sku'));
  689. $commodities = Commodity::query()->with('owner')->whereIn('sku',$skus)->get();
  690. $commodity_map = [];
  691. foreach ($commodities as $commodity) {
  692. $key = ' ownerCode='.($commodity->owner->code ?? '').' sku='.$commodity->sku;
  693. $commodity_map[$key] = $commodity;
  694. }
  695. return $commodity_map;
  696. }
  697. }