更新初级版本
This commit is contained in:
@@ -1048,4 +1048,153 @@ class DiagnosisLogic extends BaseLogic
|
||||
curl_close($ch);
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 获取诊单详情并记录查看
|
||||
* @param array $params
|
||||
* @return array|false
|
||||
*/
|
||||
public static function diagnosisDetailWithRecord(array $params)
|
||||
{
|
||||
try {
|
||||
$diagnosisId = $params['id'];
|
||||
$userId = $params['user_id'] ?? 0;
|
||||
$shareUserId = $params['share_user_id'] ?? 0;
|
||||
|
||||
// 获取诊单详情
|
||||
$diagnosis = self::detail(['id' => $diagnosisId]);
|
||||
|
||||
if (!$diagnosis) {
|
||||
throw new \Exception('诊单不存在');
|
||||
}
|
||||
|
||||
// 记录查看行为
|
||||
self::recordDiagnosisView([
|
||||
'user_id' => $userId,
|
||||
'diagnosis_id' => $diagnosisId,
|
||||
'patient_id' => $diagnosis['patient_id'] ?? 0,
|
||||
'share_user_id' => $shareUserId
|
||||
]);
|
||||
|
||||
return $diagnosis;
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 记录诊单查看
|
||||
* @param array $params
|
||||
* @return bool
|
||||
*/
|
||||
private static function recordDiagnosisView(array $params)
|
||||
{
|
||||
try {
|
||||
$userId = $params['user_id'];
|
||||
$diagnosisId = $params['diagnosis_id'];
|
||||
$patientId = $params['patient_id'];
|
||||
$shareUserId = $params['share_user_id'];
|
||||
|
||||
// 查找是否已有记录
|
||||
$record = \app\common\model\DiagnosisViewRecord::where([
|
||||
'user_id' => $userId,
|
||||
'diagnosis_id' => $diagnosisId
|
||||
])->find();
|
||||
|
||||
$now = time();
|
||||
|
||||
if ($record) {
|
||||
// 更新记录
|
||||
$record->view_count = $record->view_count + 1;
|
||||
$record->last_view_time = $now;
|
||||
$record->update_time = $now;
|
||||
$record->save();
|
||||
} else {
|
||||
// 创建新记录
|
||||
\app\common\model\DiagnosisViewRecord::create([
|
||||
'user_id' => $userId,
|
||||
'diagnosis_id' => $diagnosisId,
|
||||
'patient_id' => $patientId,
|
||||
'share_user_id' => $shareUserId,
|
||||
'view_count' => 1,
|
||||
'first_view_time' => $now,
|
||||
'last_view_time' => $now,
|
||||
'is_confirmed' => 0,
|
||||
'create_time' => $now,
|
||||
'update_time' => $now
|
||||
]);
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
\think\facade\Log::error('记录诊单查看失败: ' . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 确认诊单
|
||||
* @param array $params
|
||||
* @return bool
|
||||
*/
|
||||
public static function confirmDiagnosisRecord(array $params)
|
||||
{
|
||||
try {
|
||||
$userId = $params['user_id'] ?? 0;
|
||||
$diagnosisId = $params['id'];
|
||||
$shareUserId = $params['share_user_id'] ?? 0;
|
||||
if(!$userId){
|
||||
throw new \Exception('请重新登录!');
|
||||
}
|
||||
// 查找查看记录
|
||||
$record = \app\common\model\DiagnosisViewRecord::where([
|
||||
'user_id' => $userId,
|
||||
'diagnosis_id' => $diagnosisId
|
||||
])->find();
|
||||
|
||||
$now = time();
|
||||
|
||||
if ($record) {
|
||||
// 更新确认状态
|
||||
$record->is_confirmed = 1;
|
||||
$record->confirmed_time = $now;
|
||||
$record->update_time = $now;
|
||||
$record->save();
|
||||
} else {
|
||||
// 如果没有查看记录,创建一条已确认的记录
|
||||
$diagnosis = Diagnosis::find($diagnosisId);
|
||||
if (!$diagnosis) {
|
||||
throw new \Exception('诊单不存在');
|
||||
}
|
||||
|
||||
// 再次检查是否存在记录,防止并发创建重复记录
|
||||
$existingRecord = \app\common\model\DiagnosisViewRecord::where([
|
||||
'user_id' => $userId,
|
||||
'diagnosis_id' => $diagnosisId
|
||||
])->find();
|
||||
|
||||
if (!$existingRecord) {
|
||||
\app\common\model\DiagnosisViewRecord::create([
|
||||
'user_id' => $userId,
|
||||
'diagnosis_id' => $diagnosisId,
|
||||
'patient_id' => $diagnosis->patient_id ?? 0,
|
||||
'share_user_id' => $shareUserId,
|
||||
'view_count' => 1,
|
||||
'first_view_time' => $now,
|
||||
'last_view_time' => $now,
|
||||
'is_confirmed' => 1,
|
||||
'confirmed_time' => $now,
|
||||
'create_time' => $now,
|
||||
'update_time' => $now
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -28,7 +28,7 @@ class TcmController extends BaseApiController
|
||||
* @notes 不需要登录的方法
|
||||
* @var array
|
||||
*/
|
||||
public array $notNeedLogin = ['getPatientSignature', 'diagnosisDetail', 'getDict'];
|
||||
public array $notNeedLogin = ['getPatientSignature', 'diagnosisDetail', 'getDict', 'confirmDiagnosis'];
|
||||
|
||||
/**
|
||||
* @notes 获取患者签名(供小程序调用)
|
||||
@@ -58,20 +58,58 @@ class TcmController extends BaseApiController
|
||||
*/
|
||||
public function diagnosisDetail()
|
||||
{
|
||||
$id = $this->request->get('id');
|
||||
$params = [
|
||||
'id' => $this->request->get('id'),
|
||||
'user_id' => $this->request->get('user_id', 0),
|
||||
'share_user_id' => $this->request->get('share_user_id', 0)
|
||||
];
|
||||
|
||||
if (!$id) {
|
||||
if (!$params['id']) {
|
||||
return $this->fail('诊单ID不能为空');
|
||||
}
|
||||
|
||||
try {
|
||||
$result = DiagnosisLogic::detail(['id' => $id]);
|
||||
$result = DiagnosisLogic::diagnosisDetailWithRecord($params);
|
||||
|
||||
if ($result === false) {
|
||||
return $this->fail(DiagnosisLogic::getError());
|
||||
}
|
||||
|
||||
return $this->data($result);
|
||||
} catch (\Exception $e) {
|
||||
return $this->fail($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 确认诊单(供小程序调用)
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function confirmDiagnosis()
|
||||
{
|
||||
$params = [
|
||||
'id' => $this->request->post('id'),
|
||||
'user_id' => $this->userId,
|
||||
'share_user_id' => $this->request->post('share_user', 0)
|
||||
];
|
||||
|
||||
if (!$params['id']) {
|
||||
return $this->fail('诊单ID不能为空');
|
||||
}
|
||||
|
||||
try {
|
||||
$result = DiagnosisLogic::confirmDiagnosisRecord($params);
|
||||
|
||||
if ($result === false) {
|
||||
return $this->fail(DiagnosisLogic::getError());
|
||||
}
|
||||
|
||||
return $this->success('确认成功');
|
||||
} catch (\Exception $e) {
|
||||
return $this->fail($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 获取字典数据(供小程序调用)
|
||||
* @return \think\response\Json
|
||||
|
||||
@@ -53,6 +53,7 @@ class UserController extends BaseApiController
|
||||
*/
|
||||
public function info()
|
||||
{
|
||||
|
||||
$result = UserLogic::info($this->userId);
|
||||
return $this->data($result);
|
||||
}
|
||||
|
||||
@@ -71,6 +71,7 @@ class UserLogic extends BaseLogic
|
||||
public static function info(int $userId)
|
||||
{
|
||||
$user = User::where(['id' => $userId])
|
||||
->with('diagnosis')
|
||||
->field('id,sn,sex,account,password,nickname,real_name,avatar,mobile,create_time,user_money')
|
||||
->findOrEmpty();
|
||||
$user['has_password'] = !empty($user['password']);
|
||||
|
||||
@@ -75,6 +75,7 @@ class WechatUserService
|
||||
$unionid = $this->unionid;
|
||||
|
||||
$user = User::alias('u')
|
||||
->with('diagnosis')
|
||||
->field('u.id,u.sn,u.mobile,u.nickname,u.avatar,u.mobile,u.is_disable,u.is_new_user,au.openid')
|
||||
->join('user_auth au', 'au.user_id = u.id')
|
||||
->where(function ($query) use ($openid, $unionid) {
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use app\common\model\user\User;
|
||||
use app\common\model\tcm\Diagnosis;
|
||||
use think\model\concern\SoftDelete;
|
||||
|
||||
/**
|
||||
* 诊单查看记录模型
|
||||
*/
|
||||
class DiagnosisViewRecord extends BaseModel
|
||||
{
|
||||
use SoftDelete;
|
||||
|
||||
protected $name = 'diagnosis_view_records';
|
||||
protected $deleteTime = 'delete_time';
|
||||
|
||||
/**
|
||||
* @notes 关联用户
|
||||
*/
|
||||
public function user()
|
||||
{
|
||||
return $this->hasOne(User::class, 'id', 'user_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 关联诊单
|
||||
*/
|
||||
public function diagnosis()
|
||||
{
|
||||
return $this->hasOne(Diagnosis::class, 'id', 'diagnosis_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 关联患者
|
||||
*/
|
||||
public function patient()
|
||||
{
|
||||
return $this->hasOne(User::class, 'id', 'patient_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 关联分享用户
|
||||
*/
|
||||
public function shareUser()
|
||||
{
|
||||
return $this->hasOne(User::class, 'id', 'share_user_id');
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ use app\common\enum\user\UserEnum;
|
||||
use app\common\model\BaseModel;
|
||||
use app\common\service\FileService;
|
||||
use think\model\concern\SoftDelete;
|
||||
|
||||
use app\common\model\DiagnosisViewRecord;
|
||||
/**
|
||||
* 用户模型
|
||||
* Class User
|
||||
@@ -172,5 +172,16 @@ class User extends BaseModel
|
||||
return $sn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 关联患者表
|
||||
* @return \think\model\relation\HasOne
|
||||
* @author 段誉
|
||||
* @date 2022/9/22 16:03
|
||||
*/
|
||||
public function diagnosis()
|
||||
{
|
||||
return $this->belongsTo(DiagnosisViewRecord::class, 'id', 'user_id');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
-- 诊单查看记录表
|
||||
CREATE TABLE IF NOT EXISTS `la_diagnosis_view_records` (
|
||||
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
|
||||
`user_id` int(11) NOT NULL DEFAULT '0' COMMENT '查看用户ID',
|
||||
`diagnosis_id` int(11) NOT NULL DEFAULT '0' COMMENT '诊单ID',
|
||||
`patient_id` int(11) NOT NULL DEFAULT '0' COMMENT '患者ID',
|
||||
`share_user_id` int(11) NOT NULL DEFAULT '0' COMMENT '分享用户ID',
|
||||
`view_count` int(11) NOT NULL DEFAULT '1' COMMENT '查看次数',
|
||||
`first_view_time` int(11) NOT NULL DEFAULT '0' COMMENT '首次查看时间',
|
||||
`last_view_time` int(11) NOT NULL DEFAULT '0' COMMENT '最后查看时间',
|
||||
`is_confirmed` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否已确认 0-未确认 1-已确认',
|
||||
`confirmed_time` int(11) NOT NULL DEFAULT '0' COMMENT '确认时间',
|
||||
`create_time` int(11) NOT NULL DEFAULT '0' COMMENT '创建时间',
|
||||
`update_time` int(11) NOT NULL DEFAULT '0' COMMENT '更新时间',
|
||||
`delete_time` int(11) DEFAULT NULL COMMENT '删除时间',
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_user_diagnosis` (`user_id`, `diagnosis_id`),
|
||||
KEY `idx_diagnosis` (`diagnosis_id`),
|
||||
KEY `idx_patient` (`patient_id`),
|
||||
KEY `idx_share_user` (`share_user_id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='诊单查看记录表';
|
||||
Binary file not shown.
Reference in New Issue
Block a user