OrderCommodityService.php 13 KB

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