title = '商品管理'; return $this->_query($this->table)->equal('status,vip_mod,cate_id')->like('title')->where(['is_deleted' => '0'])->order('sort asc,id desc')->page(); } /** * 数据列表处理 * @param array $data * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ protected function _index_page_filter(&$data) { $this->clist = Db::name('StoreGoodsCate')->where(['is_deleted' => '0', 'status' => '1'])->select(); $list = Db::name('StoreGoodsList')->where('status', '1')->whereIn('goods_id', array_unique(array_column($data, 'id')))->select(); foreach ($data as &$vo) { list($vo['list'], $vo['cate']) = [[], []]; foreach ($list as $goods) if ($goods['goods_id'] === $vo['id']) array_push($vo['list'], $goods); foreach ($this->clist as $cate) if ($cate['id'] === $vo['cate_id']) $vo['cate'] = $cate; } } /** * 商品库存入库 * @return mixed * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function stock() { if ($this->request->isGet()) { $GoodsId = $this->request->get('id'); $goods = Db::name('StoreGoods')->where(['id' => $GoodsId])->find(); empty($goods) && $this->error('无效的商品信息,请稍候再试!'); $goods['list'] = Db::name('StoreGoodsList')->where(['goods_id' => $GoodsId])->select(); return $this->fetch('', ['vo' => $goods]); } list($post, $data) = [$this->request->post(), []]; if (isset($post['id']) && isset($post['spec']) && is_array($post['spec'])) { foreach ($post['spec'] as $k => $v) if ($v > 0) array_push($data, [ 'goods_id' => $post['id'], 'goods_spec' => $k, 'number_stock' => $v, ]); if (!empty($data)) { Db::name('StoreGoodsStock')->insertAll($data); \app\store\service\Goods::syncStock($post['id']); $this->success('商品信息入库成功!'); } } $this->error('没有需要商品入库的数据!'); } /** * 添加商品信息 * @return mixed */ public function add() { $this->title = '添加商品'; $this->isAddMode = '1'; return $this->_form($this->table, 'form'); } /** * 编辑商品信息 * @return mixed */ public function edit() { $this->title = '编辑商品'; $this->isAddMode = '0'; return $this->_form($this->table, 'form'); } /** * 表单数据处理 * @param array $data * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ protected function _form_filter(&$data) { // 生成商品ID if (empty($data['id'])) $data['id'] = Data::uniqidNumberCode(10); if ($this->request->isGet()) { $fields = 'goods_spec,goods_id,status,price_market market,price_selling selling,number_virtual `virtual`'; $defaultValues = Db::name('StoreGoodsList')->where(['goods_id' => $data['id']])->column($fields); $this->defaultValues = json_encode($defaultValues, JSON_UNESCAPED_UNICODE); $this->cates = Db::name('StoreGoodsCate')->where(['is_deleted' => '0', 'status' => '1'])->order('sort asc,id desc')->select(); } elseif ($this->request->isPost()) { Db::name('StoreGoodsList')->where(['goods_id' => $data['id']])->update(['status' => '0']); foreach (json_decode($data['lists'], true) as $vo) Data::save('StoreGoodsList', [ 'goods_id' => $data['id'], 'goods_spec' => $vo[0]['key'], 'price_market' => $vo[0]['market'], 'price_selling' => $vo[0]['selling'], 'number_virtual' => $vo[0]['virtual'], 'status' => $vo[0]['status'] ? 1 : 0, ], 'goods_spec', ['goods_id' => $data['id']]); } } /** * 表单结果处理 * @param boolean $result */ protected function _form_result($result) { if ($result && $this->request->isPost()) { $this->success('商品编辑成功!', 'javascript:history.back()'); } } /** * 商品禁用 */ public function forbid() { $this->_save($this->table, ['status' => '0']); } /** * 商品禁用 */ public function resume() { $this->_save($this->table, ['status' => '1']); } /** * 删除商品 */ public function del() { $this->_delete($this->table); } }