/** * 数据修复 * * @param string $type 统计类型 * - support_num 支持人数 * - paied_num 支付数量 * - partake_num 参与人数 * - amount 总金额 * 默认为空 * @param string $method 统计方式 * - product 产品统计(默认) * - activity 活动产品统计 * @param string $ids 产品ID集合 * 产品ID集合的格式为"产品ID1,产品ID2,.....产品IDN" */ public function actionRepair($type = '', $method = 'product', $ids = '') { $page = 1; $perPage = 50; $startNum = ($page - 1) * $perPage; $result = []; $options = []; $count = 0; $mOrder = new Order(); $types = ['support_num', 'paied_num', 'partake_num', 'amount']; $methods = ['product', 'activity']; $type = trim($type); $time = microtime(true); $log = new FileTarget(); if (!$type) { die("Error: Param type is empty\n"); } if (!in_array($type, $types)) { die("Error: Illegal Type\n"); } if (!in_array($method, $methods)) { die("Error: Illegal Method\n"); } if ($method == 'activity') { $ac = ActivityProductStat::find(); } else { $ac = ProductStat::find(); } $log->logFile = Yii::$app->getRuntimePath() . '/logs/' . self::LOGNAME_PREFIX . $method . '_' . $type . '_' . date('YmdHis') . '.log'; if ($ids) { $ids = array_map('trim', explode(',', $ids)); $ids = array_map('intval', $ids); $ids = array_filter($ids); $ids = array_unique($ids); $result = $ac->where(['product_id' => $ids])->all(); } else { $result = $ac->offset($startNum)->limit($perPage)->all(); } $this->_logger($log, 'Process Start'); $this->_logger($log, 'Type:' . strtoupper($type)); echo "Process Start\n"; echo 'Type:' . strtoupper($type) . "\n"; while ($result) { if ($result) { foreach ($result as $item) { if (isset($item['product_id']) && $item['product_id']) { $options['product_id'] = $item['product_id']; if (isset($item['acti_id']) && $item['acti_id']) { $options['acti_id'] = $item['acti_id']; } $count = $mOrder->getCountByType($type, $options); if ($count === NULL || $count === false) { continue; } $item[$type] = $count; if ($item->save()) { $this->_logger($log, 'Updated: pid=' . $item['product_id'] . ', count=' . $count); echo 'Updated: pid=' . $item['product_id'] . ', count=' . $count . "\n"; } else { $this->_logger($log, 'Error: pid=' . $item['product_id']); echo 'Error: pid=' . $item['product_id'] . "\n"; } } } } if ($ids) { break; } else { echo "========== Page {$page} finished processing ==========\n\n"; $startNum = ($page - 1) * $perPage; $page++; $result = $ac->offset($startNum)->limit($perPage)->all(); } } echo "Process Over\n"; $this->_logger($log, 'Process Over'); $log->export(); }