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/api/model/Goods.php

930 lines
36 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\api\model;
10 months ago
use app\api\model\GoodsSku as GoodsSkuModel;
use app\api\model\GoodsSpecRel as GoodsSpecRelModel;
use app\api\model\store\Module as StoreModuleModel;
11 months ago
use app\api\service\Goods as GoodsService;
11 months ago
use app\api\service\User as UserService;
11 months ago
use app\api\service\user\Grade as UserGradeService;
10 months ago
use app\common\enum\goods\SpecType as GoodsSpecTypeEnum;
10 months ago
use app\common\enum\goods\Status as GoodsStatusEnum;
11 months ago
use app\common\model\Goods as GoodsModel;
10 months ago
use app\common\model\GoodsCategoryRel as GoodsCategoryRelModel;
10 months ago
use app\common\model\GoodsImage as GoodsImageModel;
use app\common\model\UploadFile as UploadFileModel;
8 months ago
use app\store\model\GoodsCategoryRel;
11 months ago
use cores\exception\BaseException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
/**
* 商品模型
* Class Goods
* @package app\api\model
*/
class Goods extends GoodsModel
{
/**
* 隐藏字段
* @var array
*/
public $hidden = [
'images',
'delivery',
'deduct_stock_type',
'sort',
'is_delete',
'store_id',
'create_time',
'update_time'
];
// 是否启用会员折扣价
private bool $enableGradeMoney = true;
/**
* 商品详情:HTML实体转换回普通字符
* @param $value
* @return string
*/
public function getContentAttr($value): string
{
return \htmlspecialchars_decode((string)$value);
}
/**
* 设置是否启用会员折扣价
* @param bool $value
* @return $this
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function setEnableGradeMoney(bool $value): Goods
{
$this->enableGradeMoney = $value && StoreModuleModel::checkModuleKey('user-grade');
return $this;
}
/**
* 获取是否启用会员折扣价
* @return bool
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
private function getEnableGradeMoney(): bool
{
return $this->enableGradeMoney && StoreModuleModel::checkModuleKey('user-grade');
}
/**
* 获取商品列表
* @param array $param 查询条件
* @param int $listRows 分页数量
* @return mixed|\think\model\Collection|\think\Paginator
* @throws \think\db\exception\DbException
*/
11 months ago
public function getList(array $param = [], int $listRows = 15)
11 months ago
{
11 months ago
11 months ago
// 整理查询参数
$params = array_merge($param, ['status' => GoodsStatusEnum::ON_SALE]);
11 months ago
11 months ago
// 获取商品列表
$list = parent::getList($params, $listRows);
11 months ago
11 months ago
if ($list->isEmpty()) {
return $list;
}
// 隐藏冗余的属性
$list->hidden(static::getHidden(['content', 'goods_images', 'images']));
// 整理列表数据并返回
return $this->setGoodsListDataFromApi($list);
}
10 months ago
public function presale($param)
10 months ago
{
$userId = UserService::getCurrentLoginUserId(true);
$info = PreSaleLog::where([
'user_id' => $userId,
'store_id' => request()->header()['storeid'],
10 months ago
'goods_id' => $param['goods_id'] ?? 0,
10 months ago
])->find();
if (!$info) {
PreSaleLog::insert([
'user_id' => $userId,
'store_id' => request()->header()['storeid'],
10 months ago
'pre_id' => $param['pre_id'] ?? 0,
'goods_id' => $param['goods_id'] ?? 0,
10 months ago
'ctime' => date('Y-m-d H:i:s')
]);
return true;
}
return false;
}
11 months ago
10 months ago
/**
* @notes:浏览记录
* @return array
* @throws BaseException
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author: wanghousheng
*/
public function browseLogList(): array
{
$userId = UserService::getCurrentLoginUserId();
$model = new GoodsBrowseLog;
$end_time = date('Y-m-d');
10 months ago
$start_time = date('Y-m-d', strtotime($end_time . ' - 15 days'));
$list = $model->with(['goods', 'images.file'])
10 months ago
->whereBetween('ctime', [$start_time, $end_time])
10 months ago
->order('ctime', 'desc')
10 months ago
->where(['user_id' => $userId])
->select();
$goodsList = $list->toArray()['data'] ?? $list->toArray();
//
$origin_goods_ids = [];
foreach ($list as $key => $value) {
$origin_goods_ids[] = $value['goods']['origin_goods_id'] ?? 0;
}
$goods_images = $this->storeUsePlatformGoodsImage($origin_goods_ids);
10 months ago
$data = [];
$array = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
if (!$list->isEmpty()) {
foreach ($list as $value) {
if ($value['goods']['origin_goods_id'] && $value['store_id'] > 0) {
$value['images'] = $goods_images[$value['goods']['origin_goods_id']] ?? [];
}
10 months ago
$data[$value['ctime']]['name'] = date('m-d', strtotime($value['ctime']));
$data[$value['ctime']]['week'] = $array[date("w", strtotime($value['ctime']))];
10 months ago
$data[$value['ctime']]['list'][] = $value;
10 months ago
}
}
return array_values($data);
}
11 months ago
public function browseLog()
{
$userId = UserService::getCurrentLoginUserId(true);
$list = GoodsBrowseLog::where([
'store_id' => request()->header()['storeid'],
'user_id' => $userId,
])->order('id desc')->paginate(15)->column('ctime');
$list = array_unique($list);
$res = [];
foreach ($list as $k => $v) {
$all = GoodsBrowseLog::alias('a')
->join('goods b', 'a.goods_id = b.goods_id')
->join('goods_sku c', 'a.goods_id = c.goods_id')
->where([
'a.store_id' => request()->header()['storeid'],
'a.user_id' => $userId,
'a.ctime' => $v,
'b.is_delete' => 0,
'b.status' => 10
])->field('a.ctime,a.goods_id,c.goods_props')->order('a.id desc')->select()->toArray();
foreach ($all as $k2 => &$v2) {
$v2['goods_props'] = \Qiniu\json_decode($v2['goods_props'], 1)[0] ?? '';
$v2['image'] = $this->getDetails2($v2['goods_id'])->toArray()['goods_images'][0]->toArray()['preview_url'] ?? '';
}
$res[$v] = $all;
}
return $res;
}
public function browseDel($param)
11 months ago
{
$userId = UserService::getCurrentLoginUserId(true);
$ids = $param['ids'];
11 months ago
$info = GoodsBrowseLog::where([
'store_id' => request()->header()['storeid'],
'user_id' => $userId,
])->whereIn('id', $ids)->delete();
11 months ago
return $info;
}
10 months ago
public function presaleList()
{
$userId = UserService::getCurrentLoginUserId(true);
$info = PreSaleLog::alias('a')
->join('presale b', 'a.pre_id = b.id')
->join('goods c', 'c.goods_id = a.goods_id')
->where([
'a.store_id' => request()->header()['storeid'],
'a.user_id' => $userId,
10 months ago
])->field('c.goods_name,c.goods_price_min,a.id,b.p_time,c.goods_id')->paginate(15);
10 months ago
return $info;
}
8 months ago
/**
* @notes:预售商品
* @param array $param
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author: wanghousheng
*/
8 months ago
public function presaleGoods(array $param): array
8 months ago
{
$list = [];
$model = new PreSale();
$info = $model->where(['is_change' => 0, 'status' => 1])->field('goods_list,p_time')->find();
if (!$info->isEmpty()) {
$info = $info->toArray();
$goods_ids = explode(',', $info['goods_list']);
8 months ago
if (!empty($param['category_id'])) {
$cate_model = new GoodsCategoryRel();
$cate_goods_id = $cate_model
->where(['category_id' => $param['category_id']])
->whereIn('goods_id', $goods_ids)
->column('distinct goods_id');
8 months ago
if (!$cate_goods_id) {
8 months ago
return $list;
8 months ago
} else {
$goods_ids = $cate_goods_id;
8 months ago
}
8 months ago
}
8 months ago
$result = $this->getListByIds($goods_ids);
if (!$result->isEmpty()) {
$result = $result->toArray();
foreach ($result as $v) {
$list[] = [
'goods_id' => $v['goods_id'],
'goods_name' => $v['goods_name'],
'goods_price_min' => $v['goods_price_min'],
'sales_initial' => $v['sales_initial'],
'sales_actual' => $v['sales_actual'],
'image' => $v['goods_image'],
'end_time' => strtotime($info['p_time']) - time()
];
}
}
}
return $list;
}
10 months ago
public function presaleGoodsList()
{
$info = PreSale::where([
'store_id' => request()->header()['storeid'],
'is_change' => 0,
'status' => 1,
])->order('id desc')->find();
if ($info) {
$goods = explode(',', $info->goods_list);
$list = goods::alias('a')
10 months ago
->join('goods_category_rel b', 'a.goods_id = b.goods_id')
->where([
10 months ago
'a.store_id' => request()->header()['storeid'],
'a.status' => 10,
9 months ago
//'b.category_id' => $_GET['category_id']
10 months ago
])->whereIn('a.goods_id', $goods)->field('a.goods_id,a.goods_name,a.goods_price_min,b.category_id,a.sales_initial,a.sales_actual')->select()->toArray();
10 months ago
//暂时 要优化
foreach ($list as &$v) {
$file_id = GoodsImageModel::where([
'store_id' => request()->header()['storeid'],
'goods_id' => $v['goods_id'],
])->field('image_id')->find()->image_id;
$file_path = UploadFileModel::where([
'store_id' => request()->header()['storeid'],
'file_id' => $file_id,
9 months ago
])->find();
$v['image'] = getUrl($file_path['file_path'], $file_path['domain']);
9 months ago
//$v['end_time'] = strtotime($info->p_time) - time();
$v['end_time'] = $info->p_time ? strtotime($info->p_time) - time() : 0;
10 months ago
10 months ago
}
10 months ago
return $list;
}
return [];
}
public function presaleCateList()
{
$info = PreSale::where([
'store_id' => request()->header()['storeid'],
'is_change' => 0,
'status' => 1,
])->order('id desc')->find();
if ($info) {
$goods = explode(',', $info->goods_list);
$list = goods::alias('a')
->join('goods_category_rel b', 'a.goods_id = b.goods_id')
->join('category c', 'c.category_id = b.category_id')
->where([
'a.store_id' => request()->header()['storeid'],
'a.status' => 10,
9 months ago
//'c.parent_id' => 0,
10 months ago
])->whereIn('a.goods_id', $goods)->field('c.*,a.sales_initial,a.sales_actual')->select()->toArray();
9 months ago
// var_dump($list);
// exit();
10 months ago
$list = $this->removeDuplicatesByField($list, 'category_id');
10 months ago
$info['end_time'] = strtotime($info->p_time) - time();
10 months ago
$res['info'] = $info;
$res['category'] = $list;
return $res;
}
return [];
}
function removeDuplicatesByField($array, $field)
{
$uniqueArray = array();
foreach ($array as $item) {
if (!isset($uniqueArray[$item[$field]])) {
$uniqueArray[$item[$field]] = $item;
}
}
return array_values($uniqueArray);
}
10 months ago
public function canlpresale($param)
10 months ago
{
$userId = UserService::getCurrentLoginUserId(true);
$info = PreSaleLog::where([
10 months ago
'id' => $param['id'],
10 months ago
'user_id' => $userId
])->delete();
return $info;
}
8 months ago
8 months ago
/**
* 商品主图、商品轮播图、商品详情处理
* [dealGoodsImage description]
* @param [type] &$goodsInfo [description]
* @return [type] [description]
*/
8 months ago
public function dealGoodsImage(&$goodsInfo)
{
8 months ago
switch ($goodsInfo->channel) {
case 'jd':
$jd = new \app\common\service\Jd();
$res = $jd->getGoodsMainImageAndDetail($goodsInfo->goods_no);
if ($res) {
8 months ago
if ($res['mainImageList']) {
$goods_images = [];
foreach ($res['mainImageList'] as $seq => $image) {
$goods_images[] = [
'file_id' => $seq,
'file_type' => 10,
'preview_url' => $image,
'external_url' => $image,
];
}
$goodsInfo->goods_images = $goods_images;
$goodsInfo->goods_image = $res['mainImageList'][0];
8 months ago
8 months ago
}
8 months ago
8 months ago
if ($res['infoImageList']) {
$content = "";
foreach ($res['infoImageList'] as $value) {
$content .= '<p><img src="' . $value . '"/></p>';
}
$goodsInfo->content = $content;
8 months ago
}
8 months ago
8 months ago
}
break;
case 'sn':
// code...
break;
default:
// code...
break;
}
8 months ago
8 months ago
}
8 months ago
11 months ago
/**
* 获取商品详情 (详细数据用于页面展示)
* @param int $goodsId 商品ID
* @param bool $verifyStatus 是否验证商品状态(上架)
* @return mixed
* @throws BaseException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getDetails(int $goodsId, bool $verifyStatus = true)
{
// 关联查询(商品图片、sku列表)
9 months ago
//$with = ['images.file', 'skuList.image', 'video', 'videoCover'];
$info = $this->field('spec_type,goods_id')->find($goodsId);
// 关联查询
if ($info->spec_type == GoodsSpecTypeEnum::SINGLE) {
$with = ['images.file', 'skuList1.image', 'video', 'videoCover'];
} else {
$with = ['images.file', 'skuList.image', 'video', 'videoCover'];
}
11 months ago
// 获取商品记录
$goodsInfo = $this->getGoodsMain($goodsId, $with, $verifyStatus);
8 months ago
//$this->dealGoodsImage($goodsInfo);
8 months ago
11 months ago
// 商品规格列表
8 months ago
if ($goodsInfo->spu_id && $goodsInfo->spu_id != $goodsInfo->goods_id) {
$goodsInfo['specList'] = GoodsSpecRelModel::getSpecList($goodsInfo['spu_id']);
} else {
$goodsInfo['specList'] = GoodsSpecRelModel::getSpecList($goodsInfo['goods_id']);
}
// $GoodsCategoryRelModel = new GoodsCategoryRelModel();
// $category = $GoodsCategoryRelModel
// ->alias('a')
// ->join('category b', 'a.category_id = b.category_id')
// ->where([
// 'a.goods_id' => $goodsId,
// 'a.store_id' => request()->header()['storeid']
// ])->find();
// $goodsInfo->category = $category ? $category->toArray() : [];
11 months ago
//多规格
$goodsInfo->specifications = [];
8 months ago
//$goodsInfo = $goodsInfo->toArray();
10 months ago
8 months ago
if (isset($goodsInfo->skuList1)) {
unset($goodsInfo->skuList1);
9 months ago
}
6 months ago
$goodsInfo['merchant'] = null;
//商户信息
6 months ago
if ($goodsInfo['merchant_id'] && !in_array($goodsInfo['channel'], config('app.not_show_merchant'))) {
6 months ago
//if ($goodsInfo['merchant_id']) {
6 months ago
$merchant = \app\common\model\Merchant::detail($goodsInfo['merchant_id'], $goodsInfo->store_id);
if ($merchant) {
if ($merchant['license_img_id']) {
$img_ids = explode(",", $merchant['license_img_id']);
$files = UploadFileModel::getFileList($img_ids, $goodsInfo->store_id);
$merchant['licenseImg'] = $files ?: null;
}
if ($merchant['logo_image_id']) {
$files = UploadFileModel::getFileList([$merchant['logo_image_id']], $goodsInfo->store_id);
$merchant['logoImage'] = $files ?: null;
}
}
$goodsInfo['merchant'] = $merchant;
6 months ago
} else {
$goodsInfo['merchant_id'] = 0;
6 months ago
}
6 months ago
//处理苏宁商品,京东价拖的问题
if (in_array($goodsInfo['channel'], ['sn','sn1']) && $goodsInfo['link_other']) {
$goodsInfo['link'] = $goodsInfo['link_other'];
$goodsInfo['goods_no'] = $goodsInfo['goods_no_other'];
}
11 months ago
//加入足迹
11 months ago
$userId = UserService::getCurrentLoginUserId(false) ?? '';
if ($userId) {
$info = GoodsBrowseLog::where([
'user_id' => $userId,
'goods_id' => $goodsId,
'store_id' => request()->header()['storeid'],
'ctime' => date('Y-m-d')
])->find();
if (!$info) {
GoodsBrowseLog::insert([
'user_id' => $userId,
'goods_id' => $goodsId,
'store_id' => request()->header()['storeid'],
'ctime' => date('Y-m-d')
]);
}
}
11 months ago
10 months ago
return $goodsInfo;
11 months ago
}
11 months ago
public function getDetails2(int $goodsId, bool $verifyStatus = true)
{
// 关联查询(商品图片、sku列表)
$with = ['images.file'];
// 获取商品记录
$goodsInfo = $this->getGoodsMain($goodsId, $with, $verifyStatus);
return $goodsInfo;
}
11 months ago
/**
* 获取商品详情 (仅包含主商品信息和商品图片)
* @param int $goodsId 商品ID
* @param bool $verifyStatus 是否验证商品状态(上架)
* @return mixed
* @throws BaseException
*/
public function getBasic(int $goodsId, bool $verifyStatus = true)
{
// 关联查询(商品图片)
$with = ['images.file'];
8 months ago
9 months ago
11 months ago
// 获取商品记录
return $this->getGoodsMain($goodsId, $with, $verifyStatus);
}
/**
* 获取商品规格数据
* @param int $goodsId
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getSpecData(int $goodsId): array
{
$data = [];
// 商品SKU列表
$data['skuList'] = GoodsSkuModel::getSkuList($goodsId, true);
// 商品规格列表
$data['specList'] = GoodsSpecRelModel::getSpecList($goodsId);
return $data;
}
/**
* 获取商品主体信息
* @param int $goodsId 商品ID
* @param array $with 关联查询
* @param bool $verifyStatus 是否验证商品状态(上架)
* @return mixed
* @throws BaseException
*/
private function getGoodsMain(int $goodsId, array $with = [], bool $verifyStatus = true)
{
// 获取商品记录
$goodsInfo = static::detail($goodsId, $with);
8 months ago
//单规格和多规格处理不一致
$goodsInfo->skuList = $goodsInfo->skuList1 ? $goodsInfo->skuList1 : $goodsInfo->skuList;
8 months ago
8 months ago
if ($goodsInfo->spu_id && $goodsInfo->spu_id != $goodsInfo->goods_id) {
$goodsInfo->skuList = GoodsSkuModel::where('spu_id', $goodsInfo->spu_id)->select();
9 months ago
}
6 months ago
foreach ($goodsInfo->skuList as &$value) {
//处理苏宁商品,京东价拖的问题
if (in_array($goodsInfo['channel'], ['sn','sn1']) && $goodsInfo['link_other'] && $value->goods_id == $goodsId) {
$value->goods_sku_no = $goodsInfo['goods_no_other'];
}
}
//使用总后台的商品的图片作为商城商品的图片
if ($goodsInfo->origin_goods_id && $goodsInfo->store_id > 0) {
$goods_images = $this->storeUsePlatformGoodsImage([$goodsInfo->origin_goods_id]);
5 months ago
$goodsList = $this->storeUsePlatformGoods([$goodsInfo->origin_goods_id]);
$goodsInfo->images = $goods_images[$goodsInfo->origin_goods_id] ?? [];
5 months ago
$goodsInfo->content = $goodsList[$goodsInfo->origin_goods_id]['content'] ?? "";
$goodsInfo->goods_no = $goodsList[$goodsInfo->origin_goods_id]['goods_no'] ?? "";
$goodsInfo->goods_name = $goodsList[$goodsInfo->origin_goods_id]['goods_name'] ?? "";
// echo "<pre>";
// print_r($goodsInfo->images);
// exit();
}
6 months ago
//是否展示看同款按钮
$goodsInfo->is_show_ktk = 1;
if (in_array($goodsInfo['channel'], ['sn','sn1']) && !$goodsInfo['link_other']) {
$goodsInfo->is_show_ktk = 0;
}
11 months ago
// 判断商品是否存在
if (empty($goodsInfo) || $goodsInfo['is_delete']) {
throwError('很抱歉,商品信息不存在');
}
// 判断商品状态(上架)
if ($verifyStatus && $goodsInfo['status'] == GoodsStatusEnum::OFF_SALE) {
throwError('很抱歉,当前商品已下架');
}
8 months ago
11 months ago
// 整理商品数据并返回
return $this->setGoodsDataFromApi($goodsInfo);
}
/**
* 根据商品id集获取商品列表
* @param array $goodsIds
* @return mixed
*/
public function getListByIdsFromApi(array $goodsIds)
{
// 获取商品列表
$data = $this->getListByIds($goodsIds, GoodsStatusEnum::ON_SALE);
// 整理列表数据并返回
return $this->setGoodsListDataFromApi($data);
}
/**
* 获取商品指定的sku信息并且设置商品的会员价
* @param mixed $goodsInfo 商品信息
* @param string $goodsSkuId 商品SKUID
* @param bool $enableGradeMoney 是否设置会员折扣价
* @return \app\common\model\GoodsSku|array|null
* @throws BaseException
*/
7 months ago
public static function getSkuInfo($goods, string $goodsSkuId, bool $enableGradeMoney = true)
11 months ago
{
7 months ago
$goods['skuInfo'] = GoodsService::getSkuInfo($goods['goods_id'], $goodsSkuId);
9 months ago
//$enableGradeMoney && (new static)->setGoodsGradeMoney($goodsInfo);
7 months ago
$catService = new GoodsCategoryRel();
$catIds = $catService->where(['goods_id' => $goods->goods_id])->column('category_id');
6 months ago
$currtime = time();
7 months ago
if ($goods->getAttr('skuInfo')) {
if (UserService::isPlusMember()) {
$goods['skuInfo']['goods_price'] = \app\common\model\PriceSet::membershipPrice($goods['skuInfo']['goods_price'], $goods['skuInfo']['cost_price'], $catIds);
} elseif (UserService::isDealerMember()) {
$priceArr = \app\common\model\PriceSet::distributionPrice($goods['skuInfo']['goods_price'], $goods['skuInfo']['cost_price'], $catIds);
$goods['skuInfo']['goods_price'] = $priceArr['distributionPrice'];
}
6 months ago
//处理店长临时设置的秒杀价
if ($goods['skuInfo']['sec_start_time'] && strtotime($goods['skuInfo']['sec_start_time']) <= $currtime && (strtotime($goods['skuInfo']['sec_start_time']) + 24 * 3600) >= $currtime) {
$goods['skuInfo']['goods_price'] = $goods['skuInfo']['seckill_price'];
}
7 months ago
}
//(new static)->setGoodsMoney($goodsInfo);
return $goods['skuInfo'];
11 months ago
}
/**
* 设置商品展示的数据 api模块
* @param $data
* @return mixed
*/
private function setGoodsListDataFromApi($data)
{
return $this->setGoodsListData($data, function ($goods) {
// 整理商品数据 api模块
$this->setGoodsDataFromApi($goods);
});
}
/**
* 整理商品数据 api模块
* @param $goodsInfo
* @return mixed
*/
private function setGoodsDataFromApi($goodsInfo)
{
9 months ago
11 months ago
return $this->setGoodsData($goodsInfo, function ($goods) {
// 计算并设置商品会员价
8 months ago
//$this->getEnableGradeMoney() && $this->setGoodsGradeMoney($goods);
9 months ago
10 months ago
//计算plus 分销价格
$this->setGoodsMoney($goods);
11 months ago
});
}
10 months ago
/**
* 设置商品plus 分销价格
* @param Goods $goods
* @throws BaseException
*/
private function setGoodsMoney(self $goods)
{
9 months ago
10 months ago
// 判断是否登录
if (!UserService::isLogin()) {
9 months ago
$goods['line_price_min'] = $goods['goods_price_min'];
10 months ago
return;
}
6 months ago
$currtime = time();
8 months ago
$catService = new GoodsCategoryRel();
10 months ago
$catIds = $catService->where(['goods_id' => $goods->goods_id])->column('category_id');
9 months ago
$goods['line_price_min'] = $goods['goods_price_min'];//划线价格等于市场价
6 months ago
//$goods['discount'] = 0.0;
10 months ago
//价格判断
if (UserService::isstore()) {
$priceArr = \app\common\model\PriceSet::distributionPrice($goods['goods_price_min'], $goods['cost_price_min'], $catIds);
8 months ago
$goods['goods_price_min_plus'] = $priceArr['membershipPrice'];
$goods['goods_price_min_dealer'] = $priceArr['distributionPrice'];
$goods['goods_price_min'] = $goods['goods_price_min_plus'];//$goods['cost_price_min'];//店长售价,展示为成本价
8 months ago
10 months ago
} elseif (UserService::isPlusMember()) {
8 months ago
$membershipPrice = \app\common\model\PriceSet::membershipPrice($goods['goods_price_min'], $goods['cost_price_min'], $catIds);
6 months ago
//$goods['discount'] = bcdiv((string)($membershipPrice * 10), (string)$goods['goods_price_min'], 1);
8 months ago
$goods['goods_price_min'] = $membershipPrice;
10 months ago
} elseif (UserService::isDealerMember()) {
$priceArr = \app\common\model\PriceSet::distributionPrice($goods['goods_price_min'], $goods['cost_price_min'], $catIds);
6 months ago
//$goods['discount'] = bcdiv((string)($priceArr['distributionPrice'] * 10), (string)$goods['goods_price_min'], 1);
8 months ago
//$goods['goods_price_min'] = $membershipPrice;
8 months ago
$goods['goods_price_min'] = $priceArr['distributionPrice'];
10 months ago
}
6 months ago
//处理店长临时设置的秒杀价
if ($goods['sec_start_time'] && strtotime($goods['sec_start_time']) <= $currtime && (strtotime($goods['sec_start_time']) + 24 * 3600) >= $currtime) {
$goods['goods_price_min'] = $goods['seckill_price'];
}
6 months ago
// if ($goods['discount'] == 10) {
// $goods['discount'] = 0.0;
// }
6 months ago
10 months ago
// 会员折扣价: 商品sku列表
8 months ago
if ($goods->getRelation('skuList') || $goods->getRelation('skuList1')) {
6 months ago
10 months ago
foreach ($goods['skuList'] as &$skuItem) {
8 months ago
//处理图片和库
$goods_image = GoodsImage::where('goods_id', $skuItem['goods_id'])->order("id asc")->find();
$file_path = UploadFile::where('file_id', $goods_image['image_id'] ?? 0)->find();
if ($file_path) {
$skuItem['image_url'] = getUrlValue($file_path['file_path'], $file_path['domain'], $file_path['storage']);
}
$skuItem['stock_num'] = $goods['stock_total'] ?? 0;
10 months ago
//价格判断
if (UserService::isstore()) {
$priceArr = \app\common\model\PriceSet::distributionPrice($skuItem['goods_price'], $skuItem['cost_price'], $catIds);
9 months ago
$skuItem['goods_price'] = $skuItem['cost_price'];//店长售价,展示为成本价
10 months ago
} elseif (UserService::isPlusMember()) {
9 months ago
$skuItem['goods_price'] = \app\common\model\PriceSet::membershipPrice($skuItem['goods_price'], $skuItem['cost_price'], $catIds);
10 months ago
} elseif (UserService::isDealerMember()) {
$priceArr = \app\common\model\PriceSet::distributionPrice($skuItem['goods_price'], $skuItem['cost_price'], $catIds);
9 months ago
$skuItem['goods_price'] = $priceArr['distributionPrice'];
10 months ago
}
6 months ago
//处理店长临时设置的秒杀价
if ($skuItem['sec_start_time'] && strtotime($skuItem['sec_start_time']) <= $currtime && (strtotime($skuItem['sec_start_time']) + 24 * 3600) >= $currtime) {
$skuItem['goods_price'] = $skuItem['seckill_price'];
}
10 months ago
10 months ago
}
6 months ago
10 months ago
}
8 months ago
10 months ago
// 折扣价: 已选择的商品sku(用于购物车)
10 months ago
if ($goods->getAttr('skuInfo')) {
10 months ago
if (UserService::isPlusMember()) {
9 months ago
// $sku_price_plus = [];
// foreach ($catIds as $k => $v) {
// $sku_price_plus[] = GoodsPriceModel::getDiscountPrice($v, 1, $goods['skuInfo']['goods_price']);
// }
// $goods['skuInfo']['goods_price'] = min($sku_price_plus);
$goods['skuInfo']['goods_price'] = \app\common\model\PriceSet::membershipPrice($goods['skuInfo']['goods_price'], $goods['skuInfo']['cost_price'], $catIds);
9 months ago
10 months ago
} elseif (UserService::isDealerMember()) {
// $sku_price_dealer = [];
// foreach ($catIds as $k => $v) {
// $sku_price_dealer[] = GoodsPriceModel::getDiscountPrice($v, 2, $goods['skuInfo']['goods_price']);
// }
//$goods['skuInfo']['goods_price'] = min($sku_price_dealer);
$priceArr = \app\common\model\PriceSet::distributionPrice($goods['skuInfo']['goods_price'], $goods['skuInfo']['cost_price'], $catIds);
$goods['skuInfo']['goods_price'] = $priceArr['distributionPrice'];
10 months ago
}
6 months ago
//处理店长临时设置的秒杀价
if ($goods['skuInfo']['sec_start_time'] && strtotime($goods['skuInfo']['sec_start_time']) <= $currtime && (strtotime($goods['skuInfo']['sec_start_time']) + 24 * 3600) >= $currtime) {
$goods['skuInfo']['goods_price'] = $goods['skuInfo']['seckill_price'];
}
10 months ago
}
}
11 months ago
/**
* 设置商品的会员价
* @param Goods $goods
* @throws BaseException
*/
private function setGoodsGradeMoney(self $goods)
{
// 设置当前商品是否使用会员等级折扣价
$goods['is_user_grade'] = false;
// 获取当前登录用户的会员等级信息
$gradeInfo = UserGradeService::getCurrentGradeInfo();
// 判断商品是否参与会员折扣
if (empty($gradeInfo) || !$goods['is_enable_grade']) {
return;
}
// 默认的折扣比例
$discountRatio = $gradeInfo['equity']['discount'];
// 商品单独设置了会员折扣
if ($goods['is_alone_grade'] && isset($goods['alone_grade_equity'][$gradeInfo['grade_id']])) {
$discountRatio = $goods['alone_grade_equity'][$gradeInfo['grade_id']];
}
if (empty($discountRatio)) {
return;
}
// 标记参与会员折扣
$goods['is_user_grade'] = true;
// 会员折扣价: 商品基础价格
$goods['goods_price_min'] = UserGradeService::getDiscountPrice($goods['goods_price_min'], $discountRatio);
$goods['goods_price_max'] = UserGradeService::getDiscountPrice($goods['goods_price_max'], $discountRatio);
// 会员折扣价: 商品sku列表
if ($goods->getRelation('skuList')) {
foreach ($goods['skuList'] as &$skuItem) {
$skuItem['goods_price'] = UserGradeService::getDiscountPrice($skuItem['goods_price'], $discountRatio);
}
}
// 会员折扣价: 已选择的商品sku(用于购物车)
if ($goods->getAttr('skuInfo')) {
9 months ago
echo "11";
exit();
11 months ago
$goods['skuInfo']['goods_price'] = UserGradeService::getDiscountPrice($goods['skuInfo']['goods_price'], $discountRatio);
}
}
10 months ago
/**
* 修改商品价格
* @param $param
* @return false|void
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
8 months ago
public function editGoodsPrice($data)
{
9 months ago
if (empty($data['goods_price']) || empty($data['cost_price']) || empty($data['id'])) {
10 months ago
$this->error = "请补全信息";
return false;
}
$detail = $this->where('goods_id', '=', $data['id'])->find();
10 months ago
if ($detail->isEmpty()) {
$this->error = "异常数据";
return false;
}
// 整理商品的价格
8 months ago
// if ($detail['spec_type'] == GoodsSpecTypeEnum::MULTI) {
// if(empty($data['sku_id'])) {
// $this->error = "请输入sku_id";
// return false;
// }
// //批量修改sku价格
// $sku_ids = explode(',', $data['sku_id']);
// foreach ($sku_ids as $sku_id) {
// $skuData = GoodsSkuModel::get(['id' => $sku_id,'goods_id' => $data['id']]);
// if ($skuData) {
// $skuData->save(['goods_price' => $data['goods_price'], 'cost_price' => $data['cost_price']]);
// }
// }
// $skuList = GoodsSkuModel::getSkuList((int)$data['id']);
// [$data['goods_price_min'], $data['goods_price_max']] = GoodsSkuModel::getGoodsPrices($skuList->toArray());
// } elseif ($detail['spec_type'] == GoodsSpecTypeEnum::SINGLE) {
8 months ago
8 months ago
//}
$data['goods_price_min'] = $data['goods_price_max'] = $data['line_price_min'] = $data['line_price_max'] = $data['goods_price'];
$data['cost_price_min'] = $data['cost_price'];
$detail->save($data);
//更新sku的价格
8 months ago
GoodsSkuModel::where('goods_id', $data['id'])->update(['cost_price' => $data['cost_price'], 'goods_price' => $data['goods_price']]);
8 months ago
return true;
10 months ago
}
8 months ago
public function editGoodsSeckillPrice($data)
{
if (empty($data['seckill_price']) || empty($data['is_limit']) || empty($data['limit_times']) || empty($data['goods_id'])) {
$this->error = "请补全信息";
return false;
}
$detail = $this->where('goods_id', '=', $data['goods_id'])->find();
if (empty($detail)) {
$this->error = "异常数据";
return false;
}
8 months ago
if (empty($data['sku_id'])) {
$this->error = "请输入sku_id";
return false;
}
//批量修改秒杀价格
9 months ago
// var_dump($data['sku_id']);
$sku_id = $data['sku_id'];
// foreach ($sku_ids as $sku_id) {
8 months ago
$skuData = GoodsSkuModel::get(['id' => $sku_id, 'goods_id' => $data['goods_id']]);
if ($skuData) {
$up_data = [
'seckill_price' => $data['seckill_price'],
'is_limit' => $data['is_limit'],
'limit_times' => $data['limit_times'],
'sec_start_time' => $data['sec_start_time'],
// 'sec_end_time' => $data['sec_end_time'],
'sec_hour' => $data['sec_hour'],
];
$skuData->save($up_data);
6 months ago
GoodsModel::where('goods_id', $data['goods_id'])->update($up_data);
8 months ago
}
9 months ago
// }
return true;
}
11 months ago
}