public function actionRefreshSales()
{
$transaction = Yii::$app->db->beginTransaction();
try {
$aMonthAgo = strtotime('-1 month', strtotime("00:00:00"));
$sql = "UPDATE {{%goods}} SET sales = (SELECT IFNULL(SUM(t0.count), 0) FROM {{%order_goods}} AS t0 LEFT JOIN {{%order_volume}} AS t1 ON t0.order_id = t1.order_id WHERE t0.goods_id = {{%goods}}.id AND t1.id IS NOT NULL AND t1.created_at >= {$aMonthAgo})";
$count = Yii::$app->db->createCommand($sql)->execute();
$transaction->commit();
echo "{$count} of goods affected by the execution.\n";
return static::EXIT_CODE_NORMAL;
} catch (\Exception $e) {
$transaction->rollBack();
echo $e->getMessage();
return static::EXIT_CODE_ERROR;
}
}