ProductPriceUpdate.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace app\command;
  3. use app\common\model\shared\SharedProsperityPartnerConsumeUpdateLog;
  4. use app\common\repositories\shared\SharedProsperityRecommendConfigRepository;
  5. use think\console\Command;
  6. use think\console\Input;
  7. use think\console\Output;
  8. use think\facade\Db;
  9. class ProductPriceUpdate extends Command
  10. {
  11. // 变更数量
  12. const NUMBER = 5;
  13. const PRODUCT_IDS = [31053, 32171];
  14. protected function configure()
  15. {
  16. // 指令配置
  17. $this->setName('ProductPriceUpdate')
  18. ->setDescription('商品价格更新');
  19. }
  20. protected function execute(Input $input, Output $output)
  21. {
  22. $output->writeln('定时任务执行中...');
  23. $this->priceUpdate();
  24. $output->writeln('定时任务执行完成');
  25. }
  26. private function priceUpdate()
  27. {
  28. // 获取当前数据
  29. $productData = Db::name('store_product')->where(['product_id' => self::PRODUCT_IDS])->field('product_id,price')->select()->toArray();
  30. $attrData = Db::name('store_product_attr_value')->where(['product_id' => self::PRODUCT_IDS])->field('product_id,price')->select()->toArray();
  31. Db::startTrans();
  32. try {
  33. foreach ($productData as $product) {
  34. $log = [
  35. 'product_id' => $product['product_id'],
  36. 'old_price' => $product['price'],
  37. 'number' => self::NUMBER,
  38. 'new_price' => $product['price'] + self::NUMBER,
  39. ];
  40. Db::name('product_price_update_log')->insert($log);
  41. Db::name('store_product')->where(['product_id' => $product['product_id']])->inc('price', self::NUMBER)->update();
  42. Db::name('store_product_attr_value')->where(['product_id' => $product['product_id']])->inc('price', self::NUMBER)->update();
  43. }
  44. ;
  45. Db::commit();
  46. return true;
  47. } catch (\Exception $e) {
  48. Db::rollback();
  49. throw $e;
  50. }
  51. }
  52. }