InventoryDailyLoggingOwner.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\CommodityBarcode;
  4. use App\Services\CommodityService;
  5. use App\Services\LogService;
  6. use Illuminate\Console\Command;
  7. use App\InventoryDailyLoggingOwner as LoggingOwner;
  8. use Illuminate\Support\Facades\DB;
  9. class InventoryDailyLoggingOwner extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'InventoryDailyLoggingOwner';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '监听指定货主记录';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. * @param CommodityService $commodityService
  35. * @return int
  36. */
  37. public function handle(CommodityService $commodityService)
  38. {
  39. //获取需要查询的货主,键值对:code => id
  40. $owners = $this->getLoggingOwners();
  41. //计算数量,为0直接return
  42. $let = count($owners);
  43. if ($let == 0)return;
  44. //拼接SQL,SELECT仅取指定字段
  45. $sql = "SELECT INV_LOT.customerid,INV_LOT.sku,INV_LOT.qty,BAS_SKU.skulength,
  46. BAS_SKU.skuwidth,BAS_SKU.skuhigh,BAS_SKU.cube,BAS_SKU.descr_c,BAS_SKU.alternate_sku1,BAS_SKU.grossweight
  47. FROM INV_LOT
  48. LEFT JOIN BAS_SKU ON INV_LOT.sku = BAS_SKU.sku AND INV_LOT.customerid = BAS_SKU.customerid
  49. WHERE INV_LOT.customerid IN (";
  50. $index = 1;
  51. foreach ($owners as $code => $id){
  52. $sql .= "'".$code."'";
  53. if ($index != $let)$sql .= ",";
  54. $index++;
  55. }
  56. $sql .= ")";
  57. //执行获取结果:stdClass类型
  58. $invLots = DB::connection('oracle')->select(DB::raw($sql));
  59. //声明一个数组,作为第一次去重的容器
  60. $inventoryDailyLogs = [];
  61. foreach ($invLots as $invLot){
  62. //以MAP形式记录进数组,货主code与商品sku作为联合主键唯一标识,如重复叠加其数量
  63. if ($inventoryDailyLogs[$owners[$invLot->customerid].'-'.$invLot->sku] ?? false){
  64. $inventoryDailyLogs[$owners[$invLot->customerid].'-'.$invLot->sku]['amount'] += $invLot->qty;
  65. }else{
  66. //符合的结果取此对象的关键信息存进第一个数组
  67. $inventoryDailyLogs[$owners[$invLot->customerid].'-'.$invLot->sku] = [
  68. 'commodity' => [
  69. 'owner_id'=>$owners[$invLot->customerid],
  70. 'sku'=>$invLot->sku,
  71. 'name'=>$invLot->descr_c,
  72. 'length'=>$invLot->skulength,
  73. 'width'=>$invLot->skuwidth,
  74. 'height'=>$invLot->skuhigh,
  75. 'volumn'=>$invLot->cube,
  76. 'code'=>$invLot->alternate_sku1,
  77. 'weight' => $invLot->grossweight,
  78. ],
  79. 'amount' => $invLot->qty,
  80. 'volumn_occupied' => 0,
  81. 'gross_weight' => 0,
  82. ];
  83. }
  84. }
  85. //第二个数组作为批量插入使用
  86. $data = [];
  87. //遍历第一个数组,此时已经去重完成,直接取对应参数push进data中
  88. foreach ($inventoryDailyLogs as $inventoryDailyLog){
  89. //寻找己方库中是否存在对应商品,存在更新其长宽高体积,不存在录入
  90. $commodity = $inventoryDailyLog['commodity'];
  91. $param = ['owner_id'=>$commodity['owner_id'],'sku'=>$commodity["sku"]];
  92. //体积存在为0的情况,需再次计算一次,此处体积为m³
  93. if (!$commodity['volumn'] || $commodity['volumn'] == "0"){
  94. $commodity['volumn'] = $commodity['length']*$commodity['width']*$commodity['height'];
  95. }
  96. $column = [
  97. 'owner_id'=>$commodity['owner_id'],
  98. 'sku'=>$commodity['sku'],
  99. 'name'=>$commodity['name'],
  100. 'length'=>$commodity['length'],
  101. 'width'=>$commodity['width'],
  102. 'height'=>$commodity['height'],
  103. 'volumn'=>$commodity['volumn'],
  104. ];
  105. $result = $commodityService->updateOrCreate($param,$column);
  106. LogService::log(__METHOD__,"同步库存每日记录时修改或生成商品",json_encode($column));
  107. $commodity_id = $result->id;
  108. //寻找对应barcode是否存在,不存在录入
  109. if ($commodity['code']) CommodityBarcode::query()->firstOrCreate(['commodity_id'=>$commodity_id,'code'=>$commodity['code']]);
  110. //计算总体积,商品体积×该单数量
  111. $volumn_occupied = $commodity['volumn']*$inventoryDailyLog["amount"];
  112. $gross_weight = $commodity['weight']*$inventoryDailyLog["amount"];
  113. array_push($data,[
  114. "owner_id"=>$commodity['owner_id'],
  115. "created_at"=>date('Y-m-d H:i:s'),
  116. "commodity_id"=>$commodity_id,
  117. "amount"=>$inventoryDailyLog['amount'],
  118. "volumn_occupied"=>$volumn_occupied,
  119. "gross_weight"=>$gross_weight,
  120. ]);
  121. }
  122. DB::table('inventory_daily_logs')->insert($data);
  123. LogService::log(__METHOD__,"同步库存每日记录",json_encode($data));
  124. }
  125. public function getLoggingOwners(){
  126. $loggingOwners = LoggingOwner::with('owner')->select('id','owner_id')->where('status','启用')->get();
  127. $owners = [];
  128. foreach ($loggingOwners as $loggingOwner){
  129. if ($loggingOwner->owner){
  130. $owners[$loggingOwner->owner->code] = $loggingOwner->owner_id;
  131. }
  132. }
  133. return $owners;
  134. }
  135. }