OrderCommodityService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. unset($owner_codes,$sku_codes);
  47. $commodity_map = [];
  48. foreach ($commodities as $commodity) {
  49. $owner = $dataHandlerService->getKeyValue(['id'=>$commodity->owner_id],$owner_id_maps);
  50. $key = "_owner_code_{$owner['code']}_sku_{$commodity['sku']}";
  51. $commodity_map[$key] = $commodity;
  52. }
  53. $orderCommodities = OrderCommodity::query()->with(['order','commodity'])->whereIn('order_id',function ($query)use($orderNos){
  54. $query->from('orders')->select('id')->whereIn('code',$orderNos);
  55. })->get();
  56. $orders = Order::query()->with('packages.commodities')->whereIn('code',$orderNos)->get();
  57. $order_code_map = $dataHandlerService->dataHeader(['code'],$orders);
  58. $ActAllocationDetail_maps = $this->getRegroupActAllocationDetails($orderHeaders);
  59. $orderCommodity_maps = $this->getRegroupOrderCommodities($orderCommodities,$owner_id_maps);
  60. unset($orderCommodities,$owner_id_maps,$owners,$orderNos);
  61. $this->filterHasExist($ActAllocationDetail_maps,$orderCommodity_maps);
  62. $delete_ids = $this->getDeleteIds($ActAllocationDetail_maps,$orderCommodity_maps);
  63. $create_params = $this->getCreateParams($ActAllocationDetail_maps,$orderCommodity_maps);
  64. unset($orderCommodity_maps,$ActAllocationDetail_maps);
  65. $inner_params = $this->getInnerParamsByParams($create_params,$order_code_map,$commodity_map);
  66. unset($create_params,$order_code_map,$orders,$commodity_map);
  67. if(count($inner_params)>0){
  68. $inner_params = array_chunk($inner_params,4000);
  69. foreach ($inner_params as $inner_param) {
  70. $this->insert($inner_param);
  71. }
  72. unset($inner_params);
  73. }
  74. if(count($delete_ids)==0)return;
  75. OrderCommodity::query()->whereIn('id',$delete_ids)->delete();
  76. app('LogService')->log(__METHOD__,__FUNCTION__,"delete OrderCommodity ".json_encode($delete_ids));
  77. unset($delete_ids);
  78. }
  79. // TODO 将拉取的OrderHeader 重组为符合当前基准的数组
  80. public function getRegroupActAllocationDetails(&$orderHeaders)
  81. {
  82. $map = [];
  83. if($orderHeaders->count()==0) return $map;
  84. $orderHeaders->each(function($orderHeader)use(&$map){
  85. $orderHeader->actAllocationDetails->each(function ($details)use(&$map){
  86. $key = "order_{$details['orderno']}_owner_code_{$details['customerid']}_sku_{$details['sku']}_location_{$details['location']}_amount_{$details['qty_each']}";
  87. if(empty($map[$key]))$map[$key]=[];
  88. $map[$key][] = [
  89. 'code' => $details['orderno'],
  90. 'sku' => $details['sku'],
  91. 'owner_code'=> $details['customerid'],
  92. 'amount' => $details['qty_each'],
  93. 'location' => $details['location']
  94. ];
  95. });
  96. });
  97. return $map;
  98. }
  99. // TODO 过滤已有
  100. public function filterHasExist(&$ActAllocationDetail_maps,&$orderCommodity_maps)
  101. {
  102. foreach ($ActAllocationDetail_maps as $key=>$actAllocationDetail_map) {
  103. $orderCommodity_map = $orderCommodity_maps[$key] ?? null;
  104. if($orderCommodity_map == null)continue;
  105. foreach ($actAllocationDetail_map as $index=>$item) {
  106. if(count($orderCommodity_map)==0)break;
  107. array_shift($orderCommodity_map);
  108. unset($actAllocationDetail_map[$index]);
  109. }
  110. }
  111. }
  112. // TODO 将传入OrderCommodities 重组为符合当前基准的数组
  113. public function getRegroupOrderCommodities(&$orderCommodities,$owner_id_maps)
  114. {
  115. /** @var DataHandlerService $dataHandlerService */
  116. $dataHandlerService = app(DataHandlerService::class);
  117. $map = [];
  118. if($orderCommodities->count() == 0)return $map;
  119. $orderCommodities->each(function($orderCommodity)use(&$map,$owner_id_maps,$dataHandlerService){
  120. $order = $orderCommodity->order;
  121. $owner = $dataHandlerService->getKeyValue(['id'=>$order->owner_id ?? ''],$owner_id_maps);
  122. $sku = $orderCommodity->commodity->sku ?? '';
  123. $key = "order_{$order->code}_owner_code_{$owner['code']}_sku_{ $sku}_location_{$orderCommodity->location}_amount_{$orderCommodity->amount}";
  124. if(empty($map[$key]))$map[$key]=[];
  125. $map[$key][] =[
  126. 'id' => $orderCommodity->id,
  127. 'code' => $orderCommodity->order->code ?? '',
  128. 'sku' => $sku,
  129. 'owner_code' => $owner->code ?? '',
  130. 'amount' => $orderCommodity->amount,
  131. 'location' => $orderCommodity->location,
  132. ];
  133. });
  134. return $map;
  135. }
  136. // TODO 返回修改删除的
  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. // TODO 返回创建的数组
  162. public function getCreateParams(&$ActAllocationDetail_maps,&$orderCommodity_maps)
  163. {
  164. $params = [];
  165. foreach ($ActAllocationDetail_maps as $key=>$actAllocationDetail_map) {
  166. $orderCommodity_map = $orderCommodity_maps[$key] ?? null;
  167. if($orderCommodity_map == null){
  168. foreach ($actAllocationDetail_map as $index=>$actAllocationDetail) {
  169. $params[] = $actAllocationDetail;
  170. unset($actAllocationDetail_map[$key]);
  171. }
  172. continue;
  173. }elseif(count($actAllocationDetail_map) == count($orderCommodity_map)){
  174. continue;
  175. }elseif(count($actAllocationDetail_map) > count($orderCommodity_map)){
  176. foreach ($actAllocationDetail_map as $index=>$item) {
  177. if(count($orderCommodity_map) == 0)$params[] = $item;
  178. array_shift($orderCommodity_map);
  179. unset($actAllocationDetail_map[$key]);
  180. }
  181. }
  182. }
  183. return $params;
  184. }
  185. // TODO 根据创建数组返回可以使用的插入数组
  186. public function getInnerParamsByParams(&$create_params,&$order_code_map,&$commodity_map)
  187. {
  188. /** @var DataHandlerService $dataHandlerService */
  189. $dataHandlerService = app(DataHandlerService::class);
  190. $inner_params = [];
  191. $date = (string) Carbon::now();
  192. foreach ($create_params as $item) {
  193. $order = $dataHandlerService->getKeyValue(['code'=>$item['code']],$order_code_map);
  194. $key = "_owner_code_{$item['owner_code']}_sku_{$item['sku']}";
  195. $commodity = $commodity_map[$key] ?? null;
  196. $inner_params[] = [
  197. 'order_id' => $order['id'] ?? '',
  198. 'commodity_id' => $commodity['id'] ?? '',
  199. 'amount' => $item['amount'],
  200. 'location' => $item['location'],
  201. 'created_at' => $date,
  202. 'updated_at' => $date
  203. ];
  204. }
  205. return $inner_params;
  206. }
  207. }