OrderCommodityService.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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']}_amount_{$details['qty_each']}";
  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. $orderCommodity_map = $orderCommodity_maps[$key] ?? null;
  98. if($orderCommodity_map == null)continue;
  99. foreach ($actAllocationDetail_map as $index=>$item) {
  100. if(count($orderCommodity_map)==0)break;
  101. array_shift($orderCommodity_map);
  102. unset($actAllocationDetail_map[$index]);
  103. }
  104. }
  105. }
  106. // TODO 将传入OrderCommodities 重组为符合当前基准的数组
  107. public function getRegroupOrderCommodities(&$orderCommodities,$owner_id_maps)
  108. {
  109. /** @var DataHandlerService $dataHandlerService */
  110. $dataHandlerService = app(DataHandlerService::class);
  111. $map = [];
  112. if($orderCommodities->count() == 0)return $map;
  113. $orderCommodities->each(function($orderCommodity)use(&$map,$owner_id_maps,$dataHandlerService){
  114. $order = $orderCommodity->order;
  115. $owner = $dataHandlerService->getKeyValue(['id'=>$order->owner_id ?? ''],$owner_id_maps);
  116. $key = "order_{$order->code}_owner_code_{$owner->code}_sku_{$orderCommodity->commodity->sku}_location_{$orderCommodity->location}_amount_{$orderCommodity->amount}";
  117. if(empty($map[$key]))$map[$key]=[];
  118. $map[$key][] =[
  119. 'id' => $orderCommodity->id,
  120. 'code' => $orderCommodity->order->code ?? '',
  121. 'sku' => $orderCommodity->commodity->sku,
  122. 'owner_code' => $owner->code ?? '',
  123. 'amount' => $orderCommodity->amount,
  124. 'location' => $orderCommodity->location,
  125. ];
  126. });
  127. return $map;
  128. }
  129. // TODO 返回修改删除的
  130. public function getDeleteIds(&$ActAllocationDetail_maps,&$orderCommodity_maps)
  131. {
  132. $ids = [];
  133. if(count($orderCommodity_maps)==0)return $ids;
  134. foreach ($orderCommodity_maps as $key=>$orderCommodity_map) {
  135. if(array_key_exists($key,$orderCommodity_maps))continue;
  136. $ActAllocationDetail_map = $ActAllocationDetail_maps[$key];
  137. if(count($ActAllocationDetail_map) == count($orderCommodity_map))continue;
  138. if(count($ActAllocationDetail_map) < count($orderCommodity_map)){
  139. foreach ($orderCommodity_map as $index=>$item) {
  140. if(count($ActAllocationDetail_map) == 0)$ids[] =$item['id'];
  141. array_shift($ActAllocationDetail_map);
  142. unset($orderCommodity_map[$index]);
  143. }
  144. }
  145. }
  146. return $ids;
  147. }
  148. // TODO 返回创建的数组
  149. public function getCreateParams(&$ActAllocationDetail_maps,&$orderCommodity_maps)
  150. {
  151. $params = [];
  152. foreach ($ActAllocationDetail_maps as $key=>$actAllocationDetail_map) {
  153. $orderCommodity_map = $orderCommodity_maps[$key] ?? null;
  154. if($orderCommodity_map == null){
  155. foreach ($actAllocationDetail_map as $index=>$actAllocationDetail) {
  156. $params[] = $actAllocationDetail;
  157. unset($actAllocationDetail_map[$key]);
  158. }
  159. continue;
  160. }elseif(count($actAllocationDetail_map) == count($orderCommodity_map)){
  161. continue;
  162. }elseif(count($actAllocationDetail_map) > count($orderCommodity_map)){
  163. foreach ($actAllocationDetail_map as $index=>$item) {
  164. if(count($orderCommodity_map) == 0)$params[] = $item;
  165. array_shift($orderCommodity_map);
  166. unset($actAllocationDetail_map[$key]);
  167. }
  168. }
  169. }
  170. return $params;
  171. }
  172. // TODO 根据创建数组返回可以使用的插入数组
  173. public function getInnerParamsByParams(&$create_params,&$order_code_map,&$commodity_map)
  174. {
  175. /** @var DataHandlerService $dataHandlerService */
  176. $dataHandlerService = app(DataHandlerService::class);
  177. $inner_params = [];
  178. $date = (string) Carbon::now();
  179. foreach ($create_params as $item) {
  180. $order = $dataHandlerService->getKeyValue(['code'=>$item['code']],$order_code_map);
  181. $key = "_owner_code_{$item['owner_code']}_sku_{$item['sku']}";
  182. $commodity = $commodity_map[$key] ?? null;
  183. $inner_params[] = [
  184. 'order_id' => $order['id'] ?? '',
  185. 'commodity_id' => $commodity['id'] ?? '',
  186. 'amount' => $item['amount'],
  187. 'location' => $item['location'],
  188. 'created_at' => $date,
  189. 'updated_at' => $date
  190. ];
  191. }
  192. return $inner_params;
  193. }
  194. }