model = new VoucherPlan(); } public function index() { if (!$this->request->isAjax()) { return $this->view->fetch(); } $list = $this->model->sheepFilter()->with(['user', 'order'])->order('id', 'desc')->paginate($this->request->param('list_rows', 10)); $userIds = []; foreach ($list as $row) { $userIds[] = intval($row->user_id); } $agents = []; if ($userIds) { $rows = \app\admin\model\shopro\commission\Agent::whereIn('user_id', $userIds) ->field('user_id,voucher_used,voucher_released') ->select(); foreach ($rows as $agent) { $agents[$agent['user_id']] = $agent; } } foreach ($list as $row) { $agent = $agents[$row->user_id] ?? []; $used = (float) ($agent['voucher_used'] ?? 0); $released = (float) ($agent['voucher_released'] ?? 0); $row->voucher_used = number_format($used, 2, '.', ''); $row->voucher_available = number_format(max(0, $released - $used), 2, '.', ''); } $this->success('获取成功', null, $list); } public function close() { $id = $this->request->param('id'); $plan = $this->model->where('id', $id)->find(); if (!$plan) { $this->error('计划不存在'); } (new VoucherService())->closeByOrder($plan->order_id); $this->success('已关闭'); } /** * 按日释放日志 */ public function logs() { $id = intval($this->request->param('id')); $plan = $this->model->where('id', $id)->find(); if (!$plan) { $this->error('计划不存在'); } $list = \app\admin\model\shopro\partner\Reward::where('user_id', $plan->user_id) ->where('reward_type', 'voucher_release') ->order('settle_date', 'desc') ->order('id', 'desc') ->limit(200) ->select(); $rows = []; foreach ($list as $item) { $ext = is_array($item->ext) ? $item->ext : (json_decode($item->ext ?: '{}', true) ?: []); $planIds = $ext['plan_ids'] ?? []; if (!$planIds && !empty($ext['plan_id'])) { $planIds = [$ext['plan_id']]; } $planIds = array_map('intval', (array) $planIds); if ($planIds && !in_array(intval($plan->id), $planIds, true)) { continue; } $rows[] = [ 'id' => $item->id, 'settle_date' => $item->settle_date, 'amount' => $item->amount, 'status_text' => $item->status_text, 'formula' => $item->formula, 'createtime' => $item->createtime, ]; } $this->success('ok', null, ['plan' => $plan, 'logs' => $rows]); } }