storeModel = new StoreModel(); $this->departmentModel = new DepartmentModel(); parent::__construct($app); } public function index(Request $request) { $params = $request->param(); View::assign([ 'list' => $this->storeModel->findByPaginate($params), 'params' => $params, ]); return view(); } public function add(Request $request) { if($request->isAjax()) { $params = $request->param(); list($name,$abbr,$address) = [$params['name'],$params['abbr'],$params['address']]; $store = $this->storeModel->doesItExist($abbr); if ($store) return $this->fail(lang('Fail to add. Data duplication')); $last_id = $this->storeModel->insert([ 'name' => $name, 'abbr' => $abbr, 'address' => $address, 'update_time' => time(), 'create_time' => time() ],true); $this->departmentModel->save([ 'name' => $name, 'store_id' => $last_id, 'pid' => 5, 'describe' => $name ]); return $this->ok($last_id); } return view(); } public function edit(Request $request) { $params = $request->param(); if(!isset($params['id'])) return $this->fail(lang('ID not exist')); $store = $this->storeModel->findById($params['id']); if (!$store) return $this->fail(lang('Data not exist')); if ($request->isAjax()) { $exist_store = $this->storeModel->doesItExist($params['abbr']); if ($exist_store && $exist_store->id != $params['id']) return $this->fail(lang('Data duplication')); $res = $this->storeModel->where('id',$params['id'])->update([ 'name' => $params['name'], 'abbr' => $params['abbr'], 'address' => $params['address'], 'update_time' => time() ]); $this->departmentModel->where('store_id', $store->id)->update([ 'name' => $params['name'], 'describe' => $params['name'] ]); return $this->ok($res); } View::assign('store', $store); return view(); } public function delete(Request $request) { $params = $request->param(); if(!isset($params['ids'])) return $this->fail(lang("Please select the data you want to delete")); $this->storeModel->deleteByIds(explode(',', $params['ids'])); $this->departmentModel->where('store_id', 'in', $params['ids'])->delete(); return $this->ok(true); } public function total_data(Request $request) { $params = $request->param(); if(!isset($params['id'])) return $this->fail(lang('ID not exist')); $store = $this->storeModel->findById($params['id']); if (!$store) return $this->fail(lang('Data not exist')); View::assign('store', $store); return view(); } }