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.php

510 lines
18 KiB

11 months ago
<?php
5 months ago
11 months ago
declare (strict_types=1);
namespace app\store\model;
use app\common\library\helper;
use app\store\model\Spec as SpecModel;
use app\common\model\Goods as GoodsModel;
use app\store\model\GoodsSku as GoodsSkuModel;
use app\store\model\GoodsImage as GoodsImageModel;
use app\store\model\GoodsSpecRel as GoodsSpecRelModel;
use app\store\model\goods\ServiceRel as GoodsServiceRelModel;
use app\store\model\GoodsCategoryRel as GoodsCategoryRelModel;
7 months ago
use app\store\model\Category as CategoryRelModel;
11 months ago
use app\store\service\Goods as GoodsService;
use app\common\enum\goods\SpecType as GoodsSpecTypeEnum;
use app\common\enum\goods\Status as GoodsStatusEnum;
use cores\exception\BaseException;
9 months ago
use app\common\model\Region;
6 months ago
use think\facade\Db;
5 months ago
use app\common\model\Store as StoreModel;
11 months ago
/**
* 商品模型
* Class Goods
* @package app\store\model
*/
class Goods extends GoodsModel
{
/**
* 获取商品详情
* @param int $goodsId
* @return mixed
* @throws BaseException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getDetail(int $goodsId)
{
// 获取商品基础信息
$goodsInfo = $this->getBasic($goodsId);
4 months ago
if ($goodsInfo['spec_type'] == GoodsSpecTypeEnum::SINGLE && isset($goodsInfo['skuList1']) && $goodsInfo['skuList1']) {
9 months ago
$goodsInfo['skuList'] = $goodsInfo['skuList1'];
unset($goodsInfo['skuList1']);
}
4 months ago
// echo "<pre>";
// print_r($goodsInfo->toArray());
// exit();
11 months ago
// 分类ID集
$goodsInfo['categoryIds'] = GoodsCategoryRelModel::getCategoryIds($goodsInfo['goods_id']);
// 商品多规格属性列表
if ($goodsInfo['spec_type'] == GoodsSpecTypeEnum::MULTI) {
$goodsInfo['specList'] = GoodsSpecRelModel::getSpecList($goodsInfo['goods_id']);
}
// 服务与承诺
$goodsInfo['serviceIds'] = GoodsServiceRelModel::getServiceIds($goodsInfo['goods_id']);
// 商品规格是否锁定(锁定状态下不允许编辑规格)
$goodsInfo['isSpecLocked'] = GoodsService::checkSpecLocked($goodsId);
4 months ago
11 months ago
// 返回商品详细信息
return $goodsInfo;
}
/**
* 获取商品基础信息
* @param int $goodsId
* @return mixed
* @throws BaseException
*/
public function getBasic(int $goodsId)
{
1 month ago
$info = self::withoutGlobalScope()->field('spec_type,goods_id')->find($goodsId);
11 months ago
// 关联查询
9 months ago
if ($info->spec_type == GoodsSpecTypeEnum::SINGLE) {
$with = ['images.file', 'skuList1.image', 'video', 'videoCover'];
} else {
$with = ['images.file', 'skuList.image', 'video', 'videoCover'];
}
8 months ago
// $goodsInfo->rules = [];
11 months ago
// 获取商品记录
$goodsInfo = static::detail($goodsId, $with);
//使用总后台的商品的图片作为商城商品的图片
if ($goodsInfo->origin_goods_id && $goodsInfo->store_id > 0) {
$goods_images = $this->storeUsePlatformGoodsImage([$goodsInfo->origin_goods_id]);
$goodsInfo->images = $goods_images[$goodsInfo->origin_goods_id] ?? [];
}
8 months ago
if ($goodsInfo->region && $goodsInfo->region_text) {
$goodsInfo->rules = [['key' => 0, 'region' => json_decode($goodsInfo->region), 'region_text' => json_decode($goodsInfo->region_text)]];
}
9 months ago
// echo "<pre>";
// print_r($goodsInfo->toArray());
// exit();
11 months ago
empty($goodsInfo) && throwError('很抱歉,商品信息不存在');
// 整理商品数据并返回
return parent::setGoodsData($goodsInfo);
}
/**
* 添加商品
* @param array $data
* @return bool
* @throws \cores\exception\BaseException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function add(array $data): bool
{
// 创建商品数据
$data = $this->createData($data);
// 事务处理
$this->transaction(function () use ($data) {
3 months ago
$data['data_type'] = 1;
1 month ago
11 months ago
// 添加商品
$this->save($data);
// 新增商品与分类关联
GoodsCategoryRelModel::increased((int)$this['goods_id'], $data['categoryIds']);
// 新增商品与图片关联
GoodsImageModel::increased((int)$this['goods_id'], $data['imagesIds']);
// 新增商品与规格关联
GoodsSpecRelModel::increased((int)$this['goods_id'], $data['newSpecList']);
// 新增商品sku信息
GoodsSkuModel::add((int)$this['goods_id'], $data['spec_type'], $data['newSkuList']);
// 新增服务与承诺关联
8 months ago
GoodsServiceRelModel::increased((int)$this['goods_id'], $data['serviceIds'] ?? []);
11 months ago
});
return true;
}
/**
* 编辑商品
* @param array $data
* @return bool
* @throws \cores\exception\BaseException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
9 months ago
public function edit(array $data, $goodsModel = null): bool
1 month ago
{
11 months ago
// 创建商品数据
$data = $this->createData($data);
// 事务处理
9 months ago
$ret = $this->transaction(function () use ($data, $goodsModel) {
9 months ago
//如果是编辑多规格商品
if ($data['spec_type'] == GoodsSpecTypeEnum::MULTI) {
$data['spu_id'] = $this['goods_id'];
2 months ago
//取消之前所有的商品组商品
2 months ago
} else {
$data['spu_id'] = 0;
9 months ago
}
1 month ago
11 months ago
// 更新商品
$this->save($data);
// 更新商品与分类关联
GoodsCategoryRelModel::updates((int)$this['goods_id'], $data['categoryIds']);
// 更新商品与图片关联
GoodsImageModel::updates((int)$this['goods_id'], $data['imagesIds']);
// 更新商品与规格关联
GoodsSpecRelModel::updates((int)$this['goods_id'], $data['newSpecList']);
// 更新商品sku信息
9 months ago
$ret = GoodsSkuModel::edit((int)$this['goods_id'], $data['spec_type'], $data['newSkuList']);
if ($ret !== true) {
$this->error = $ret;
return false;
}
11 months ago
// 更新服务与承诺关联
8 months ago
GoodsServiceRelModel::updates((int)$this['goods_id'], $data['serviceIds'] ?? []);
9 months ago
return true;
11 months ago
});
9 months ago
// var_dump($ret);
// exit();
return $ret;
11 months ago
}
9 months ago
/**
* 修改商品状态
* @param array $goodsIds 商品id集
* @param bool $state 为true表示上架
* @return bool|false
*/
9 months ago
public function setIsPool(array $goodsIds, int $is_pool): bool
9 months ago
{
// 批量更新记录
6 months ago
return static::updateBase(['is_pool' => $is_pool,'update_time' => time(),'sale_time' => time()], [['goods_id', 'in', $goodsIds]]);
9 months ago
}
/**
* 修改商品状态
* @param array $goodsIds 商品id集
* @param bool $state 为true表示上架
* @return bool|false
*/
9 months ago
public function setIsSale(array $goodsIds, int $is_sale): bool
9 months ago
{
// 批量更新记录
6 months ago
return static::updateBase(['is_sale' => $is_sale,'update_time' => time(),'sale_time' => time()], [['goods_id', 'in', $goodsIds]]);
9 months ago
}
7 months ago
/**
* 修改商品状态
* @param array $goodsIds 商品id集
* @param bool $state 为true表示上架
* @return bool|false
*/
public function setIsJingpin(array $goodsIds, int $is_sale): bool
{
// 批量更新记录
return static::updateBase(['is_jingpin' => $is_sale], [['goods_id', 'in', $goodsIds]]);
}
11 months ago
/**
* 修改商品状态
* @param array $goodsIds 商品id集
* @param bool $state 为true表示上架
* @return bool|false
*/
public function setStatus(array $goodsIds, bool $state): bool
{
// 批量更新记录
return static::updateBase(['status' => $state ? 10 : 20], [['goods_id', 'in', $goodsIds]]);
}
6 months ago
/**
* 修改商品状态
* @param array $goodsIds 商品id集
* @param bool $state 为true表示上架
* @return bool|false
*/
public function setBrand(array $goodsIds, bool $is_brand): bool
{
// 批量更新记录
return static::updateBase(['is_brand' => $is_brand], [['goods_id', 'in', $goodsIds]]);
}
/**
* 修改商品状态
* @param array $goodsIds 商品id集
* @param bool $state 为true表示上架
* @return bool|false
*/
public function setNew(array $goodsIds, bool $is_new): bool
{
// 批量更新记录
return static::updateBase(['is_new' => $is_new], [['goods_id', 'in', $goodsIds]]);
}
/**
* 修改商品状态
* @param array $goodsIds 商品id集
* @param bool $state 为true表示上架
* @return bool|false
*/
public function setInstore(array $goodsIds, bool $is_in_store): bool
{
// 批量更新记录
return static::updateBase(['is_in_store' => $is_in_store], [['goods_id', 'in', $goodsIds]]);
}
/**
* 修改商品状态
* @param array $goodsIds 商品id集
* @param bool $state 为true表示上架
* @return bool|false
*/
public function setRank(array $goodsIds, int $is_paihang): bool
{
// 批量更新记录
return static::updateBase(['paihang' => $is_paihang], [['goods_id', 'in', $goodsIds]]);
}
11 months ago
/**
* 软删除
* @param array $goodsIds
* @return bool
*/
public function setDelete(array $goodsIds): bool
{
foreach ($goodsIds as $goodsId) {
if (!GoodsService::checkIsAllowDelete($goodsId)) {
$this->error = '当前商品正在参与其他活动,不允许删除';
return false;
}
}
// 批量更新记录
return static::updateBase(['is_delete' => 1], [['goods_id', 'in', $goodsIds]]);
}
// 获取已售罄的商品
public function getSoldoutGoodsTotal(): int
{
$filter = [
['stock_total', '=', 0],
['status', '=', GoodsStatusEnum::ON_SALE]
];
return $this->getGoodsTotal($filter);
}
/**
* 获取当前商品总数
* @param array $where
* @return int
*/
public function getGoodsTotal(array $where = []): int
{
1 month ago
return self::withoutGlobalScope()->where($where)->where('is_delete', '=', 0)->count();
11 months ago
}
5 months ago
/**
* 获取当前商品已上架总数
* @param array $where
* @return int
*/
public function getGoodsGroundingTotal(array $where = []): int
5 months ago
{
$detail = StoreModel::where('status',1)->find();
1 month ago
$list = self::withoutGlobalScope()->where($where)->where('is_delete', '=', 0)//->whereIn('channel',$detail['open_channel'])
3 months ago
->where('status','=',10)->count();
5 months ago
return $list;
5 months ago
}
11 months ago
/**
* 创建商品数据
* @param array $data
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \cores\exception\BaseException
*/
private function createData(array $data): array
{
8 months ago
$data['goods_sku_no'] = $data['goods_no'] ?? "";
5 months ago
$data['link'] = $data['goods_no'] ? "https://item.jd.com/".$data['goods_no'].".html" : "";
11 months ago
// 默认数据
$data = array_merge($data, [
'line_price' => $data['line_price'] ?? 0,
'content' => $data['content'] ?? '',
'newSpecList' => [],
3 months ago
//'data_type' => 1,//数据类型
11 months ago
'newSkuList' => [],
1 month ago
//'store_id' => self::$storeId,
8 months ago
//'sale_areas' => $arr ? implode("、", $arr) : "",
11 months ago
]);
8 months ago
if (isset($data['rules'])) {
$data['region'] = isset($data['rules'][0]['region']) ? json_encode($data['rules'][0]['region']) :"";
$data['region_text'] = isset($data['rules'][0]['region_text']) ? json_encode($data['rules'][0]['region_text'], JSON_UNESCAPED_UNICODE) :"";
}
11 months ago
// 整理商品的价格和库存总量
if ($data['spec_type'] == GoodsSpecTypeEnum::MULTI) {
9 months ago
// $data['stock_total'] = GoodsSkuModel::getStockTotal($data['specData']['skuList']);
// [$data['goods_price_min'], $data['goods_price_max']] = GoodsSkuModel::getGoodsPrices($data['specData']['skuList']);
// [$data['line_price_min'], $data['line_price_max']] = GoodsSkuModel::getLinePrices($data['specData']['skuList']);
11 months ago
} elseif ($data['spec_type'] == GoodsSpecTypeEnum::SINGLE) {
$data['goods_price_min'] = $data['goods_price_max'] = $data['goods_price'];
9 months ago
//$data['line_price_min'] = $data['line_price_max'] = $data['line_price'];
$data['line_price_min'] = $data['line_price_max'] = $data['goods_price'];
11 months ago
$data['stock_total'] = $data['stock_num'];
9 months ago
$data['cost_price_min'] = $data['cost_price']??0;
8 months ago
$profit = (float)$data['goods_price_min'] - (float)$data['cost_price_min'];
$profit_rate = (float)$data['goods_price_min'] > 0 ? bcmul((string)($profit / (float)$data['goods_price_min']) , "100", 2) : 0.00;
$data['profit'] = $profit;
$data['profit_rate'] = $profit_rate;
11 months ago
}
// 规格和sku数据处理
if ($data['spec_type'] == GoodsSpecTypeEnum::MULTI) {
// 验证规格值是否合法
SpecModel::checkSpecData($data['specData']['specList']);
// 生成多规格数据 (携带id)
$data['newSpecList'] = SpecModel::getNewSpecList($data['specData']['specList']);
// 生成skuList (携带goods_sku_id)
$data['newSkuList'] = GoodsSkuModel::getNewSkuList($data['newSpecList'], $data['specData']['skuList']);
} elseif ($data['spec_type'] == GoodsSpecTypeEnum::SINGLE) {
// 生成skuItem
8 months ago
$data['newSkuList'] = helper::pick($data, ['goods_price', 'line_price', 'cost_price','stock_num', 'goods_weight','goods_sku_no']);
11 months ago
}
// 单独设置折扣的配置
$data['is_enable_grade'] == 0 && $data['is_alone_grade'] = 0;
$aloneGradeEquity = [];
if ($data['is_alone_grade'] == 1) {
foreach ($data['alone_grade_equity'] as $key => $value) {
$gradeId = str_replace('grade_id:', '', $key);
$aloneGradeEquity[$gradeId] = $value;
}
}
8 months ago
11 months ago
$data['alone_grade_equity'] = $aloneGradeEquity;
6 months ago
//兼容分类前端传参不一致问题
6 months ago
$categoryIds = $data['categoryIds'];
if ($categoryIds && isset($categoryIds[0]['value'])) {
$categoryIds = array_column($categoryIds, 'value');
}
$data['categoryIds'] = $this->dealCategory($categoryIds);
6 months ago
//兼容图片前端传参不一致问题
$imagesIds = $data['imagesIds'];
if ($imagesIds && isset($imagesIds[0]['file_id'])) {
$images = [];
foreach ($imagesIds as $value) {
if ($value['file_id'] > 0) {
$images[] = $value['file_id'];
continue;
}
$inImage = [
5 months ago
'file_path' => $value['preview_url'],
6 months ago
'store_id' => self::$storeId,
'merchant_id' => $data['merchant_id'] ?? 0,
'storage' => "external",
'create_time' => time(),
'update_time' => time(),
];
$file_id = Db::name('upload_file')->insertGetId($inImage);
$images[] = $file_id;
}
$data['imagesIds'] = $images;
}
3 months ago
if ($data['categoryIds']) {
$data['cate_status'] = 1;
2 months ago
} else {
$data['cate_status'] = 0;
3 months ago
}
2 months ago
if ($data['content']) {
$data['is_has_detail'] = 1;
} else {
$data['is_has_detail'] = 0;
}
if ($data['imagesIds']) {
$data['is_has_banner'] = 1;
} else {
$data['is_has_banner'] = 0;
}
11 months ago
return $data;
}
7 months ago
public function dealCategory($category){
$arr = [];
// var_dump($category);
6 months ago
// exit();
7 months ago
foreach ($category as $key => $value) {
//一级
$cate = CategoryRelModel::where('category_id', $value)->find();
if (!$cate) {
continue;
}
$arr[] = $cate['category_id'];
if ($cate['parent_id'] == 0) {
continue;
}
//二级
$cate1 = CategoryRelModel::where('category_id', $cate['parent_id'])->find();
if (!$cate1) {
continue;
}
$arr[] = $cate1['category_id'];
if ($cate1['parent_id'] == 0) {
continue;
}
//三级
$cate2 = CategoryRelModel::where('category_id', $cate1['parent_id'])->find();
if (!$cate2) {
continue;
}
$arr[] = $cate2['category_id'];
if ($cate2['parent_id'] == 0) {
continue;
}
//四级
$cate3 = CategoryRelModel::where('category_id', $cate2['parent_id'])->find();
if (!$cate3) {
continue;
}
$arr[] = $cate3['category_id'];
if ($cate3['parent_id'] == 0) {
continue;
}
//五级
$cate4 = CategoryRelModel::where('category_id', $cate3['parent_id'])->find();
if (!$cate4) {
continue;
}
$arr[] = $cate4['category_id'];
if ($cate4['parent_id'] == 0) {
continue;
}
//五级
$cate5 = CategoryRelModel::where('category_id', $cate4['parent_id'])->find();
if (!$cate5) {
continue;
}
$arr[] = $cate5['category_id'];
if ($cate5['parent_id'] == 0) {
continue;
}
}
return array_unique($arr);
}
11 months ago
}