This commit is contained in:
Your Name
2026-04-23 15:56:01 +08:00
parent aabf2f3a38
commit f9c8b2a632
258 changed files with 489 additions and 367 deletions
@@ -70,6 +70,8 @@ class DiagnosisController extends BaseAdminController
public function add()
{
$params = (new DiagnosisValidate())->post()->goCheck('add');
// 诊单创建人(需表 zyt_tcm_diagnosis 已增加 admin_id 列,见 sql 脚本)
$params['admin_id'] = (int) $this->adminId;
$result = DiagnosisLogic::add($params);
if ($result) {
return $this->success('添加成功', ['id' => $result], 1, 1);
@@ -92,7 +92,16 @@ class DiagnosisLogic extends BaseLogic
if (isset($params['diagnosis_date'])) {
$params['diagnosis_date'] = strtotime($params['diagnosis_date']);
}
// 表未增加 admin_id 列时勿写入,避免 SQL 报错(见 sql/1.9.20260421/add_tcm_diagnosis_admin_id.sql
if (array_key_exists('admin_id', $params)) {
$diagM = new Diagnosis();
$fieldNames = $diagM->getConnection()->getTableFields($diagM->getTable());
if (! is_array($fieldNames) || ! in_array('admin_id', $fieldNames, true)) {
unset($params['admin_id']);
}
}
$model = Diagnosis::create($params);
// 自动为患者创建 TRTC 账号
@@ -13,6 +13,8 @@ use app\common\model\tcm\PrescriptionOrder;
use app\common\model\tcm\PrescriptionOrderLog;
use app\common\model\tcm\PrescriptionOrderPayOrder;
use app\common\model\auth\Admin;
use app\common\model\auth\AdminDept;
use app\common\model\dept\Dept;
use app\common\service\ExpressTrackService;
use app\common\service\gancao\GancaoScmRecipelService;
use think\facade\Config;
@@ -32,6 +34,38 @@ class PrescriptionOrderLogic
return self::$error;
}
/**
* 部门自底向上链式名称,含父级(如:总部 / 华东 / 上海门诊)
*/
private static function buildDeptPath(int $deptId): string
{
if ($deptId <= 0) {
return '';
}
$names = [];
$id = $deptId;
for ($i = 0; $i < 50; $i++) {
$row = Dept::where('id', $id)->whereNull('delete_time')->field(['id', 'name', 'pid'])->find();
if ($row === null) {
break;
}
$data = $row->toArray();
array_unshift($names, (string) ($data['name'] ?? ''));
$pid = (int) ($data['pid'] ?? 0);
if ($pid <= 0) {
break;
}
$id = $pid;
}
if ($names === []) {
return '';
}
$names = array_filter($names, static fn (string $n): bool => $n !== '');
return implode(' / ', $names);
}
/**
* @param int[] $allowRoleIds
*/
@@ -607,6 +641,54 @@ class PrescriptionOrderLogic
unset($arr['prescription']['herbs']);
}
// 诊单医助 / 诊单创建人 及部门(多部门、含父级路径用「;」与「 / 」分隔);创建人为 tcm_diagnosis.admin_id
$arr['assistant_id'] = 0;
$arr['assistant_name'] = '';
$arr['assistant_dept_path'] = '';
$arr['diagnosis_creator_id'] = 0;
$arr['diagnosis_creator_name'] = '';
$arr['diagnosis_creator_dept_path'] = '';
$diagIdForMeta = (int) ($arr['diagnosis_id'] ?? 0);
if ($diagIdForMeta > 0) {
// 勿在 field 中写 admin_id:未执行建表补充列时会报 Unknown column
$dg = Diagnosis::where('id', $diagIdForMeta)->whereNull('delete_time')->find();
if ($dg !== null) {
$drow = $dg->toArray();
$astId = (int) ($drow['assistant_id'] ?? 0);
$arr['assistant_id'] = $astId;
if ($astId > 0) {
$an = Admin::where('id', $astId)->whereNull('delete_time')->value('name');
$arr['assistant_name'] = $an !== null && (string) $an !== '' ? (string) $an : '';
$deptIds = AdminDept::where('admin_id', $astId)->column('dept_id');
$pathSegments = [];
foreach ($deptIds as $did) {
$p = self::buildDeptPath((int) $did);
if ($p !== '') {
$pathSegments[] = $p;
}
}
$pathSegments = array_values(array_unique($pathSegments));
$arr['assistant_dept_path'] = $pathSegments === [] ? '' : implode('', $pathSegments);
}
$creId = (int) ($drow['admin_id'] ?? 0);
$arr['diagnosis_creator_id'] = $creId;
if ($creId > 0) {
$cn = Admin::where('id', $creId)->whereNull('delete_time')->value('name');
$arr['diagnosis_creator_name'] = $cn !== null && (string) $cn !== '' ? (string) $cn : '';
$deptIdsC = AdminDept::where('admin_id', $creId)->column('dept_id');
$pathSegmentsC = [];
foreach ($deptIdsC as $didC) {
$pc = self::buildDeptPath((int) $didC);
if ($pc !== '') {
$pathSegmentsC[] = $pc;
}
}
$pathSegmentsC = array_values(array_unique($pathSegmentsC));
$arr['diagnosis_creator_dept_path'] = $pathSegmentsC === [] ? '' : implode('', $pathSegmentsC);
}
}
}
return $arr;
}
@@ -8,6 +8,7 @@ use app\common\model\ExpressTracking;
use app\common\model\ExpressTrace;
use app\common\model\ExpressStateLog;
use app\common\model\ExpressQueryLog;
use app\common\model\tcm\PrescriptionOrder;
use think\facade\Log;
/**
@@ -86,13 +87,15 @@ class ExpressTrackingService
return null;
}
self::backfillRecipientPhoneFromPrescriptionOrder($tracking);
$startTime = microtime(true);
// 调用快递100查询
// 调用快递100查询(顺丰等单号需带收件人手机号后四位,见 ExpressTrackService
$result = ExpressTrackService::query(
$tracking->express_company,
$tracking->tracking_number,
$tracking->recipient_phone
(string) $tracking->recipient_phone
);
$responseTime = (int) ((microtime(true) - $startTime) * 1000);
@@ -167,6 +170,30 @@ class ExpressTrackingService
return $result;
}
/**
* 物流表未存收件人手机时,从关联业务订单回填(顺丰/京东等查件必填后四位)
*/
private static function backfillRecipientPhoneFromPrescriptionOrder(ExpressTracking $tracking): void
{
$current = trim((string) $tracking->recipient_phone);
if ($current !== '') {
return;
}
if ((string) $tracking->order_type !== 'prescription' || (int) $tracking->order_id <= 0) {
return;
}
$po = PrescriptionOrder::where('id', (int) $tracking->order_id)
->whereNull('delete_time')
->find();
if ($po === null) {
return;
}
$p = trim((string) ($po->recipient_phone ?? ''));
if ($p !== '') {
$tracking->recipient_phone = $p;
}
}
/**
* 保存轨迹明细
*/
@@ -298,6 +325,7 @@ class ExpressTrackingService
public static function autoUpdateBatch(int $limit = 50): array
{
$now = time();
$poTable = (new PrescriptionOrder())->getTable();
// 查询需要更新的记录
// 条件:
@@ -305,15 +333,23 @@ class ExpressTrackingService
// 2. next_update_time <= now(到达更新时间)
// 3. is_signed = 0(未签收)
// 4. current_state 不是终态(排除已签收、退签、拒签)
$list = ExpressTracking::where('auto_update', 1)
->where('next_update_time', '<=', $now)
->where('is_signed', 0)
->whereNotIn('current_state', [
// 5. 关联业务订单 tcm_prescription_order:已存在甘草单号 gancao_reciperl_order_no 的不再走本任务(由甘草侧物流拉取)
$list = ExpressTracking::alias('et')
->leftJoin("{$poTable} po", "et.order_id = po.id AND et.order_type = 'prescription'")
->where('et.auto_update', 1)
->where('et.next_update_time', '<=', $now)
->where('et.is_signed', 0)
->whereNotIn('et.current_state', [
ExpressTracking::STATE_SIGNED,
ExpressTracking::STATE_RETURN_SIGNED,
ExpressTracking::STATE_REJECTED,
])
->whereNull('delete_time')
->whereNull('et.delete_time')
->whereRaw(
"(et.order_type <> ? OR TRIM(COALESCE(po.gancao_reciperl_order_no, '')) = '')",
['prescription']
)
->field('et.*')
->limit($limit)
->select();