diff --git a/app/common/model/dict/DictData.php b/app/common/model/dict/DictData.php index 22a72836b..b4f24bcbb 100644 --- a/app/common/model/dict/DictData.php +++ b/app/common/model/dict/DictData.php @@ -44,4 +44,14 @@ class DictData extends BaseModel return $data['status'] ? '正常' : '停用'; } + public static function getDictValue($type, $key) + { + $dictType = DictType::where('type', $type)->where('status', 1)->find(); + if ($dictType) { + $dictData = DictData::where('type_id', $dictType['id'])->where('name', $key)->where('status', 1)->value('value'); + return empty($dictData) ? '' : $dictData; + } + return ''; + } + } \ No newline at end of file diff --git a/process/Task.php b/process/Task.php index 198660c65..aa10565b3 100644 --- a/process/Task.php +++ b/process/Task.php @@ -4,11 +4,14 @@ namespace process; use app\common\enum\OrderEnum; use app\common\logic\PayNotifyLogic; +use app\common\model\dict\DictData; use app\common\model\store_branch_product\StoreBranchProduct; use app\common\model\store_order\StoreOrder; use app\common\model\store_order_cart_info\StoreOrderCartInfo; use app\common\model\store_product\StoreProduct; +use app\common\model\store_product_price\StoreProductPrice; use app\common\model\user_recharge\UserRecharge; +use support\Cache; use think\facade\Db; use Webman\RedisQueue\Redis; use Workerman\Crontab\Crontab; @@ -62,5 +65,41 @@ class Task } } }); + + $this->updateProductPrice(); } + + /** + * 将商品价格更改列表的价格同步至商品 + * @return void + */ + public function updateProductPrice() + { + new Crontab('0 */10 * * * *', function () { + $value = DictData::getDictValue('update_product_price_task', 'status'); + if ($value == 1) { + $lastProductId = Cache::get('update_product_price_last_product_id', 0); + $productIds = StoreProduct::where('purchase', 0)->where('id', '>', $lastProductId)->limit(200)->column('id'); + $lastProductId = end($productIds); + if (count($productIds) < 200) { + $lastProductId = 0; + } + Cache::set('update_product_price_last_product_id', $lastProductId); + $productPrices = StoreProductPrice::whereIn('product_id', $productIds)->where('status', 1)->distinct('product_id')->order('id desc')->select()->toArray(); + $update = []; + foreach ($productPrices as $productPrice) { + $update[] = [ + 'id' => $productPrice['product_id'], + 'purchase' => $productPrice['purchase'] ?? 0, + 'cost' => $productPrice['cost'] ?? 0, + 'price' => $productPrice['purchase'] ?? 0, + ]; + } + if (count($update) > 0) { + (new StoreProduct())->saveAll($update); + } + } + }); + } + }