|
<?php
namespace app\controller;
use think\App;
use think\facade\View;
use app\model\Seller as SellerModel;
use app\model\Department as DepartmentModel;
class Seller extends Base
{
protected $model;
public function __construct(App $app)
{
parent::__construct($app);
$this->model = new SellerModel();
}
public function index($did=0)
{
if ($did) {
$list = $this->model->where('department_id', $did)->select();
} else {
$list = $this->model->select();
}
return View::fetch('',['list'=>$list, 'did'=>$did]);
}
public function save()
{
$departmentModel = new DepartmentModel();
$departments = $departmentModel->column('name', 'id');
if ($this->request->isPost()) {
$param = $this->request->param();
try {
if ($param['id']) {
$seller = $this->model->find(intval($param['id']));
} else {
$seller = new SellerModel();
$seller->create_time = time();
}
$seller->name = $param['name'];
$seller->department_id = $param['department_id'];
$seller->department_name = $departments[$param['department_id']];
$seller->save();
} catch (\Exception $e) {
$this->error($e->getMessage());
}
$this->success('操作成功', 'seller/index');
} else {
$id = $this->request->has('id','route') ? intval($this->request->param('id')) : 0;
if ($id) {
$data = $this->model->find($id);
} else {
$data = [
'id' => 0,
'name' => '',
'department_id' => 1,
];
}
return View::fetch('', [
'data' => $data,
'departments' => $departments
]);
}
}
public function delete($id)
{
if ($this->model->destroy($id)) {
return json(['code'=>2]);
} else {
return json(['code'=>0]);
}
}
}
|