public static function diagnose() { $errors = []; $errors[] = "请确保cron.yml配置了定时刷新的命令\n\t\thazardous-control-orders:\n\t\t\tinterval: '30 3 * * *'\n\t\t\tcommand: hazardouscontrol relationshipop build\n\t\t\tcomment: 定时将订单的信息刷新到表里,为管理方查数据提供支持"; $db = \Gini\Database::db(); $tableName = \Gini\Config::get('hazardous-control-orders.table') ?: '_hazardous_control_order_product'; if (!$db->query("DESC {$tableName}")) { $errors[] = "请确保 {$tableName} 表已经创建: gini hazardouscontrol relationshipop prepare-table"; $errors[] = "如果是初次部署建议初始化数据: gini hazardouscontrol relationshipop build"; } return $errors; }
public function actionInitProduct() { $db = \Gini\Database::db(); $db->query('truncate product'); $types = ['drug_precursor', 'highly_toxic', 'hazardous', 'explosive']; foreach ($types as $type) { $csv = new \Gini\CSV(APP_PATH . '/' . DATA_DIR . '/product/' . $type . '.csv', 'r'); $csv->read(); while ($row = $csv->read()) { $cas_no = trim($row[1]); if (strstr($cas_no, ';')) { $cas_nos = explode(';', $cas_no); foreach ($cas_nos as $cas_no) { if (!$cas_no) { continue; } $sql = "INSERT INTO `product` (`name`, `cas_no`, `type`) values (" . $db->quote($row[0]) . "," . $db->quote($cas_no) . ", " . $db->quote($type) . ")"; if (!$db->query($sql)) { var_dump($sql); echo $type . "初始化失败 中断\n"; die; } } } else { if (!$cas_no) { continue; } $sql = "INSERT INTO `product` (`name`, `cas_no`, `type`) values (" . $db->quote($row[0]) . "," . $db->quote($cas_no) . ", " . $db->quote($type) . ")"; if (!$db->query($sql)) { var_dump($sql); echo $type . "初始化失败 中断\n"; die; } } } echo $type . "初始化完毕\n"; } }
private function getSubWhere() { $db = \Gini\Database::db(); $args = func_get_args(); $result = ''; switch (count($args)) { case 3: // =,!=,>=,like... if (strtoupper($args[1]) == 'IN') { if (is_array($args[2])) { $ins = []; foreach ($args[2] as $in) { $ins[] = $db->quote($in); } $result = strtr(":col IN :val", [':col' => $db->quoteIdent($args[0]), ':val' => '(' . implode(',', $ins) . ')']); } } else { $result = strtr(":col :op :val", [':col' => $db->quoteIdent($args[0]), ':op' => $args[1], ':val' => $db->quote($args[2])]); } break; case 4: // between ... and $result = strtr(":col BETWEEN :from AND :to", [':col' => $db->quoteIdent($args[0]), ':from' => $db->quote($args[2]), ':to' => $db->quote($args[3])]); break; } return $result; }
public function actionInit() { $db = \Gini\Database::db(); $tableName = self::_getTableName(); $max = $db->query("SELECT max(order_mtime) FROM {$tableName}")->value() ?: 0; $start = 0; $perpage = 100; $schema = self::$schema; $keys = array_keys($schema['fields']); array_shift($keys); $keys = implode(',', $keys); while (true) { $rows = those('order')->whose('mtime')->isGreaterThan($max)->andWhose('customized')->is(0)->orderBy('mtime', 'asc')->limit($start, $perpage); if (!count($rows)) { break; } $start += $perpage; $values = []; foreach ($rows as $row) { $items = (array) $row->items; $qRowID = $db->quote($row->id); $qRowMd5 = $db->quote($this->getMd5($row)); // 检测有没有历史数据 $hisCount = $db->query("SELECT COUNT(*) FROM {$tableName} WHERE order_id={$qRowID}")->value() ?: 0; if ($hisCount) { // 检测历史数据是不是已经跟最新的数据不一致了 // 如果不一致,需要删除重建 // 如果一致,就不需要在重复添加了 $query = $db->query("DELETE FROM {$tableName} WHERE order_id={$qRowID} AND order_md5!={$qRowMd5}"); if (!$query || !$query->count()) { echo "\norder#{$row->id} 更新数据失败\n"; continue; } } foreach ($items as $i => $item) { // 如果商品价格是待询价, 当成无效订单处理 if ($item['unit_price'] < 0) { continue; } $values[] = '(' . implode(',', [$db->quote($row->id), $db->quote($row->mtime), $db->quote($this->getMd5($row)), $db->quote($item['id']), $db->quote(''), $db->quote(''), $db->quote($row->vendor->id), $db->quote($row->vendor->name), $db->quote($row->group->id), $db->quote($row->group->title), $db->quote(round($row->price, 2)), $db->quote(''), $db->quote($item['quantity']), $db->quote($item['unit_price']), $db->quote($item['price']), $db->quote($row->status), $db->quote('')]) . ')'; } } if (empty($values)) { continue; } $values = implode(',', $values); $db = \Gini\Database::db(); $sql = "INSERT INTO {$tableName}({$keys}) VALUES {$values};"; if ($db->query($sql)) { echo '.'; } else { echo 'x'; } } }
public static function diagnose($items = null) { $errors = []; if (!$items || in_array('dependencies', $items)) { echo "Checking module dependencies...\n"; // check gini dependencies foreach (\Gini\Core::$MODULE_INFO as $name => $info) { if (!$info->error) { continue; } $errors['dependencies'][] = "{$name}: {$info->error}"; } if ($errors['dependencies']) { static::_outputErrors($errors['dependencies']); } else { echo " [32mdone.[0m\n"; } echo "\n"; } // check composer requires if (!$items || in_array('composer', $items)) { echo "Checking composer dependencies...\n"; foreach (\Gini\Core::$MODULE_INFO as $name => $info) { if ($info->composer) { if (!file_exists(APP_PATH . '/vendor')) { $errors['composer'][] = $name . ': composer packages missing!'; } break; } } if ($errors['composer']) { static::_outputErrors($errors['composer']); } else { echo " [32mdone.[0m\n"; } echo "\n"; } if (!$items || in_array('file', $items)) { echo "Checking file/directory modes...\n"; // check if /tmp/gini-session is writable $path_gini_session = sys_get_temp_dir() . '/gini-session'; if (is_dir($path_gini_session) && !is_writable($path_gini_session)) { $errors['file'][] = "{$path_gini_session} is not writable!"; } if ($errors['file']) { static::_outputErrors($errors['file']); } else { echo " [32mdone.[0m\n"; } echo "\n"; } if (!$items || in_array('web', $items)) { echo "Checking web dependencies...\n"; if (!file_exists(APP_PATH . '/web')) { $errors['web'][] = "Please run [1m\"gini web update\"[0m to generate web directory!"; } if ($errors['web']) { static::_outputErrors($errors['web']); } else { echo " [32mdone.[0m\n"; } echo "\n"; } // if (!$items || in_array('database', $items)) { $conf = \Gini\Config::get('database'); if (!empty($conf)) { echo "Checking Database...\n"; foreach ((array) $conf as $key => $opts) { $db = \Gini\Database::db($key); $database_errors = $db->diagnose(); if ($database_errors) { static::_outputErrors($module_errors); } else { echo " [32mdone.[0m\n"; } echo "\n"; } } } if (defined('I18N_PATH') && (!$items || in_array('i18n', $items))) { echo "Checking Locale...\n"; $locale = \Gini\Config::get('system.locale') ?: 'en_US'; $lodir = I18N_PATH . '/' . $locale . '/LC_MESSAGES'; $mofile = $lodir . '/' . APP_ID . '.mo'; $pofile = $lodir . '/' . APP_ID . '.po'; if (!file_exists($mofile) || !file_exists($pofile)) { static::_outputErrors(['Please run: gini i18n format ' . $locale]); } else { echo " [32mdone.[0m\n"; } echo "\n"; } // enumerate all doctor extensions if ((!$items || in_array('dependencies', $items) && in_array('module_spec', $items)) && !isset($errors['dependencies'])) { // check gini dependencies foreach (\Gini\Core::$MODULE_INFO as $name => $info) { $class = '\\Gini\\Module\\' . strtr($name, ['-' => '', '_' => '', '/' => '']); $diag_func = "{$class}::diagnose"; if (is_callable($diag_func)) { echo "Checking Module[{$name}]...\n"; $module_errors = call_user_func($diag_func); if ($module_errors) { static::_outputErrors($module_errors); $errors['dependencies'][] = "Module[{$name}] found some error"; } else { echo " [32mdone.[0m\n"; } echo "\n"; } } } return $errors; }
private function _getSelect($type) { $db = \Gini\Database::db(); $tableName = self::_getOPTableName(); $sql = 'SELECT '; switch ($type) { case 'group': $subSql = 'group_name,'; break; case 'college': $subSql = 'college_name,'; break; case 'vendor': default: $subSql = 'vendor_name,'; break; } if ($subSql) { $sql .= $subSql; } // 待付款金额总计 $pending_status = \Gini\ORM\Order::STATUS_APPROVED; $transferred_status = \Gini\ORM\Order::STATUS_TRANSFERRED; $paid_status = \Gini\ORM\Order::STATUS_PAID; $canceled_status = \Gini\ORM\Order::STATUS_CANCELED; $sql .= "SUM( CASE WHEN order_status={$pending_status} THEN product_total_price ELSE 0 END) AS pending_balance,"; $sql .= "SUM( CASE WHEN order_status={$transferred_status} THEN product_total_price ELSE 0 END) AS transferred_balance,"; $sql .= "SUM( CASE WHEN order_status={$paid_status} THEN product_total_price ELSE 0 END) AS paid_balance,"; $sql .= "SUM( CASE WHEN order_status={$canceled_status} THEN product_total_price ELSE 0 END) AS canceled_balance,"; $sql .= "SUM(product_total_price) AS total_balance,"; $sql .= "COUNT(DISTINCT CASE WHEN order_status={$pending_status} THEN order_id END) AS pending_count,"; $sql .= "COUNT(DISTINCT CASE WHEN order_status={$transferred_status} THEN order_id END) AS transferred_count,"; $sql .= "COUNT(DISTINCT CASE WHEN order_status={$paid_status} THEN order_id END) AS paid_count,"; $sql .= "COUNT(DISTINCT CASE WHEN order_status={$canceled_status} THEN order_id END) AS canceled_count,"; $sql .= "COUNT(DISTINCT order_id) AS total_count,"; $sql .= "SUM(product_quantity) as product_count"; $sql .= ' FROM ' . $db->quoteIdent($tableName); return $sql; }