138 lines
4.4 KiB
PHP
138 lines
4.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Constants\HttpEnumCode;
|
|
use App\Model\DoctorPharmacy;
|
|
use App\Model\Pharmacy;
|
|
use Hyperf\DbConnection\Db;
|
|
|
|
class DoctorPharmacyService extends BaseService
|
|
{
|
|
/**
|
|
* 获取医生已绑定的药房列表
|
|
*/
|
|
public function getDoctorPharmacyList(): array
|
|
{
|
|
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
|
$doctor_id = $user_info['client_user_id'] ?? 0;
|
|
|
|
if (empty($doctor_id)) {
|
|
return fail(HttpEnumCode::CLIENT_HTTP_UNAUTHORIZED, "用户未登录");
|
|
}
|
|
|
|
$list = DoctorPharmacy::with(['Pharmacy'])->where([
|
|
'doctor_id' => (string) $doctor_id,
|
|
'status' => 1,
|
|
])->get();
|
|
|
|
$data = [];
|
|
foreach ($list as $item) {
|
|
$pharmacy = $item->Pharmacy;
|
|
if (empty($pharmacy) || $pharmacy->status != 1) {
|
|
continue;
|
|
}
|
|
|
|
$data[] = [
|
|
'doctor_pharmacy_id' => $item->doctor_pharmacy_id,
|
|
'pharmacy_id' => $pharmacy->pharmacy_id,
|
|
'pharmacy_name' => $pharmacy->pharmacy_name,
|
|
'pharmacy_code' => $pharmacy->pharmacy_code,
|
|
'postage' => $pharmacy->postage,
|
|
'free_shipping_threshold' => $pharmacy->free_shipping_threshold,
|
|
'is_pickup' => $pharmacy->is_pickup,
|
|
'telephone' => $pharmacy->telephone,
|
|
'province' => $pharmacy->province,
|
|
'city' => $pharmacy->city,
|
|
'county' => $pharmacy->county,
|
|
'address' => $pharmacy->address,
|
|
'full_address' => $pharmacy->province . $pharmacy->city . $pharmacy->county . $pharmacy->address,
|
|
'is_default' => $item->is_default,
|
|
];
|
|
}
|
|
|
|
return success($data);
|
|
}
|
|
|
|
/**
|
|
* 设置医生默认药房
|
|
*/
|
|
public function setDoctorDefaultPharmacy(): array
|
|
{
|
|
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
|
$doctor_id = $user_info['client_user_id'] ?? 0;
|
|
$pharmacy_id = $this->request->input('pharmacy_id');
|
|
|
|
if (empty($doctor_id)) {
|
|
return fail(HttpEnumCode::CLIENT_HTTP_UNAUTHORIZED, "用户未登录");
|
|
}
|
|
|
|
if (!isset($pharmacy_id) || $pharmacy_id === '') {
|
|
return fail(HttpEnumCode::HTTP_ERROR, "缺少药房ID");
|
|
}
|
|
|
|
// 验证医生是否绑定了该药房
|
|
$bind = DoctorPharmacy::where([
|
|
'doctor_id' => (string) $doctor_id,
|
|
'pharmacy_id' => (string) $pharmacy_id,
|
|
'status' => 1,
|
|
])->first();
|
|
|
|
if (empty($bind)) {
|
|
return fail(HttpEnumCode::HTTP_ERROR, "未绑定该药房或绑定已被禁用");
|
|
}
|
|
|
|
// 验证药房是否存在且有效
|
|
$pharmacy = Pharmacy::getValidPharmacy($pharmacy_id);
|
|
if (empty($pharmacy)) {
|
|
return fail(HttpEnumCode::HTTP_ERROR, "药房不存在或已暂停服务");
|
|
}
|
|
|
|
if (empty($pharmacy->pharmacy_code)) {
|
|
return fail(HttpEnumCode::HTTP_ERROR, "该药房未配置药房编码,无法设为默认药房");
|
|
}
|
|
|
|
Db::beginTransaction();
|
|
try {
|
|
// 将该医生所有绑定的 is_default 置为 0
|
|
DoctorPharmacy::where('doctor_id', (string) $doctor_id)->update(['is_default' => 0]);
|
|
|
|
// 将目标药房的 is_default 置为 1
|
|
DoctorPharmacy::where([
|
|
'doctor_id' => (string) $doctor_id,
|
|
'pharmacy_id' => (string) $pharmacy_id,
|
|
])->update(['is_default' => 1]);
|
|
|
|
Db::commit();
|
|
return success(null, "设置默认药房成功");
|
|
} catch (\Throwable $e) {
|
|
Db::rollBack();
|
|
return fail(HttpEnumCode::SERVER_ERROR, "设置默认药房失败:" . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取平台公开正常运营的药房列表
|
|
*/
|
|
public function getPublicPharmacyList(): array
|
|
{
|
|
$list = Pharmacy::where('status', 1)->get([
|
|
'pharmacy_id',
|
|
'pharmacy_name',
|
|
'pharmacy_code',
|
|
'postage',
|
|
'free_shipping_threshold',
|
|
'is_pickup',
|
|
'telephone',
|
|
'province',
|
|
'city',
|
|
'county',
|
|
'address',
|
|
]);
|
|
|
|
return success($list->toArray());
|
|
}
|
|
}
|