setName('ProductPriceUpdate') ->setDescription('商品价格更新'); } protected function execute(Input $input, Output $output) { $output->writeln('定时任务执行中...'); $this->priceUpdate(); $output->writeln('定时任务执行完成'); } private function priceUpdate() { // 获取当前数据 $productData = Db::name('store_product')->where(['product_id' => self::PRODUCT_IDS])->field('product_id,price')->select()->toArray(); $attrData = Db::name('store_product_attr_value')->where(['product_id' => self::PRODUCT_IDS])->field('product_id,price')->select()->toArray(); Db::startTrans(); try { foreach ($productData as $product) { $log = [ 'product_id' => $product['product_id'], 'old_price' => $product['price'], 'number' => self::NUMBER, 'new_price' => $product['price'] + self::NUMBER, ]; Db::name('product_price_update_log')->insert($log); Db::name('store_product')->where(['product_id' => $product['product_id']])->inc('price', self::NUMBER)->update(); Db::name('store_product_attr_value')->where(['product_id' => $product['product_id']])->inc('price', self::NUMBER)->update(); } ; Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); throw $e; } } }