DiscountCouponController.php
4.7 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
<?php
/**
* Created by PhpStorm.
* User: yhbr
* Date: 2018/8/30
* Time: 14:29
*/
namespace app\admin\controller;
use cmf\controller\AdminBaseController;
use think\Db;
class DiscountCouponController extends AdminBaseController
{
public function add()
{
$request = request();
if ($request->isPost()) {
$post = $request->param();
if (empty($post['discount_coupon_name'])) {
$this->error('请输入优惠券名称');
}
if (empty($post['overflow']) || $post['overflow'] < 0) {
$this->error('请输入正确满减金额');
}
if (empty($post['reduce']) || $post['reduce'] < 0) {
$this->error('请输入正确满减金额');
}
if (empty($post['deadline'])) {
$this->error('请选择截止日期');
}
if (empty($post['user_id'])) {
$this->error('请匹配用户');
}
$n = count($post['user_id']);
$data = [];
for ($i = 0; $i < $n; $i++) {
$data[$i] = array(
'user_id' => $post['user_id'][$i],
'discount_coupon_name' => $post['discount_coupon_name'],
'overflow' => $post['overflow'],
'reduce' => $post['reduce'],
'deadline' => strtotime($post['deadline']),
'status' => 1
);
}
if (Db::name('discount_coupon')->insertAll($data)) {
$this->success('创建优惠券成功');
} else {
$this->error('创建优惠券失败');
}
} else {
return $this->fetch('', [
'user' => $this->getUserList()
]);
}
}
public function index()
{
$posts = Db::name('discount_coupon')->alias('d')->field('sami_discount_coupon.id,discount_coupon_name,deadline,user_nickname,status')
->join('sami_user u', 'u.id=d.user_id')
->order("id DESC")
->paginate(20);
return $this->fetch('', [
'posts' => $posts,
'page' => $posts->render()
]);
}
public function getUserList()
{
$keywords = request()->param('keywords');
$map = array();
$map['user_type'] = ['eq', 2];
if (!empty($keywords)) {
$map['user_nickname'] = array('like', "%$keywords%");
}
$user = Db::name('user')->field('id,user_nickname')->where($map)->select();
$html = '<tr>
<th><input type="checkbox" id="All" onclick="checkAll()"></th>
<th width="300"><input style="width: 200px; display: unset" class="form-control" type="text" placeholder="搜索" id="Search" value="' . $keywords . '" onchange="ajax()"> 搜索</th>
</tr>';
foreach ($user as $item) {
$html .= '<tr>
<td><input type="checkbox" name="user_id[]" value="' . $item['id'] . '"></td>
<td>' . $item['user_nickname'] . '</td>
</tr>';
}
if (request()->isAjax()) {
echo json_encode(['data' => $html]);
exit();
} else {
return $user;
}
}
public function edit()
{
$request = request();
$id = $request->param('id');
if ($request->isPost()) {
$post = $request->param();
if (empty($post['discount_coupon_name'])) {
$this->error('请输入优惠券名称');
}
if (empty($post['overflow']) || $post['overflow'] < 0) {
$this->error('请输入正确满减金额');
}
if (empty($post['reduce']) || $post['reduce'] < 0) {
$this->error('请输入正确满减金额');
}
if (empty($post['deadline'])) {
$this->error('请选择截止日期');
}
$post['id'] = $id;
$post['deadline'] = strtotime($post['deadline']);
if (Db::name('discount_coupon')->update($post)) {
$this->success('编辑优惠券成功');
} else {
$this->error('您未作出任何修改');
}
} else {
return $this->fetch('', [
'id' => $id,
'info' => Db::name('discount_coupon')->where(array('id' => $id))->find()
]);
}
}
public function del()
{
$id = request()->param('id');
if (Db::name('discount_coupon')->delete($id)) {
$this->success('销毁成功');
} else {
$this->error('销毁失败');
}
}
}