<?php
|
|
namespace app\shop\model\plus\groupbuy;
|
|
use app\common\model\plus\groupbuy\Product as ProductModel;
|
|
/**
|
* 商家端团购商品模型
|
*/
|
class Product extends ProductModel
|
{
|
/**
|
* 获取团购商品列表
|
*/
|
public function getList($param)
|
{
|
$filter = [];
|
if (!empty($param['search'])) {
|
$filter[] = ['p.product_name', 'like', '%' . trim($param['search']) . '%'];
|
}
|
|
if (!empty($param['groupbuy_active_id'])) {
|
$filter[] = ['gp.groupbuy_active_id', '=', (int)$param['groupbuy_active_id']];
|
}
|
|
// 店铺ID筛选 - 明确指定表别名
|
$filter[] = ['gp.shop_supplier_id', '=', $param['shop_supplier_id'] ?? 0];
|
|
$order = ['gp.create_time' => 'desc'];
|
if (!empty($param['order'])) {
|
$order = [$param['order_field'] => $param['order']];
|
}
|
|
return $this->with(['product', 'groupbuySku', 'active'])
|
->alias('gp')
|
->join('product p', 'p.product_id = gp.product_id')
|
->where($filter)
|
->order($order)
|
->paginate([
|
'list_rows' => $param['list_rows'] ?? 15,
|
'query' => $param
|
]);
|
}
|
|
/**
|
* 添加团购商品
|
*/
|
public function add($data)
|
{
|
$this->startTrans();
|
$data['app_id'] = $this->app_id;
|
try {
|
// 检查商品是否已存在该活动中
|
$exist = $this->where([
|
'product_id' => $data['product_id'],
|
'groupbuy_active_id' => $data['groupbuy_active_id']
|
])->find();
|
if ($exist) {
|
$this->error = '该商品已在当前活动中';
|
return false;
|
}
|
|
// 保存团购商品基本信息
|
$result = $this->save($data);
|
|
if ($result !== false) {
|
// 处理团购SKU
|
if (!empty($data['sku_list'])) {
|
$this->handleGroupbuySku($data['sku_list']);
|
}
|
}
|
|
$this->commit();
|
return $result !== false;
|
} catch (\Exception $e) {
|
$this->rollback();
|
$this->error = $e->getMessage();
|
return false;
|
}
|
}
|
|
/**
|
* 批量添加团购商品
|
*/
|
public function batchAdd($productsData)
|
{
|
$results = [];
|
foreach ($productsData as $productData) {
|
$results[] = $this->add($productData);
|
}
|
return array_filter($results); // 返回成功的数量
|
}
|
|
/**
|
* 编辑团购商品
|
*/
|
public function edit($data)
|
{
|
$this->startTrans();
|
try {
|
$result = $this->save($data);
|
|
if ($result !== false && !empty($data['sku_list'])) {
|
// 更新团购SKU
|
$this->handleGroupbuySku($data['sku_list']);
|
}
|
|
$this->commit();
|
return $result !== false;
|
} catch (\Exception $e) {
|
$this->rollback();
|
$this->error = $e->getMessage();
|
return false;
|
}
|
}
|
|
/**
|
* 处理团购SKU数据
|
*/
|
private function handleGroupbuySku($skuList)
|
{
|
$skuModel = new \app\shop\model\plus\groupbuy\ProductSku();
|
|
// 先获取现有的SKU ID 列表
|
$existingSkus = $skuModel->where('groupbuy_product_id', $this->groupbuy_product_id)->select();
|
$existingSkuIds = [];
|
foreach ($existingSkus as $existingSku) {
|
$existingSkuIds[$existingSku['product_sku_id']] = $existingSku['groupbuy_product_sku_id'];
|
}
|
|
// 添加或更新团购SKU
|
foreach ($skuList as $sku) {
|
$sku['groupbuy_product_id'] = $this->groupbuy_product_id;
|
$sku['app_id'] = $this->app_id;
|
|
// 检查是否是现有SKU
|
if (isset($existingSkuIds[$sku['product_sku_id']])) {
|
// 更新现有SKU
|
$skuModel->save($sku, ['groupbuy_product_sku_id' => $existingSkuIds[$sku['product_sku_id']]]);
|
} else {
|
// 创建新SKU
|
$skuModel->create($sku);
|
}
|
}
|
}
|
|
/**
|
* 删除团购商品
|
*/
|
public function remove()
|
{
|
$this->startTrans();
|
try {
|
// 删除团购SKU
|
$skuModel = new \app\shop\model\plus\groupbuy\ProductSku();
|
$skuModel->where('groupbuy_product_id', $this->groupbuy_product_id)->delete();
|
|
// 删除团购订单用户记录
|
$billUserModel = new \app\shop\model\plus\groupbuy\BillUser();
|
$billUserModel->where('groupbuy_product_id', $this->groupbuy_product_id)->delete();
|
|
// 删除团购订单
|
$billModel = new \app\shop\model\plus\groupbuy\Bill();
|
$billModel->where('groupbuy_product_id', $this->groupbuy_product_id)->delete();
|
|
// 最后删除团购商品
|
$result = $this->delete();
|
|
$this->commit();
|
return $result !== false;
|
} catch (\Exception $e) {
|
$this->rollback();
|
$this->error = $e->getMessage();
|
return false;
|
}
|
}
|
}
|