SchoolController.php
5.1 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
<?php
namespace app\admin\controller;
use app\admin\model\RouteModel;
use cmf\controller\AdminBaseController;
use think\Db;
class SchoolController extends AdminBaseController{
/*
* 学校管理列表
* */
public function school_list(){
if($this -> request -> isPost()){
if(!empty($_POST['start_time']) && !empty($_POST['end_time'])){
$start_time = strtotime($_POST['start_time']);
$end_time = strtotime($_POST['end_time']);
$where['create_time'] = [['>=',$start_time],['<=',$end_time]];
}
if(!empty($_POST['keyword'])){
$keyword = $_POST['keyword'];
$where['school'] = ['like',"%$keyword%"];
}
$data1 = Db::name('school') -> where($where) -> paginate(1000000) ;
$data = $data1 -> toArray();
}else{
$data1 = Db::name('school') -> paginate(12) ;
$data = $data1 -> toArray();
}
foreach ($data['data'] as $key => $val){
$user_data = Db::name('user') -> where('id',$val['uid']) -> find();
$data['data'][$key]['uname'] = $user_data['user_nickname'];
}
$this -> assign('data',$data['data']);
$this -> assign('res',$data1);
return $this -> fetch();
}
/*
* 学校管理编辑
* */
public function school_edit(){
$id = $this -> request -> param();
$data = Db::name('school') -> where('id',$id['ids']) -> find();
$data_grade = Db::name('grade_class') -> where('school_id',$id['ids']) -> select();
$grade_id = [];
$grade_class = [];
foreach ($data_grade as $key => $val){
$grade_id[] = $val['id'];
$grade_class[] = $val['grade'].','.$val['class'];
}
$grade_id_str = implode('-',$grade_id);
$grade_class_str = implode('|',$grade_class);
$this -> assign('data',$data);
$this -> assign('grade_class_str',$grade_class_str);
$this -> assign('grade_id_str',$grade_id_str);
$province = Db::name('area')->where(['level'=>1])->select();
$data_city = Db::name('area')->where(['level'=>2,'name'=>$data['work_city']])->find();
$city = Db::name('area')->where(['pid'=>$data_city['pid'],'level'=>2])->select();
$data_county = Db::name('area')->where(['level'=>3,'name'=>$data['work_county']])->find();
$county = Db::name('area')->where(['level'=>3,'pid'=>$data_county['pid']])->select();
$this->assign('province',$province);
$this->assign('city',$city);
$this->assign('county',$county);
return $this -> fetch();
}
/**
* 获取城市
* @return string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function select_city(){
$province_name = $this->request->param('province_name');
if(!isset($province_name)){
$arr['code'] = 40005;
$arr['msg'] = '缺少必要参数';
}else{
$province = Db::name('area')->where(['name'=>$province_name,'level'=>1])->find();
$city = Db::name('area')->where(['pid'=>$province['id'],'level'=>2])->select();
$arr['code'] = 20000;
$arr['msg'] = 'SUCCESS';
$arr['data'] = $city;
}
return json_encode($arr);
}
/**
* 获取区/县
* @return string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function select_county(){
$city_name = $this->request->param('city_name');
if(!isset($city_name)){
$arr['code'] = 40005;
$arr['msg'] = '缺少必要参数';
}else{
$city = Db::name('area')->where(['name'=>$city_name,'level'=>2])->find();
$county = Db::name('area')->where(['pid'=>$city['id'],'level'=>3])->select();
$arr['code'] = 20000;
$arr['msg'] = 'SUCCESS';
$arr['data'] = $county;
}
return json_encode($arr);
}
/*
* 学校管理编辑提交 修改数据
* */
public function school_edit_post(){
$data['id'] = $_POST['id'];
$data['region'] = $_POST['region'];
$data['school'] = $_POST['school'];
$data['type'] = $_POST['type'];
$res = Db::name('school') -> update($data);
$gread = explode('|',$_POST['grade_class_str']);
foreach($gread as $key => $val){
$greads[] = explode(',',$gread[$key]);
}
$id = explode('-',$_POST['grade_id_str']);
foreach($id as $key => $val){
Db::name('grade_class') -> where('id',$id[$key]) -> update(['grade'=>$greads[$key][0],'class'=>$greads[$key][1]]);
}
$this -> success('保存成功',url('School/school_edit',array('ids'=>$_POST['id'])));
}
/**
* 删除数据
*
*/
public function school_del(){
$id = $_POST['id'];
Db::name('school') -> delete($id);
return true;
}
}