AdminCompanyController.php
11.3 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;
use app\portal\model\UserModel;
use cmf\controller\AdminBaseController;
use app\portal\model\CompanyModel;
use think\Db;
use think\db\Query;
class AdminCompanyController extends AdminBaseController
{
//列表页
public function index(){
$list = Db::name('company')
->where('pid',0)
->where(function (Query $query) {
$data = $this->request->param();
if (!empty($data['company_name'])) {
$company_name = $data['company_name'];
$query->where('company_name', 'like', "%$company_name%");
}
})
->field('id,company_name,u_l_id')
->order('id desc')
->paginate(10,false,['query'=>request()->param()]);
$res = $list->toArray();
// 获取分页显示
$page = $list->render();
foreach($res['data'] as &$value){
$ids = explode(',',trim($value['u_l_id'],','));
$user = $this->getLeader($ids);
$value['user_login'] = $user['user_login'];
// $value['mobile'] = $user['mobile'];
}
$this->assign('list', $res['data']);
$this->assign('page', $page);
//获取身份信息
$user = new \app\user\controller\AdminIndexController();
$identity = $user->identity();
$this->assign('identity',$identity);
// 渲染模板输出
return $this->fetch();
}
//获取领导用户名
public function getLeader($ids){
$res = Db::name('user')
->whereIn('id',$ids)
->where('user_status',1)
->field('id,user_login,mobile')
->select()
->toArray();
$arr['user_login'] = implode(',',array_values(array_column($res,'user_login')));
// $arr['mobile'] = implode(',',array_values(array_column($res,'mobile')));
return $arr;
}
//新增页面
public function add(){
//查询领导
$userLeader = $this->getStaff(1);
$this->assign('user_leader',$userLeader);
//查询总领导
$userLeaders = $this->getStaff(2);
$this->assign('user_leaders',$userLeaders);
//查询员工
$userStaff = $this->getStaff(0);
$this->assign('user_staff',$userStaff);
return $this->fetch();
}
//获取用户信息
public function getStaff($identity){
$user = Db::name('user')
->where(['user_type'=>2,'user_status'=>1,'identity'=>$identity])
->field('id,user_login,mobile')
->select()
->toArray();
return $user;
}
//新增提交
public function addPost(){
$data = $this->request->param();
$result = $this->validate($data, 'Company');
if ($result !== true) {
$this->error($result);
}
//甲方数据
$companyModel = new CompanyModel();
// if(strpos($data['logo'],$companyModel::HOST) === false){
// $data['logo'] = $companyModel::HOST.$data['logo'];
// }
// if(strpos($data['logo1'],$companyModel::HOST) === false){
// $data['logo1'] = $companyModel::HOST.$data['logo1'];
// }
// if(strpos($data['license'],$companyModel::HOST) === false){
// $data['license'] = $companyModel::HOST.$data['license'];
// }
// if(strpos($data['license1'],$companyModel::HOST) === false){
// $data['license1'] = $companyModel::HOST.$data['license1'];
// }
//乙方数据
$part_B['company_name'] = $data['company_name'];
//负责人uid
$part_B['u_l_id'] = ','.implode(',',$data['u_l_id']).',';
//员工uid,以逗号隔开
$part_B['u_s_id'] = ','.implode(',',$data['u_s_id']).',';
//营业执照
$part_B['license'] = $data['license'];
$part_B['create_time'] = time();
//公司logo
$part_B['logo'] = $data['logo'];
$res = $companyModel->create($part_B);
//甲方数据
if(isset($data['flag'])){
foreach($data['flag'] as $value){
$part_A = [];
$part_A['pid'] = $res->id;
$part_A['company_name'] = $data['company_name'.$value];
$is_children = $data['is_children'.$value];
if($is_children == 1){
//总领导uid
$part_A['u_ls_id'] = $data['u_ls_id'.$value];
$part_A['address'] = $data['address'.$value];
$part_A['company_name_head'] = $data['company_name_head'.$value];
}
$part_A['is_children'] = $data['is_children'.$value];
//负责人uid
$part_A['u_l_id'] = ','.implode(',',$data['u_l_id'.$value]).',';
//员工uid,以逗号隔开
$part_A['u_s_id'] = ','.implode(',',$data['u_s_id'.$value]).',';
//营业执照
$part_A['license'] = $data['license'.$value];
$part_A['create_time'] = time();
//公司logo
$part_A['logo'] = $data['logo'.$value];
$companyModel->create($part_A);
}
}
// //创建乙方登录普通管理员账号
// $userModel = new UserModel();
// $arr['user_login'] = $res->company_name;
// $arr['user_pass'] = cmf_password('123456');
// $arr['company_id'] = $res->id;
// $res1 = $userModel->create($arr);
// //role_user表
// $role['role_id'] = 2;
// $role['user_id'] = $res1->id;
// Db::name('RoleUser')->insert($role);
$this->success("添加成功!", url("AdminCompany/index"));
}
//编辑页面
public function edit(){
$id = $this->request->param('id', 0, 'intval');
//获取乙方数据
$res = Db::name('company')
->where('id',$id)
->find();
$res['u_l_id'] = explode(',',trim($res['u_l_id'],','));
$res['u_s_id'] = explode(',',trim($res['u_s_id'],','));
//获取甲方数据
$res1 = Db::name('company')
->where('pid',$id)
->select()
->toArray();
foreach($res1 as &$value){
$value['u_l_id'] = explode(',',trim($value['u_l_id'],','));
$value['u_s_id'] = explode(',',trim($value['u_s_id'],','));
}
//数组数量
$index = count($res1);
$this->assign('index',$index);
$this->assign('post', $res);
$this->assign('post1', $res1);
//查询领导
$userLeader = $this->getStaff(1);
$this->assign('user_leader',$userLeader);
//查询总领导
$userLeaders = $this->getStaff(2);
$this->assign('user_leaders',$userLeaders);
//查询员工
$userStaff = $this->getStaff(0);
$this->assign('user_staff',$userStaff);
return $this->fetch();
}
//获取甲方数据时候有子类
public function is_children(){
$id = $this->request->param('id', 0, 'intval');
$res = Db::name('company')
->where('pid',$id)
->field('id,pid,is_children')
->select()
->toArray();
return json_encode($res);
}
//编辑保存页面
public function editPost(){
$data = $this->request->param();
$result = $this->validate($data, 'Company');
if ($result !== true) {
$this->error($result);
}
//甲方数据
$companyModel = new CompanyModel();
// if(strpos($data['logo'],$companyModel::HOST) === false){
// $data['logo'] = $companyModel::HOST.$data['logo'];
// }
// if(strpos($data['logo1'],$companyModel::HOST) === false){
// $data['logo1'] = $companyModel::HOST.$data['logo1'];
// }
// if(strpos($data['license'],$companyModel::HOST) === false){
// $data['license'] = $companyModel::HOST.$data['license'];
// }
// if(strpos($data['license1'],$companyModel::HOST) === false){
// $data['license1'] = $companyModel::HOST.$data['license1'];
// }
//乙方数据
$part_B['company_name'] = $data['company_name'];
//负责人uid
$part_B['u_l_id'] = ','.implode(',',$data['u_l_id']).',';
//员工uid,以逗号隔开
$part_B['u_s_id'] = ','.implode(',',$data['u_s_id']).',';
//营业执照
$part_B['license'] = $data['license'];
$part_B['update_time'] = time();
//公司logo
$part_B['logo'] = $data['logo'];
$companyModel->where('id',$data['id'])->update($part_B);
//甲方数据
if(isset($data['flag'])){
foreach($data['flag'] as $value){
$part_A = [];
$part_A['pid'] = $data['id'];
$part_A['company_name'] = $data['company_name'.$value];
$is_children = $data['is_children'.$value];
if($is_children == 1){
//总领导uid
$part_A['u_ls_id'] = $data['u_ls_id'.$value];
$part_A['address'] = $data['address'.$value];
$part_A['company_name_head'] = $data['company_name_head'.$value];
}
$part_A['is_children'] = $data['is_children'.$value];
//负责人uid
$part_A['u_l_id'] = ','.implode(',',$data['u_l_id'.$value]).',';
//员工uid,以逗号隔开
$part_A['u_s_id'] = ','.implode(',',$data['u_s_id'.$value]).',';
//营业执照
$part_A['license'] = $data['license'.$value];
//公司logo
$part_A['logo'] = $data['logo'.$value];
if(isset($data['id'.$value])){
$id = $data['id'.$value];
$part_A['update_time'] = time();
$companyModel->where('id',$id)->update($part_A);
}else{
$part_A['create_time'] = time();
$companyModel->create($part_A);
}
}
}
//移除数据
$del_ids = $data['del'];
if($del_ids != 0){
$del_ids = explode(',',$del_ids);
$res = $this->is_project($del_ids);
if($res){
$this->error("甲方公司存在项目,不能删除!");
}
$companyModel->whereIn('id',$del_ids)->delete();
}
$this->success("编辑成功!", url("AdminCompany/index"));
}
//查询公司下是否有甲方项目
public function is_project($del_ids){
$res = Db::name('project')
->whereIn('a_cid',$del_ids)
->select()
->toArray();
return $res;
}
}