chengkun
2025-09-05 4822304b63e1bd6327860af7f3db0133cecf167f
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
<?php
 
namespace app\admin\controller;
 
use app\admin\validate\SupplierAccountAdd;
use app\admin\validate\SupplierAccountEdit;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
 
class Supplier extends Common {
    
    public function index() {
        ////////
        View::assign('menuitem', strtolower('supplier-index'));
        
        $other['title'] = '供应商';
        $guide['one']['text'] = '供应商管理';
        $guide['two']['text'] = '供应商列表';
        View::assign('guide', $guide);
        
        
        View::assign('other', $other);
        return View::fetch('index');
    }
    
    /**
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public function supplier_list() {
        if (Request::isPost()) {
            $kw = input('kw');
            $condition[] = ['initialize', '=', 1];
            if (!empty($kw)) {
                $condition[] = array('user_name|real_name', 'like', "%$kw%");
            }
            $p = input('p', 1);
            $_GET['p'] = $p;
            $other['page_size'] = input('page_size', 20);
            $order = 'b.id asc';
            
            $count = Db::name('supplier')
                ->alias('a')
                ->join('supplier_account b', 'a.id = b.supplier_id')
                ->where($condition)->count();
            
            $list = Db::name('supplier')
                ->alias('a')
                ->join('supplier_account b', 'a.id = b.supplier_id')
                ->field("a.id,b.supplier_id,b.user_name,a.real_name,ban_access,a.phone,supplier_name,initialize,login_time,login_ip,a.end_time,a.start_time,a.audit_status")
                ->where($condition)
                ->order($order)
                ->page($p, $other['page_size'])
                ->select()
                ->toArray();
            
            $other['count'] = $count;
            if (!$list) {
                $list = [];
            } else {
                foreach ($list as &$item) {
                    $item['endtime'] = date('Y-m-d', $item['end_time']);
                }
            }
            $other['admin_info'] = $this->cinfo;
            $d['code'] = 200;
            $d['list'] = $list;
            $d['other'] = $other;
            return $d;
        }
    }
    
    public function update_ban_access() {
        header('Content-Type:text/html;charset=utf-8');
        if (Request::isPost()) {
            
            $id = input('id');
            $sub_ban_access = input('ban_access');
            $info = Db::name('supplier')->field('id')->where('id', $id)->find();
            if (empty($info)) {
                $d['code'] = 400;
                $d['message'] = '信息不存在';
                return $d;
            }
            ///
            
            if (is_numeric($id)) {
                if ($sub_ban_access == '1') {
                    $dd['ban_access'] = 1;
                    $d['ban_access'] = 1;
                    $d['message'] = '允许登录';
                } else {
                    $dd['ban_access'] = 0;
                    $d['ban_access'] = 0;
                    $d['message'] = '禁止登录';
                }
                $backval = Db::name('supplier')->where('id', $id)->save($dd);
                if ($backval === FALSE) {
                    $d['code'] = 400;
                } else {
                    $d['code'] = 200;
                }
            } else {
                $d['code'] = 400;
            }
        } else {
            $d['code'] = 400;
        }
        return $d;
    }
    
    
    public function setAuditStatus() {
        header('Content-Type:text/html;charset=utf-8');
        if (Request::isPost()) {
            
            $id = input('id');
            $audit_status = input('audit_status');
            $info = Db::name('supplier')->field('id')->where('id', $id)->find();
            if (empty($info)) {
                $d['code'] = 400;
                $d['message'] = '信息不存在';
                return $d;
            }
            ///
            
            if (is_numeric($id)) {
                if ($audit_status == '1') {
                    $dd['audit_status'] = 1;
                    $d['audit_status'] = 1;
                    $d['message'] = '通过审核';
                } else {
                    $dd['audit_status'] = 0;
                    $d['audit_status'] = 0;
                    $d['message'] = '未审核';
                }
                $backval = Db::name('supplier')->where('id', $id)->save($dd);
                if ($backval === FALSE) {
                    $d['code'] = 400;
                } else {
                    $d['code'] = 200;
                }
            } else {
                $d['code'] = 400;
            }
        } else {
            $d['code'] = 400;
        }
        return $d;
    }
    /**
     * 保存供应商信息(废弃)
     * @return array
     */
    public function save_supplier() {
        if (Request::isPost()) {
            header('Content-Type:text/html;charset=utf-8');
            $data = Request::post();
            $id = input('id');
            $password = input('password');
            if (empty($password)) {
            } else {
                $data['password'] = joinmd5($password);
            }
            
            if (is_numeric($id)) {//更新数据
                $supplier_id = input('supplier_id');
                /////验证密码复杂度///////////
                $validate = new SupplierAccountEdit();
                $result = $validate->scene('edit')->check($data);
                if (!$result) {
                    $d['code'] = 400;
                    $d['message'] = $validate->getError();
                    return $d;
                }
                //////保存供应商信息////////////
                $supplier_data['supplier_name'] = $data['supplier_name'];
                $supplier_data['real_name'] = $data['real_name'];
                $supplier_data['phone'] = $data['phone'];
                $supplier_data['end_time'] = strtotime($data['endtime']);
                Db::name('supplier')->where('id', $supplier_id)->save($supplier_data);
                ////////////////////////
                if (!empty($password)) {
                    /////修改密码////////////////
                    $supplier_account_data['password'] = $data['password'];
                    Db::name('supplier_account')->where('id', $id)->save($supplier_account_data);
                }
                $d['code'] = 200;
                $d['end_time'] = strtotime($data['endtime']);
                $d['message'] = '编辑成功';
            } else {
                unset($data['id']);
                $validate = new SupplierAccountAdd();
                $result = $validate->check($data);
                if (!$result) {
                    $d['code'] = 400;
                    $d['message'] = $validate->getError();
                    return $d;
                }
                //////////////////////
                
                Db::startTrans();
                try {
                    $end_time = getcurrdate() + 3600 * 24 * 30;
                    $supplier_data['phone'] = $data['phone'];
                    $supplier_data['supplier_name'] = $data['supplier_name'];
                    $supplier_data['real_name'] = $data['real_name'];
                    $supplier_data['add_time'] = time();
                    $supplier_data['start_time'] = time();
                    $supplier_data['end_time'] = $end_time;
                    $supplier_id = Db::name('supplier')->insertGetId($supplier_data);
                    /////--------------------////
                    $supplier_account_data['supplier_id'] = $supplier_id;
                    $supplier_account_data['user_name'] = $data['user_name'];
                    $supplier_account_data['password'] = $data['password'];
                    $supplier_account_data['phone'] = $data['phone'];
                    $supplier_account_data['real_name'] = $data['real_name'];
                    $supplier_account_data['add_time'] = time();
                    $supplier_account_data['initialize'] = 1;
                    $supplier_account_id = Db::name('supplier_account')->insertGetId($supplier_account_data);
                    if ($supplier_id && $supplier_account_id) {
                        ///////执行成功,提交事务///////
                        Db::commit();
                        $d['code'] = 200;
                        $d['message'] = '供应商添加成功';
                    } else {
                        //任一执行失败,执行回滚操作,相当于均不执行
                        Db::rollback();
                        $d['code'] = 400;
                        $d['message'] = '供应商添加失败';
                    }
                    
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    $d['code'] = 400;
                    $d['message'] = '供应商添加失败';
                }
            }
        } else {
            $d['code'] = 400;
            $d['message'] = '非法请求';
        }
        return $d;
    }
    
    public function delete_admin() {
        if (Request::isPost()) {
            $id = input('id');
            if (is_numeric($id)) {
                $condition['id'] = $id;
                $info = Db::name('administrators')->field('id,initialize')->where($condition)->find();
                if ($info) {
                    ///////////////////
                    if ($info['initialize'] == '1') {
                        $d['code'] = 400;
                        $d['message'] = '该账号不能删除!';
                        return $d;
                    }
                    if ($this->cinfo['initialize'] == 1 || $this->admin_id == $info['id']) {
                        Db::name('administrators')->where('id', $id)->delete();
                        /////////////////////////////
                        $d['code'] = 200;
                        $d['message'] = '删除成功!';
                    } else {
                        $d['code'] = 400;
                        $d['message'] = '您没有删除权限';
                    }
                } else {
                    $d['code'] = 400;
                    $d['message'] = '子账号不存在';
                }
            } else {
                $d['code'] = 400;
                $d['message'] = '参数有误!';
            }
            return $d;
        }
    }
    
    /**
     * 供应商编辑页
     * @param int $id
     * @return string
     */
    public function add(int $id = 0): string {
        View::assign('menuitem', strtolower('supplier-index'));
        if ($id && is_numeric($id)) {
            $other['title'] = '编辑供应商信息';
        } else {
            $other['title'] = '添加供应商信息';
        }
        $guide['one']['text'] = '供应商管理';
        $guide['two']['text'] = $other['title'];
        
        $other['id'] = $id;
        View::assign('guide', $guide);
        View::assign('other', $other);
        return View::fetch('add');
    }
    
    /**
     * 获取供应商详细信息
     * @return array
     */
    public function get_supplier_info(): array {
        try {
            if (!Request::isPost()) {
                throw new Exception("请求方式有误");
            }
            $supplier_id = input('id');
            if (!$supplier_id || !is_numeric($supplier_id)) {
                throw new Exception("参数有误");
            }
            $where = [
                'a.id' => $supplier_id
            ];
            $supplier_info = Db::name('supplier')
                ->alias("a")
                ->field("a.*,b.id as supplier_account_id,b.user_name,b.sub_ban_access,b.phone as account_phone,b.real_name as account_name")
                ->join("web_supplier_account b", "b.supplier_id = a.id and b.initialize = 1")
                ->where($where)
                ->find();
            if (!$supplier_info) {
                throw new Exception("供应商信息有误");
            }
//            $supplier_info['sub_ban_access'] = (bool)$supplier_info['sub_ban_access'];
            $supplier_info['end_time'] = date("Y-m-d", $supplier_info['end_time']);
            
            $result = [
                'code'    => 200,
                'message' => "成功",
                'data'    => $supplier_info
            ];
        } catch (Exception $exc) {
            $result = [
                'code'    => $exc->getCode(),
                'message' => $exc->getMessage()
            ];
        }
        return $result;
    }
    
    /**
     * 保存供应商信息
     * @return array
     */
    public function save_supplier_info(): array {
        try {
            if (!Request::isPost()) {
                throw new Exception("请求方式有误");
            }
            $params = Request::param();
            if (!$params['supplier_name']) {
                throw new Exception("请填写供应商名称");
            }
            if (!$params['user_name']) {
                throw new Exception("请填写供应商账号");
            }
            if (!$params['account_name']) {
                throw new Exception("请填写供应商联系人姓名");
            }
            if (!$params['account_phone']) {
                throw new Exception("请填写供应商联系人手机号");
            }
            
//            dump($params['sub_ban_access']);exit;
//
//            if ($params['sub_ban_access']) {
//                $params['sub_ban_access'] = 1;
//            } else {
//                $params['sub_ban_access'] = 0;
//            }
            
            Db::startTrans();
            if ($params['id']) {
                /////更新/////
                $where = [
                    'id' => $params['id']
                ];
                $supplier_info = Db::name('supplier')->where($where)->find();
                if (!$supplier_info) {
                    throw new Exception("供应商信息有误");
                }
                /////更新供应商信息/////
                $update_supplier_data = [
                    'supplier_name'          => $params['supplier_name'],/////供应商名称/////
                    'supplier_en_name'       => $params['supplier_en_name'],/////供应商英文名称/////
                    'real_name'              => $params['real_name'],/////供应商联系人姓名/////
                    'phone'                  => $params['phone'],/////供应商联系人手机/////
                    'email'                  => $params['email'],/////供应商联系人邮箱/////
                    'phone_number'           => $params['phone_number'],/////供应商联系人座机/////
                    'unique_code'            => $params['unique_code'],/////供应商唯一代码。营业执照上的统一代码/////
                    'legal_person_name'      => $params['legal_person_name'],/////供应商法人/////
                    'legal_person_id_number' => $params['legal_person_id_number'],/////供应商法人身份证号/////
                    'address'                => $params['address'],/////供应商地址/////
                    'postal_code'            => $params['postal_code'],/////邮政编码/////
                    'end_time'               => strtotime($params['end_time']) + 86400 - 1,/////到期时间/////
                    'ban_access'             => $params['sub_ban_access'],/////账号状态/////
                    'account_executive'     => $params['account_executive'],/////业务经理/////
                    'account_executive_phone'   => $params['account_executive_phone'],///业务经理联系方式////
                ];
                $update_supplier_result = Db::name('supplier')->where($where)->update($update_supplier_data);
                if ($update_supplier_result === FALSE) {
                    throw new Exception("更新供应商信息失败");
                }
                
                /////更新供应商主账号信息/////
                $supplier_account_where = [
                    'id'          => $params['supplier_account_id'],
                    'supplier_id' => $params['id'],
                ];
                $supplier_account_info = Db::name('supplier_account')->where($supplier_account_where)->find();
                if (!$supplier_account_info) {
                    throw new Exception("供应商主账号信息有误");
                }
                
                if ($params['user_name'] != $supplier_account_info['user_name']) {
                    /////账号有修改,检查新账号是否存在/////
                    $check_supplier_account_where = [
                        'user_name' => $params['user_name'],
                    ];
                    $check_supplier_account_result = Db::name('supplier_account')->where($check_supplier_account_where)->find();
                    if ($check_supplier_account_result) {
                        throw new Exception("此账号已存在,请换一个账号");
                    }
                }
                $updae_supplier_account_data = [
                    'user_name'      => $params['user_name'],
                    'real_name'      => $params['account_name'],
                    'phone'          => $params['account_phone'],
                    'sub_ban_access' => $params['sub_ban_access'],/////账号状态/////
                ];
                if ($params['password']) {
                    $updae_supplier_account_data['password'] = joinmd5($params['password']);
                }
                $update_supplier_account_result = Db::name('supplier_account')->where($supplier_account_where)->data($updae_supplier_account_data)->update();
                if ($update_supplier_account_result === FALSE) {
                    throw new Exception("更新供应商主账号信息失败");
                }
                $jump_url = "";
            } else {
                /////添加/////
                
                /////1、先添加供应商/////
                $add_supplier_data = [
                    'supplier_name' => $params['supplier_name'],
                    'real_name'     => $params['account_name'],
                    'phone'         => $params['account_phone'],
                    'start_time'    => time(),
                    'end_time'      => strtotime($params['end_time']) + 86400 - 1,
                    'ban_access'    => $params['sub_ban_access'] ? 1 : 0,
                    'account_executive'     => $params['account_executive'],/////业务经理/////
                    'account_executive_phone'   => $params['account_executive_phone'],///业务经理联系方式////
                    'add_time'      => time()
                ];
                $add_supplier_result = Db::name('supplier')->insertGetId($add_supplier_data);
                if (!$add_supplier_result) {
                    throw new Exception("添加供应商失败");
                }
                /////2、再添加供应商账号/////
                $check_user_name_where = [
                    'user_name' => $params['user_name'],
                ];
                if (Db::name('supplier_account')->where($check_user_name_where)->find()) {
                    throw new Exception("账号已存在,请换一个账号");
                }
                
                $add_supplier_account_data = [
                    'supplier_id'    => $add_supplier_result,
                    'user_name'      => $params['user_name'],
                    'password'       => joinmd5($params['password']),
                    'real_name'      => $params['account_name'],
                    'phone'          => $params['account_phone'],
                    'initialize'     => 1,
                    'sub_ban_access' => $params['sub_ban_access'] ? 1 : 0,
                    'add_time'       => time()
                ];
                $add_supplier_account_result = Db::name('supplier_account')->insertGetId($add_supplier_account_data);
                if (!$add_supplier_account_result) {
                    throw new Exception("添加供应商账号失败");
                }
                $jump_url = url('admin/supplier/add/id/' . $add_supplier_result);
            }
            
            Db::commit();
            $result = [
                'code'     => 200,
                'message'  => "保存成功",
                'jump_url' => $jump_url
            ];
        } catch (Exception $exc) {
            Db::rollback();
            $result = [
                'code'    => $exc->getCode(),
                'message' => $exc->getMessage()
            ];
        }
        return $result;
    }
    
}