文件
yjyf/server/application/admin/controller/Coupon.php
MiAn fafddceaf2 feat: 新增发票抬头、晒单、钱包日志等模型及后台管理功能
- 新增 invoice_title、showcase、wallet_log 模型及对应语言包
- 新增后台控制器、视图及搜索字段配置
- 修复服务类目删除保护逻辑
- 优化后台登录页面文案及样式
- 新增前端角色切换逻辑及页面拦截
- 新增 SVG 图标及样式变量
- 修复接口权限及下拉框默认值问题
2026-11-11 17:02:35 +08:00

208 行
7.0 KiB
PHP

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
namespace app\admin\controller;
use app\common\controller\Backend;
use think\Db;
/**
* 优惠券模板管理
*
* @icon fa fa-ticket
*/
class Coupon extends Backend
{
/**
* Coupon模型对象
* @var \app\admin\model\Coupon
*/
protected $model = null;
protected $searchFields = 'id,name';
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\Coupon;
$this->view->assign("typeList", $this->model->getTypeList());
$this->view->assign("statusList", $this->model->getStatusList());
}
/**
* 优惠券模板列表(聚合真实领取张数与核销使用张数)
*/
public function index()
{
$this->request->filter(['strip_tags', 'trim']);
if (false === $this->request->isAjax()) {
return $this->view->fetch();
}
if ($this->request->request('keyField')) {
return $this->selectpage();
}
[$where, $sort, $order, $offset, $limit] = $this->buildparams();
$list = $this->model
->where($where)
->order($sort, $order)
->paginate($limit);
$rows = $list->items();
$ids = [];
foreach ($rows as $r) {
$ids[] = (int)$r['id'];
}
$claimMap = [];
$usedMap = [];
if ($ids) {
$stats = Db::name('user_coupon')
->where('coupon_id', 'in', $ids)
->field('coupon_id, COUNT(*) AS claim_cnt, SUM(CASE WHEN status = \'used\' THEN 1 ELSE 0 END) AS used_cnt')
->group('coupon_id')
->select();
foreach ($stats as $st) {
$cid = (int)$st['coupon_id'];
$claimMap[$cid] = (int)$st['claim_cnt'];
$usedMap[$cid] = (int)$st['used_cnt'];
}
}
foreach ($rows as &$item) {
$cid = (int)$item['id'];
$item['real_claimed'] = max((int)$item['received'], $claimMap[$cid] ?? 0);
$item['used_count'] = $usedMap[$cid] ?? 0;
}
unset($item);
return json(['total' => $list->total(), 'rows' => $rows]);
}
/**
* 优惠券领用与核销全景详情(关联 fa_user_coupon + fa_user + fa_order)
*/
public function detail($ids = null)
{
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
$cid = (int)$row['id'];
$totalClaimed = (int)Db::name('user_coupon')->where('coupon_id', $cid)->count();
$totalUsed = (int)Db::name('user_coupon')->where('coupon_id', $cid)->where('status', 'used')->count();
$records = Db::name('user_coupon')->alias('uc')
->join('__USER__ u', 'u.id = uc.user_id', 'LEFT')
->join('__ORDER__ o', 'o.id = uc.order_id', 'LEFT')
->where('uc.coupon_id', $cid)
->field('uc.*, u.nickname, u.username, u.mobile, o.order_no, o.service_name, o.pay_amount, o.discount_amount')
->order('uc.id desc')
->limit(30)
->select();
$drivenGmv = (float)Db::name('user_coupon')->alias('uc')
->join('__ORDER__ o', 'o.id = uc.order_id', 'INNER')
->where('uc.coupon_id', $cid)
->where('uc.status', 'used')
->sum('o.pay_amount');
$this->view->assign([
'row' => $row,
'totalClaimed' => max((int)$row['received'], $totalClaimed),
'totalUsed' => $totalUsed,
'drivenGmv' => round($drivenGmv, 2),
'records' => $records,
]);
return $this->view->fetch();
}
/**
* 新增卡券模板(强制初始化已领取数为 0)
*/
public function add()
{
if (false === $this->request->isPost()) {
return $this->view->fetch();
}
$params = $this->request->post('row/a', []);
if (!$params) {
$this->error(__('Parameter %s can not be empty', ''));
}
$params = $this->preExcludeFields($params);
$params['received'] = 0;
if (($params['type'] ?? 'cash') === 'discount') {
$disc = (float)($params['discount'] ?? 0);
if ($disc <= 0 || $disc >= 1) {
$this->error('折扣券的折扣系数须大于 0 且小于 1(如 0.85 表示 8.5 折)');
}
$params['amount'] = 0.00;
} else {
if ((float)($params['amount'] ?? 0) <= 0) {
$this->error('满减代金券的减免金额须大于 0');
}
$params['discount'] = 1.00;
}
$result = $this->model->allowField(true)->save($params);
if ($result !== false) {
$this->success();
}
$this->error($this->model->getError() ?: __('No rows were inserted'));
}
/**
* 编辑卡券模板(锁定已领取数量防止人工篡改库存)
*/
public function edit($ids = null)
{
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
if (false === $this->request->isPost()) {
$this->view->assign('row', $row);
return $this->view->fetch();
}
$params = $this->request->post('row/a', []);
if (!$params) {
$this->error(__('Parameter %s can not be empty', ''));
}
$params = $this->preExcludeFields($params);
unset($params['received']);
if (($params['type'] ?? $row['type']) === 'discount') {
$disc = (float)($params['discount'] ?? $row['discount']);
if ($disc <= 0 || $disc >= 1) {
$this->error('折扣券的折扣系数须大于 0 且小于 1(如 0.85 表示 8.5 折)');
}
$params['amount'] = 0.00;
} else {
if ((float)($params['amount'] ?? $row['amount']) <= 0) {
$this->error('满减代金券的减免金额须大于 0');
}
$params['discount'] = 1.00;
}
$result = $row->allowField(true)->save($params);
if ($result !== false) {
$this->success();
}
$this->error($row->getError() ?: __('No rows were updated'));
}
/**
* 删除保护:已有用户领取的卡券禁止物理删除
*/
public function del($ids = null)
{
if (false === $this->request->isPost()) {
$this->error(__('Invalid parameters'));
}
$ids = $ids ?: $this->request->post('ids');
$idArr = array_values(array_filter(array_map('intval', explode(',', (string)$ids))));
if (!$idArr) {
$this->error(__('Parameter %s can not be empty', 'ids'));
}
$claimedCnt = (int)Db::name('user_coupon')->where('coupon_id', 'in', $idArr)->count();
if ($claimedCnt > 0) {
$this->error('所选优惠券已有用户领取记录(共 ' . $claimedCnt . ' 张),禁止物理删除,请改为「隐藏」停止发放');
}
return parent::del($ids);
}
}