InventoryDailyLoggingOwner.php 5.3 KB

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