|
<?php
namespace app\controller;
use think\App;
use think\facade\View;
use app\model\Company as CompanyModel;
class Company extends Base
{
protected $model;
public function __construct(App $app)
{
parent::__construct($app);
$this->model = new CompanyModel();
}
public function index($key="")
{
$map = [];
if ($key) {
$map[] = ['name','LIKE', '%'.$key.'%'];
}
$list = $this->model->where($map)->paginate($this->limit);
$page = $list->render();
return View::fetch('',[
'list' => $list,
'page' => $page,
'key' => $key,
'limit' => $this->limit,
]);
}
public function save()
{
if ($this->request->isPost()) {
$param = $this->request->param();
try {
if ($param['id']) {
$company = $this->model->find(intval($param['id']));
} else {
$company = new CompanyModel();
$company->create_time = time();
}
$company->name = $param['name'];
$company->initial_balance = intval(bcmul($param['initial_balance'], 100));
$company->save();
} catch (\Exception $e) {
$this->error($e->getMessage());
}
$this->success('操作成功', 'company/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' => '',
'initial_balance' => 0,
];
}
return View::fetch('', [
'data' => $data,
]);
}
}
public function delete($id)
{
if ($this->model->destroy($id)) {
return json(['code'=>2]);
} else {
return json(['code'=>0]);
}
}
}
|