php">public function clearGoods(Input $input){$options = $input->getOptions();$start_time = microtime(true);$clearTime = strtotime($options['endTime'] ?? '') ?: strtotime(date('2020-01-01'));$where = ['completetime' => ['<', $clearTime],'retread_time' => ['<', $clearTime]];$data = Goods::where($where)->column('goods_spu');$this->output->writeln(Goods::getLastSql());$data = array_unique($data);$dataCount = count($data);$limit = 1000;$totalPage = ceil($dataCount / $limit);$this->output->writeln("总数据量:{$dataCount}");$this->output->writeln("每次查询:{$limit}");$this->output->writeln("总页数:{$totalPage}");if (empty($dataCount)) {$this->output->writeln('查无数据');return;}for ($pageNo = 1; $pageNo <= $totalPage; $pageNo++) {$this->output->info("第 {$pageNo} 页");$spus = array_slice($data, ($pageNo - 1) * $limit, $limit);$this->output->writeln('开始处理数据');$result = ClearService::instance()->dealClearGoods($spus);$message = $result['message'] ?? ($result['messages'] ?? '');$this->_showMsg($message);$this->output->info("第 {$pageNo} 页处理完成");}$this->output->writeln("执行时间:" . (microtime(true) - $start_time));}
php"> private function _showMsg($message){if (is_array($message)) {foreach ($message as $key => $value) {$this->output->writeln("{$key}:" . (is_array($value) ? json_encode($value) : $value));}} else {$this->output->writeln($message);}}
php"> public function dealClearGoods($goods_spus){if (empty($goods_spus)) return back(0, "SPU为空");$skus_arr = DevGoodsSku::where(['goods_spu' => ['IN', $goods_spus]])->column('goods_sku');$stock_break_skus = $this->checkStockBySkus($skus_arr);$sale_break_skus = $this->checkSalesBySkus($skus_arr);$all_break_skus = sep_unique_str(array_merge($stock_break_skus, $sale_break_skus));$need_break_spus = DevGoodsSku::where(['goods_sku' => ['IN', $all_break_skus]])->group('goods_spu')->column('goods_spu');$this->output->writeln("需要剔除的spu总数:".count($need_break_spus));$need_up_spus = array_diff($goods_spus, $need_break_spus);$this->output->writeln("需要更新的spu总数:".count($need_up_spus));$this->updateIsClear($need_up_spus,1);return back(1, '执行完成');}