Files
zyt/server/app/adminapi/lists/order/OrderLists.php
T
2026-03-19 12:19:00 +08:00

146 lines
4.8 KiB
PHP

<?php
declare(strict_types=1);
namespace app\adminapi\lists\order;
use app\adminapi\lists\BaseAdminDataLists;
use app\common\lists\ListsSearchInterface;
use app\common\model\Order;
use app\common\model\auth\AdminRole;
/**
* 订单列表
* Class OrderLists
* @package app\adminapi\lists\order
*/
class OrderLists extends BaseAdminDataLists implements ListsSearchInterface
{
/**
* @notes 设置搜索条件
* @return array
*/
public function setSearch(): array
{
return [
'=' => ['order_type', 'status'],
'like' => ['order_no'],
];
}
/**
* @notes 权限过滤 - 普通员工只能看自己创建的订单
* @param array $where
* @return void
*/
private function filterByPermission(array &$where): void
{
// 检查是否是超管(root字段为1)
if (!empty($this->adminInfo['root']) && $this->adminInfo['root'] == 1) {
// 超管不过滤,可以看所有订单
return;
}
// 主管角色ID列表(可根据实际调整)
$supervisorRoles = [0, 3];
// 检查当前用户是否是主管
$roleIds = \app\common\model\auth\AdminRole::where('admin_id', $this->adminId)->column('role_id');
// 如果不是主管,只能看自己创建的订单
$isSupervisor = count(array_intersect($roleIds, $supervisorRoles)) > 0;
if (!$isSupervisor) {
$where[] = ['creator_id', '=', $this->adminId];
}
}
/**
* @notes 获取列表
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function lists(): array
{
$where = $this->searchWhere;
// 权限控制:普通员工只能看自己创建的订单
$this->filterByPermission($where);
// 处理患者关联状态:pending=待关联, associated=已关联
$patientAssociation = $this->params['patient_association'] ?? '';
if ($patientAssociation === 'pending') {
$where[] = ['patient_id', 'null', ''];
} elseif ($patientAssociation === 'associated') {
$where[] = ['patient_id', 'not null', ''];
}
// 处理患者关键词搜索(从诊单表搜索)
if (!empty($this->params['patient_keyword'])) {
$where[] = ['patient_id', 'in', function ($query) {
$query->table('zyt_tcm_diagnosis')
->where('patient_name|patient_phone', 'like', '%' . $this->params['patient_keyword'] . '%')
->field('id');
}];
}
// 处理创建时间范围
if (!empty($this->params['create_time_start'])) {
$where[] = ['create_time', '>=', $this->params['create_time_start'] . ' 00:00:00'];
}
if (!empty($this->params['create_time_end'])) {
$where[] = ['create_time', '<=', $this->params['create_time_end'] . ' 23:59:59'];
}
return Order::where($where)
->with(['patient', 'creator'])
->order(['create_time' => 'desc'])
->limit($this->limitOffset, $this->pageSize)
->select()
->toArray();
}
/**
* @notes 获取数量
* @return int
*/
public function count(): int
{
$where = $this->searchWhere;
// 权限控制:普通员工只能看自己创建的订单
$this->filterByPermission($where);
// 处理患者关联状态
$patientAssociation = $this->params['patient_association'] ?? '';
if ($patientAssociation === 'pending') {
$where[] = ['patient_id', 'null', ''];
} elseif ($patientAssociation === 'associated') {
$where[] = ['patient_id', 'not null', ''];
}
// 处理患者关键词搜索(从诊单表搜索)
if (!empty($this->params['patient_keyword'])) {
$where[] = ['patient_id', 'in', function ($query) {
$query->table('zyt_tcm_diagnosis')
->where('patient_name|patient_phone', 'like', '%' . $this->params['patient_keyword'] . '%')
->field('id');
}];
}
// 处理创建时间范围
if (!empty($this->params['create_time_start'])) {
$where[] = ['create_time', '>=', $this->params['create_time_start'] . ' 00:00:00'];
}
if (!empty($this->params['create_time_end'])) {
$where[] = ['create_time', '<=', $this->params['create_time_end'] . ' 23:59:59'];
}
return Order::where($where)->count();
}
}