OrderCommodityService.php 10 KB

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