<?php
|
|
namespace app\admin\model\plus\groupbuy;
|
|
use app\common\model\plus\groupbuy\Active as ActiveModel;
|
|
/**
|
* 团购活动管理模型
|
*/
|
class Active extends ActiveModel
|
{
|
/**
|
* 获取团购活动列表
|
*/
|
public function getList($param)
|
{
|
$filter = [];
|
if (!empty($param['search'])) {
|
$filter[] = ['active_name', 'like', '%' . trim($param['search']) . '%'];
|
}
|
|
if (isset($param['status']) && $param['status'] !== '') {
|
$filter[] = ['status', '=', (int)$param['status']];
|
}
|
|
// 时间范围筛选
|
if (!empty($param['start_time']) && !empty($param['end_time'])) {
|
$filter[] = ['start_time', '>=', strtotime($param['start_time'])];
|
$filter[] = ['end_time', '<=', strtotime($param['end_time'])];
|
}
|
|
$order = ['create_time' => 'desc'];
|
if (!empty($param['order'])) {
|
$order = [$param['order_field'] => $param['order']];
|
}
|
|
return $this->with(['file'])
|
->where($filter)
|
->order($order)
|
->paginate([
|
'list_rows' => $param['list_rows'] ?? 15,
|
'query' => $param
|
]);
|
}
|
|
/**
|
* 添加团购活动
|
*/
|
public function add($data)
|
{
|
$this->startTrans();
|
try {
|
// 格式化数据
|
$data['start_time'] = strtotime($data['start_time']);
|
$data['end_time'] = strtotime($data['end_time']);
|
|
$result = $this->save($data);
|
$this->commit();
|
return $result !== false;
|
} catch (\Exception $e) {
|
$this->rollback();
|
$this->error = $e->getMessage();
|
return false;
|
}
|
}
|
|
/**
|
* 编辑团购活动
|
*/
|
public function edit($data)
|
{
|
$this->startTrans();
|
try {
|
// 格式化数据
|
if (isset($data['start_time'])) {
|
$data['start_time'] = strtotime($data['start_time']);
|
}
|
if (isset($data['end_time'])) {
|
$data['end_time'] = strtotime($data['end_time']);
|
}
|
|
$result = $this->save($data);
|
$this->commit();
|
return $result !== false;
|
} catch (\Exception $e) {
|
$this->rollback();
|
$this->error = $e->getMessage();
|
return false;
|
}
|
}
|
|
/**
|
* 更新状态
|
*/
|
public function updateStatus($status)
|
{
|
return $this->save(['status' => $status]) !== false;
|
}
|
|
/**
|
* 删除团购活动
|
*/
|
public function remove()
|
{
|
// 检查是否有团购商品关联
|
$productModel = new \app\admin\model\plus\groupbuy\Product();
|
$count = $productModel->where('groupbuy_active_id', $this->groupbuy_active_id)->count();
|
if ($count > 0) {
|
$this->error = '该活动下还有团购商品,无法删除';
|
return false;
|
}
|
|
return $this->delete();
|
}
|
}
|