model = new RewardModel(); } public function index() { if (!$this->request->isAjax()) { $exportConfig = (new \addons\shopro\library\Export())->getConfig(); $this->assignconfig("save_type", $exportConfig['save_type'] ?? 'download'); $this->assignconfig("rewardTypeMap", PartnerConfig::rewardTypeMap()); return $this->view->fetch(); } $showSkip = $this->request->param('show_skip', 0); $query = $this->model->sheepFilter()->with(['buyer', 'user', 'order']); if (!$showSkip) { $query->where(function ($q) { $q->where('amount', '>', 0)->whereOr('madow', '>', 0); }); } $list = $query->paginate($this->request->param('list_rows', 10)); $this->success('获取成功', null, $list); } public function export() { $cellTitles = [ 'id' => 'Id', 'createtime' => '入账时间', 'user_nickname' => '受益人', 'buyer_nickname' => '买家', 'order_id' => '订单', 'reward_type_text' => '奖项', 'role_text' => '角色', 'amount' => '金额', 'madow' => '马豆', 'base_amount' => '基数', 'status_text' => '状态', 'formula' => '公式', ]; $total = $this->model->sheepFilter()->count(); if ($total <= 0) { $this->error('导出数据为空'); } $export = new \addons\shopro\library\Export(); $params = [ 'file_name' => '合伙股东收益流水', 'cell_titles' => $cellTitles, 'total' => $total, 'is_sub_cell' => false, ]; $totalAmount = 0; $totalMadow = 0; $result = $export->export($params, function ($pages) use (&$totalAmount, &$totalMadow, $total) { $datas = $this->model->sheepFilter()->with(['buyer', 'user', 'order']) ->limit((($pages['page'] - 1) * $pages['list_rows']), $pages['list_rows']) ->select(); $newDatas = []; foreach ($datas as $reward) { $newDatas[] = [ 'id' => $reward['id'], 'createtime' => $reward['createtime'], 'user_nickname' => $reward['user']['nickname'] ?? '-', 'buyer_nickname' => $reward['buyer']['nickname'] ?? '-', 'order_id' => $reward['order']['order_sn'] ?? $reward['order_id'], 'reward_type_text' => $reward['reward_type_text'], 'role_text' => $reward['role_text'] ?? $reward['role'], 'amount' => $reward['amount'], 'madow' => $reward['madow'], 'base_amount' => $reward['base_amount'], 'status_text' => $reward['status_text'], 'formula' => $reward['formula'], ]; } $totalAmount += array_sum(array_column($newDatas, 'amount')); $totalMadow += array_sum(array_column($newDatas, 'madow')); if ($pages['is_last_page']) { $newDatas[] = ['id' => "总数:{$total};金额:{$totalAmount};马豆:{$totalMadow}"]; } return $newDatas; }); $this->success('导出成功', null, $result); } public function reissue() { $params = $this->request->only(['user_id', 'reward_type', 'amount', 'order_id', 'memo']); if (empty($params['user_id']) || empty($params['reward_type']) || empty($params['memo'])) { $this->error('用户、奖项、备注必填'); } $amount = round((float) ($params['amount'] ?? 0), 2); if ($amount == 0) { $this->error('请输入金额'); } Db::transaction(function () use ($params, $amount) { $row = PartnerRewardService::write([ 'order_id' => intval($params['order_id'] ?? 0) ?: -time(), 'user_id' => intval($params['user_id']), 'buyer_id' => 0, 'role' => 'system', 'reward_type' => 'manual_' . $params['reward_type'], 'amount' => $amount, 'base_amount' => $amount, 'ext' => ['formula' => '手工补发', 'memo' => $params['memo'], 'manual' => 1], 'status' => 1, ]); Wallet::change($params['user_id'], 'commission', $amount, 'reward_income', [ 'partner_reward_id' => $row->id ?? 0, 'manual' => 1, ], $params['memo']); }); $this->success('补发成功'); } public function back() { $id = $this->request->param('id'); $memo = trim((string) $this->request->param('memo', '')); if ($memo === '') { $this->error('请填写退回原因'); } $row = $this->model->where('id', $id)->find(); if (!$row) { $this->error('记录不存在'); } if ($row->status != 1 || $row->settle_date) { $this->error('仅已入账的即时奖可退回'); } Db::transaction(function () use ($row, $memo) { if ((float) $row->amount > 0 && $row->user_id > 0) { Wallet::change($row->user_id, 'commission', -$row->amount, 'reward_back', [ 'partner_reward_id' => $row->id, ], $memo); } $ext = is_array($row->ext) ? $row->ext : (json_decode($row->ext ?: '{}', true) ?: []); $ext['back_memo'] = $memo; $row->ext = $ext; $row->status = -2; $row->save(); }); $this->success('已退回'); } public function backOrder() { $orderId = intval($this->request->param('order_id')); if ($orderId <= 0) { $this->error('请选择订单'); } (new PartnerRewardService())->refund($orderId); $this->success('整单已退回'); } }