123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- declare (strict_types = 1);
- namespace app\store\controller;
- use app\common\exception\BaseException;
- use app\store\model\ShopGoods as ShopGoodsModel;
- /**
- * 门店自提商品管理控制器
- * Class ShopGoods
- * @package app\store\controller
- */
- class ShopGoods extends Controller
- {
- /**
- * 列表
- * @return array
- * @throws \think\db\exception\DbException
- */
- public function list()
- {
- // 获取列表记录
- $model = new ShopGoodsModel;
- $params = $this->request->param();
- $list = $model->getList($params);
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 详情
- * @param int $id
- * @return array
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function detail(int $id)
- {
- // 获取详情
- $model = new ShopGoodsModel;
- $info = $model->getDetail($id);
- return $this->renderSuccess(compact('info'));
- }
- /**
- * 添加商品(支持批量)
- *
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function add()
- {
- $model = new ShopGoodsModel;
- if ($model->add($this->postForm())) {
- return $this->renderSuccess('添加成功');
- }
- return $this->renderError($model->getError() ?: '添加失败');
- }
- /**
- * 设置门店商品库存
- */
- public function setStock($id)
- {
- $model = ShopGoodsModel::get($id);
- if (empty($model)) {
- return $this->renderError("数据不存在");
- }
- if ($model->setStock($this->postForm())) {
- return $this->renderSuccess('保存成功');
- }
- return $this->renderError($model->getError() ?: '保存失败');
- }
- /**
- * 移除商品
- * @param $id
- * @return array
- */
- public function delete($id)
- {
- $model = ShopGoodsModel::get($id);
- if (empty($model)) {
- return $this->renderError("数据不存在");
- }
- if (!$model->remove()) {
- return $this->renderError($model->getError() ?: '删除失败');
- }
- return $this->renderSuccess('删除成功');
- }
- /**
- * 获取门店自提商品ID集合
- */
- public function getShopGoodsIds($shopId)
- {
- $goods_ids = ShopGoodsModel::where('shop_id', $shopId)->column('goods_id');
- return $this->renderSuccess(compact('goods_ids'));
- }
- }
|