OrderCommodityService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace App\Services;
  3. use App\Order;
  4. use App\OrderCommodity;
  5. use App\Services\common\BatchUpdateService;
  6. use App\Services\common\DataHandlerService;
  7. use Carbon\Carbon;
  8. Class OrderCommodityService
  9. {
  10. public function insert($innerParams){
  11. if(!$innerParams)return false;
  12. if(count($innerParams)==0)return false;
  13. try {
  14. $bool = OrderCommodity::query()->insert($innerParams);
  15. if ($bool) app('LogService')->log(__METHOD__, __FUNCTION__, '批量添加 OrderCommodities SUCCESS' . ' || ' . count($innerParams) . ' || ' . json_encode($innerParams));
  16. else app('LogService')->log(__METHOD__, __FUNCTION__, '批量添加 OrderCommodities FAULT' . ' || ' . count($innerParams) . ' || ' . json_encode($innerParams));
  17. return $bool;
  18. } catch (\Exception $e) {
  19. app('LogService')->log(__METHOD__, __FUNCTION__, '批量添加 OrderCommodities ERROR'. ' || ' . count($innerParams) . ' || ' . json_encode($innerParams).' || '.json_encode($e->getMessage()).' || '.json_encode($e->getTraceAsString()));
  20. return false;
  21. }
  22. }
  23. public function batchUpdate($updateParams){
  24. return app(BatchUpdateService::class)->batchUpdate('order_commodities',$updateParams);
  25. }
  26. public function syncOrderCommodity(&$orderHeaders)
  27. {
  28. /**
  29. * @var OwnerService $ownerService
  30. * @var CommodityService $commodityService
  31. * @var DataHandlerService $dataHandlerService
  32. */
  33. $ownerService = app('OwnerService');
  34. $commodityService = app('CommodityService');
  35. $dataHandlerService = app('DataHandlerService');
  36. $orderNos = data_get($orderHeaders,'*.orderno');
  37. $owner_codes = [];$sku_codes = [];
  38. foreach ($orderHeaders as $orderHeader) {
  39. $actAllocationDetails = $orderHeader->actAllocationDetails;
  40. foreach ($actAllocationDetails as $actAllocationDetail) {
  41. $owner_codes[$actAllocationDetail['customerid']] = $actAllocationDetail['customerid'];
  42. $sku_codes[$actAllocationDetail['sku']] = $actAllocationDetail['sku'];
  43. }
  44. }
  45. if(count($sku_codes) ==0)return;
  46. $owners = $ownerService->getOwnerByCodes($owner_codes);
  47. $owner_id_maps = $dataHandlerService->dataHeader(['id'],$owners);
  48. $commodities = $commodityService->get_(data_get($owners,'*.id'),$sku_codes,[],true);
  49. unset($owner_codes,$sku_codes);
  50. $commodity_map = [];
  51. foreach ($commodities as $commodity) {
  52. $owner = $dataHandlerService->getKeyValue(['id'=>$commodity->owner_id],$owner_id_maps);
  53. $key = "_owner_code_{$owner['code']}_sku_{$commodity['sku']}";
  54. $commodity_map[$key] = $commodity;
  55. }
  56. $orderCommodities = OrderCommodity::query()->with(['order','commodity'])->whereIn('order_id',function ($query)use($orderNos){
  57. $query->from('orders')->select('id')->whereIn('code',$orderNos);
  58. })->get();
  59. $orders = Order::query()->with('packages.commodities')->whereIn('code',$orderNos)->get();
  60. $order_code_map = $dataHandlerService->dataHeader(['code'],$orders);
  61. $ActAllocationDetail_maps = $this->getRegroupActAllocationDetails($orderHeaders);
  62. $orderCommodity_maps = $this->getRegroupOrderCommodities($orderCommodities,$owner_id_maps);
  63. unset($orderCommodities,$owner_id_maps,$owners,$orderNos);
  64. $this->filterHasExist($ActAllocationDetail_maps,$orderCommodity_maps);
  65. $delete_ids = $this->getDeleteIds($ActAllocationDetail_maps,$orderCommodity_maps);
  66. $create_params = $this->getCreateParams($ActAllocationDetail_maps,$orderCommodity_maps);
  67. unset($orderCommodity_maps,$ActAllocationDetail_maps);
  68. $inner_params = $this->getInnerParamsByParams($create_params,$order_code_map,$commodity_map);
  69. unset($create_params,$order_code_map,$orders,$commodity_map);
  70. // $update_params = $this->getUpdateParamsByParamsAndDeleteIds($inner_params,$delete_ids);
  71. // if(count($update_params)>0)$this->batchUpdate($update_params);
  72. // unset($update_params);
  73. if(count($inner_params)>0){
  74. $inner_params = array_chunk($inner_params,4000);
  75. foreach ($inner_params as $inner_param) {$this->insert($inner_param);}
  76. unset($inner_params);
  77. }
  78. if(count($delete_ids)==0)return;
  79. OrderCommodity::query()->whereIn('id',$delete_ids)->delete();
  80. app('LogService')->log(__METHOD__,__FUNCTION__,"delete OrderCommodity ".json_encode($delete_ids));
  81. unset($delete_ids);
  82. }
  83. public function getRegroupActAllocationDetails(&$orderHeaders)
  84. {
  85. $map = [];
  86. if($orderHeaders->count()==0) return $map;
  87. $orderHeaders->each(function($orderHeader)use(&$map){
  88. $orderHeader->actAllocationDetails->each(function ($details)use(&$map){
  89. $key = "order_{$details['orderno']}_owner_code_{$details['customerid']}_sku_{$details['sku']}_location_{$details['location']}_amount_{$details['qty_each']}";
  90. if(empty($map[$key]))$map[$key]=[];
  91. $map[$key][] = [
  92. 'code' => $details['orderno'],
  93. 'sku' => $details['sku'],
  94. 'owner_code'=> $details['customerid'],
  95. 'amount' => $details['qty_each'],
  96. 'location' => $details['location']
  97. ];
  98. });
  99. });
  100. return $map;
  101. }
  102. public function filterHasExist(&$ActAllocationDetail_maps,&$orderCommodity_maps)
  103. {
  104. foreach ($ActAllocationDetail_maps as $key=>$actAllocationDetail_map) {
  105. $orderCommodity_map = $orderCommodity_maps[$key] ?? null;
  106. if($orderCommodity_map == null)continue;
  107. foreach ($actAllocationDetail_map as $index=>$item) {
  108. if(count($orderCommodity_map)==0)break;
  109. array_shift($orderCommodity_map);
  110. unset($actAllocationDetail_map[$index]);
  111. }
  112. }
  113. }
  114. public function getRegroupOrderCommodities(&$orderCommodities,$owner_id_maps)
  115. {
  116. /** @var DataHandlerService $dataHandlerService */
  117. $dataHandlerService = app(DataHandlerService::class);
  118. $map = [];
  119. if($orderCommodities->count() == 0)return $map;
  120. $orderCommodities->each(function($orderCommodity)use(&$map,$owner_id_maps,$dataHandlerService){
  121. $order = $orderCommodity->order;
  122. $owner = $dataHandlerService->getKeyValue(['id'=>$order->owner_id ?? ''],$owner_id_maps);
  123. $sku = $orderCommodity->commodity->sku ?? '';
  124. $key = "order_{$order->code}_owner_code_{$owner['code']}_sku_{ $sku}_location_{$orderCommodity->location}_amount_{$orderCommodity->amount}";
  125. if(empty($map[$key]))$map[$key]=[];
  126. $map[$key][] =[
  127. 'id' => $orderCommodity->id,
  128. 'code' => $orderCommodity->order->code ?? '',
  129. 'sku' => $sku,
  130. 'owner_code' => $owner->code ?? '',
  131. 'amount' => $orderCommodity->amount,
  132. 'location' => $orderCommodity->location,
  133. ];
  134. });
  135. return $map;
  136. }
  137. public function getDeleteIds(&$ActAllocationDetail_maps,&$orderCommodity_maps)
  138. {
  139. $ids = [];
  140. if(count($orderCommodity_maps)==0)return $ids;
  141. foreach ($orderCommodity_maps as $key=>$orderCommodity_map) {
  142. $ActAllocationDetail_map = $ActAllocationDetail_maps[$key] ?? null;
  143. if($ActAllocationDetail_map==null){
  144. foreach ($orderCommodity_map as $index=>$orderCommodity) {
  145. $ids[] =$orderCommodity['id'];
  146. unset($orderCommodity_map[$index]);
  147. }
  148. continue;
  149. }
  150. if(count($ActAllocationDetail_map) == count($orderCommodity_map))continue;
  151. if(count($ActAllocationDetail_map) < count($orderCommodity_map)){
  152. foreach ($orderCommodity_map as $index=>$item) {
  153. if(count($ActAllocationDetail_map) == 0)$ids[] =$item['id'];
  154. array_shift($ActAllocationDetail_map);
  155. unset($orderCommodity_map[$index]);
  156. }
  157. }
  158. }
  159. return $ids;
  160. }
  161. public function getCreateParams(&$ActAllocationDetail_maps,&$orderCommodity_maps)
  162. {
  163. $params = [];
  164. foreach ($ActAllocationDetail_maps as $key=>$actAllocationDetail_map) {
  165. $orderCommodity_map = $orderCommodity_maps[$key] ?? null;
  166. if($orderCommodity_map == null){
  167. foreach ($actAllocationDetail_map as $index=>$actAllocationDetail) {
  168. $params[] = $actAllocationDetail;
  169. unset($actAllocationDetail_map[$key]);
  170. }
  171. continue;
  172. }elseif(count($actAllocationDetail_map) == count($orderCommodity_map)){
  173. continue;
  174. }elseif(count($actAllocationDetail_map) > count($orderCommodity_map)){
  175. foreach ($actAllocationDetail_map as $index=>$item) {
  176. if(count($orderCommodity_map) == 0)$params[] = $item;
  177. array_shift($orderCommodity_map);
  178. unset($actAllocationDetail_map[$key]);
  179. }
  180. }
  181. }
  182. return $params;
  183. }
  184. public function getInnerParamsByParams(&$create_params,&$order_code_map,&$commodity_map)
  185. {
  186. /** @var DataHandlerService $dataHandlerService */
  187. $dataHandlerService = app(DataHandlerService::class);
  188. $inner_params = [];
  189. $date = (string) Carbon::now();
  190. foreach ($create_params as $item) {
  191. $order = $dataHandlerService->getKeyValue(['code'=>$item['code']],$order_code_map);
  192. $key = "_owner_code_{$item['owner_code']}_sku_{$item['sku']}";
  193. $commodity = $commodity_map[$key] ?? null;
  194. $inner_params[] = [
  195. 'order_id' => $order['id'] ?? '',
  196. 'commodity_id' => $commodity['id'] ?? '',
  197. 'amount' => $item['amount'],
  198. 'location' => $item['location'],
  199. 'created_at' => $date,
  200. 'updated_at' => $date
  201. ];
  202. }
  203. return $inner_params;
  204. }
  205. // TODO 更新后续添加进更新逻辑
  206. public function getUpdateParamsByParamsAndDeleteIds(&$inner_params,&$ids)
  207. {
  208. $update_params = [['id','order_id', 'commodity_id', 'amount', 'location', 'created_at', 'updated_at']];
  209. if(count($inner_params)==0)return $update_params;
  210. $date = (string)Carbon::now();
  211. foreach ($inner_params as $key=>$inner_param) {
  212. if(count($ids)==0)break;
  213. $inner_param['id'] = array_shift($ids);
  214. $inner_param['updated_at'] = $date;
  215. $inner_param['created_at'] = $date;
  216. $update_params[] = $inner_param;
  217. unset($inner_params[$key]);
  218. }
  219. return $update_params;
  220. }
  221. }