OrderCommodityService.php 13 KB

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