2023-03-09 18:35:13 +08:00

123 lines
4.2 KiB
PHP

<?php
namespace App\Services;
use App\Constants\DoctorTitleCode;
use App\Exception\BusinessException;
use App\Model\Hospital;
use App\Model\User;
use App\Model\UserDoctor;
use App\Model\UserPatient;
use App\Model\UserPharmacist;
use App\Utils\Log;
use Extend\TencentIm\Account;
use Extend\TencentIm\Profile;
use GuzzleHttp\Exception\GuzzleException;
class ImService extends BaseService
{
/**
* 检测并创建账号
* @param string $user_id
* @return bool
* @throws GuzzleException
*/
public function createAccount(string $user_id): bool
{
try {
$params = array();
$params['user_id'] = $user_id;
$user = User::getOne($params);
if (empty($user)){
throw new BusinessException("用户数据错误");
}
if ($user['user_type'] == 1){
// 患者
$params = array();
$params['user_id'] = $user['user_id'];
$user_patient = UserPatient::getOne($params);
if (empty($user_patient)){
throw new BusinessException("患者数据错误");
}
$avatar = addAliyunOssWebsite($user_patient['avatar']);
$user_name = $user_patient['user_name'];
}elseif ($user['user_type'] == 2){
// 医生
$params = array();
$params['user_id'] = $user['user_id'];
$user_doctor = UserDoctor::getOne($params);
if (empty($user_doctor)){
throw new BusinessException("医生数据错误");
}
$avatar = addAliyunOssWebsite($user_doctor['avatar']);
$user_name = $user_doctor['user_name'];
}elseif ($user['user_type'] == 3){
// 药师
$params = array();
$params['user_id'] = $user['user_id'];
$user_pharmacist = UserPharmacist::getOne($params);
if (empty($user_pharmacist)){
throw new BusinessException("药师数据错误");
}
$avatar = addAliyunOssWebsite($user_pharmacist['avatar']);
$user_name = $user_pharmacist['user_name'];
}
$account = new Account();
// 查询账号导入状态
$res = $account->checkAccountStatus($user_patient['user_id']);
if (!$res){
// 创建单个账号
$account->createAccount($user_id,$user_name,$avatar);
}
// 医生检测并设置资料
if ($user['user_type'] == 2){
// 检测用户资料
$profile = new Profile();
$result = $profile->getOneAccountPortraitList("123456");
if (!empty($result)){
// 获取订单医生医院
$params = array();
$params['hospital_id'] = $user_doctor['hospital_id'];
$hospital = Hospital::getOne($params);
if (empty($hospital)){
throw new BusinessException("医生医院数据错误");
}
// 设置用户资料
$arg = array();
// 性别
if (in_array('Tag_Profile_IM_Gender',$result)){
$arg['Tag_Profile_IM_Gender'] = sexToImSex($user_doctor['sex']);
}
// 医院
if (in_array('Tag_Profile_Custom_Hname',$result)){
$arg['Tag_Profile_Custom_Hname'] = $hospital['hospital'];
}
// 职称
if (in_array('Tag_Profile_Custom_Hname',$result)){
$arg['Tag_Profile_Custom_Title'] = $user_doctor['doctor_title'] ?? DoctorTitleCode::getMessage($user_doctor['doctor_title']);
}
if (!empty($arg)){
$profile->setProfile($user_doctor['user_id'],$arg);
}
}
}
return true;
} catch (\Exception $e) {
throw new BusinessException($e->getMessage());
}
}
}