table_common_credit_log.php
8.14 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: table_common_credit_log.php 31381 2012-08-21 07:56:35Z monkey $
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
class table_common_credit_log extends discuz_table
{
public function __construct() {
$this->_table = 'common_credit_log';
$this->_pk = 'logid';
parent::__construct();
}
public function fetch_by_operation_relatedid($operation, $relatedid) {
$relatedid = dintval($relatedid, true);
$parameter = array($this->_table, $operation, $relatedid);
$wherearr = array();
$wherearr[] = is_array($operation) && $operation ? 'operation IN(%n)' : 'operation=%s';
$wherearr[] = is_array($relatedid) && $relatedid ? 'relatedid IN(%n)' : 'relatedid=%d';
return DB::fetch_all('SELECT * FROM %t WHERE '.implode(' AND ', $wherearr), $parameter);
}
public function fetch_all_by_operation($operation, $start = 0, $limit = 0) {
return DB::fetch_all('SELECT * FROM %t WHERE operation=%s ORDER BY dateline DESC '.DB::limit($start, $limit), array($this->_table, $operation));
}
public function fetch_all_by_uid_operation_relatedid($uid, $operation, $relatedid) {
$parameter = array($this->_table);
$wherearr = array();
if($uid) {
$uid = dintval($uid, true);
$wherearr[] = is_array($uid) && $uid ? 'uid IN(%n)' : 'uid=%d';
$parameter[] = $uid;
}
$relatedid = dintval($relatedid, true);
$wherearr[] = is_array($operation) && $operation ? 'operation IN(%n)' : 'operation=%s';
$wherearr[] = is_array($relatedid) && $relatedid ? 'relatedid IN(%n)' : 'relatedid=%d';
$parameter[] = $operation;
$parameter[] = $relatedid;
return DB::fetch_all('SELECT * FROM %t WHERE '.implode(' AND ', $wherearr).' ORDER BY dateline', $parameter);
}
public function fetch_all_by_uid($uid, $start = 0, $limit = 0) {
$array = DB::fetch_all('SELECT * FROM %t WHERE uid=%d ORDER BY dateline DESC '.DB::limit($start, $limit), array($this->_table, $uid), 'logid');
if(!$array) {
return array();
}
$fieldids = array();
foreach($array as $key => $value) {
if(!in_array($value['operation'], lang('spacecp', 'logs_credit_update_INDEX'))) {
$fieldids[] = $key;
}
}
if($fieldids) {
$arrayfield = DB::fetch_all('SELECT * FROM %t WHERE logid IN(%n)', array('common_credit_log_field', $fieldids), 'logid');
foreach($arrayfield as $key => $value) {
$array[$key] += $value;
}
}
return $array;
}
public function fetch_all_by_search($uid, $optype, $begintime = 0, $endtime = 0, $exttype = 0, $income = 0, $extcredits = array(), $start = 0, $limit = 0, $relatedid = 0) {
$condition = $this->make_query_condition($uid, $optype, $begintime, $endtime, $exttype, $income, $extcredits, $relatedid);
$array = DB::fetch_all('SELECT * FROM %t '.$condition[0].' ORDER BY dateline DESC '.DB::limit($start, $limit), $condition[1], 'logid');
if(!$array) {
return array();
}
$fieldids = array();
foreach($array as $key => $value) {
if(!in_array($value['operation'], lang('spacecp', 'logs_credit_update_INDEX'))) {
$fieldids[] = $key;
}
}
if($fieldids) {
$arrayfield = DB::fetch_all('SELECT * FROM %t WHERE logid IN(%n)', array('common_credit_log_field', $fieldids), 'logid');
foreach($arrayfield as $key => $value) {
$array[$key] += $value;
}
}
return $array;
}
public function delete_by_operation_relatedid($operation, $relatedid) {
$relatedid = dintval($relatedid, true);
if($operation && $relatedid) {
return DB::delete($this->_table, DB::field('operation', $operation).' AND '.DB::field('relatedid', $relatedid));
}
return 0;
}
public function delete_by_uid_operation_relatedid($uid, $operation, $relatedid) {
$relatedid = dintval($relatedid, true);
$uid = dintval($uid, true);
if($relatedid && $uid && $operation) {
return DB::delete($this->_table, DB::field('uid', $uid).' AND '.DB::field('operation', $operation).' AND '.DB::field('relatedid', $relatedid));
}
return 0;
}
public function update_by_uid_operation_relatedid($uid, $operation, $relatedid, $data) {
$relatedid = dintval($relatedid, true);
$uid = dintval($uid, true);
if(!empty($data) && is_array($data) && $relatedid && $uid && $operation) {
return DB::update($this->_table, $data, DB::field('uid', $uid).' AND '.DB::field('operation', $operation).' AND '.DB::field('relatedid', $relatedid));
}
return 0;
}
public function count_by_uid_operation_relatedid($uid, $operation, $relatedid) {
$relatedid = dintval($relatedid, true);
$uid = dintval($uid, true);
if($relatedid && $uid && $operation) {
$wherearr = array();
$wherearr[] = is_array($uid) && $uid ? 'uid IN(%n)' : 'uid=%d';
$wherearr[] = is_array($operation) && $operation ? 'operation IN(%n)' : 'operation=%s';
$wherearr[] = is_array($relatedid) && $relatedid ? 'relatedid IN(%n)' : 'relatedid=%d';
return DB::result_first('SELECT COUNT(*) FROM %t WHERE '.implode(' AND ', $wherearr), array($this->_table, $uid, $operation, $relatedid));
}
return 0;
}
public function count_by_uid($uid) {
return DB::result_first('SELECT COUNT(*) FROM %t WHERE uid=%d', array($this->_table, $uid));
}
public function count_by_operation($operation) {
return DB::result_first('SELECT COUNT(*) FROM %t WHERE operation=%s', array($this->_table, $operation));
}
public function count_stc_by_relatedid($relatedid, $creditid, $operation = 'STC') {
$creditid = intval($creditid);
if($creditid) {
return DB::fetch_first("SELECT COUNT(*) AS payers, SUM(extcredits%d) AS income FROM %t WHERE relatedid=%d AND operation=%s", array($creditid, $this->_table, $relatedid, $operation));
}
return 0;
}
public function count_credit_by_uid_operation_relatedid($uid, $operation, $relatedid, $creditid) {
$creditid = intval($creditid);
if($creditid) {
$relatedid = dintval($relatedid, true);
$uid = dintval($uid, true);
$wherearr = array();
$wherearr[] = is_array($uid) && $uid ? 'uid IN(%n)' : 'uid=%d';
$wherearr[] = is_array($operation) && $operation ? 'operation IN(%n)' : 'operation=%s';
$wherearr[] = is_array($relatedid) && $relatedid ? 'relatedid IN(%n)' : 'relatedid=%d';
return DB::result_first('SELECT SUM(extcredits%d) AS credit FROM %t WHERE '.implode(' AND ', $wherearr), array($creditid, $this->_table, $uid, $operation, $relatedid));
}
return 0;
}
public function count_by_search($uid, $optype, $begintime = 0, $endtime = 0, $exttype = 0, $income = 0, $extcredits = array(), $relatedid = 0) {
$condition = $this->make_query_condition($uid, $optype, $begintime, $endtime, $exttype, $income, $extcredits, $relatedid);
return DB::result_first('SELECT COUNT(*) FROM %t '.$condition[0], $condition[1]);
}
private function make_query_condition($uid, $optype, $begintime = 0, $endtime = 0, $exttype = 0, $income = 0, $extcredits = array(), $relatedid = 0) {
$wherearr = array();
$parameter = array($this->_table);
if($uid) {
$uid = dintval($uid, true);
$wherearr[] = is_array($uid) && $uid ? 'uid IN(%n)' : 'uid=%d';
$parameter[] = $uid;
}
if($optype) {
$wherearr[] = is_array($optype) && $optype ? 'operation IN(%n)' : 'operation=%s';
$parameter[] = $optype != -1 ? $optype : '';
}
if($relatedid) {
$relatedid = dintval($relatedid, true);
$wherearr[] = is_array($relatedid) && $relatedid ? 'relatedid IN(%n)' : 'relatedid=%d';
$parameter[] = $relatedid;
}
if($begintime) {
$wherearr[] = 'dateline>%d';
$parameter[] = $begintime;
}
if($endtime) {
$wherearr[] = 'dateline<%d';
$parameter[] = $endtime;
}
if($exttype && $extcredits[$exttype]) {
$wherearr[] = "extcredits{$exttype}!=0";
}
if($income && $extcredits) {
$incomestr = $income < 0 ? '<' : '>';
$incomearr = array();
foreach(array_keys($extcredits) as $id) {
$incomearr[] = 'extcredits'.$id.$incomestr.'0';
}
if($incomearr) {
$wherearr[] = '('.implode(' OR ', $incomearr).')';
}
}
$wheresql = !empty($wherearr) && is_array($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
return array($wheresql, $parameter);
}
}
?>