OrderCommodityService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. $key = "order_{$order->code}_owner_code_{$owner->code}_sku_{$orderCommodity->commodity->sku}_location_{$orderCommodity->location}_amount_{$orderCommodity->amount}";
  123. if(empty($map[$key]))$map[$key]=[];
  124. $map[$key][] =[
  125. 'id' => $orderCommodity->id,
  126. 'code' => $orderCommodity->order->code ?? '',
  127. 'sku' => $orderCommodity->commodity->sku,
  128. 'owner_code' => $owner->code ?? '',
  129. 'amount' => $orderCommodity->amount,
  130. 'location' => $orderCommodity->location,
  131. ];
  132. });
  133. return $map;
  134. }
  135. // TODO 返回修改删除的
  136. public function getDeleteIds(&$ActAllocationDetail_maps,&$orderCommodity_maps)
  137. {
  138. $ids = [];
  139. if(count($orderCommodity_maps)==0)return $ids;
  140. foreach ($orderCommodity_maps as $key=>$orderCommodity_map) {
  141. $ActAllocationDetail_map = $ActAllocationDetail_maps[$key] ?? null;
  142. if($ActAllocationDetail_map==null){
  143. foreach ($orderCommodity_map as $index=>$orderCommodity) {
  144. $ids[] =$orderCommodity['id'];
  145. unset($orderCommodity_map[$index]);
  146. }
  147. continue;
  148. }
  149. if(count($ActAllocationDetail_map) == count($orderCommodity_map))continue;
  150. if(count($ActAllocationDetail_map) < count($orderCommodity_map)){
  151. foreach ($orderCommodity_map as $index=>$item) {
  152. if(count($ActAllocationDetail_map) == 0)$ids[] =$item['id'];
  153. array_shift($ActAllocationDetail_map);
  154. unset($orderCommodity_map[$index]);
  155. }
  156. }
  157. }
  158. return $ids;
  159. }
  160. // TODO 返回创建的数组
  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. // TODO 根据创建数组返回可以使用的插入数组
  185. public function getInnerParamsByParams(&$create_params,&$order_code_map,&$commodity_map)
  186. {
  187. /** @var DataHandlerService $dataHandlerService */
  188. $dataHandlerService = app(DataHandlerService::class);
  189. $inner_params = [];
  190. $date = (string) Carbon::now();
  191. foreach ($create_params as $item) {
  192. $order = $dataHandlerService->getKeyValue(['code'=>$item['code']],$order_code_map);
  193. $key = "_owner_code_{$item['owner_code']}_sku_{$item['sku']}";
  194. $commodity = $commodity_map[$key] ?? null;
  195. $inner_params[] = [
  196. 'order_id' => $order['id'] ?? '',
  197. 'commodity_id' => $commodity['id'] ?? '',
  198. 'amount' => $item['amount'],
  199. 'location' => $item['location'],
  200. 'created_at' => $date,
  201. 'updated_at' => $date
  202. ];
  203. }
  204. return $inner_params;
  205. }
  206. }