Skip to content

Commit

Permalink
完成高级查询的接口开发
Browse files Browse the repository at this point in the history
  • Loading branch information
weichaoduo committed Dec 4, 2019
1 parent 92f806a commit 155edd9
Show file tree
Hide file tree
Showing 151 changed files with 564 additions and 32,382 deletions.
171 changes: 170 additions & 1 deletion app/classes/IssueFilterLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public function getList($page = 1, $pageSize = 50)
$followIssueIdArr[] = $issueFollow['issue_id'];
}
$followIssueIdArr = array_unique($followIssueIdArr);
if(!empty($followIssueIdArr)){
if (!empty($followIssueIdArr)) {
$issueIdStr = implode(',', $followIssueIdArr);
$sql .= " AND id in ({$issueIdStr})";
}
Expand Down Expand Up @@ -412,6 +412,175 @@ public function getList($page = 1, $pageSize = 50)
}
}

/**
* 高级查询
* @param int $page
* @param int $pageSize
* @return array
* @throws \Exception
*/
public function getAdvQueryList($page = 1, $pageSize = 50)
{
// sys_filter=1&fav_filter=2&project=2&reporter=2&title=fdsfdsfsd&assignee=2&created_start=232131&update_start=43432&sort_by=&32323&mod=123&reporter=12&priority=2&status=23&resolution=2
$params = [];
$sql = " WHERE 1";

// 项目筛选
$projectId = null;
if (isset($_GET['project']) && !empty($_GET['project'])) {
$projectId = (int)$_GET['project'];
$sql .= " AND project_id=:project";
$params['project'] = $projectId;
} else {
// 如果没有指定某一项目,则获取用户参与的项目
$userJoinProjectIdArr = PermissionLogic::getUserRelationProjectIdArr(UserAuth::getId());
if (!empty($userJoinProjectIdArr)) {
$projectIdStr = implode(',', $userJoinProjectIdArr);
$sql .= " AND project_id IN ({$projectIdStr}) ";
}
}

$queryJson = null;
$queryArr = [];
if (isset($_GET['adv_query_json'])) {
$queryJson = $_GET['adv_query_json'];
$queryArr = json_decode($queryJson, true);
}

if (!$queryArr) {
return [false, '查询条件格式错误', 0];
}
// 先获取Mysql版本号
$versionSql = 'select version() as vv';
$issueModel = new IssueModel();
$versionStr = $issueModel->db->getOne($versionSql);
$versionNum = floatval($versionStr);
if (strpos($versionStr, 'MariaDB') !== false) {
$versionNum = 0;
}
$startBracesNum = 0;
$endBracesNum = 0;
$sql .= 'AND ( ';
$i = 0;
foreach ($queryArr as $item) {
$i++;
$logic = strtoupper($item['logic']);
if ($i == 1) {
$logic = '';
}
$startBraces = trimStr($item['start_braces']);
if ($startBraces == '(') {
$startBracesNum++;
}
$endBraces = trimStr($item['end_braces']);
if ($endBraces == ')') {
$endBracesNum++;
}
$field = trimStr($item['field']);
$opt = urldecode($item['opt']);
$value = $item['value'] ;

if ($field == 'updated' || $field == 'created') {
$value = strtotime($value);
}

$sql .= " {$logic} {$startBraces} ";

switch ($opt) {
case '=':
case '!=':
case '>':
case '>=':
case '<=':
case '<':
$sql .= " $field {$opt}:$field ";
$params[$field] = $value;
break;
case 'in':
case 'not in':
$sql .= " $field {$opt} ( :$field ) ";
$params[$field] = $value;
break;
case 'like':
$sql .= " $field {$opt} ':$field' ";
$params[$field] = $value;
break;
case 'like %...%':
if ($versionNum < 5.70) {
$sql .= " LOCATE(:$field,$field)>0 ";
$params[$field] = $value;
} else {
// 使用全文索引
$sql .= " MATCH ($field) AGAINST (:$field IN NATURAL LANGUAGE MODE) ";
$params[$field] = $value;
}
break;
case 'is null':
case 'is not null':
$sql .= " $field {$opt} ";
$params[$field] = $value;
break;
case 'between':
case 'not between':
$sql .= " $field {$opt} :$field";
$params[$field] = $value;
break;
case 'regexp':
$value = urldecode($value);
$sql .= " $field {$opt} '$value' ";
break;
case 'regexp ^...$':
$sql .= " $field {$opt} '^{$value}$' ";
break;
default:
$sql .= " $field {$opt} :$field ";
$params[$field] = $value;
}
$sql .= " {$endBraces} ";
}
$sql .= ' ) ';
if ($startBracesNum != $endBracesNum) {
return [false, '查询条件的括号 ( ) 条件错误', 0];
}

$orderBy = 'id';
if (isset($_GET['sort_field'])) {
$orderBy = $_GET['sort_field'];
}
$sortBy = 'DESC';
if (isset($_GET['sort_by']) && !empty($_GET['sort_by'])) {
$sortBy = $_GET['sort_by'];
}

$start = $pageSize * ($page - 1);
$limit = " limit $start, " . $pageSize;
$order = empty($orderBy) ? '' : " Order By $orderBy $sortBy";

$model = new IssueModel();
$table = $model->getTable();

try {

// 获取总数
$sqlCount = "SELECT count(*) as cc FROM {$table} " . $sql;
//echo $sqlCount;
//print_r($params);
$count = $model->db->getOne($sqlCount, $params);
$fields = '*';
$sql = "SELECT {$fields} FROM {$table} " . $sql;

$sql .= ' ' . $order . $limit;
//print_r($params);
//echo $sql;die;

$arr = $model->db->getRows($sql, $params);
// var_dump( $arr, $count);
return [true, $arr, $count];
} catch (\PDOException $e) {
return [false, $e->getMessage(), 0];
}
}

/**
* @param int $userId
* @param int $page
Expand Down
34 changes: 33 additions & 1 deletion app/ctrl/issue/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,6 @@ public function pageQrMobileUpload()
*/
public function mobileUpload()
{

$tmpIssueId = '';
if (isset($_GET['tmp_issue_id'])) {
$tmpIssueId = $_GET['tmp_issue_id'];
Expand Down Expand Up @@ -561,6 +560,39 @@ public function filter()
}
}

/**
* 高级查询
* @throws \Exception
*/
public function advFilter()
{
$issueFilterLogic = new IssueFilterLogic();

$pageSize = 20;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$page = max(1, $page);
if (isset($_GET['page'])) {
$page = max(1, intval($_GET['page']));
}

list($ret, $data['issues'], $total) = $issueFilterLogic->getAdvQueryList($page, $pageSize);
if ($ret) {
foreach ($data['issues'] as &$issue) {
IssueFilterLogic::formatIssue($issue);
}
$data['total'] = (int)$total;
$data['pages'] = ceil($total / $pageSize);
$data['page_size'] = $pageSize;
$data['page'] = $page;
$_SESSION['filter_current_page'] = $page;
$_SESSION['filter_pages'] = $data['pages'];
$_SESSION['filter_page_size'] = $pageSize;
$this->ajaxSuccess('success', $data);
} else {
$this->ajaxFailed('failed', $data['issues']);
}
}

/**
* 获取保存过的过滤器列表
* @throws \Exception
Expand Down
Loading

0 comments on commit 155edd9

Please sign in to comment.