OrderCommodityService.php 12 KB

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