OrderCommodityService.php 12 KB

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