Route.php
3.5 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
<?php
namespace app\api\controller\pipes;
use fast\Random;
/**
* 路线
*/
class Route extends Base
{
/**
* Network模型对象
* @var \app\admin\model\pipes\Route
*/
protected $model = null;
protected $noNeedRight = ['*'];
protected $noNeedLogin = ['*'];
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\pipes\Route();
}
public function lists(){
$params = $this->request->get();
$where = [];
if (!empty($params['query'])){
$where['route.route_name|route.route_code'] = ['LIKE', "%{$params['query']}%", 'OR'];
}
if (!empty($params['network_id'])){
$where['route.network_id'] = $params['network_id'];
}else{
//没有管网ID参数则默认获取第一个管网的ID
$networkModel = new \app\admin\model\pipes\Network();
$find = $networkModel->field('id')->order(['id' => 'asc'])->find();
if ($find){
$where['route.network_id'] = $find['id'];
}
}
$list = $this->model
->with('network')
->where($where)
->select();
$this->success('成功', $list);
}
public function add(){
$params = $this->request->post();
//生成编号
do {
$params['route_code'] = 'R' . Random::alnum(5);
} while ($this->model->where(['route_code' => $params['route_code']])->find());
//数据验证
$result = $this->validate($params,'app\admin\validate\pipes\Route');
if(true !== $result){
// 验证失败 输出错误信息
$this->error($result);
}
//获取管网编码
$network = \app\admin\model\pipes\Network::get($params['network_id']);
if (!$network){
$this->error('管网不存在');
}
$params['network_code'] = $network['network_code'];
$result = $this->model->allowField(true)->save($params);
if ($result !== false){
$this->success('添加成功');
}
$this->error('添加失败');
}
public function edit(){
$params = $this->request->post();
if (empty($params['id'])){
$this->error('id必须');
}
$row = $this->model->where(['id' => $params['id']])->find();
if (!$row){
$this->error('数据不存在');
}
//数据验证
$result = $this->validate($params,'app\admin\validate\pipes\Route');
if(true !== $result){
// 验证失败 输出错误信息
$this->error($result);
}
//获取管网编码
$network = \app\admin\model\pipes\Network::get($params['network_id']);
if (!$network){
$this->error('管网不存在');
}
$params['network_code'] = $network['network_code'];
$result = $row->allowField(true)->save($params);
if ($result !== false){
$this->success('编辑成功');
}
$this->error('编辑失败');
}
public function del(){
$params = $this->request->post();
if (empty($params['id'])){
$this->error('id必须');
}
$row = $this->model->where(['id' => $params['id']])->find();
if (!$row){
$this->error('数据不存在');
}
$result = $row->delete();
if ($result !== false){
$this->success('删除成功');
}
$this->error('删除失败');
}
}