From b8ae85799fdb9ead604cf3b80b5daf04b4205e44 Mon Sep 17 00:00:00 2001 From: limu Date: Thu, 1 Feb 2024 18:29:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=93=81=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 | 16 ++++ app/common/model/Category.php | 6 ++ app/common/model/Goods.php | 15 +++ app/common/model/goods/GoodsPrice.php | 48 ++++++++++ app/store/model/goods/GoodsPrice.php | 129 ++++++++++++++++++++++++++ 5 files changed, 214 insertions(+) create mode 100644 app/common/model/goods/GoodsPrice.php create mode 100644 app/store/model/goods/GoodsPrice.php diff --git a/app/api/controller/StoreKeeper.php b/app/api/controller/StoreKeeper.php index ce257ac9..82fd0876 100644 --- a/app/api/controller/StoreKeeper.php +++ b/app/api/controller/StoreKeeper.php @@ -30,6 +30,7 @@ use app\store\model\Order as OrderModel; use app\store\model\OrderRefund as OrderRefundModel; use app\store\model\dealer\Order as DealerOrderModel; use app\store\model\Article as ArticleModel; +use app\store\model\goods\GoodsPrice as GoodsPriceModel; /** * 店主管理 @@ -500,4 +501,19 @@ class StoreKeeper extends Controller } + //会员、分销价格配置 + + /** + * 配置列表 + * @return Json + * @throws \think\db\exception\DbException + */ + public function priceList(): Json + { + $model = new GoodsPriceModel; + $list = $model->getAll($this->request->param()); + return $this->renderSuccess(compact('list')); + } + + } \ No newline at end of file diff --git a/app/common/model/Category.php b/app/common/model/Category.php index dc09acaf..861d47fa 100644 --- a/app/common/model/Category.php +++ b/app/common/model/Category.php @@ -14,6 +14,7 @@ namespace app\common\model; use cores\BaseModel; use think\model\relation\HasOne; +use think\model\relation\HasMany; /** * 商品分类模型 @@ -37,6 +38,11 @@ class Category extends BaseModel return $this->hasOne('UploadFile', 'file_id', 'image_id'); } + public function price(): HasMany + { + $model = "app\\common\\model\\goods\\GoodsPrice"; + return $this->HasMany($model, 'cat_id', 'category_id'); + } /** * 分类详情 * @param int|array $where diff --git a/app/common/model/Goods.php b/app/common/model/Goods.php index 24e277ab..ac356c7f 100644 --- a/app/common/model/Goods.php +++ b/app/common/model/Goods.php @@ -13,6 +13,7 @@ declare (strict_types=1); namespace app\common\model; +use app\api\service\User as UserService; use cores\BaseModel; use think\db\BaseQuery; use think\db\exception\DbException; @@ -356,11 +357,25 @@ class Goods extends BaseModel $goodsInfo['goods_image'] = current($goodsInfo['goods_images'])['preview_url']; // 商品销量(实际显示=初始虚拟销量+实际销量) $goodsInfo['goods_sales'] = $goodsInfo['sales_initial'] + $goodsInfo['sales_actual']; + //商品价格判断 + if (UserService::isLogin()) { + if (UserService::isStore()) {//店主 + $goodsInfo['goods_price_min_plus'] = 0; + $goodsInfo['goods_price_min_dealer'] = 0; + } elseif (UserService::isDealerMember()) { //分销商 + $goodsInfo['goods_price_min_dealer'] = 0; + } elseif (UserService::isPlusMember()) {//升级会员 + $goodsInfo['goods_price_min_plus'] = 0; + } + } // 回调函数 is_callable($callback) && call_user_func($callback, $goodsInfo); return $goodsInfo; } + + + /** * 根据商品id集获取商品列表 * @param array $goodsIds diff --git a/app/common/model/goods/GoodsPrice.php b/app/common/model/goods/GoodsPrice.php new file mode 100644 index 00000000..8ea60710 --- /dev/null +++ b/app/common/model/goods/GoodsPrice.php @@ -0,0 +1,48 @@ + +// +---------------------------------------------------------------------- +declare (strict_types=1); + +namespace app\common\model\goods; + +use cores\BaseModel; +use think\model\relation\HasOne; + +/** + * 商品服务与承诺模型 + * Class Service + */ +class GoodsPrice extends BaseModel +{ + // 定义表名 + protected $name = 'goods_price'; + + // 定义主键 + protected $pk = 'id'; + + /** + * 帮助详情 + * @param int $helpId + * @return static|array|null + */ + public static function detail(int $helpId) + { + return self::get($helpId); + } + /** + * 分类图片 + * @return HasOne + */ + public function cat(): HasOne + { + $model = "app\\common\\model\\Category"; + return $this->hasOne($model, 'category_id', 'cat_id'); + } +} diff --git a/app/store/model/goods/GoodsPrice.php b/app/store/model/goods/GoodsPrice.php new file mode 100644 index 00000000..7a47906a --- /dev/null +++ b/app/store/model/goods/GoodsPrice.php @@ -0,0 +1,129 @@ + +// +---------------------------------------------------------------------- +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 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) { + 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 $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; + } + + /** + * 新增记录 + * @param array $data + * @return bool|false + */ + public function add(array $data): bool + { + $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; + } +}