// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\store\model\goods; use think\Paginator; use think\Collection; use app\common\model\goods\GoodsPrice as GoodsPriceModel; use app\common\model\Category as CategoryModel; /** * 商品价格配比 * Class Service */ class GoodsPrice extends GoodsPriceModel { /** * 获取全部记录 * @param array $param * @return array|Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getAll($params) { $catFids = CategoryModel::where(['parent_id' => 0])->column('category_id'); $catList = CategoryModel::with(['price'])->whereIn('parent_id', $catFids)->select()->toArray(); $all = []; foreach ($catList as $k => $v) { $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; } } else { if ($pv['type'] == 1) { $temp[] = $pv; } } } } $all[] = [ 'category_id' => $v['category_id'], 'name' => $v['name'], 'price' => $temp ]; } return $all; } /** * 新增记录 * @param array $data * @return bool|false */ 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); } /** * 更新记录 * @param array $data * @return bool */ public function edit(array $data): bool { return $this->save($data) !== false; } /** * 删除记录(软删除) * @return bool * @throws \Exception */ public function remove(): bool { // 判断该服务是否被商品引用 $goodsCount = ServiceRelModel::getCountByServiceId($this['service_id']); if ($goodsCount > 0) { $this->error = "该记录被{$goodsCount}个商品引用,不允许删除"; return false; } return $this->save(['is_delete' => 1]) !== false; } }