OrderCommodityService.php 8.2 KB

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