OrderPackageCommoditiesService.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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. // TODO
  554. public function deleteUnnecessaryOrderCommodities($ids)
  555. {
  556. if(!$ids)return;
  557. if(count($ids)==0)return;
  558. $items = OrderPackageCommodities::query()->whereIn('order_package_id',$ids)->get();
  559. OrderPackageCommodities::query()->whereIn('order_package_id',$ids)->delete();
  560. app('LogService')->log(__METHOD__,__FUNCTION__,'删除 OrderPackageCommodities'.json_encode($items));
  561. }
  562. // TODO
  563. public function syncOrderPackageCommodities(&$orderHeaders)
  564. {
  565. /**
  566. * @var DataHandlerService $dataHandService
  567. * @var CommodityService $commodityService
  568. * @var OrderPackageService $orderPackageService
  569. * @var OwnerService $ownerService
  570. */
  571. $dataHandService = app('DataHandlerService');
  572. $commodityService = app('CommodityService');
  573. $orderPackageService = app( 'OrderPackageService');
  574. $ownerService = app( 'OwnerService');
  575. if(!$orderHeaders)return ;
  576. $map = [];
  577. foreach ($orderHeaders as $orderHeader) {
  578. $Order_Details = $orderHeader->oracleDOCOrderDetails;
  579. $Order_Details->each(function($item)use(&$map){
  580. if(!empty($item['customerid']) && !empty($item['sku'])){
  581. $key = "owner_code_{$item['customerid']}_sku_{$item['sku']}";
  582. $map[$key] = ['owner_code'=>$item['customerid'],'sku'=>$item['sku']]; // 货主编码 sku编码
  583. }
  584. });
  585. }
  586. $commodities = $commodityService->getCommoditiesByMap($map); // 预先处理 3s
  587. $orderPackages = $orderPackageService->getByWmsOrders($orderHeaders);
  588. $orderCommodities = $this->getByWmsOrder($orderHeaders);
  589. $owner_code_map = [];
  590. $owner_id_map = (function()use(&$orderHeaders,&$ownerService,&$owner_code_map){
  591. $owner_id_map = [];
  592. $owners = $ownerService->getOwnerByCodes(array_unique(data_get($orderHeaders,'*.customerid')));
  593. if(count($owners) == 0) return $owner_id_map;
  594. foreach ($owners as $owner) {
  595. $owner_id_map[$owner['id']] = $owner;
  596. $owner_code_map[$owner['code']] = $owner;
  597. }
  598. return $owner_id_map;
  599. })();
  600. $package_map = $dataHandService->dataHeader(['logistic_number'],$orderPackages);
  601. $orderCommodities = $this->regroupOrderCommodities($orderCommodities);
  602. $orderAllocationDetails = $this->regroupWmsOrderAllocationDetails($orderHeaders);
  603. $del_orderCommodities =[];
  604. $update_params =$this->filterUpdateParams($orderAllocationDetails,$orderCommodities,$del_orderCommodities); // 修改
  605. $inner_params = $this->filterInnerParams($orderAllocationDetails,$orderCommodities); // 创建
  606. $delete_params = $this->filterDeleteParams($orderAllocationDetails,$orderCommodities); // 删除
  607. if(count($inner_params)>0)
  608. $this->createOrderCommodities($inner_params,$package_map,$commodities,$owner_id_map); // 创建 3s
  609. if(count($update_params)>0)
  610. $this->updateOrderCommodities($update_params); // 更新
  611. if(count($delete_params)>0)
  612. $this->deleteOrderCommodities($delete_params); // 删除
  613. if(count($del_orderCommodities)>0)
  614. $this->deleteOrderCommodities($del_orderCommodities); // 删除
  615. }
  616. // TODO 重组已有的OrderCommodities
  617. public function regroupOrderCommodities(&$orderCommodities)
  618. {
  619. $params = [];
  620. $orderCommodities->each(function($orderCommodity)use(&$params){
  621. $order_no = $orderCommodity->package->order->code ?? '';
  622. $logistic_number = $orderCommodity->package->logistic_number ?? '';
  623. $sku = $orderCommodity->commodity->sku ?? '';
  624. $key = ' orderno='.$order_no.' logsitic_number='.$logistic_number.' sku='.$sku.' ';
  625. $params[$key][] = [
  626. 'id' =>$orderCommodity->id,
  627. 'orderno'=>$order_no,
  628. 'logistic_number'=>$logistic_number,
  629. 'sku' => $sku,
  630. 'amount' => $orderCommodity->amount,
  631. 'order_package_id'=>$orderCommodity->package->id ?? '',
  632. 'commodity_id'=>$orderCommodity->commodity->id ?? '',
  633. ];
  634. });
  635. return $params;
  636. }
  637. // TODO 重组OrderAllocationDetails
  638. public function regroupWmsOrderAllocationDetails(&$orderHeaders)
  639. {
  640. /**
  641. * @var OracleDOCOrderHeader $orderHeader
  642. */
  643. $params =[];
  644. foreach ($orderHeaders as $orderHeader) {
  645. $order_no = $orderHeader->orderno;
  646. $actAllocationDetails = $orderHeader->actAllocationDetails ?? [];
  647. $actAllocationDetails->each(function($item)use(&$params,$order_no){
  648. $logistic_number = $item->picktotraceid;
  649. $sku = $item->sku;
  650. $key = ' orderno='.$order_no.' logsitic_number='.$logistic_number.' sku='.$sku.' ';
  651. if(isset($params[$key])){
  652. $params[$key]['amount']+= $item->qty_each;
  653. }else{
  654. $params[$key] = [
  655. 'orderno'=>$item->orderno,
  656. 'logistic_number'=>$item->picktotraceid,
  657. 'sku' => $sku,
  658. 'amount' => $item->qty_each,
  659. 'owner_code' => $item->customerid
  660. ];
  661. }
  662. });
  663. }
  664. return $params;
  665. }
  666. // TODO 过滤已有的
  667. public function filterUpdateParams(&$orderAllocationDetails,&$orderCommodities,&$del_orderCommodities)
  668. {
  669. $update_params = [];
  670. foreach ($orderCommodities as $key=>$orderCommodity) {
  671. if(isset($orderAllocationDetails[$key])){
  672. if(count($orderCommodity) == 1 && $orderAllocationDetails[$key]['amount'] != $orderCommodity[0]['amount']){
  673. $update_params[$key] = $orderCommodity[0];
  674. $update_params[$key]['amount'] = $orderAllocationDetails[$key]['amount'];
  675. }elseif(count($orderCommodity)>1){
  676. $update_params[$key] = array_shift($orderCommodity);
  677. $update_params[$key][0]['amount'] = $orderAllocationDetails[$key]['amount'];
  678. collect($orderCommodity)->each(function($item)use(&$del_orderCommodities){
  679. $del_orderCommodities[] = $item;
  680. });
  681. }
  682. unset($orderCommodities[$key],$orderAllocationDetails[$key]);
  683. }
  684. }
  685. return $update_params;
  686. }
  687. // TODO 创建
  688. public function filterInnerParams(&$orderAllocationDetails,&$orderCommodities)
  689. {
  690. $inner_params = [];
  691. foreach ($orderAllocationDetails as $key=>$orderAllocationDetail) {
  692. if(!($orderCommodities[$key] ?? false)){
  693. $inner_params[] = $orderAllocationDetail;
  694. unset($orderCommodities[$key],$orderAllocationDetail);
  695. }
  696. }
  697. return $inner_params;
  698. }
  699. // TODO 更新
  700. // public function filterUpdateParams(&$orderAllocationDetails,&$orderCommodities)
  701. // {
  702. // $update_params = [];
  703. // foreach ($orderCommodities as $key => $orderCommodity) {
  704. // if(isset($orderAllocationDetails[$key])){
  705. // if($orderCommodity->amount != $orderAllocationDetails[$key]->amount){
  706. // $update_params[$key] = $orderCommodity[$key]->amount = $orderAllocationDetails[$key]->amount;
  707. // unset($orderCommodity,$orderAllocationDetails[$key]);
  708. // }
  709. // }
  710. // }
  711. // return $update_params;
  712. // }
  713. // TODO
  714. public function filterDeleteParams(&$orderAllocationDetails,&$orderCommodities)
  715. {
  716. $del_params = [];
  717. foreach ($orderCommodities as $key => $orderCommodity) {
  718. if(!isset($orderAllocationDetails[$key])){
  719. collect($orderCommodity)->each(function ($item)use(&$del_params){
  720. $del_params[] = $item;
  721. });
  722. unset($orderCommodities[$key]);
  723. }
  724. }
  725. return $del_params;
  726. }
  727. // TODO 根据数据创建
  728. public function createOrderCommodities(&$inner_params,&$package_map,&$commodities,&$owner_id_map)
  729. {
  730. /** @var DataHandlerService $dataHandlerService */
  731. $dataHandlerService = app('DataHandlerService');
  732. $commodity_map = (function()use(&$commodities,&$owner_id_map){
  733. $map = [];
  734. foreach ($commodities as $commodity) {
  735. $owner = $owner_id_map[$commodity['owner_id']] ;
  736. $key = ' owner='.$owner->code.' sku='.$commodity->sku;
  737. $map[$key] = $commodity;
  738. }
  739. return $map;
  740. })();
  741. $create_params =[];
  742. $date = Carbon::now();
  743. foreach ($inner_params as $inner_param) {
  744. if($inner_param['logistic_number'] === '*' || $inner_param['logistic_number'] === ' ')continue;
  745. $package = $dataHandlerService->getKeyValue(['logistic_number'=>$inner_param['logistic_number']],$package_map);
  746. if(!$package)continue;
  747. $commodity = $commodity_map[' owner='.$inner_param['owner_code'].' sku='.$inner_param['sku']] ?? null;
  748. $create_params[] = [
  749. 'order_package_id'=>$package->id,
  750. 'commodity_id'=>$commodity->id ?? null,
  751. 'amount' => $inner_param['amount'],
  752. 'created_at' => $date,
  753. 'updated_at' => $date
  754. ];
  755. }
  756. if(count($create_params)>0){
  757. $inner_array = array_chunk($create_params,5000);
  758. foreach ($inner_array as $item) {
  759. try {
  760. $bool = $this->insert($item);
  761. LogService::log(__METHOD__, __FUNCTION__, '批量添加 orderCommodity ' . ($bool > 0 ? 'SUCCESS' : 'FAULT') . " || " . count($item) . ' || ' . json_encode($create_params));
  762. } catch (\Exception $e) {
  763. LogService::log(__METHOD__,__FUNCTION__.'error','批量添加 orderCommodity ERROR'." || ".count($item).' || '.json_encode($item). ' || '.json_encode($e->getMessage()). ' || '.json_encode($e->getTraceAsString()));
  764. }
  765. }
  766. }
  767. }
  768. // TODO
  769. public function updateOrderCommodities(&$orderCommodities)
  770. {
  771. if(!$orderCommodities)return ;
  772. $updated_at = Carbon::now();
  773. $update_params = [['id','order_package_id','commodity_id','amount','updated_at']];
  774. foreach ($orderCommodities as $orderCommodity) {
  775. $update_params[] = [
  776. 'id' => $orderCommodity['id'] ?? '',
  777. 'order_package_id'=>$orderCommodity['order_package_id'] ?? '',
  778. 'commodity_id'=>$orderCommodity['commodity_id'] ?? '',
  779. 'amount' => $orderCommodity['amount'],
  780. 'updated_at' => $updated_at
  781. ];
  782. }
  783. if(count($update_params)>1)
  784. $this->batchUpdate($update_params);
  785. }
  786. // TODO 删除
  787. public function deleteOrderCommodities(&$orderCommodities){
  788. if(!$orderCommodities)return;
  789. if(count($orderCommodities)==0)return;
  790. try {
  791. $ids = data_get($orderCommodities, '*.id');
  792. if(count($ids) >0){
  793. $bool = OrderPackageCommodities::query()->whereIn('id', data_get($orderCommodities, '*.id'))->delete();
  794. LogService::log(__METHOD__, __FUNCTION__, '批量删除 OrderCommodity ' . ($bool > 0 ? 'SUCCESS' : 'FAULT') .'||' .count($orderCommodities) . ' || ' . json_encode($orderCommodities));
  795. }
  796. } catch (\Exception $e) {
  797. LogService::log(__METHOD__, __FUNCTION__.' error', '批量删除 OrderCommodity Error' . ' || ' . json_encode($e->getMessage()) . ' || ' . json_encode($e->getTraceAsString()));
  798. }
  799. }
  800. }