添加价格

pull/1/head
limu 10 months ago
parent dd79cf3b7d
commit f994e3a56c
  1. 9
      app/api/controller/StoreKeeper.php
  2. 47
      app/store/model/goods/GoodsPrice.php

@ -515,5 +515,14 @@ class StoreKeeper extends Controller
return $this->renderSuccess(compact('list')); 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('添加成功');
}
} }

@ -26,7 +26,7 @@ class GoodsPrice extends GoodsPriceModel
/** /**
* 获取全部记录 * 获取全部记录
* @param array $param * @param array $param
* @return Collection * @return array|Collection
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
@ -40,6 +40,8 @@ class GoodsPrice extends GoodsPriceModel
$temp = []; $temp = [];
if (!empty($v['price'])) { if (!empty($v['price'])) {
foreach ($v['price'] as $pk => $pv) { 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 (!empty($params['type'])) {
if ($pv['type'] == $params['type']) { if ($pv['type'] == $params['type']) {
$temp[] = $pv; $temp[] = $pv;
@ -51,7 +53,6 @@ class GoodsPrice extends GoodsPriceModel
} }
} }
} }
$all[] = [ $all[] = [
'category_id' => $v['category_id'], 'category_id' => $v['category_id'],
'name' => $v['name'], 'name' => $v['name'],
@ -61,34 +62,6 @@ class GoodsPrice extends GoodsPriceModel
return $all; 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 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; $data['store_id'] = self::$storeId;
return $this->save($data); return $this->save($data);
} }

Loading…
Cancel
Save