OrderCommodityService.php 14 KB

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