OrderCommodityService.php 11 KB

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