ShopGoods.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\store\controller;
  4. use app\common\exception\BaseException;
  5. use app\store\model\ShopGoods as ShopGoodsModel;
  6. /**
  7. * 门店自提商品管理控制器
  8. * Class ShopGoods
  9. * @package app\store\controller
  10. */
  11. class ShopGoods extends Controller
  12. {
  13. /**
  14. * 列表
  15. * @return array
  16. * @throws \think\db\exception\DbException
  17. */
  18. public function list()
  19. {
  20. // 获取列表记录
  21. $model = new ShopGoodsModel;
  22. $params = $this->request->param();
  23. $list = $model->getList($params);
  24. return $this->renderSuccess(compact('list'));
  25. }
  26. /**
  27. * 详情
  28. * @param int $id
  29. * @return array
  30. * @throws BaseException
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. */
  35. public function detail(int $id)
  36. {
  37. // 获取详情
  38. $model = new ShopGoodsModel;
  39. $info = $model->getDetail($id);
  40. return $this->renderSuccess(compact('info'));
  41. }
  42. /**
  43. * 添加商品(支持批量)
  44. *
  45. * @return array
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. */
  50. public function add()
  51. {
  52. $model = new ShopGoodsModel;
  53. if ($model->add($this->postForm())) {
  54. return $this->renderSuccess('添加成功');
  55. }
  56. return $this->renderError($model->getError() ?: '添加失败');
  57. }
  58. /**
  59. * 设置门店商品库存
  60. */
  61. public function setStock($id)
  62. {
  63. $model = ShopGoodsModel::get($id);
  64. if (empty($model)) {
  65. return $this->renderError("数据不存在");
  66. }
  67. if ($model->setStock($this->postForm())) {
  68. return $this->renderSuccess('保存成功');
  69. }
  70. return $this->renderError($model->getError() ?: '保存失败');
  71. }
  72. /**
  73. * 移除商品
  74. * @param $id
  75. * @return array
  76. */
  77. public function delete($id)
  78. {
  79. $model = ShopGoodsModel::get($id);
  80. if (empty($model)) {
  81. return $this->renderError("数据不存在");
  82. }
  83. if (!$model->remove()) {
  84. return $this->renderError($model->getError() ?: '删除失败');
  85. }
  86. return $this->renderSuccess('删除成功');
  87. }
  88. /**
  89. * 获取门店自提商品ID集合
  90. */
  91. public function getShopGoodsIds($shopId)
  92. {
  93. $goods_ids = ShopGoodsModel::where('shop_id', $shopId)->column('goods_id');
  94. return $this->renderSuccess(compact('goods_ids'));
  95. }
  96. }