完成了基本调整调整

This commit is contained in:
2026-09-18 20:55:21 +08:00
parent 864187e105
commit 57e0854a0e
56 changed files with 3122 additions and 105 deletions
@@ -0,0 +1,195 @@
<?php
// +----------------------------------------------------------------------
// | 后台 · 企油通司机管理
// +----------------------------------------------------------------------
// | list() 司机档案 + 从订单实时汇总的累计发放 / 已退款 / 订单数
// | detail() 档案、涉及企业、最近订单(发放失败原因里能看到二要素核验结果)
// | upstream() 实时查企油通:balance 7.12 钱包余额 / wallet 7.13 钱包流水 / voucher 7.17 支付凭证
// |
// | 身份证号全程只在后端使用,返回前端一律打码。
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\store\controller\qyt;
use app\common\model\qyt\Enterprise as EnterpriseModel;
use app\common\service\QytOil;
use app\common\service\QytRefund;
use app\store\controller\Controller;
use think\facade\Db;
use think\response\Json;
class Driver extends Controller
{
public function list(): Json
{
$param = $this->request->param();
$keyword = trim((string) ($param['keyword'] ?? ''));
$query = Db::name('qyt_driver')->where('store_id', $this->storeId);
if ($keyword !== '') {
if (preg_match('/^\d{17}[\dXx]$/', $keyword)) {
$query->where('id_card', strtoupper($keyword));
} elseif (preg_match('/^1\d{10}$/', $keyword)) {
$query->where('mobile', $keyword);
} else {
$query->whereLike('name', '%' . $keyword . '%');
}
}
if (isset($param['conflict']) && $param['conflict'] !== '' && (int) $param['conflict'] >= 0) {
$query->where('conflict', (int) $param['conflict']);
}
$list = $query->order('last_order_time', 'desc')->paginate(15)->toArray();
$stats = $this->stats(array_column($list['data'], 'id_card'));
foreach ($list['data'] as &$row) {
$row = $this->formatDriver($row, $stats[$row['id_card']] ?? []);
}
return $this->renderSuccess(compact('list'));
}
public function detail(int $driverId): Json
{
$driver = $this->findDriver($driverId);
if (!$driver) {
return $this->renderError('司机不存在');
}
$stats = $this->stats([$driver['id_card']]);
// 涉及过的企业(上游查询要按企业查)
$entIds = Db::name('partners_order')->where('store_id', $this->storeId)
->where('id_card', $driver['id_card'])->where('qyt_enterprise_id', '>', 0)
->group('qyt_enterprise_id')->column('qyt_enterprise_id');
$enterprises = $entIds
? EnterpriseModel::whereIn('enterprise_id', $entIds)
->field('enterprise_id,company_name,passageway,status')->select()->toArray()
: [];
$orders = Db::name('partners_order')->where('store_id', $this->storeId)->where('id_card', $driver['id_card'])
->field('order_id,oilCardNo,partners_order_no,total_price,driver_name,mobile,oil_status_zsy,oil_text,use_status,refund_status,refund_text,qyt_enterprise_id,create_time')
->order('order_id', 'desc')->limit(20)->select()->toArray();
foreach ($orders as &$o) {
$o['mobile'] = QytOil::maskMobile((string) $o['mobile']);
$o['refund_status_text'] = QytRefund::STATUS_TEXT[(int) $o['refund_status']] ?? '';
$o['create_time_text'] = date('Y-m-d H:i:s', (int) $o['create_time']);
}
return $this->renderSuccess([
'detail' => $this->formatDriver($driver, $stats[$driver['id_card']] ?? []),
'enterprises' => $enterprises,
'orders' => $orders,
]);
}
/**
* 实时查企油通
* type: balance 7.12 / wallet 7.13 / voucher 7.17
*/
public function upstream(int $driverId, int $enterpriseId, string $type = 'balance', int $page = 1): Json
{
$driver = $this->findDriver($driverId);
if (!$driver) {
return $this->renderError('司机不存在');
}
$enterprise = EnterpriseModel::detail($enterpriseId);
if (!$enterprise) {
return $this->renderError('企业不存在');
}
$qyt = QytOil::forEnterprise($enterprise);
$mobile = (string) $driver['mobile'];
$idCard = (string) $driver['id_card'];
$page = max(1, $page);
switch ($type) {
case 'balance':
$res = $qyt->driverBalanceQuery($mobile, $idCard, $page, 20);
break;
case 'wallet':
$res = $qyt->driverWalletQuery($mobile, $idCard, $page, 20);
break;
case 'voucher':
$res = $qyt->getVoucherList($mobile, $idCard, $page, 20);
break;
default:
return $this->renderError('查询类型不合法');
}
if ($res['code'] !== '00') {
return $this->renderError('企油通返回:' . ($res['message'] ?: '查询失败'));
}
$data = (array) $res['data'];
// 7.12 / 7.13 金额是「分」,统一转成元再给前端;7.17 本来就是元
if ($type === 'balance' || $type === 'wallet') {
foreach (['totalGrantAmount', 'availableBalance'] as $k) {
isset($data[$k]) && $data[$k . 'Yuan'] = QytOil::fenToYuan($data[$k]);
}
foreach (['list', 'transactionDetails'] as $listKey) {
foreach ((array) ($data[$listKey] ?? []) as $i => $item) {
foreach (['total_amount', 'available_amount', 'amount'] as $k) {
if (isset($item[$k]) && is_numeric($item[$k])) {
$data[$listKey][$i][$k . '_yuan'] = QytOil::fenToYuan((int) $item[$k]);
}
}
}
}
}
return $this->renderSuccess(['type' => $type, 'data' => QytOil::maskDeep($data)]);
}
// ================================================================
private function findDriver(int $driverId): ?array
{
$row = Db::name('qyt_driver')->where('store_id', $this->storeId)->where('driver_id', $driverId)->find();
return $row ?: null;
}
/**
* 从订单实时汇总,按身份证号分组
*/
private function stats(array $idCards): array
{
if (!$idCards) {
return [];
}
$rows = Db::name('partners_order')
->where('store_id', $this->storeId)
->whereIn('id_card', $idCards)
->fieldRaw('id_card,'
. ' COUNT(*) AS order_num,'
. ' SUM(IF(oil_status_zsy = 2, total_price, 0)) AS grant_amount,'
. ' SUM(IF(refund_status = 2, refund_amount, 0)) AS refund_amount,'
. ' SUM(IF(use_status = 1, 1, 0)) AS used_num,'
. ' SUM(used_amount) AS used_amount,'
. ' SUM(IF(oil_status_zsy = 3, 1, 0)) AS fail_num,'
. ' SUM(IF(oil_status_zsy = 1, 1, 0)) AS pending_num')
->group('id_card')
->select()->toArray();
return array_column($rows, null, 'id_card');
}
private function formatDriver(array $row, array $stat): array
{
$grant = round((float) ($stat['grant_amount'] ?? 0), 2);
$refund = round((float) ($stat['refund_amount'] ?? 0), 2);
$out = [
'driver_id' => (int) $row['driver_id'],
'name' => $row['name'],
'mobile' => $row['mobile'],
'id_card_mask' => QytOil::maskIdCard((string) $row['id_card']),
'conflict' => (int) $row['conflict'],
'conflict_text' => $row['conflict_text'],
'last_order_time_text' => $row['last_order_time'] ? date('Y-m-d H:i:s', (int) $row['last_order_time']) : '',
'order_num' => (int) ($stat['order_num'] ?? 0),
'used_num' => (int) ($stat['used_num'] ?? 0),
'fail_num' => (int) ($stat['fail_num'] ?? 0),
'pending_num' => (int) ($stat['pending_num'] ?? 0),
'grant_amount' => number_format($grant, 2, '.', ''),
'refund_amount' => number_format($refund, 2, '.', ''),
'used_amount' => number_format((float) ($stat['used_amount'] ?? 0), 2, '.', ''),
// 本地口径的净发放额,仅供参考;真实可用额度以企油通 7.12 为准
'net_amount' => number_format($grant - $refund, 2, '.', ''),
];
return $out;
}
}
@@ -234,6 +234,25 @@ class Enterprise extends Controller
return $this->renderSuccess("同步完成:新增 {$added} 家,更新 {$updated} 家");
}
/**
* 7.11 企业账户余额(仅平安银行通道;银行侧实时查询,勿高频调用)
*/
public function balance(int $enterpriseId): Json
{
$model = EnterpriseModel::detail($enterpriseId);
if (!$model) {
return $this->renderError('企业不存在');
}
$res = QytOil::forEnterprise($model)->distributable();
if ($res['code'] !== '00') {
return $this->renderError('企油通返回:' . ($res['message'] ?: '查询失败'));
}
return $this->renderSuccess([
'balance' => QytOil::fenToYuan($res['data']['balance'] ?? 0),
'available_balance' => QytOil::fenToYuan($res['data']['available_balance'] ?? 0),
], '查询成功');
}
/** 记录同步结果 */
private function saveSync($model, string $text): void
{
@@ -0,0 +1,71 @@
<?php
// +----------------------------------------------------------------------
// | 后台 · 企油通发票管理(7.14)
// +----------------------------------------------------------------------
// | list() 发票记录(按 企业 + 通道 + 月份 + 司机手机号 维度)
// | sync() 同步:指定企业则同步该企业(可带司机手机号);不指定则批量同步所有启用企业的当月全量
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\store\controller\qyt;
use app\common\model\qyt\Enterprise as EnterpriseModel;
use app\common\service\QytInvoice;
use app\store\controller\Controller;
use think\facade\Db;
use think\response\Json;
class Invoice extends Controller
{
public function list(): Json
{
$param = $this->request->param();
$query = Db::name('qyt_invoice');
!empty($param['qyt_enterprise_id']) && $query->where('qyt_enterprise_id', (int) $param['qyt_enterprise_id']);
!empty($param['month']) && $query->where('month', (string) $param['month']);
!empty($param['mobile']) && $query->where('mobile', trim((string) $param['mobile']));
if (isset($param['has_invoice']) && $param['has_invoice'] !== '' && (int) $param['has_invoice'] >= 0) {
(int) $param['has_invoice'] === 1 ? $query->where('invoice_count', '>', 0) : $query->where('invoice_count', 0);
}
$list = $query->order(['month' => 'desc', 'invoice_id' => 'desc'])->paginate(15)->toArray();
$names = EnterpriseModel::column('company_name', 'enterprise_id');
foreach ($list['data'] as &$row) {
$row = QytInvoice::format($row);
$row['company_name'] = $names[$row['qyt_enterprise_id']] ?? '(企业已删除)';
}
return $this->renderSuccess(compact('list'));
}
public function sync(): Json
{
$month = trim((string) $this->request->param('month'));
$mobile = trim((string) $this->request->param('mobile', ''));
$enterpriseId = (int) $this->request->param('enterpriseId', 0);
if ($enterpriseId > 0) {
$enterprise = EnterpriseModel::detail($enterpriseId);
if (!$enterprise) {
return $this->renderError('企业不存在');
}
$res = QytInvoice::sync($enterprise, $month, $mobile);
return $res['ok'] ? $this->renderSuccess($res['row'], $res['message']) : $this->renderError($res['message']);
}
// 批量:所有启用企业的该月全量
$ok = $fail = 0;
$errors = [];
foreach (EnterpriseModel::where('is_enable', 1)->select() as $enterprise) {
$res = QytInvoice::sync($enterprise, $month, '');
if ($res['ok']) {
$ok++;
} else {
$fail++;
$errors[] = $enterprise['company_name'] . ':' . $res['message'];
}
}
$message = "批量同步完成:成功 {$ok} 家,失败 {$fail} 家" . ($errors ? '(' . implode(';', array_slice($errors, 0, 3)) . ')' : '');
return $fail && !$ok ? $this->renderError($message) : $this->renderSuccess([], $message);
}
}