'省级', 2 => '市级', 3 => '区县']; public function _initialize() { parent::_initialize(); $this->model = new \app\admin\model\shopro\regional_agent\Reward; } public function index() { $this->request->filter(['strip_tags']); if ($this->request->isAjax()) { $nobuildfields = ['agent_nickname', 'order_sn', 'createtime', 'status']; list($where, $sort, $order, $offset, $limit) = $this->custombuildparams(null, $nobuildfields); $query = $this->buildSearch(); $total = $query->where($where)->count(); $list = $this->buildSearch() ->with($this->getRewardWith()) ->where($where) ->order($sort, $order) ->limit($offset, $limit) ->select(); $list = $this->formatRewardList($list); $summary = [ 'commission_total' => number_format($this->buildSearch()->where($where)->sum('commission'), 2, '.', ''), 'commission_waiting' => number_format($this->buildSearch()->where($where)->where('status', 0)->sum('commission'), 2, '.', ''), 'commission_settled' => number_format($this->buildSearch()->where($where)->where('status', 1)->sum('commission'), 2, '.', ''), ]; return $this->success('差价明细', null, [ 'total' => $total, 'rows' => $list, 'summary' => $summary, ]); } return $this->view->fetch(); } public function export() { $nobuildfields = ['agent_nickname', 'order_sn', 'createtime', 'status']; list($where, $sort, $order) = $this->custombuildparams(null, $nobuildfields); $expCellName = [ 'id' => 'ID', 'createtime_text' => '产生时间', 'source_order_sn' => '订单号', 'source_goods_title' => '商品', 'source_buyer_name' => '买家', 'source_area_text' => '公司区域', 'agent_nickname' => '公司用户', 'commission' => '差价(元)', 'commission_rule_text' => '差价规则', 'status_text' => '状态', 'settle_time_text' => '入账时间', ]; $list = $this->buildSearch() ->with($this->getRewardWith()) ->where($where) ->order($sort, $order) ->limit(5000) ->select(); if (!$list) { $this->error('导出数据为空'); } $list = $this->formatRewardList($list); $exportData = []; foreach ($list as $row) { $exportData[] = [ 'id' => $row['id'], 'createtime_text' => $row['createtime_text'], 'source_order_sn' => $row['source_order_sn'], 'source_goods_title' => $row['source_goods_title'], 'source_buyer_name' => $row['source_buyer_name'], 'source_area_text' => $row['source_area_text'], 'agent_nickname' => $row['agent_nickname'], 'commission' => $row['commission'], 'commission_rule_text' => $row['commission_rule_text'], 'status_text' => $row['status_text'], 'settle_time_text' => $row['settle_time_text'], ]; } $export = new Export(); $export->exportExcel('区域公司差价明细', $expCellName, $exportData); } public function runCommission($id = null) { if (!$id) { $id = $this->request->post('id'); } $reward = $this->model->get($id); if (!$reward) { $this->error('记录不存在'); } $result = Db::transaction(function () use ($reward) { return (new \addons\shopro\library\regional_agent\Reward())->settleReward($reward, 'admin'); }); $result ? $this->success('结算成功') : $this->error('结算失败'); } public function backCommission($id = null) { if (!$id) { $id = $this->request->post('id'); } $reward = $this->model->get($id); if (!$reward) { $this->error('记录不存在'); } $result = Db::transaction(function () use ($reward) { return (new \addons\shopro\library\regional_agent\Reward())->backReward($reward); }); $result ? $this->success('退回成功') : $this->error('退回失败'); } protected function getRewardWith() { return [ 'agentUser' => function ($q) { $q->field('id,nickname,mobile,avatar'); }, 'buyer' => function ($q) { $q->field('id,nickname,mobile'); }, 'order' => function ($q) { $q->field('id,order_sn,user_id,paytime,createtime,status'); }, 'orderItem' => function ($q) { $q->field('id,order_id,goods_id,goods_title,goods_image,pay_price,goods_num'); }, 'agent' => function ($q) { $q->field('id,user_id,area_id,area_level,area_name,level,commission_type,commission_rate,commission_money'); }, 'regionalOrder' => function ($q) { $q->field('id,order_id,order_item_id,buyer_id,agent_id,area_id,area_level,amount,commission_event,commission_time,createtime'); }, ]; } protected function formatRewardList($list) { $statusMap = [0 => '待入账', 1 => '已入账', -1 => '已退回']; $levelMap = $this->getRegionalLevelMap(); foreach ($list as &$row) { if (!is_array($row)) { $row = array_merge($row->getData(), [ 'order' => $row->order ? $row->order->getData() : [], 'buyer' => $row->buyer ? $row->buyer->getData() : [], 'order_item' => $row->order_item ? $row->order_item->getData() : ($row->orderItem ? $row->orderItem->getData() : []), 'agent' => $row->agent ? $row->agent->getData() : [], 'regional_order' => $row->regional_order ? $row->regional_order->getData() : ($row->regionalOrder ? $row->regionalOrder->getData() : []), 'agent_user' => $row->agent_user ? $row->agent_user->getData() : ($row->agentUser ? $row->agentUser->getData() : []), ]); } $order = $row['order'] ?? []; $buyer = $row['buyer'] ?? []; $orderItem = $row['order_item'] ?? []; $agent = $row['agent'] ?? []; $regionalOrder = $row['regional_order'] ?? []; $agentUser = $row['agent_user'] ?? []; $areaLevel = intval($regionalOrder['area_level'] ?? $agent['area_level'] ?? 0); $areaLevelText = $this->areaLevelMap[$areaLevel] ?? ''; $areaName = $agent['area_name'] ?? ''; $orderSn = $order['order_sn'] ?? ''; if (!$orderSn && !empty($row['order_id'])) { $orderSn = '订单#' . $row['order_id']; } $row['createtime_text'] = !empty($row['createtime']) ? date('Y-m-d H:i:s', $row['createtime']) : '-'; $row['settle_time_text'] = (intval($row['status']) === 1 && !empty($row['updatetime'])) ? date('Y-m-d H:i:s', $row['updatetime']) : '-'; $row['status_text'] = $statusMap[intval($row['status'])] ?? '-'; $row['source_order_sn'] = $orderSn ?: '-'; $row['source_goods_title'] = $orderItem['goods_title'] ?? '-'; $row['source_buyer_name'] = $buyer['nickname'] ?? '-'; $row['source_buyer_mobile'] = $buyer['mobile'] ?? ''; $row['source_area_text'] = ($areaLevelText ? $areaLevelText . ' · ' : '') . ($areaName ?: '-'); $row['source_amount'] = $regionalOrder['amount'] ?? ($orderItem['pay_price'] ?? ''); $row['source_event'] = $this->getCommissionEventText($regionalOrder['commission_event'] ?? ''); $row['source_text'] = trim(sprintf( '%s · %s · 买家 %s', $orderSn ?: ('订单#' . ($row['order_id'] ?? '-')), $orderItem['goods_title'] ?? '商品', $buyer['nickname'] ?? '-' ), ' ·'); $row['agent_nickname'] = $agentUser['nickname'] ?? '-'; $row['commission_rule_text'] = $this->formatCommissionRuleText($agent, $levelMap); } unset($row); return $list; } protected function getRegionalLevelMap() { static $map = null; if ($map !== null) { return $map; } $map = []; $rows = \app\admin\model\shopro\regional_agent\Level::order('level asc')->select(); foreach ($rows as $level) { $data = is_array($level) ? $level : $level->getData(); $map[intval($data['level'])] = $data; } return $map; } protected function formatCommissionRuleText($agent, $levelMap) { if (!empty($agent['commission_type'])) { if ($agent['commission_type'] === 'rate') { return number_format(floatval($agent['commission_rate']), 2, '.', '') . '%(单独配置)'; } return number_format(floatval($agent['commission_money']), 2, '.', '') . '元/单(单独配置)'; } $level = $levelMap[intval($agent['level'] ?? 0)] ?? null; if (!$level) { return '-'; } if (($level['commission_type'] ?? 'rate') === 'rate') { return number_format(floatval($level['commission_rate']), 2, '.', '') . '%(等级通用)'; } return number_format(floatval($level['commission_money']), 2, '.', '') . '元/单(等级通用)'; } protected function getCommissionEventText($event) { $map = [ 'payed' => '支付后结算', 'confirm' => '确认收货后结算', 'finish' => '订单完成后结算', ]; return $map[$event] ?? ($event ?: '订单分佣'); } protected function buildSearch() { $filter = (array) json_decode($this->request->get('filter', ''), true); $query = $this->model; if (!empty($filter['agent_nickname'])) { $userIds = \app\admin\model\shopro\user\User::where('nickname', 'like', '%' . $filter['agent_nickname'] . '%')->column('id'); $query = $query->where('agent_user_id', 'in', $userIds ?: [0]); } if (!empty($filter['order_sn'])) { $orderIds = \app\admin\model\shopro\order\Order::where('order_sn', 'like', '%' . $filter['order_sn'] . '%')->column('id'); $query = $query->where('order_id', 'in', $orderIds ?: [0]); } if (isset($filter['status']) && $filter['status'] !== '') { $query = $query->where('status', intval($filter['status'])); } if (!empty($filter['createtime'])) { $parts = explode(' - ', $filter['createtime']); if (count($parts) === 2) { $start = strtotime($parts[0]); $end = strtotime($parts[1] . ' 23:59:59'); if ($start && $end) { $query->where('createtime', 'between', [$start, $end]); } } } return $query; } }