OrderCommodityService.php 11 KB

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