OrderCommodityService.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace App\Services;
  3. use App\Order;
  4. use App\OrderCommodity;
  5. use App\Services\common\DataHandlerService;
  6. use Carbon\Carbon;
  7. Class OrderCommodityService
  8. {
  9. public function insert($innerParams){
  10. if(!$innerParams)return false;
  11. if(count($innerParams)==0)return false;
  12. try {
  13. $bool = OrderCommodity::query()->insert($innerParams);
  14. if ($bool) app('LogService')->log(__METHOD__, __FUNCTION__, '批量添加 OrderCommodities SUCCESS' . ' || ' . count($innerParams) . ' || ' . json_encode($innerParams));
  15. else app('LogService')->log(__METHOD__, __FUNCTION__, '批量添加 OrderCommodities FAULT' . ' || ' . count($innerParams) . ' || ' . json_encode($innerParams));
  16. return $bool;
  17. } catch (\Exception $e) {
  18. app('LogService')->log(__METHOD__, __FUNCTION__, '批量添加 OrderCommodities ERROR'. ' || ' . count($innerParams) . ' || ' . json_encode($innerParams).' || '.json_encode($e->getMessage()).' || '.json_encode($e->getTraceAsString()));
  19. return false;
  20. }
  21. }
  22. // TODO 根据传入的$orderHeaders 同步订单
  23. public function syncOrderCommodity(&$orderHeaders)
  24. {
  25. /**
  26. * @var OwnerService $ownerService
  27. * @var CommodityService $commodityService
  28. * @var DataHandlerService $dataHandlerService
  29. */
  30. $ownerService = app('OwnerService');
  31. $commodityService = app('CommodityService');
  32. $dataHandlerService = app('DataHandlerService');
  33. $orderNos = data_get($orderHeaders,'*.orderno');
  34. $owner_codes = [];$sku_codes = [];
  35. foreach ($orderHeaders as $orderHeader) {
  36. $actAllocationDetails = $orderHeader->actAllocationDetails;
  37. foreach ($actAllocationDetails as $actAllocationDetail) {
  38. $owner_codes[$actAllocationDetail['customerid']] = $actAllocationDetail['customerid'];
  39. $sku_codes[$actAllocationDetail['sku']] = $actAllocationDetail['sku'];
  40. }
  41. }
  42. if(count($sku_codes) ==0)return;
  43. $owners = $ownerService->getOwnerByCodes($owner_codes);
  44. $owner_id_maps = $dataHandlerService->dataHeader(['id'],$owners);
  45. $commodities = $commodityService->get_(data_get($owners,'*.id'),$sku_codes,[],true);
  46. $commodity_map = [];
  47. foreach ($commodities as $commodity) {
  48. $owner = $dataHandlerService->getKeyValue(['id'=>$commodity->owner_id],$owner_id_maps);
  49. $key = "_owner_code_{$owner['code']}_sku_{$commodity['sku']}";
  50. $commodity_map[$key] = $commodity;
  51. }
  52. $orderCommodities = OrderCommodity::query()->with(['order','commodity'])->whereIn('order_id',function ($query)use($orderNos){
  53. $query->from('orders')->select('id')->whereIn('code',$orderNos);
  54. })->get();
  55. $orders = Order::query()->with('packages.commodities')->whereIn('code',$orderNos)->get();
  56. $order_code_map = $dataHandlerService->dataHeader(['code'],$orders);
  57. $ActAllocationDetail_maps = $this->getRegroupActAllocationDetails($orderHeaders);
  58. $orderCommodity_maps = $this->getRegroupOrderCommodities($orderCommodities,$owner_id_maps);
  59. $delete_ids = $this->getDeleteIds($ActAllocationDetail_maps,$orderCommodity_maps);
  60. $this->filterHasExist($ActAllocationDetail_maps,$orderCommodity_maps);
  61. $create_params = $this->getCreateParams($ActAllocationDetail_maps,$orderCommodity_maps);
  62. $inner_params = $this->getInnerParamsByParams($create_params,$order_code_map,$commodity_map);
  63. if(count($inner_params)>0){
  64. $inner_params = array_chunk($inner_params,4000);
  65. foreach ($inner_params as $inner_param) {
  66. $this->insert($inner_param);
  67. }
  68. }
  69. if(count($delete_ids)==0)return;
  70. OrderCommodity::query()->whereIn('id',$delete_ids)->delete();
  71. app('LogService')->log(__METHOD__,__FUNCTION__,"delete OrderCommodity ".json_encode($delete_ids));
  72. }
  73. // TODO 将拉取的OrderHeader 重组为符合当前基准的数组
  74. public function getRegroupActAllocationDetails(&$orderHeaders)
  75. {
  76. $map = [];
  77. if($orderHeaders->count()==0) return $map;
  78. $orderHeaders->each(function($orderHeader)use(&$map){
  79. $orderHeader->actAllocationDetails->each(function ($details)use(&$map){
  80. $key = "order_{$details['orderno']}_owner_code_{$details['customerid']}_sku_{$details['sku']}_location_{$details['location']}";
  81. if(empty($map[$key]))$map[$key]=[];
  82. $map[$key][] = [
  83. 'code' => $details['orderno'],
  84. 'sku' => $details['sku'],
  85. 'owner_code'=> $details['customerid'],
  86. 'amount' => $details['qty_each'],
  87. 'location' => $details['location']
  88. ];
  89. });
  90. });
  91. return $map;
  92. }
  93. // TODO 过滤已有
  94. public function filterHasExist(&$ActAllocationDetail_maps,&$orderCommodity_maps)
  95. {
  96. foreach ($ActAllocationDetail_maps as $key=>$actAllocationDetail_map) {
  97. if(isset($orderCommodity_maps[$key]))unset($ActAllocationDetail_maps[$key]);
  98. }
  99. }
  100. // TODO 将传入OrderCommodities 重组为符合当前基准的数组
  101. public function getRegroupOrderCommodities(&$orderCommodities,$owner_id_maps)
  102. {
  103. /** @var DataHandlerService $dataHandlerService */
  104. $dataHandlerService = app(DataHandlerService::class);
  105. $map = [];
  106. if($orderCommodities->count() == 0)return $map;
  107. $orderCommodities->each(function($orderCommodity)use(&$map,$owner_id_maps,$dataHandlerService){
  108. $order = $orderCommodity->order;
  109. $owner = $dataHandlerService->getKeyValue(['id'=>$order->owner_id ?? ''],$owner_id_maps);
  110. $key = "order_{$order->code}_owner_code_{$owner->code}_sku_{$orderCommodity->commodity->sku}_location_{$orderCommodity->location}";
  111. if(empty($map[$key]))$map[$key]=[];
  112. $map[$key][] =[
  113. 'id' => $orderCommodity->id,
  114. 'code' => $orderCommodity->order->code ?? '',
  115. 'sku' => $orderCommodity->commodity->sku,
  116. 'owner_code' => $owner->code ?? '',
  117. 'amount' => $orderCommodity->amount,
  118. 'location' => $orderCommodity->location,
  119. ];
  120. });
  121. return $map;
  122. }
  123. // TODO 返回修改删除的
  124. public function getDeleteIds(&$ActAllocationDetail_maps,&$orderCommodity_maps)
  125. {
  126. $ids = [];
  127. foreach ($orderCommodity_maps as $key=>$orderCommodity_map) {
  128. if(isset($ActAllocationDetail_maps[$key]))continue;
  129. foreach ($orderCommodity_map as $key1=>$orderCommodity) {
  130. $ids[] = $orderCommodity['id'];
  131. unset($orderCommodity_map[$key1]);
  132. }
  133. }
  134. return $ids;
  135. }
  136. // TODO 返回创建的数组
  137. public function getCreateParams(&$ActAllocationDetail_maps,&$orderCommodity_maps)
  138. {
  139. $params = [];
  140. foreach ($ActAllocationDetail_maps as $key=>$actAllocationDetail_map) {
  141. if(isset($orderCommodity_maps[$key]))continue;
  142. foreach ($actAllocationDetail_map as $actAllocationDetail) {
  143. $params[] = $actAllocationDetail;
  144. }
  145. }
  146. return $params;
  147. }
  148. // TODO 根据创建数组返回可以使用的插入数组
  149. public function getInnerParamsByParams(&$create_params,&$order_code_map,&$commodity_map)
  150. {
  151. /** @var DataHandlerService $dataHandlerService */
  152. $dataHandlerService = app(DataHandlerService::class);
  153. $inner_params = [];
  154. $date = (string) Carbon::now();
  155. foreach ($create_params as $item) {
  156. $order = $dataHandlerService->getKeyValue(['code'=>$item['code']],$order_code_map);
  157. $key = "_owner_code_{$item['owner_code']}_sku_{$item['sku']}";
  158. $commodity = $commodity_map[$key] ?? null;
  159. $inner_params[] = [
  160. 'order_id' => $order['id'] ?? '',
  161. 'commodity_id' => $commodity['id'] ?? '',
  162. 'amount' => $item['amount'],
  163. 'location' => $item['location'],
  164. 'created_at' => $date,
  165. 'updated_at' => $date
  166. ];
  167. }
  168. return $inner_params;
  169. }
  170. }