You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yanzong/app/store/model/goods/GoodsPrice.php

173 lines
6.2 KiB

11 months ago
<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\store\model\goods;
10 months ago
use app\common\library\helper;
11 months ago
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
10 months ago
* @return array|Collection
11 months ago
* @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 $data
* @return bool|false
*/
public function add(array $data): bool
{
10 months ago
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)) {
10 months ago
10 months ago
foreach ($catList as $k => $v) {
10 months ago
if ($data['min_price'] >= $v->min_price && $data['min_price'] <= $v->max_price) {
10 months ago
$this->error = "该分类价格区间冲突,请重新设置价格";
return false;
}
10 months ago
if ($data['max_price'] <= $v->max_price && $data['max_price'] >= $v->min_price) {
10 months ago
$this->error = "该分类价格区间冲突,请重新设置价格";
return false;
}
}
}
10 months ago
$data['profit_min'] = round($data['min_price'] * $data['markup_rate'] / 100, 2);
$data['profit_max'] = round($data['max_price'] * $data['markup_rate'] / 100, 2);
11 months ago
$data['store_id'] = self::$storeId;
return $this->save($data);
}
/**
* 更新记录
* @param array $data
* @return bool
*/
public function edit(array $data): bool
{
10 months ago
if (empty($data['type']) || empty($data['max_price']) || empty($data['cat_id']) || empty($data['markup_rate']) || empty($data['id'])) {
$this->error = "请补全信息";
return false;
}
10 months ago
$detail = $this->where('id', '=', $data['id'])->find();
if ($detail->isEmpty()) {
$this->error = "异常数据";
return false;
}
10 months ago
//校验是否有冲突的区间
10 months ago
$catList = $this->where(['cat_id' => $data['cat_id'], 'type' => $data['type']])->where('id', '<>', $data['id'])->select();
10 months ago
if (!empty($catList)) {
foreach ($catList as $k => $v) {
10 months ago
if ($data['min_price'] >= $v->min_price && $data['min_price'] <= $v->max_price) {
10 months ago
$this->error = "该分类价格区间冲突,请重新设置价格";
return false;
}
10 months ago
if ($data['max_price'] <= $v->max_price && $data['max_price'] >= $v->min_price) {
10 months ago
$this->error = "该分类价格区间冲突,请重新设置价格";
return false;
}
}
}
10 months ago
$data['profit_min'] = round($data['min_price'] * $data['markup_rate'] / 100, 2);
$data['profit_max'] = round($data['max_price'] * $data['markup_rate'] / 100, 2);
return $detail->save($data) !== false;
11 months ago
}
/**
* 删除记录(软删除)
* @return bool
* @throws \Exception
*/
10 months ago
public function del($data): bool
11 months ago
{
// 判断该服务是否被商品引用
10 months ago
$detail = $this->where('id', '=', $data['id'])->find();
if ($detail->isEmpty()) {
$this->error = "异常数据";
11 months ago
return false;
}
10 months ago
return $detail->delete() !== false;
11 months ago
}
10 months ago
/**
* 获取和计算折扣后的价格
* @param $catId
* @param $type
* @param $originalPrice
* @return string
*/
public static function getDiscountPrice($catId, $type = 1, $originalPrice): string
{
$info = self::where(['cat_id' => $catId, 'type' => $type])
->where('min_price', '<=', $originalPrice)
->order('min_price desc')
->find();
//没有配置返回原价
if (!$info) {
return $originalPrice;
}
// 使用高精度方法计算等级折扣;
$discountPrice = helper::bcmul($originalPrice, $info->markup_rate / 100, 2);
//取优惠后的价格
$lastPrice = bcsub($originalPrice, $discountPrice, 2);
return helper::number2($lastPrice, true);
}
11 months ago
}