isPost()) { $input = Request::instance()->param(); } else { $input = Request::instance()->get(); } // if (!empty($input['name'])){ // $map['register_id'] = $input['registerId']; // } $drugs = Drugs::where(['name'=>['like',"%{$input['name']}%"]]) ->paginate(10,false,[ "query" => [ 'name' => $input['name'], ] ]); foreach ($drugs as $val){ if(empty($val['use_amount'])){ $val['use_amount'] = 0; } if($val['description'] == NULL){ $val['description'] = "暂时为空"; } } $total = $drugs->total(); $page = $drugs->render(); $this->assign('page',$page); $this->assign('total',$total); $this->assign('name',$input['name']); $this->assign('drugs',$drugs); return $this->fetch(); } //添加药品 public function addDrugs(){ $roleId = Session::get('roleId'); if($roleId != 1){ throw new HtmlException('您没有权限进行该操作!'); } if(Request::instance()->isPost()) { $input = Request::instance()->param(); // p($input);die(); $data = new Drugs([ 'name' => $input['name'], 'price'=> $input['price'], 'quantity' => $input['quantity'], 'description' => $input['description'], ]); $data->save(); if ($data === false) return $this->error('添加失败'); return $this->success('添加成功'); } return $this->fetch(); } //查看药品详细信息 public function check(){ $input = Request::instance()->param(); // p($input);die(); $drugs = Drugs::where($input)->find(); if(empty($drugs['use_amount'])){ $drugs['use_amount'] = 0; } $this->assign('drugs',$drugs); return $this->fetch(); } //修改药品信息 public function write(){ $roleId = Session::get('roleId'); if($roleId != 1){ throw new HtmlException('您没有权限进行该操作!'); } $id = Request::instance()->param(); $drugs = Drugs::where($id)->find(); if(empty($drugs['use_amount'])){ $drugs['use_amount'] = 0; } if(Request::instance()->isPost()){ $data = []; $input = Request::instance()->param(); $drugsInfo = Drugs::where(['id'=>$input['id']])->find(); $quantity = $drugsInfo['quantity']; if(!empty($input['quantity'])) { if ($input['quantity'] == 2) { if ($input['use_amount'] > $drugsInfo['quantity']) { throw new HtmlException("减少的数量大于库存量!"); } $quantity = $drugsInfo['quantity'] - $input['use_amount']; } else { $quantity = $drugsInfo['quantity'] + $input['use_amount']; } } $data =([ 'name' => $input['name'], 'price' => $input['price'], 'description' => $input['description'], 'quantity' => $quantity ]); $drugsInfo->save($data); if ($drugsInfo === false) return $this->error('修改失败'); return $this->success('修改成功'); } $this->assign('drugs',$drugs); return $this->fetch(); } }