OrderCommodityService.php 8.3 KB

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