OrderCommodityService.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace App\Services;
  3. use App\Order;
  4. use App\OrderCommodity;
  5. use Carbon\Carbon;
  6. Class OrderCommodityService
  7. {
  8. public function syncOrderCommodities(&$orderHeaders)
  9. {
  10. /**
  11. * @var CommodityService $commodityService
  12. * @var OwnerService $ownerService
  13. */
  14. $commodityService = app('CommodityService');
  15. $ownerService = app('OwnerService');
  16. if(count($orderHeaders) == 0)return ;
  17. $order_nos = array_unique(data_get($orderHeaders,'*.orderno'));
  18. $commodities_map = [];$owners_id_maps = [];
  19. $owner_code_maps = (function()use(&$ownerService,&$orderHeaders,&$owners_id_maps){
  20. $customer_ids= array_unique(data_get($orderHeaders,'*.customerid'));
  21. $owners = $ownerService->getOwnerByCodes($customer_ids);
  22. $owner_code_maps = [];
  23. if(count($owners) == 0)return $owner_code_maps;
  24. foreach ($owners as $owner) {
  25. $owner_code_maps[$owner['code']]= $owner['id'];
  26. $owners_id_maps[$owner['id']] = $owner['code'];
  27. }
  28. return $owner_code_maps;
  29. })();
  30. // 重组OrderDetails
  31. $orderDetails_map = (function()use(&$orderHeaders,&$owner_code_maps,&$commodities_map){
  32. $map = [];
  33. if(count($orderHeaders)==0)return $map;
  34. foreach ($orderHeaders as $orderHeader) {
  35. $Order_Details = $orderHeader->oracleDOCOrderDetails;
  36. $Order_Details->each(function($item)use(&$map,&$owner_code_maps,&$commodities_map){
  37. if(!empty($item['customerid']) && !empty($item['sku'])){
  38. $key = "owner_code_{$item['customerid']}_sku_{$item['sku']}";
  39. $commodities_map[$key] = ['owner_code'=>$item['customerid'],'sku'=>$item['sku']];
  40. }
  41. $key = "orderno_{$item['orderno']}_sku{$item['sku']}_each_{$item['qtyordered']}_location_{$item['location']}";
  42. if(empty($map[$key]))$map[$key]=[];
  43. $map[$key][] = [
  44. 'code' => $item['orderno'],
  45. 'sku' => $item['sku'],
  46. 'owner_id'=>$owner_code_maps[$item['customerid']],
  47. 'amount' => $item['qtyordered'],
  48. 'location' => $item['location']
  49. ];
  50. });
  51. }
  52. return $map;
  53. })();
  54. $commodities = $commodityService->getCommoditiesByMap($commodities_map);
  55. $commodities_maps = (function()use($commodities,$owners_id_maps){
  56. $map = [];
  57. if(count($commodities) == 0)return $map;
  58. foreach ($commodities as $commodity) {
  59. $owner_code = $owners_id_maps[$commodity['owner_id']];
  60. $key ="owner_code_{$owner_code}_sku_{$commodity['sku']}";
  61. $map[$key] = $commodity;
  62. }
  63. return $map;
  64. })();
  65. $order_id_map = [];
  66. $orders_map = (function()use($order_nos,&$order_id_map){
  67. $map = [];
  68. if(count($order_nos)==0)return $map;
  69. $orders = Order::query()->whereIn('code',$order_nos)->get();
  70. $orders->each(function($item)use(&$map,&$order_id_map){
  71. $map[$item['code']] = ['id'=> $item['id'], 'owner_id' => $item['owner_id']];
  72. $order_id_map[$item['id']] = ['code'=> $item['code'], 'owner_id' => $item['owner_id']];
  73. });
  74. return $map;
  75. })();
  76. $orderCommodities = OrderCommodity::query()->with('commodity','order')->whereHas('order',function($query)use($order_nos){
  77. $query->whereIn('code',$order_nos);
  78. })->get();
  79. $orderCommodities_map = $this->regroupOrderCommodities($orderCommodities); // 重组orderCommodities
  80. $inner_params = $this->filterInnerParams($orderDetails_map,$orderCommodities_map);
  81. $del_ids = $this->filterDeleteParams($orderDetails_map,$orderCommodities_map);
  82. if(count($inner_params)>0){
  83. $inner_arr = array_chunk($inner_params,4000);
  84. foreach ($inner_arr as $item) {
  85. $created_params = $this->createByInnerParams($item,$orders_map,$commodities_maps,$owners_id_maps);
  86. $this->insert($created_params);
  87. }
  88. }
  89. if(count($del_ids)>0){
  90. $orderCommodities = OrderCommodity::query()->whereIn('id',$del_ids)->get();
  91. $this->batchDelete($orderCommodities);
  92. }
  93. }
  94. private function regroupOrderCommodities(&$orderCommodities)
  95. {
  96. $map = [];
  97. if(count($orderCommodities)==0)return $map;
  98. foreach ($orderCommodities as $orderCommodity) {
  99. $key = "orderno_{$orderCommodity['order']['code']}_sku{$orderCommodity['commodity']['sku']}_each_{$orderCommodity['amount']}_location_{$orderCommodity['location']}";
  100. if(empty($map[$key]))$map[$key] =[];
  101. $map[$key][] = [
  102. 'id' =>$orderCommodity['id'],
  103. 'code' => $orderCommodity['order']['orderno'],
  104. 'sku' => $orderCommodity['commodity']['sku'],
  105. 'owner_id' => $orderCommodity['order']['owner_id'],
  106. 'amount' => $orderCommodity['amount'],
  107. 'location' => $orderCommodity['location']
  108. ];
  109. }
  110. return $map;
  111. }
  112. private function filterInnerParams(&$orderDetails_map,&$orderCommodities_map)
  113. {
  114. $inner_params = [];
  115. if(count($orderDetails_map) == 0)return $inner_params;
  116. foreach ($orderDetails_map as $key=>&$map) {
  117. if(empty($orderCommodities_map[$key])){
  118. foreach ($map as &$item) {
  119. $inner_params[] = $item;
  120. }
  121. }elseif(count($map)>count($orderCommodities_map[$key])){
  122. foreach ($map as &$obj) {
  123. if($orderCommodities_map[$key]>0)array_shift($orderCommodities_map[$key]);
  124. elseif($orderCommodities_map[$key]==0)$inner_params[] = $obj;
  125. }
  126. } elseif(count($map)==count($orderCommodities_map[$key]))continue;
  127. unset($orderDetails_map[$key]);
  128. $map = null;
  129. }
  130. return $inner_params;
  131. }
  132. private function filterDeleteParams(&$orderDetails_map,&$orderCommodities_map)
  133. {
  134. $del_ids = [];
  135. if(count($orderDetails_map) == 0)return $del_ids;
  136. foreach ($orderCommodities_map as $key=>$map) {
  137. if(empty($orderDetails_map[$key])){
  138. foreach ($map as &$item) {
  139. $del_ids[] = $item['id'];
  140. }
  141. }elseif(count($map)>count($orderDetails_map[$key])){
  142. foreach ($map as $key1=>&$obj) {
  143. if(count($orderDetails_map[$key]) == 0){
  144. $del_ids [] = $obj['id'];
  145. unset($map[$key1]);
  146. } else{
  147. array_shift($orderDetails_map[$key]);
  148. }
  149. }
  150. }
  151. }
  152. return $del_ids;
  153. }
  154. private function createByInnerParams(&$inner_params,&$orders_map,&$commodities_maps,&$owner_id_maps)
  155. {
  156. $created_params = [];
  157. if(count($inner_params) == 0)return $created_params;
  158. $data = Carbon::now();
  159. foreach ($inner_params as &$item) {
  160. $order = $orders_map[$item['code']] ?? false;
  161. if(!$order)continue;
  162. $owner_code = $owner_id_maps[$item['owner_id']];
  163. $key = "owner_code_{$owner_code}_sku_{$item['sku']}";
  164. $commodity = $commodities_maps[$key];
  165. $created_params[] = [
  166. 'order_id' =>$order['id'],
  167. 'commodity_id' =>$commodity['id'],
  168. 'amount' =>$item['amount'],
  169. 'location' =>$item['location'],
  170. 'created_at' =>$data,
  171. 'updated_at' => $data
  172. ];
  173. }
  174. return $created_params;
  175. }
  176. public function batchDelete($orderCommodities)
  177. {
  178. if(count($orderCommodities) == 0)return true;
  179. try {
  180. $bool = OrderCommodity::destroy(data_get($orderCommodities, '*.id'));
  181. if($bool)app('LogService')->log(__METHOD__,__FUNCTION__,'批量删除 OrderCommodities'.' || '.count($orderCommodities).' || '.json_encode($orderCommodities));
  182. else app('LogService')->log(__METHOD__,__FUNCTION__,'批量删除 OrderCommodities FAULT'.' || '.count($orderCommodities).' || '.json_encode($orderCommodities).' || ');
  183. return $bool;
  184. } catch (\Exception $e) {
  185. app('LogService')->log(__METHOD__,__FUNCTION__,'批量删除 OrderCommodities ERROR'.' || '.count($orderCommodities).' || '.json_encode($orderCommodities).' || '.json_encode($e->getMessage()).' || '.json_encode($e->getTraceAsString()));
  186. return false;
  187. }
  188. }
  189. public function insert($innerParams){
  190. if(!$innerParams)return false;
  191. if(count($innerParams)==0)return false;
  192. try {
  193. $bool = OrderCommodity::query()->insert($innerParams);
  194. if ($bool) app('LogService')->log(__METHOD__, __FUNCTION__, '批量添加 OrderCommodities SUCCESS' . ' || ' . count($innerParams) . ' || ' . json_encode($innerParams));
  195. else app('LogService')->log(__METHOD__, __FUNCTION__, '批量添加 OrderCommodities FAULT' . ' || ' . count($innerParams) . ' || ' . json_encode($innerParams));
  196. return $bool;
  197. } catch (\Exception $e) {
  198. app('LogService')->log(__METHOD__, __FUNCTION__, '批量添加 OrderCommodities ERROR'. ' || ' . count($innerParams) . ' || ' . json_encode($innerParams).' || '.json_encode($e->getMessage()).' || '.json_encode($e->getTraceAsString()));
  199. return false;
  200. }
  201. }
  202. }