CommonChooselogModel.class.php
4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* CommonChooselogModel.class.php
* 选人记录表 Model
* @author: 原习斌
* @date :2016-09-02
*/
namespace Common\Model;
class CommonChooselogModel extends AbstractModel
{
/**
* 最少选择3次,才能算常用人员
* @var unknown
*/
const MIN_CHOOSIE_TIME = 3;
/**
* 选择人员的数据库类型值
* @var int
*/
const CHOOSE_MEM = 1;
/**
* 选择部门的数据库类型值
* @var int
*/
const CHOOSE_DEP = 2;
/**
* 选择标签的数据库类型值
* @var int
*/
const CHOOSE_TAG = 3;
// 选人组件 flag类型: 部门
const CHOOSE_FLAG_DEP = 1;
// 选人组件 flag类型: 人员
const CHOOSE_FLAG_MEMBER = 3;
// 选人组件 flag类型: 职位
const CHOOSE_FLAG_JOB = 5;
// 构造方法
public function __construct()
{
parent::__construct();
}
/**
* 获取常用人员
*
* @param string $eaID 管理员ID
* @param array $condition 查询参数
* @param int|array $page_option 分页参数
* @return array|bool
*/
public function get_often_used($eaID, $condition = array(), $page_option = null)
{
$limit = '';
if (!$this->_limit($limit, $page_option)) {
return false;
}
// 次数子查询
$ct_sql = '(
SELECT
COUNT(*)
FROM
`oa_common_chooselog`
WHERE
`domain`=`main_tb`.`domain`
AND `status`<' . self::ST_DELETE . '
AND `chooseId`=`main_tb`.`chooseId`
AND `choose_type`=`main_tb`.`choose_type`
) AS `ct` ';
// 组建sql语句
list($sql, $params) = $this->_sql_for_often_used($eaID, $condition);
$sql = 'SELECT `cid`,`choose_type`,`chooseId`,' . $ct_sql . $sql . $limit;
return $this->_m->fetch_array($sql, $params);
}
/**
* 获取常用人员的数量
* @param array $condition 查询参数
* @param string $eaID 管理员ID
* @return int
*/
public function count_for_often_used($eaID, $condition = array())
{
// 次数子查询
$ct_sql = '(
SELECT
COUNT(*)
FROM
`oa_common_chooselog`
WHERE
`domain`=`main_tb`.`domain`
AND `status`<' . self::ST_DELETE . '
AND `chooseId`=`main_tb`.`chooseId`
AND `choose_type`=`main_tb`.`choose_type`
) AS `ct` ';
// 组建sql语句
list($sql, $params) = $this->_sql_for_often_used($eaID, $condition);
$sql = 'SELECT COUNT(*) AS `ct` FROM (SELECT ' . $ct_sql . $sql . ') AS tmp';
$res = $this->_m->fetch_row($sql, $params);
return isset($res['ct']) ? intval($res['ct']) : 0;
}
/**
* 获取常用人员的sql和param,
* 可以在获取列表和获取总数的时候用,
* 从from语句开始,
* 例如:FROM xxx WHERE xxx GROUP BY xxx ....... ORDER BY xxx
*
* @param string $eaID 管理员ID
* @param array $condition 查询参数
* @return array array(sql语句, param参数数组)
*/
protected function _sql_for_often_used($eaID, $condition = array())
{
$params = array(
$eaID,
QY_DOMAIN,
self::ST_DELETE,
);
// 初始化
$wheres = [];
// 如果查询人员条件存在
if (!empty($condition['uids'])) {
$wheres[] = 'chooseId IN (?)';
$params[] = $condition['uids'];
}
// sql语句
$sql = 'FROM __TABLE__ AS `main_tb`
WHERE `eaId`=? AND `domain`=? AND `status`<? ';
// 拼sql语句
$sql = empty($wheres) ? $sql : $sql . ' AND ' . implode(' AND ', $wheres);
$sql .= " GROUP BY `chooseId`,`choose_type`
HAVING `ct` > ?
ORDER BY `ct` DESC";
$params[] = self::MIN_CHOOSIE_TIME;
return array($sql, $params);
}
}