storeModel = new StoreModel(); $this->storeProductModel = new StoreProductModel(); $this->productModel = new ProductModel(); } /** * @param Request $request * @return \think\response\View * @throws \think\db\exception\DbException */ public function index(Request $request) { $params = $request->param(); $format_params = [ 'store_id' => format_string($params['store_id'] ?? null), 'sort' => format_string($params['sort'] ?? null) ]; $list = $this->storeProductModel->findByPaginate($format_params); View::assign([ 'list' => $list, 'params' => $format_params, 'stores' => $this->storeModel->findAllStore(), ]); return view(); } public function add(Request $request) { $params = $request->param(); $store_id = format_string($params['store_id'] ?? null); if($request->isAjax()) { $store = $this->storeModel->findById($store_id); $product = $this->productModel->findById(format_string($params['product_id'] ?? null)); if (!$store || !$product) return $this->fail(lang('Store|Product does not exist')); $relation = $this->storeProductModel->doesItExist($store->id,$product->id); if($relation) return $this->fail(lang('Fail to add. Data duplication')); $res = $this->storeProductModel->save([ 'store_id' => $store->id, 'product_id' => $product->id, 'product_bar_code' => $product->bar_code, 'product_name' => $product->name, 'now_stock' => $params['now_stock'], ]); return $this->ok($res); } View::assign([ 'store_id' => $store_id, 'stores' => $this->storeModel->findAllStore(), 'products' => $this->productModel->findProducts() ]); return view(); } public function edit(Request $request) { $params = $request->param(); if(!isset($params['id'])) return $this->fail(lang('ID not exist')); $relation = $this->storeProductModel->findById($params['id']); if($request->isAjax()) { $exist = $this->storeProductModel->doesItExist($params['store_id'],$params['product_id']); if(!is_null($exist) && $relation->id != $exist->id) return $this->fail(lang(('Data duplication'))); $store = $this->storeModel->findById($params['store_id']); $product = $this->productModel->findById(format_string($params['product_id'] ?? null)); if (!$store || !$product) return $this->fail(lang('Store|Product does not exist')); $res = $this->storeProductModel->where('id',$params['id'])->update([ 'store_id' => $store->id, 'product_id' => $product->id, 'product_bar_code' => $product->bar_code, 'product_name' => $product->name, 'now_stock' => $params['now_stock'], 'update_time' => time() ]); return $this->ok($res); } View::assign([ 'relation' => $relation, 'stores' => $this->storeModel->findAllStore(), 'products' => $this->productModel->findProducts() ]); 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->storeProductModel->deleteByIds(explode(',',$params['ids'])); return $this->ok(true); } public function test() { return view(); } }