model = new \app\admin\model\UserCoupon; $this->view->assign('statusList', $this->model->getStatusList()); } /** * 领券记录列表(支持按状态 unused/used/expired 筛选及关联会员/优惠券/核销订单检索) */ 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(); } // 自动将已超过优惠券截止时间的未使用领券记录同步为 expired $now = time(); $expiredCouponIds = Db::name('coupon') ->where('end_time', '>', 0) ->where('end_time', '<', $now) ->column('id'); if (!empty($expiredCouponIds)) { Db::name('user_coupon') ->where('status', 'unused') ->where('coupon_id', 'in', $expiredCouponIds) ->update(['status' => 'expired']); } [$where, $sort, $order, $offset, $limit] = $this->buildparams(); $list = $this->model ->with(['user', 'coupon', 'order']) ->where($where) ->order($sort, $order) ->paginate($limit); return json(['total' => $list->total(), 'rows' => $list->items()]); } /** * 禁止后台空造领券流水 */ public function add() { $this->error('用户领券记录由客户端领券中心真实发放生成,不支持后台手工伪造'); } /** * 删除未使用或已过期的无效领券记录(已核销关联订单的记录受账务保护禁止删除) */ 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')); } $usedCnt = Db::name('user_coupon')->where('id', 'in', $idArr)->where('status', 'used')->count(); if ($usedCnt > 0) { $this->error('所选记录中包含已核销抵扣订单的优惠券,为保障账务闭环禁止删除'); } Db::startTrans(); try { $deleted = Db::name('user_coupon')->where('id', 'in', $idArr)->delete(); Db::commit(); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($deleted) { $this->success(); } $this->error(__('No rows were deleted')); } }