OrderCommodityService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. $this->deleteByIds($delete_ids);
  105. // OrderCommodity::query()->whereIn('id',$delete_ids)->delete();
  106. app('LogService')->log(__METHOD__,__FUNCTION__,"delete OrderCommodity ".json_encode($delete_ids));
  107. unset($delete_ids);
  108. }
  109. public function deleteByIds($ids){
  110. $deleteIds = array_chunk($ids,150);
  111. foreach ($deleteIds as $delete_ids) {
  112. OrderCommodity::query()->whereIn('id',$delete_ids)->delete();
  113. }
  114. }
  115. public function getCommoditiesByOrderNos($orderNos)
  116. {
  117. $orderIds = [];
  118. $order_chunks = array_chunk($orderNos,150);
  119. foreach ($order_chunks as $order_chunk) {
  120. $ids = Order::query()->select('id')->whereIn('code',$order_chunk)->get()->map(function($order){
  121. return $order->id;
  122. })->toArray();
  123. $orderIds = array_merge($orderIds,$ids);
  124. }
  125. return $this->getCommoditiesByOrderIds($orderIds);
  126. }
  127. public function getCommoditiesByOrderIds($orderIds)
  128. {
  129. $orderCommodities = null;
  130. $orderId_chunks = array_chunk($orderIds,150);
  131. foreach ($orderId_chunks as $orderId_chunk){
  132. $items = OrderCommodity::query()->with(['order','commodity'])->whereIn('order_id',$orderId_chunk)->get();
  133. if ($orderCommodities == null){
  134. $orderCommodities = $items;
  135. }else{
  136. $orderCommodities = $orderCommodities->concat($items);
  137. }
  138. }
  139. return $orderCommodities ?? new Collection();
  140. }
  141. public function getRegroupActAllocationDetails(&$orderHeaders)
  142. {
  143. $map = [];
  144. if($orderHeaders->count()==0) return $map;
  145. $orderHeaders->each(function($orderHeader)use(&$map){
  146. $orderHeader->actAllocationDetails->each(function ($details)use(&$map){
  147. $key = "order_{$details['orderno']}_owner_code_{$details['customerid']}_sku_{$details['sku']}_location_{$details['location']}_amount_{$details['qty_each']}";
  148. if(empty($map[$key]))$map[$key]=[];
  149. $map[$key][] = [
  150. 'code' => $details['orderno'],
  151. 'sku' => $details['sku'],
  152. 'owner_code'=> $details['customerid'],
  153. 'amount' => $details['qty_each'],
  154. 'location' => $details['location']
  155. ];
  156. });
  157. });
  158. return $map;
  159. }
  160. public function filterHasExist(&$ActAllocationDetail_maps,&$orderCommodity_maps)
  161. {
  162. foreach ($ActAllocationDetail_maps as $key=>$actAllocationDetail_map) {
  163. $orderCommodity_map = $orderCommodity_maps[$key] ?? null;
  164. if($orderCommodity_map == null)continue;
  165. foreach ($actAllocationDetail_map as $index=>$item) {
  166. if(count($orderCommodity_map)==0)break;
  167. array_shift($orderCommodity_map);
  168. unset($actAllocationDetail_map[$index]);
  169. }
  170. }
  171. }
  172. public function getRegroupOrderCommodities(&$orderCommodities,$owner_id_maps)
  173. {
  174. /** @var DataHandlerService $dataHandlerService */
  175. $dataHandlerService = app(DataHandlerService::class);
  176. $map = [];
  177. if($orderCommodities->count() == 0)return $map;
  178. $orderCommodities->each(function($orderCommodity)use(&$map,$owner_id_maps,$dataHandlerService){
  179. $order = $orderCommodity->order;
  180. $owner = $dataHandlerService->getKeyValue(['id'=>$order->owner_id ?? ''],$owner_id_maps);
  181. $sku = $orderCommodity->commodity->sku ?? '';
  182. $key = "order_{$order->code}_owner_code_{$owner['code']}_sku_{ $sku}_location_{$orderCommodity->location}_amount_{$orderCommodity->amount}";
  183. if(empty($map[$key]))$map[$key]=[];
  184. $map[$key][] =[
  185. 'id' => $orderCommodity->id,
  186. 'code' => $order->code ?? '',
  187. 'sku' => $sku,
  188. 'owner_code' => $owner->code ?? '',
  189. 'amount' => $orderCommodity->amount,
  190. 'location' => $orderCommodity->location,
  191. ];
  192. });
  193. return $map;
  194. }
  195. public function getDeleteIds(&$ActAllocationDetail_maps,&$orderCommodity_maps)
  196. {
  197. $ids = [];
  198. if(count($orderCommodity_maps)==0)return $ids;
  199. foreach ($orderCommodity_maps as $key=>$orderCommodity_map) {
  200. $ActAllocationDetail_map = $ActAllocationDetail_maps[$key] ?? null;
  201. if($ActAllocationDetail_map==null){
  202. foreach ($orderCommodity_map as $index=>$orderCommodity) {
  203. $ids[] =$orderCommodity['id'];
  204. unset($orderCommodity_map[$index]);
  205. }
  206. continue;
  207. }
  208. if(count($ActAllocationDetail_map) == count($orderCommodity_map))continue;
  209. if(count($ActAllocationDetail_map) < count($orderCommodity_map)){
  210. foreach ($orderCommodity_map as $index=>$item) {
  211. if(count($ActAllocationDetail_map) == 0)$ids[] =$item['id'];
  212. array_shift($ActAllocationDetail_map);
  213. unset($orderCommodity_map[$index]);
  214. }
  215. }
  216. }
  217. return $ids;
  218. }
  219. public function getCreateParams(&$ActAllocationDetail_maps,&$orderCommodity_maps)
  220. {
  221. $params = [];
  222. foreach ($ActAllocationDetail_maps as $key=>$actAllocationDetail_map) {
  223. $orderCommodity_map = $orderCommodity_maps[$key] ?? null;
  224. if($orderCommodity_map == null){
  225. foreach ($actAllocationDetail_map as $index=>$actAllocationDetail) {
  226. $params[] = $actAllocationDetail;
  227. unset($actAllocationDetail_map[$key]);
  228. }
  229. continue;
  230. }elseif(count($actAllocationDetail_map) == count($orderCommodity_map)){
  231. continue;
  232. }elseif(count($actAllocationDetail_map) > count($orderCommodity_map)){
  233. foreach ($actAllocationDetail_map as $index=>$item) {
  234. if(count($orderCommodity_map) == 0)$params[] = $item;
  235. array_shift($orderCommodity_map);
  236. unset($actAllocationDetail_map[$key]);
  237. }
  238. }
  239. }
  240. return $params;
  241. }
  242. public function getInnerParamsByParams($create_params,$order_code_map,$commodity_map)
  243. {
  244. /** @var DataHandlerService $dataHandlerService */
  245. $dataHandlerService = app(DataHandlerService::class);
  246. $inner_params = [];
  247. $date = (string) Carbon::now();
  248. foreach ($create_params as $item) {
  249. $order = $dataHandlerService->getKeyValue(['code'=>$item['code']],$order_code_map);
  250. if(!$order)continue;
  251. $key = "_owner_code_{$item['owner_code']}_sku_{$item['sku']}";
  252. $commodity = $commodity_map[$key] ?? null;
  253. $inner_params[] = [
  254. 'order_id' => $order['id'] ?? '',
  255. 'commodity_id' => $commodity['id'] ?? '',
  256. 'amount' => $item['amount'],
  257. 'location' => $item['location'],
  258. 'created_at' => $date,
  259. 'updated_at' => $date
  260. ];
  261. }
  262. return $inner_params;
  263. }
  264. //更新后续添加进更新逻辑
  265. public function getUpdateParamsByParamsAndDeleteIds(&$inner_params,&$ids)
  266. {
  267. $update_params = [['id','order_id', 'commodity_id', 'amount', 'location', 'created_at', 'updated_at']];
  268. if(count($inner_params)==0)return $update_params;
  269. $date = (string)Carbon::now();
  270. foreach ($inner_params as $key=>$inner_param) {
  271. if(count($ids)==0)break;
  272. $inner_param['id'] = array_shift($ids);
  273. $inner_param['updated_at'] = $date;
  274. $inner_param['created_at'] = $date;
  275. $update_params[] = $inner_param;
  276. unset($inner_params[$key]);
  277. }
  278. return $update_params;
  279. }
  280. }