OrderCommodityService.php 13 KB

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