OrderCommodityService.php 11 KB

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