PrizeModel.class.php
5.55 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* PrizeModel.class.php
* 奖品设置表 Model
* @author: zhoutao
* @version: $Id$
* @copyright: vchangyi.com
*/
namespace Common\Model;
class PrizeModel extends AbstractModel
{
/** 最多图片张数 */
const MAX_PICTURE_NUMBER = 5;
/** 每人限定兑换次数数据含义:不限制 */
const MEAN_TIMES_NO_LIMIT = -1;
/** 最长名称文字长度 */
const MAX_NAME_COUNT = 50;
/** 最长所需积分 */
const MAX_INTEGRAL_LEN = 5;
/** 奖品状态: 已上架 */
const ON_SALE = 1;
/** 奖品状态: 已下架 */
const OFF_SALE = 2;
/** 兑换范围是否全公司: 是 */
const IS_ALL = 1;
/** 兑换范围是否全公司: 否 */
const NOT_IS_ALL = 2;
// 构造方法
public function __construct()
{
parent::__construct();
}
/**
* Apicp 奖品列表总数
* @param $conds
* @return array
*/
public function countPrizeList($conds)
{
list($where, $params) = $this->getListWhere($conds);
$sql = 'SELECT COUNT(*) FROM __TABLE__' . $where;
return $this->_m->result($sql, $params);
}
/**
* Apicp 查询奖品列表
*
* @param array $conds
* @param array $pageOption
* @param array $orderOption
*
* @return int 数量
*/
public function getPrizeList($conds, $pageOption = null, $orderOption = array())
{
list($where, $params) = $this->getListWhere($conds);
// 排序
$orderby = '';
if (!$this->_order_by($orderby, $orderOption)) {
return false;
}
// 分页参数
$limit = '';
if (!$this->_limit($limit, $pageOption)) {
return false;
}
// 查询已经兑换的次数
$secondSql = 'SELECT COUNT(*) FROM oa_integral_convert AS c WHERE c.`ia_id`= p.`ia_id` AND `convert_status` = ' . ConvertModel::CONVERT_STATUS_AGREE;
// 查询列表
$sql = "SELECT *, ({$secondSql}) AS exchanged_times FROM __TABLE__ AS p" . $where . $orderby . $limit;
return $this->_m->fetch_array($sql, $params);
}
/**
* 查询列表的where语句
*
* @return array [where语句, 参数数组]
*/
protected function getListWhere($conds)
{
$where = ' WHERE `status`<? AND `domain`=?';
$params = array(
self::ST_DELETE,
QY_DOMAIN,
);
// 名称
if (!empty($conds['name'])) {
$where .= ' AND `name` LIKE ?';
$params[] = '%' . $conds['name'] . '%';
}
// 上下架
if (!empty($conds['on_sale'])) {
$where .= ' AND `on_sale` = ?';
$params[] = $conds['on_sale'];
}
return array($where, $params);
}
/**
* 微信端查询奖品分页列表
* @param $conds
* @param null $pageOption
* @param array $orderOption
* @return array|bool
*/
public function getWxPrizePageList($conds, $pageOption = null, $orderOption = array())
{
list($where, $params) = $this->getBaseWhere();
$where .= ' AND on_sale = ' . self::ON_SALE;
$where .= ' AND ( is_all = ' . self::IS_ALL;
if (!empty($conds['rangeMem'])) {
$where .= ' OR range_mem REGEXP (?)';
$params[] = $conds['rangeMem'];
}
if (!empty($conds['rangeDep'])) {
$where .= ' OR range_dep REGEXP(?) ';
$params[] = $conds['rangeDep'];
}
$where .= ')';
// 排序
$orderby = 'ORDER BY `sequence`, `updated` DESC';
// 分页参数
$limit = '';
if (!$this->_limit($limit, $pageOption)) {
return false;
}
$sql = "SELECT * FROM __TABLE__ " . $where . $orderby . $limit;
return $this->_m->fetch_array($sql, $params);
}
/**
* 微信端 查询符合条件的奖品记录总数
* @param $conds
* @return array
*/
public function countWxPrize($conds)
{
list($where, $params) = $this->getBaseWhere();
$where .= ' AND on_sale = ' . self::ON_SALE;
$where .= ' AND ( is_all = ' . self::IS_ALL;
if (!empty($conds['rangeMem'])) {
$where .= ' OR range_mem REGEXP (?)';
$params[] = $conds['rangeMem'];
}
if (!empty($conds['rangeDep'])) {
$where .= ' OR range_dep REGEXP(?) ';
$params[] = $conds['rangeDep'];
}
$where .= ')';
$sql = 'SELECT COUNT(*) FROM __TABLE__' . $where;
return $this->_m->result($sql, $params);
}
/**
* 操作库存
* @param int $id 主键ID
* @param int $number 操作库存数
* @return mixed
*/
public function changeReserve($id, $number)
{
list($where, $params) = $this->getBaseWhere();
// 更新库存
array_unshift($params, $number);
// 主键查询
$where .= ' AND ia_id=?';
$params[] = $id;
// 防止过量减库存
if ($number < 0) {
$where .= ' AND reserve >= ?';
$params[] = abs($number);
}
$sql = "UPDATE __TABLE__ SET `reserve` = `reserve` + ?" . $where;
return $this->_m->execsql($sql, $params);
}
/**
* 查询奖品 无视逻辑删除
* @param int $iaId 奖品ID
* @return mixed
*/
public function getWithOutDeleted($iaId)
{
$sql = "SELECT * FROM __TABLE__ WHERE `" . $this->_m->getPk() . "`=? AND `{$this->prefield}domain`=?";
return $this->_m->fetch_row($sql, [$iaId, QY_DOMAIN]);
}
}