From f994e3a56c14f2339aa1239ab09c655176f48919 Mon Sep 17 00:00:00 2001 From: limu Date: Fri, 2 Feb 2024 10:57:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BB=B7=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/StoreKeeper.php | 9 ++++++ app/store/model/goods/GoodsPrice.php | 47 ++++++++++------------------ 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/app/api/controller/StoreKeeper.php b/app/api/controller/StoreKeeper.php index 82fd0876..6ff987ac 100644 --- a/app/api/controller/StoreKeeper.php +++ b/app/api/controller/StoreKeeper.php @@ -515,5 +515,14 @@ class StoreKeeper extends Controller return $this->renderSuccess(compact('list')); } + public function addPrice(): Json + { + $model = new GoodsPriceModel; + if (!$model->add($this->request->param())) { + return $this->renderError($model->getError() ?: '添加失败'); + } + return $this->renderSuccess('添加成功'); + } + } \ No newline at end of file diff --git a/app/store/model/goods/GoodsPrice.php b/app/store/model/goods/GoodsPrice.php index 7a47906a..3dfabca6 100644 --- a/app/store/model/goods/GoodsPrice.php +++ b/app/store/model/goods/GoodsPrice.php @@ -26,7 +26,7 @@ class GoodsPrice extends GoodsPriceModel /** * 获取全部记录 * @param array $param - * @return Collection + * @return array|Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException @@ -40,6 +40,8 @@ class GoodsPrice extends GoodsPriceModel $temp = []; if (!empty($v['price'])) { foreach ($v['price'] as $pk => $pv) { + $pv['profit_min'] = round($pv['min_price'] * $pv['markup_rate'] / 100, 2); + $pv['profit_max'] = round($pv['max_price'] * $pv['markup_rate'] / 100, 2); if (!empty($params['type'])) { if ($pv['type'] == $params['type']) { $temp[] = $pv; @@ -51,7 +53,6 @@ class GoodsPrice extends GoodsPriceModel } } } - $all[] = [ 'category_id' => $v['category_id'], 'name' => $v['name'], @@ -61,34 +62,6 @@ class GoodsPrice extends GoodsPriceModel return $all; } - /** - * 获取列表记录 - * @param array $param - * @return Paginator - * @throws \think\db\exception\DbException - */ - public function getList(array $param = []): Paginator - { - return $this->where($this->getFilter($param)) - ->where('is_delete', '=', 0) - ->order(['sort', $this->getPk()]) - ->paginate(); - } - - /** - * 获取查询条件 - * @param array $param - * @return array - */ - private function getFilter(array $param = []): array - { - // 默认查询参数 - $params = $this->setQueryDefaultValue($param, ['search' => '']); - // 检索查询条件 - $filter = []; - !empty($params['search']) && $filter[] = ['name', 'like', "%{$params['search']}%"]; - return $filter; - } /** * 新增记录 @@ -97,6 +70,20 @@ class GoodsPrice extends GoodsPriceModel */ public function add(array $data): bool { + if (empty($data['type']) || empty($data['max_price']) || empty($data['cat_id']) || empty($data['markup_rate'])) { + $this->error = "请补全信息"; + return false; + } + //校验是否有冲突的区间 + $catList = $this->where(['cat_id' => $data['cat_id'], 'type' => $data['type']])->select(); + if (!empty($catList)) { + foreach ($catList as $k => $v) { + if ($v->max_price <= $data['min_price'] || $v->min_price >= $data['max_price']) { + $this->error = "该分类价格区间冲突,请重新设置价格"; + return false; + } + } + } $data['store_id'] = self::$storeId; return $this->save($data); }