OrderPackageCommoditiesService.php 37 KB

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