OrderCommodityService.php 11 KB

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