| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- namespace app\command;
- use app\common\model\shared\SharedProsperityPartnerConsumeUpdateLog;
- use app\common\repositories\shared\SharedProsperityRecommendConfigRepository;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\facade\Db;
- class ProductPriceUpdate extends Command
- {
- // 变更数量
- const NUMBER = 5;
- const PRODUCT_IDS = [31053, 32171];
- protected function configure()
- {
- // 指令配置
- $this->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;
- }
- }
- }
|