OrderCommodityService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 syncOrderCommodities(&$orderHeaders)
  10. {
  11. /**
  12. * @var CommodityService $commodityService
  13. * @var OwnerService $ownerService
  14. */
  15. $commodityService = app('CommodityService');
  16. $ownerService = app('OwnerService');
  17. if(count($orderHeaders) == 0)return ;
  18. $order_nos = array_unique(data_get($orderHeaders,'*.orderno'));
  19. $commodities_map = [];$owners_id_maps = [];
  20. $owner_code_maps = (function()use(&$ownerService,&$orderHeaders,&$owners_id_maps){
  21. $customer_ids= array_unique(data_get($orderHeaders,'*.customerid'));
  22. $owners = $ownerService->getOwnerByCodes($customer_ids);
  23. $owner_code_maps = [];
  24. if(count($owners) == 0)return $owner_code_maps;
  25. foreach ($owners as $owner) {
  26. $owner_code_maps[$owner['code']]= $owner['id'];
  27. $owners_id_maps[$owner['id']] = $owner['code'];
  28. }
  29. return $owner_code_maps;
  30. })();
  31. // 重组OrderDetails
  32. $orderDetails_map = (function()use(&$orderHeaders,&$owner_code_maps,&$commodities_map){
  33. $map = [];
  34. if(count($orderHeaders)==0)return $map;
  35. foreach ($orderHeaders as $orderHeader) {
  36. $Order_Details = $orderHeader->oracleDOCOrderDetails;
  37. $Order_Details->each(function($item)use(&$map,&$owner_code_maps,&$commodities_map){
  38. if(!empty($item['customerid']) && !empty($item['sku'])){
  39. $key = "owner_code_{$item['customerid']}_sku_{$item['sku']}";
  40. $commodities_map[$key] = ['owner_code'=>$item['customerid'],'sku'=>$item['sku']];
  41. }
  42. $key = "orderno_{$item['orderno']}_sku{$item['sku']}_each_{$item['qtyordered']}_location_{$item['location']}";
  43. if(empty($map[$key]))$map[$key]=[];
  44. $map[$key][] = [
  45. 'code' => $item['orderno'],
  46. 'sku' => $item['sku'],
  47. 'owner_id'=>$owner_code_maps[$item['customerid']],
  48. 'amount' => $item['qtyordered'],
  49. 'location' => $item['location']
  50. ];
  51. });
  52. }
  53. return $map;
  54. })();
  55. $commodities = $commodityService->getCommoditiesByMap($commodities_map);
  56. $commodities_maps = (function()use($commodities,$owners_id_maps){
  57. $map = [];
  58. if(count($commodities) == 0)return $map;
  59. foreach ($commodities as $commodity) {
  60. $owner_code = $owners_id_maps[$commodity['owner_id']];
  61. $key ="owner_code_{$owner_code}_sku_{$commodity['sku']}";
  62. $map[$key] = $commodity;
  63. }
  64. return $map;
  65. })();
  66. $order_id_map = [];
  67. $orders_map = (function()use($order_nos,&$order_id_map){
  68. $map = [];
  69. if(count($order_nos)==0)return $map;
  70. $orders = Order::query()->whereIn('code',$order_nos)->get();
  71. $orders->each(function($item)use(&$map,&$order_id_map){
  72. $map[$item['code']] = ['id'=> $item['id'], 'owner_id' => $item['owner_id']];
  73. $order_id_map[$item['id']] = ['code'=> $item['code'], 'owner_id' => $item['owner_id']];
  74. });
  75. return $map;
  76. })();
  77. $orderCommodities = OrderCommodity::query()->with('commodity','order')->whereHas('order',function($query)use($order_nos){
  78. $query->whereIn('code',$order_nos);
  79. })->get();
  80. $orderCommodities_map = $this->regroupOrderCommodities($orderCommodities); // 重组orderCommodities
  81. $inner_params = $this->filterInnerParams($orderDetails_map,$orderCommodities_map);
  82. $del_ids = $this->filterDeleteParams($orderDetails_map,$orderCommodities_map);
  83. if(count($inner_params)>0){
  84. $inner_arr = array_chunk($inner_params,4000);
  85. foreach ($inner_arr as $item) {
  86. $created_params = $this->createByInnerParams($item,$orders_map,$commodities_maps,$owners_id_maps);
  87. $this->insert($created_params);
  88. }
  89. }
  90. if(count($del_ids)>0){
  91. $orderCommodities = OrderCommodity::query()->whereIn('id',$del_ids)->get();
  92. $this->batchDelete($orderCommodities);
  93. }
  94. }
  95. private function regroupOrderCommodities(&$orderCommodities)
  96. {
  97. $map = [];
  98. if(count($orderCommodities)==0)return $map;
  99. foreach ($orderCommodities as $orderCommodity) {
  100. $key = "orderno_{$orderCommodity['order']['code']}_sku{$orderCommodity['commodity']['sku']}_each_{$orderCommodity['amount']}_location_{$orderCommodity['location']}";
  101. if(empty($map[$key]))$map[$key] =[];
  102. $map[$key][] = [
  103. 'id' =>$orderCommodity['id'],
  104. 'code' => $orderCommodity['order']['orderno'],
  105. 'sku' => $orderCommodity['commodity']['sku'],
  106. 'owner_id' => $orderCommodity['order']['owner_id'],
  107. 'amount' => $orderCommodity['amount'],
  108. 'location' => $orderCommodity['location'] ?? ''
  109. ];
  110. }
  111. return $map;
  112. }
  113. private function createByInnerParams(&$inner_params,&$orders_map,&$commodities_maps,&$owner_id_maps)
  114. {
  115. $created_params = [];
  116. if(count($inner_params) == 0)return $created_params;
  117. $data = Carbon::now();
  118. foreach ($inner_params as &$item) {
  119. $order = $orders_map[$item['code']] ?? false;
  120. if(!$order)continue;
  121. $owner_code = $owner_id_maps[$item['owner_id']];
  122. $key = "owner_code_{$owner_code}_sku_{$item['sku']}";
  123. $commodity = $commodities_maps[$key];
  124. $created_params[] = [
  125. 'order_id' =>$order['id'],
  126. 'commodity_id' =>$commodity['id'],
  127. 'amount' =>$item['amount'],
  128. 'location' =>$item['location'],
  129. 'created_at' =>$data,
  130. 'updated_at' => $data
  131. ];
  132. }
  133. return $created_params;
  134. }
  135. public function batchDelete($orderCommodities)
  136. {
  137. if(count($orderCommodities) == 0)return true;
  138. try {
  139. $bool = OrderCommodity::destroy(data_get($orderCommodities, '*.id'));
  140. if($bool)app('LogService')->log(__METHOD__,__FUNCTION__,'批量删除 OrderCommodities'.' || '.count($orderCommodities).' || '.json_encode($orderCommodities));
  141. else app('LogService')->log(__METHOD__,__FUNCTION__,'批量删除 OrderCommodities FAULT'.' || '.count($orderCommodities).' || '.json_encode($orderCommodities).' || ');
  142. return $bool;
  143. } catch (\Exception $e) {
  144. app('LogService')->log(__METHOD__,__FUNCTION__,'批量删除 OrderCommodities ERROR'.' || '.count($orderCommodities).' || '.json_encode($orderCommodities).' || '.json_encode($e->getMessage()).' || '.json_encode($e->getTraceAsString()));
  145. return false;
  146. }
  147. }
  148. public function insert($innerParams){
  149. if(!$innerParams)return false;
  150. if(count($innerParams)==0)return false;
  151. try {
  152. $bool = OrderCommodity::query()->insert($innerParams);
  153. if ($bool) app('LogService')->log(__METHOD__, __FUNCTION__, '批量添加 OrderCommodities SUCCESS' . ' || ' . count($innerParams) . ' || ' . json_encode($innerParams));
  154. else app('LogService')->log(__METHOD__, __FUNCTION__, '批量添加 OrderCommodities FAULT' . ' || ' . count($innerParams) . ' || ' . json_encode($innerParams));
  155. return $bool;
  156. } catch (\Exception $e) {
  157. app('LogService')->log(__METHOD__, __FUNCTION__, '批量添加 OrderCommodities ERROR'. ' || ' . count($innerParams) . ' || ' . json_encode($innerParams).' || '.json_encode($e->getMessage()).' || '.json_encode($e->getTraceAsString()));
  158. return false;
  159. }
  160. }
  161. public function syncOrderCommodity($orderHeaders)
  162. {
  163. /**
  164. * @var OwnerService $ownerService
  165. * @var CommodityService $commodityService
  166. * @var DataHandlerService $dataHandlerService
  167. */
  168. $ownerService = app('OwnerService');
  169. $commodityService = app('CommodityService');
  170. $dataHandlerService = app('DataHandlerService');
  171. $map = [];$owner_codes = [];
  172. foreach ($orderHeaders as $orderHeader) {
  173. $actAllocationDetails = $orderHeader->actAllocationDetails;
  174. foreach ($actAllocationDetails as $actAllocationDetail) {
  175. $key = "owner_code_{$actAllocationDetail['customerid']}_'sku'";
  176. $owner_codes[$actAllocationDetail['customerid']] = $actAllocationDetail['customerid'];
  177. $map[$key] = [
  178. 'owner_code'=>$actAllocationDetail['customerid'],
  179. 'sku'=>$actAllocationDetail['sku']
  180. ];
  181. }
  182. }
  183. $owners = $ownerService->getOwnerByCodes($owner_codes);
  184. $owner_map = $dataHandlerService->dataHeader(['id'],$owners);
  185. $commodities = $commodityService->getParamsByBasSku($map);
  186. $commodity_map = [];
  187. foreach ($commodities as $commodity) {
  188. $owner = $dataHandlerService->getKeyValue(['id'=>$commodity->id],$owner_map);
  189. $key = "_owner_code_{$owner['code']}_sku_{$commodity['sku']}";
  190. $commodity_map[$key] = $commodity;
  191. }
  192. }
  193. public function getRegroupActAllocationDetails(&$orderHeaders)
  194. {
  195. $map = [];
  196. if($orderHeaders->count()==0) return $map;
  197. $orderHeaders->each(function($orderHeader)use(&$map){
  198. $orderHeader->actAllocationDetails->each(function ($details)use(&$map){
  199. $key = "order_{$details['orderno']}_owner_code_{$details['customerid']}_sku_{$details['sku']}_location_{$details['location']}";
  200. if(empty($map[$key]))$map[$key]=[];
  201. $map[$key][] = [
  202. 'code' => $details['orderno'],
  203. 'sku' => $details['sku'],
  204. 'owner_code'=> $details['customerid'],
  205. 'amount' => $details['qty_each'],
  206. 'location' => $details['location']
  207. ];
  208. });
  209. });
  210. return $map;
  211. }
  212. public function getRegroupOrderCommodities(&$orderCommodities,$owner_id_maps)
  213. {
  214. /** @var DataHandlerService $dataHandlerService */
  215. $dataHandlerService = app(DataHandlerService::class);
  216. $map = [];
  217. if($orderCommodities->count() == 0)return $map;
  218. $orderCommodities->each(function($orderCommodity)use(&$map,$owner_id_maps,$dataHandlerService){
  219. $order = $orderCommodity->order;
  220. $owner = $dataHandlerService->getKeyValue(['id'=>$order->owner_id ?? ''],$owner_id_maps);
  221. $key = "order_{($order->code ?? '')}_owner_code_{($owner->code)}_sku_{($orderCommodity->commodity->sku ?? '')}_location_{($orderCommodity->location)}";
  222. if(empty($map[$key]))$map[$key]=[];
  223. $map[$key][] =[
  224. 'id' => $orderCommodity->id,
  225. 'code' => $orderCommodity->order->code ?? '',
  226. 'sku' => $orderCommodity->commodity->sku,
  227. 'owner_code' => $owner->code ?? '',
  228. 'amount' => $orderCommodity->amount,
  229. 'location' => $orderCommodity->location,
  230. ];
  231. });
  232. return $map;
  233. }
  234. public function getDeleteIds(&$ActAllocationDetail_maps,&$orderCommodity_maps)
  235. {
  236. $ids = [];
  237. foreach ($orderCommodity_maps as $key=>$orderCommodity_map) {
  238. if(isset($ActAllocationDetail_maps[$key]))continue;
  239. foreach ($orderCommodity_map as $key1=>$orderCommodity) {
  240. $ids[] = $orderCommodity['id'];
  241. unset($orderCommodity_map[$key1]);
  242. }
  243. }
  244. return $ids;
  245. }
  246. public function getCreateParams(&$ActAllocationDetail_maps,&$orderCommodity_maps)
  247. {
  248. $params = [];
  249. foreach ($ActAllocationDetail_maps as $key=>$actAllocationDetail_map) {
  250. if(empty($orderCommodity_maps[$key]))continue;
  251. foreach ($actAllocationDetail_map as $actAllocationDetail) {
  252. $params[] = $actAllocationDetail;
  253. }
  254. }
  255. return $params;
  256. }
  257. public function getInnerParamsByParams($create_params,$order_code_map,$commodity_map)
  258. {
  259. /** @var DataHandlerService $dataHandlerService */
  260. $dataHandlerService = app(DataHandlerService::class);
  261. $inner_params = [];
  262. $date = (string) Carbon::now();
  263. foreach ($create_params as $item) {
  264. $order = $dataHandlerService->getKeyValue(['code'=>$item['code']],$order_code_map);
  265. $key = "_owner_code_{$item['owner_code']}_sku_{$item['sku']}";
  266. $commodity = $commodity_map[$key] ?? null;
  267. $inner_params[] = [
  268. 'order_id' => $order['id'] ?? '',
  269. 'commodity_id' => $commodity['id'] ?? '',
  270. 'amount' => $item['amount'],
  271. 'location' => $item['location'],
  272. 'created_at' => $date,
  273. 'updated_at' => $date
  274. ];
  275. }
  276. return $inner_params;
  277. }
  278. }