ProductPriceUpdate.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace app\command;
  3. use app\common\repositories\store\product\ProductRepository;
  4. use think\console\Command;
  5. use think\console\Input;
  6. use think\console\Output;
  7. use think\facade\Db;
  8. class ProductPriceUpdate extends Command
  9. {
  10. // 变更数量
  11. const NUMBER = 5;
  12. const PRODUCT_IDS = [31053, 32171];
  13. const MER_ID = 3447;
  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])->select()->toArray();
  30. /** @var ProductRepository $repository */
  31. $repository = app()->make(ProductRepository::class);
  32. foreach ($productData as $product) {
  33. $log = [
  34. 'product_id' => $product['product_id'],
  35. 'old_price' => $product['price'],
  36. 'number' => self::NUMBER,
  37. 'new_price' => $product['price'] + self::NUMBER,
  38. ];
  39. Db::name('product_price_update_log')->insert($log);
  40. $attrValue = Db::name('store_product_attr_value')->where(['product_id' => $product['product_id']])->select()->toArray();
  41. $attrValue[0]['price'] = $attrValue[0]['price'] + self::NUMBER;
  42. $content = Db::name('store_product_content')->where(['product_id' => $product['product_id']])->field('content')->find();
  43. // $update = [
  44. //
  45. // 'couponData' => [],
  46. //
  47. // 'is_gift_bag' => $product['is_gift_bag'],
  48. // ];
  49. $update = [
  50. 'image' => $product['image'],
  51. 'slider_image' => explode(',',$product['slider_image']),
  52. 'store_name' => $product['store_name'],
  53. 'store_info' => $product['store_info'],
  54. 'keyword' => $product['keyword'],
  55. 'bar_code' => 0,
  56. 'brand_id' => $product['brand_id'],
  57. 'cate_id' => $product['cate_id'],
  58. 'mer_cate_id' => [],
  59. 'unit_name' => $product['unit_name'],
  60. 'sort' => $product['sort'],
  61. 'is_good' => $product['is_good'],
  62. 'video_link' => '',
  63. 'temp_id' => $product['temp_id'],
  64. 'content' => $content['content'],
  65. 'spec_type' => $product['spec_type'],
  66. 'extension_type' => $product['extension_type'],
  67. 'attr' => [],
  68. 'attrValue' => $attrValue,
  69. 'give_coupon_ids' => [],
  70. 'volunteer' => $product['volunteer'],
  71. 'type' => $product['type'],
  72. 'pension' => $product['pension'],
  73. 'commission' => $product['commission'],
  74. 'pension_is' => $product['pension_is'],
  75. 'share_is' => $product['share_is'],
  76. 'ficti' => $product['ficti'],
  77. 'icon_type' => [],
  78. 'status' => $product['status']
  79. ];
  80. $repository->update($product['product_id'],$update,self::MER_ID);
  81. Db::name('product_share')->where('product_id',$product['product_id'])->where('share_channel',1)->update(['url'=>'']);
  82. }
  83. // // 获取当前数据
  84. // $productData = Db::name('store_product')->where(['product_id' => self::PRODUCT_IDS])->field('product_id,price')->select()->toArray();
  85. // $attrData = Db::name('store_product_attr_value')->where(['product_id' => self::PRODUCT_IDS])->field('product_id,price')->select()->toArray();
  86. // Db::startTrans();
  87. // try {
  88. // foreach ($productData as $product) {
  89. // $log = [
  90. // 'product_id' => $product['product_id'],
  91. // 'old_price' => $product['price'],
  92. // 'number' => self::NUMBER,
  93. // 'new_price' => $product['price'] + self::NUMBER,
  94. // ];
  95. // Db::name('product_price_update_log')->insert($log);
  96. //
  97. // Db::name('store_product')->where(['product_id' => $product['product_id']])->inc('price', self::NUMBER)->update();
  98. // Db::name('store_product_attr_value')->where(['product_id' => $product['product_id']])->inc('price', self::NUMBER)->update();
  99. // }
  100. //
  101. // ;
  102. // Db::commit();
  103. // return true;
  104. // } catch (\Exception $e) {
  105. // Db::rollback();
  106. // throw $e;
  107. // }
  108. }
  109. }