- 升级系统版本至1.0.100 - 新增订单详情页物流、资质等信息展示 - 优化师傅报价页收益计算与显示 - 新增企业资质管理模块 - 优化后台标签页管理与交互 - 修复标签解析与地址脱敏问题 - 优化预约时段选择逻辑 - 调整服务类型配置界面布局
593 行
26 KiB
PHP
593 行
26 KiB
PHP
<?php
|
|
|
|
namespace app\admin\controller;
|
|
|
|
use app\common\controller\Backend;
|
|
use app\common\library\InvoiceService;
|
|
use think\Db;
|
|
use Exception;
|
|
|
|
/**
|
|
* 合作商家与企业档案管理
|
|
*
|
|
* @icon fa fa-building-o
|
|
*/
|
|
class Merchant extends Backend
|
|
{
|
|
/**
|
|
* Merchant模型对象
|
|
* @var \app\admin\model\Merchant
|
|
*/
|
|
protected $model = null;
|
|
protected $relationSearch = true;
|
|
protected $searchFields = 'id,name,license_no,contact,mobile';
|
|
|
|
public function _initialize()
|
|
{
|
|
parent::_initialize();
|
|
$this->model = new \app\admin\model\Merchant;
|
|
$this->view->assign("settleTypeList", $this->model->getSettleTypeList());
|
|
$this->view->assign("verifyStatusList", $this->model->getVerifyStatusList());
|
|
}
|
|
|
|
/** 独立展示用户提交的企业资质;审核接口沿用原权限。 */
|
|
public function certificates()
|
|
{
|
|
$this->request->filter(['strip_tags', 'trim']);
|
|
$certStatusList = ['pending' => '待审核', 'pass' => '已通过', 'reject' => '已驳回'];
|
|
if (false === $this->request->isAjax()) {
|
|
$this->view->assign('certStatusList', $certStatusList);
|
|
return $this->view->fetch();
|
|
}
|
|
|
|
$offset = max(0, (int)$this->request->get('offset', 0));
|
|
$limit = min(100, max(1, (int)$this->request->get('limit', 20)));
|
|
$search = trim((string)$this->request->get('search', ''));
|
|
$sort = (string)$this->request->get('sort', 'id');
|
|
$order = strtolower((string)$this->request->get('order', 'desc')) === 'asc' ? 'asc' : 'desc';
|
|
|
|
$fieldMap = [
|
|
'id' => 'c.id',
|
|
'company_name' => 'c.company_name',
|
|
'license_no' => 'c.license_no',
|
|
'legal_person' => 'c.legal_person',
|
|
'verify_status' => 'c.verify_status',
|
|
'audit_msg' => 'c.audit_msg',
|
|
'createtime' => 'c.createtime',
|
|
'updatetime' => 'c.updatetime',
|
|
'nickname' => 'u.nickname',
|
|
'mobile' => 'u.mobile',
|
|
'user_type' => 'u.user_type',
|
|
];
|
|
$sortCol = $fieldMap[$sort] ?? 'c.id';
|
|
|
|
$filter = json_decode((string)$this->request->get('filter', '{}'), true);
|
|
$op = json_decode((string)$this->request->get('op', '{}'), true);
|
|
$filter = is_array($filter) ? $filter : [];
|
|
$op = is_array($op) ? $op : [];
|
|
|
|
$makeQuery = function () use ($search, $filter, $op, $fieldMap) {
|
|
$query = Db::name('user_enterprise_cert')->alias('c')->join('user u', 'u.id = c.user_id', 'LEFT');
|
|
if ($search !== '') {
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('c.company_name', 'like', '%' . $search . '%')
|
|
->whereOr('c.license_no', 'like', '%' . $search . '%')
|
|
->whereOr('c.legal_person', 'like', '%' . $search . '%')
|
|
->whereOr('u.nickname', 'like', '%' . $search . '%')
|
|
->whereOr('u.mobile', 'like', '%' . $search . '%');
|
|
});
|
|
}
|
|
foreach ($filter as $k => $v) {
|
|
if (!isset($fieldMap[$k]) || $v === '' || $v === null) {
|
|
continue;
|
|
}
|
|
$col = $fieldMap[$k];
|
|
$sym = strtoupper(trim((string)($op[$k] ?? '=')));
|
|
if ($sym === 'LIKE') {
|
|
$query->where($col, 'like', '%' . $v . '%');
|
|
} elseif ($sym === 'RANGE' && is_string($v) && strpos($v, ' - ') !== false) {
|
|
[$startStr, $endStr] = explode(' - ', $v, 2);
|
|
$start = strtotime(trim($startStr));
|
|
$end = strtotime(trim($endStr));
|
|
if ($start !== false && $end !== false) {
|
|
$query->where($col, 'between', [$start, $end]);
|
|
}
|
|
} else {
|
|
$query->where($col, '=', $v);
|
|
}
|
|
}
|
|
return $query;
|
|
};
|
|
|
|
$total = $makeQuery()->count();
|
|
$rows = $makeQuery()
|
|
->field('c.*, u.nickname, u.mobile, u.user_type')
|
|
->order($sortCol, $order)
|
|
->limit($offset, $limit)
|
|
->select();
|
|
|
|
foreach ($rows as &$row) {
|
|
$row['license_url'] = !empty($row['license_image']) ? cdnurl($row['license_image'], true) : '';
|
|
}
|
|
unset($row);
|
|
|
|
return json(['total' => $total, 'rows' => $rows]);
|
|
}
|
|
|
|
/**
|
|
* 企业资质详情与审核弹窗(待审核时可提交审核结论,已处理时展示只读审核结果)
|
|
*/
|
|
public function auditCert($ids = null)
|
|
{
|
|
$id = (int)$ids;
|
|
$row = Db::name('user_enterprise_cert')->alias('c')
|
|
->join('user u', 'u.id = c.user_id', 'LEFT')
|
|
->field('c.*, u.nickname, u.mobile, u.user_type')
|
|
->where('c.id', $id)
|
|
->find();
|
|
if (!$row) {
|
|
$this->error(__('No Results were found'));
|
|
}
|
|
|
|
$canAudit = $this->auth->check('merchant/edit') || $this->auth->check('merchant/auditcert');
|
|
|
|
if (false === $this->request->isPost()) {
|
|
$row['license_url'] = !empty($row['license_image']) ? cdnurl($row['license_image'], true) : '';
|
|
$this->view->assign('row', $row);
|
|
$this->view->assign('canAudit', $canAudit);
|
|
return $this->view->fetch('audit_cert');
|
|
}
|
|
|
|
if (!$canAudit) {
|
|
$this->error('无权审核企业资质');
|
|
}
|
|
|
|
$params = $this->request->post('row/a', []);
|
|
$decision = trim((string)($params['verify_status'] ?? ''));
|
|
$reason = trim((string)($params['audit_msg'] ?? ''));
|
|
|
|
if (!in_array($decision, ['pass', 'reject'], true)) {
|
|
$this->error('请选择审核结论(通过或驳回)');
|
|
}
|
|
if ($decision === 'reject' && $reason === '') {
|
|
$this->error('驳回申请时必须填写驳回原因');
|
|
}
|
|
if (mb_strlen($reason) > 500) {
|
|
$this->error('审核意见不能超过500字');
|
|
}
|
|
|
|
$updated = Db::name('user_enterprise_cert')
|
|
->where('id', $id)
|
|
->where('verify_status', 'pending')
|
|
->update([
|
|
'verify_status' => $decision,
|
|
'audit_msg' => $decision === 'reject' ? $reason : '',
|
|
'updatetime' => time(),
|
|
]);
|
|
if (!$updated) {
|
|
$this->error('申请不存在或已处理,请刷新');
|
|
}
|
|
$this->success('审核完成');
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$this->request->filter(['strip_tags', 'trim']);
|
|
if (false === $this->request->isAjax()) {
|
|
return $this->view->fetch();
|
|
}
|
|
if ($this->request->isPost() && $this->request->post('view') === 'audit_cert') {
|
|
if (!$this->auth->check('merchant/edit')) $this->error('无权审核企业资质');
|
|
$this->token();
|
|
$id = (int)$this->request->post('id');
|
|
$decision = (string)$this->request->post('decision', '');
|
|
$reason = trim((string)$this->request->post('audit_msg', ''));
|
|
if (!in_array($decision, ['pass', 'reject'], true)) $this->error('审核状态无效');
|
|
if ($decision === 'reject' && $reason === '') $this->error('请输入驳回原因');
|
|
if (mb_strlen($reason) > 500) $this->error('驳回原因过长');
|
|
$updated = Db::name('user_enterprise_cert')->where('id', $id)->where('verify_status', 'pending')->update([
|
|
'verify_status' => $decision, 'audit_msg' => $decision === 'reject' ? $reason : '', 'updatetime' => time(),
|
|
]);
|
|
if (!$updated) $this->error('申请不存在或已处理,请刷新');
|
|
$this->success('审核完成');
|
|
}
|
|
if ($this->request->get('view') === 'certs') {
|
|
$offset = max(0, (int)$this->request->get('offset', 0));
|
|
$limit = min(100, max(1, (int)$this->request->get('limit', 20)));
|
|
$search = trim((string)$this->request->get('search', ''));
|
|
$makeQuery = function () use ($search) {
|
|
$query = Db::name('user_enterprise_cert')->alias('c')->join('user u', 'u.id = c.user_id', 'LEFT');
|
|
if ($search !== '') $query->where(function ($q) use ($search) {
|
|
$q->where('c.company_name', 'like', '%' . $search . '%')->whereOr('c.license_no', 'like', '%' . $search . '%')->whereOr('u.mobile', 'like', '%' . $search . '%');
|
|
});
|
|
return $query;
|
|
};
|
|
$total = $makeQuery()->count();
|
|
$rows = $makeQuery()->field('c.*,u.nickname,u.mobile,u.user_type')->order('c.id', 'desc')->limit($offset, $limit)->select();
|
|
foreach ($rows as &$row) $row['license_url'] = cdnurl($row['license_image'], true);
|
|
unset($row);
|
|
return json(['total' => $total, 'rows' => $rows]);
|
|
}
|
|
if ($this->request->get('view') === 'accounts') {
|
|
$offset = max(0, (int)$this->request->get('offset', 0));
|
|
$limit = min(100, max(1, (int)$this->request->get('limit', 20)));
|
|
$search = trim((string)$this->request->get('search', ''));
|
|
$makeQuery = function () use ($search) {
|
|
$query = Db::name('user')->where('user_type', 'merchant')->where('identity_selected', 1);
|
|
if ($search !== '') $query->where(function ($q) use ($search) {
|
|
$q->where('nickname', 'like', '%' . $search . '%')
|
|
->whereOr('mobile', 'like', '%' . $search . '%')
|
|
->whereOr('company_name', 'like', '%' . $search . '%');
|
|
});
|
|
return $query;
|
|
};
|
|
$total = $makeQuery()->count();
|
|
$rows = $makeQuery()->field('id,nickname,mobile,company_name,company_legal_person,company_legal_mobile,company_tech_mobile,createtime')
|
|
->order('id', 'desc')->limit($offset, $limit)->select();
|
|
return json(['total' => $total, 'rows' => $rows]);
|
|
}
|
|
if ($this->request->request('keyField')) {
|
|
return $this->selectpage();
|
|
}
|
|
[$where, $sort, $order, $offset, $limit] = $this->buildparams();
|
|
$list = $this->model
|
|
->with(['user'])
|
|
->where($where)
|
|
->order($sort, $order)
|
|
->paginate($limit);
|
|
|
|
$rows = $list->items();
|
|
if (!empty($rows)) {
|
|
$mIds = array_column($rows, 'id');
|
|
|
|
// 1. 查询子账号归属映射
|
|
$members = Db::name('merchant_member')->where('merchant_id', 'in', $mIds)->select();
|
|
$mUidsMap = [];
|
|
$memberCntMap = [];
|
|
foreach ($rows as $r) {
|
|
$mid = (int)$r['id'];
|
|
$mUidsMap[$mid] = (int)$r['user_id'] > 0 ? [(int)$r['user_id']] : [];
|
|
$memberCntMap[$mid] = 0;
|
|
}
|
|
foreach ($members as $mb) {
|
|
$mid = (int)$mb['merchant_id'];
|
|
$uid = (int)$mb['user_id'];
|
|
$memberCntMap[$mid] = ($memberCntMap[$mid] ?? 0) + 1;
|
|
if ($uid > 0 && !in_array($uid, $mUidsMap[$mid], true)) {
|
|
$mUidsMap[$mid][] = $uid;
|
|
}
|
|
}
|
|
|
|
// 2. 汇总所有可能关联订单(通过 merchant_order 或商户主/子账号下单)
|
|
$allUids = [];
|
|
foreach ($mUidsMap as $uArr) {
|
|
foreach ($uArr as $u) {
|
|
$allUids[$u] = $u;
|
|
}
|
|
}
|
|
$moRows = Db::name('merchant_order')->where('merchant_id', 'in', $mIds)->select();
|
|
$moOrderIds = array_column($moRows, 'order_id');
|
|
$orderToMid = [];
|
|
foreach ($moRows as $mo) {
|
|
$orderToMid[(int)$mo['order_id']] = (int)$mo['merchant_id'];
|
|
}
|
|
|
|
$uidToMid = [];
|
|
foreach ($mUidsMap as $mid => $uArr) {
|
|
foreach ($uArr as $u) {
|
|
$uidToMid[$u] = $mid;
|
|
}
|
|
}
|
|
|
|
$orderQuery = Db::name('order')->field('id,user_id,pay_amount,pay_status,status');
|
|
if (!empty($allUids) && !empty($moOrderIds)) {
|
|
$orderQuery->where(function ($q) use ($allUids, $moOrderIds) {
|
|
$q->where('user_id', 'in', array_values($allUids))
|
|
->whereOr('id', 'in', $moOrderIds);
|
|
});
|
|
$relOrders = $orderQuery->select();
|
|
} elseif (!empty($allUids)) {
|
|
$relOrders = $orderQuery->where('user_id', 'in', array_values($allUids))->select();
|
|
} elseif (!empty($moOrderIds)) {
|
|
$relOrders = $orderQuery->where('id', 'in', $moOrderIds)->select();
|
|
} else {
|
|
$relOrders = [];
|
|
}
|
|
|
|
$statCnt = [];
|
|
$statAmt = [];
|
|
foreach ($relOrders as $ord) {
|
|
$oid = (int)$ord['id'];
|
|
$mid = $orderToMid[$oid] ?? ($uidToMid[(int)$ord['user_id']] ?? 0);
|
|
if ($mid <= 0) continue;
|
|
$statCnt[$mid] = ($statCnt[$mid] ?? 0) + 1;
|
|
if ((int)$ord['pay_status'] === 1 && (int)$ord['status'] !== 9) {
|
|
$statAmt[$mid] = round(($statAmt[$mid] ?? 0) + (float)$ord['pay_amount'], 2);
|
|
}
|
|
}
|
|
|
|
foreach ($rows as &$r) {
|
|
$mid = (int)$r['id'];
|
|
$r['order_count'] = $statCnt[$mid] ?? (int)$r['order_count'];
|
|
$r['total_amount'] = isset($statAmt[$mid]) ? number_format($statAmt[$mid], 2, '.', '') : number_format((float)$r['total_amount'], 2, '.', '');
|
|
$r['member_count'] = $memberCntMap[$mid] ?? 0;
|
|
}
|
|
unset($r);
|
|
}
|
|
|
|
return json(['total' => $list->total(), 'rows' => $rows]);
|
|
}
|
|
|
|
/**
|
|
* 新增合作企业档案(防重复绑定同一用户,审核通过自动同步默认企业发票抬头)
|
|
*/
|
|
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);
|
|
$uid = (int)($params['user_id'] ?? 0);
|
|
if ($uid <= 0) {
|
|
$this->error('请选择关联的平台注册用户账号');
|
|
}
|
|
if (Db::name('merchant')->where('user_id', $uid)->count() > 0) {
|
|
$this->error('该用户账号已绑定企业商家主体,请勿重复创建');
|
|
}
|
|
// 统一社会信用代码唯一性校验,防止同一企业重复建档(兼容历史数据大小写差异)
|
|
$licenseNo = strtoupper(trim((string)($params['license_no'] ?? '')));
|
|
$licenseRaw = trim((string)($params['license_no'] ?? ''));
|
|
if ($licenseNo !== '') {
|
|
$dupLicense = Db::name('merchant')
|
|
->where(function ($q) use ($licenseNo, $licenseRaw) {
|
|
$q->where('license_no', $licenseNo)->whereOr('license_no', $licenseRaw);
|
|
})
|
|
->order('id', 'asc')
|
|
->find();
|
|
if ($dupLicense) {
|
|
$this->error('统一社会信用代码已绑定企业【' . $dupLicense['name'] . '】,同一企业请勿重复建档');
|
|
}
|
|
}
|
|
if ($licenseNo !== '') {
|
|
$params['license_no'] = $licenseNo;
|
|
}
|
|
$params['order_count'] = 0;
|
|
$params['total_amount'] = 0.00;
|
|
if (($params['verify_status'] ?? '') === 'reject' && empty(trim((string)($params['audit_msg'] ?? '')))) {
|
|
$this->error('驳回企业入驻时请填写审核驳回原因');
|
|
}
|
|
|
|
$result = false;
|
|
Db::startTrans();
|
|
try {
|
|
$result = $this->model->allowField(true)->save($params);
|
|
if (($params['verify_status'] ?? '') === 'pass' && $uid > 0) {
|
|
$ctx = InvoiceService::resolveContext($uid);
|
|
InvoiceService::ensureMerchantDefaultTitle($ctx);
|
|
}
|
|
Db::commit();
|
|
} catch (Exception $e) {
|
|
Db::rollback();
|
|
$this->error($e->getMessage());
|
|
}
|
|
if ($result !== false) {
|
|
$this->success();
|
|
}
|
|
$this->error($this->model->getError() ?: __('No rows were inserted'));
|
|
}
|
|
|
|
/**
|
|
* 删除保护:已产生企业批次订单或开票记录的商户禁止物理删除
|
|
*/
|
|
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'));
|
|
}
|
|
$hasOrder = Db::name('merchant_order')->where('merchant_id', 'in', $idArr)->count();
|
|
$hasInvoice = Db::name('invoice')->where('merchant_id', 'in', $idArr)->count();
|
|
if ($hasOrder > 0 || $hasInvoice > 0) {
|
|
$this->error('所选企业已关联历史发包订单或开票记录,禁止物理删除');
|
|
}
|
|
Db::startTrans();
|
|
try {
|
|
Db::name('merchant_member')->where('merchant_id', 'in', $idArr)->delete();
|
|
$deleted = Db::name('merchant')->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'));
|
|
}
|
|
|
|
/**
|
|
* 编辑企业档案与入驻审核(审核通过自动同步企业默认发票抬头并发送通知)
|
|
*/
|
|
public function edit($ids = null)
|
|
{
|
|
$row = $this->model->get($ids);
|
|
if (!$row) {
|
|
$this->error(__('No Results were found'));
|
|
}
|
|
$oldVerify = (string)$row['verify_status'];
|
|
|
|
if ($this->request->isPost()) {
|
|
$params = $this->request->post("row/a");
|
|
if ($params) {
|
|
$params = $this->preExcludeFields($params);
|
|
unset($params['order_count'], $params['total_amount']);
|
|
if (isset($params['city']) && trim((string)$params['city']) === '' && !empty($row['city'])) {
|
|
unset($params['city']);
|
|
}
|
|
if (isset($params['verify_status']) && $params['verify_status'] === 'reject' && empty(trim((string)($params['audit_msg'] ?? '')))) {
|
|
$this->error('驳回企业入驻时请填写审核驳回原因');
|
|
}
|
|
// 统一社会信用代码唯一性校验(排除自身,兼容历史数据大小写差异)
|
|
if (array_key_exists('license_no', $params)) {
|
|
$licenseNo = strtoupper(trim((string)$params['license_no']));
|
|
$licenseRaw = trim((string)$params['license_no']);
|
|
if ($licenseNo !== '') {
|
|
$dupLicense = Db::name('merchant')
|
|
->where('id', '<>', (int)$row['id'])
|
|
->where(function ($q) use ($licenseNo, $licenseRaw) {
|
|
$q->where('license_no', $licenseNo)->whereOr('license_no', $licenseRaw);
|
|
})
|
|
->order('id', 'asc')
|
|
->find();
|
|
if ($dupLicense) {
|
|
$this->error('统一社会信用代码已绑定企业【' . $dupLicense['name'] . '】,同一企业请勿重复建档');
|
|
}
|
|
$params['license_no'] = $licenseNo;
|
|
}
|
|
}
|
|
$result = false;
|
|
Db::startTrans();
|
|
try {
|
|
$result = $row->allowField(true)->save($params);
|
|
$newVerify = isset($params['verify_status']) ? (string)$params['verify_status'] : $oldVerify;
|
|
if ($newVerify !== $oldVerify && in_array($newVerify, ['pass', 'reject'], true)) {
|
|
if ($newVerify === 'pass' && (int)$row['user_id'] > 0) {
|
|
$ctx = InvoiceService::resolveContext((int)$row['user_id']);
|
|
InvoiceService::ensureMerchantDefaultTitle($ctx);
|
|
}
|
|
if ((int)$row['user_id'] > 0) {
|
|
Db::name('message')->insert([
|
|
'receive_type' => 'user',
|
|
'receive_id' => (int)$row['user_id'],
|
|
'type' => 'audit',
|
|
'title' => $newVerify === 'pass' ? '企业商家认证已通过' : '企业商家认证未通过',
|
|
'content' => $newVerify === 'pass'
|
|
? ('您提交的企业【' . $row['name'] . '】资质审核已通过,现可使用商家工作台批量派单与开票服务。')
|
|
: ('企业【' . $row['name'] . '】资质审核未通过,原因:' . (trim((string)($params['audit_msg'] ?? '')) ?: '请核对营业执照信息后重新提交')),
|
|
'order_id' => 0,
|
|
'is_read' => 0,
|
|
'createtime' => time(),
|
|
]);
|
|
}
|
|
}
|
|
Db::commit();
|
|
} catch (Exception $e) {
|
|
Db::rollback();
|
|
$this->error($e->getMessage());
|
|
}
|
|
if ($result !== false) {
|
|
$this->success();
|
|
}
|
|
$this->error(__('No rows were updated'));
|
|
}
|
|
$this->error(__('Parameter %s can not be empty', ''));
|
|
}
|
|
|
|
$this->view->assign("row", $row);
|
|
return $this->view->fetch();
|
|
}
|
|
|
|
/**
|
|
* 商户企业全景资质与业务协同看板
|
|
*/
|
|
public function detail($ids = null)
|
|
{
|
|
$row = $this->model->with(['user'])->where('merchant.id', (int)$ids)->find();
|
|
if (!$row) {
|
|
$this->error(__('No Results were found'));
|
|
}
|
|
|
|
$mid = (int)$row['id'];
|
|
$uids = (int)$row['user_id'] > 0 ? [(int)$row['user_id']] : [];
|
|
|
|
// 1. 企业员工协同子账号
|
|
$members = Db::name('merchant_member')
|
|
->alias('mm')
|
|
->join('user u', 'u.id = mm.user_id', 'LEFT')
|
|
->where('mm.merchant_id', $mid)
|
|
->field('mm.*, u.nickname, u.username, u.mobile')
|
|
->order('mm.id desc')
|
|
->select();
|
|
foreach ($members as $mb) {
|
|
if ((int)$mb['user_id'] > 0 && !in_array((int)$mb['user_id'], $uids, true)) {
|
|
$uids[] = (int)$mb['user_id'];
|
|
}
|
|
}
|
|
|
|
// 2. 商户关联工单统计与最近列表(兼容 merchant_order 与账号下单)
|
|
$moOrderIds = Db::name('merchant_order')->where('merchant_id', $mid)->column('order_id');
|
|
$buildOrderScope = function ($q) use ($uids, $moOrderIds) {
|
|
if (!empty($uids) && !empty($moOrderIds)) {
|
|
$q->where('o.user_id', 'in', $uids)->whereOr('o.id', 'in', $moOrderIds);
|
|
} elseif (!empty($uids)) {
|
|
$q->where('o.user_id', 'in', $uids);
|
|
} elseif (!empty($moOrderIds)) {
|
|
$q->where('o.id', 'in', $moOrderIds);
|
|
} else {
|
|
$q->where('o.id', 0);
|
|
}
|
|
};
|
|
|
|
$orderAgg = Db::name('order')->alias('o')
|
|
->field('COUNT(*) AS total_cnt, SUM(CASE WHEN o.pay_status = 1 AND o.status <> 9 THEN o.pay_amount ELSE 0 END) AS total_gmv')
|
|
->where($buildOrderScope)
|
|
->find();
|
|
|
|
$statusMap = (new \app\admin\model\Order)->getStatusList();
|
|
$recentOrders = Db::name('order')->alias('o')
|
|
->join('master m', 'm.id = o.master_id', 'LEFT')
|
|
->join('merchant_order mo', 'mo.order_id = o.id', 'LEFT')
|
|
->where($buildOrderScope)
|
|
->field('o.*, m.realname as master_name, mo.batch_no')
|
|
->order('o.id desc')
|
|
->limit(8)
|
|
->select();
|
|
foreach ($recentOrders as &$o) {
|
|
$o['status_label'] = $statusMap[(int)$o['status']] ?? ('状态' . $o['status']);
|
|
}
|
|
unset($o);
|
|
|
|
// 3. 关联发票抬头与历史开票记录
|
|
$titleList = Db::name('invoice_title')
|
|
->where(function ($q) use ($mid, $uids) {
|
|
$q->where('merchant_id', $mid);
|
|
if (!empty($uids)) {
|
|
$q->whereOr('user_id', 'in', $uids);
|
|
}
|
|
})
|
|
->order('is_default desc, id desc')
|
|
->select();
|
|
|
|
$invoiceList = Db::name('invoice')
|
|
->where(function ($q) use ($mid, $uids) {
|
|
$q->where('merchant_id', $mid);
|
|
if (!empty($uids)) {
|
|
$q->whereOr('user_id', 'in', $uids);
|
|
}
|
|
})
|
|
->order('id desc')
|
|
->limit(6)
|
|
->select();
|
|
|
|
$this->view->assign([
|
|
'row' => $row,
|
|
'members' => $members,
|
|
'recentOrders' => $recentOrders,
|
|
'titleList' => $titleList,
|
|
'invoiceList' => $invoiceList,
|
|
'orderCount' => (int)($orderAgg['total_cnt'] ?? 0),
|
|
'orderGmv' => number_format((float)($orderAgg['total_gmv'] ?? 0), 2, '.', ''),
|
|
]);
|
|
|
|
return $this->view->fetch();
|
|
}
|
|
}
|