From d3fce247e506c1a66851cdd1f35c15a291864430 Mon Sep 17 00:00:00 2001 From: haomingming Date: Tue, 8 Sep 2026 10:09:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E8=8D=AF=E6=88=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Command/getProductCommand.php | 28 +++- app/Command/getProductStockCommand.php | 7 +- app/Controller/DoctorPharmacyController.php | 45 ++++++ app/Model/DoctorPharmacy.php | 132 ++++++++++++++++++ app/Model/OrderPrescription.php | 18 ++- app/Model/OrderProduct.php | 20 ++- app/Model/Pharmacy.php | 133 ++++++++++++++++++ app/Model/Product.php | 8 +- app/Model/ProductPlatform.php | 3 +- app/Model/UserDoctor.php | 9 ++ app/Request/DoctorPharmacyRequest.php | 35 +++++ app/Request/PatientOrderRequest.php | 4 +- app/Request/UserDoctorRequest.php | 2 + app/Services/BasicDataService.php | 44 ++++++ app/Services/DoctorPharmacyService.php | 137 ++++++++++++++++++ app/Services/OrderPrescriptionService.php | 7 +- app/Services/PatientOrderService.php | 146 ++++++++++++++------ app/Services/UserDoctorService.php | 44 +++++- config/routes.php | 13 ++ extend/Prescription/Prescription.php | 12 +- 20 files changed, 782 insertions(+), 65 deletions(-) create mode 100644 app/Controller/DoctorPharmacyController.php create mode 100644 app/Model/DoctorPharmacy.php create mode 100644 app/Model/Pharmacy.php create mode 100644 app/Request/DoctorPharmacyRequest.php create mode 100644 app/Services/DoctorPharmacyService.php diff --git a/app/Command/getProductCommand.php b/app/Command/getProductCommand.php index 9d61b2b..d3b969a 100644 --- a/app/Command/getProductCommand.php +++ b/app/Command/getProductCommand.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Command; +use App\Model\Pharmacy; use App\Model\Product; use App\Model\ProductPlatform; use App\Utils\Log; @@ -21,6 +22,18 @@ use Symfony\Component\Console\Input\InputArgument; #[Command] class getProductCommand extends HyperfCommand { + protected array $active_pharmacies = []; + + protected function getPharmacy(string $code) + { + if (empty($this->active_pharmacies)) { + $list = Pharmacy::where('status', 1)->get(); + foreach ($list as $p) { + $this->active_pharmacies[$p->pharmacy_code] = $p; + } + } + return $this->active_pharmacies[$code] ?? null; + } public function __construct(protected ContainerInterface $container) { parent::__construct('getProduct:command'); @@ -117,13 +130,8 @@ class getProductCommand extends HyperfCommand try { Db::beginTransaction(); - $pharmacy_code = \Hyperf\Config\config("prescription_platform.pharmacy_code"); - if (empty($pharmacy_code)) { - $pharmacy_code = "JG-10009"; - } - - // 非药店编码 - if ($pharmacy_code != $item['thirdCode']) { + $pharmacy = $this->getPharmacy($item['thirdCode']); + if (empty($pharmacy)) { Db::rollBack(); return false; } @@ -133,6 +141,7 @@ class getProductCommand extends HyperfCommand $params['product_platform_code'] = $item['drugCode']; $params['product_pharmacy_code'] = $item['thirdDrugCode']; $params['license_number'] = $item['approvalNumber']; + $params['pharmacy_code'] = $item['thirdCode']; $product_platform = ProductPlatform::getOne($params); if (!empty($product_platform)) { // 已存在,更新 @@ -214,6 +223,10 @@ class getProductCommand extends HyperfCommand } } + $product_platform_data['pharmacy_code'] = $item['thirdCode']; + $product_data['pharmacy_code'] = $item['thirdCode']; + $product_data['pharmacy_id'] = (string)$pharmacy->pharmacy_id; + if (!empty($product_platform_data)) { // 更新商品表-处方平台 $params = array(); @@ -236,6 +249,7 @@ class getProductCommand extends HyperfCommand } else { // 不存在,创建 $data = array(); + $data['pharmacy_code'] = $item['thirdCode']; // 商品名称 if (isset($item['tradeName'])) { $data['product_name'] = $item['tradeName']; diff --git a/app/Command/getProductStockCommand.php b/app/Command/getProductStockCommand.php index 356bd37..fed73e1 100644 --- a/app/Command/getProductStockCommand.php +++ b/app/Command/getProductStockCommand.php @@ -48,7 +48,8 @@ class getProductStockCommand extends HyperfCommand foreach ($product['data'] as $item){ if (!empty($item['product_pharmacy_code'])){ - $result = $prescription->getProdStock($item['product_pharmacy_code']); + $pharmacy_code = $item['pharmacy_code'] ?? ''; + $result = $prescription->getProdStock($item['product_pharmacy_code'], $pharmacy_code); $this->handleData($item['product_platform_id'],$item['product_platform_code'],$result[0]); } } @@ -57,6 +58,7 @@ class getProductStockCommand extends HyperfCommand for ($i = 2; $i <= $product['last_page']; $i++) { // 获取商品 $params = array(); + $params['product_status'] = 1; $product = Product::getPage($params,['*'],$i,10); if (empty($product['data'])){ $this->line("商品库存更新成功,无可更新库存商品"); @@ -67,7 +69,8 @@ class getProductStockCommand extends HyperfCommand foreach ($product['data'] as $item){ if (!empty($item['product_pharmacy_code'])){ - $result = $prescription->getProdStock($item['product_pharmacy_code']); + $pharmacy_code = $item['pharmacy_code'] ?? ''; + $result = $prescription->getProdStock($item['product_pharmacy_code'], $pharmacy_code); $this->handleData($item['product_platform_id'],$item['product_platform_code'],$result[0]); } } diff --git a/app/Controller/DoctorPharmacyController.php b/app/Controller/DoctorPharmacyController.php new file mode 100644 index 0000000..c6ba813 --- /dev/null +++ b/app/Controller/DoctorPharmacyController.php @@ -0,0 +1,45 @@ +getDoctorPharmacyList(); + return $this->response->json($data); + } + + /** + * 设置医生默认药房 + */ + public function setDoctorDefaultPharmacy(): ResponseInterface + { + $request = $this->container->get(DoctorPharmacyRequest::class); + $request->scene('setDefault')->validateResolved(); + + $service = new DoctorPharmacyService(); + $data = $service->setDoctorDefaultPharmacy(); + return $this->response->json($data); + } + + /** + * 获取平台公开正常运营的药房列表 + */ + public function getPublicPharmacyList(): ResponseInterface + { + $service = new DoctorPharmacyService(); + $data = $service->getPublicPharmacyList(); + return $this->response->json($data); + } +} diff --git a/app/Model/DoctorPharmacy.php b/app/Model/DoctorPharmacy.php new file mode 100644 index 0000000..2e29916 --- /dev/null +++ b/app/Model/DoctorPharmacy.php @@ -0,0 +1,132 @@ + 'string', + 'doctor_id' => 'string', + 'pharmacy_id' => 'string', + 'is_default' => 'integer', + 'status' => 'integer', + 'created_at' => 'datetime', + 'updated_at' => 'datetime', + ]; + + /** + * 关联药房信息表 + */ + public function Pharmacy(): HasOne + { + return $this->hasOne(Pharmacy::class, 'pharmacy_id', 'pharmacy_id'); + } + + /** + * 关联医生表 + */ + public function UserDoctor(): HasOne + { + return $this->hasOne(UserDoctor::class, 'doctor_id', 'doctor_id'); + } + + /** + * 获取医生当前绑定的有效默认药房(包含药房有效性及 pharmacy_code 非空校验) + */ + public static function getDoctorDefaultPharmacy(int|string $doctorId): ?Pharmacy + { + $bind = self::where([ + 'doctor_id' => (string) $doctorId, + 'is_default' => 1, + 'status' => 1, + ])->first(); + + if (empty($bind)) { + return null; + } + + $pharmacy = Pharmacy::getValidPharmacy($bind->pharmacy_id); + if (empty($pharmacy) || empty($pharmacy->pharmacy_code)) { + return null; + } + + return $pharmacy; + } + + /** + * 获取单条数据 + */ + public static function getOne(array $params, array $fields = ['*']): ?self + { + return self::where($params)->first($fields); + } + + /** + * 获取列表数据 + */ + public static function getList(array $params = [], array $fields = ['*']): Collection + { + return self::where($params)->get($fields); + } + + /** + * 新增绑定 + */ + public static function addDoctorPharmacy(array $data): self + { + return self::create($data); + } + + /** + * 修改 + */ + public static function edit(array $params, array $data): int + { + return self::where($params)->update($data); + } +} diff --git a/app/Model/OrderPrescription.php b/app/Model/OrderPrescription.php index 3c33adf..0303a6a 100644 --- a/app/Model/OrderPrescription.php +++ b/app/Model/OrderPrescription.php @@ -36,12 +36,16 @@ use Hyperf\Snowflake\Concern\Snowflake; * @property string $patient_name 患者姓名-就诊人 * @property int $patient_sex 患者性别-就诊人(1:男 2:女) * @property int $patient_age 患者年龄-就诊人 + * @property int|string $pharmacy_id 开方药房id + * @property string $pharmacy_code 开方药房代码快照 + * @property string $pharmacy_name 开方药房名称快照 * @property string $doctor_advice 医嘱 * @property \Carbon\Carbon $created_at 创建时间 * @property \Carbon\Carbon $updated_at 修改时间 * @property-read \Hyperf\Database\Model\Collection|OrderPrescriptionIcd[] $OrderPrescriptionIcd * @property-read \Hyperf\Database\Model\Collection|OrderPrescriptionProduct[] $OrderPrescriptionProduct * @property-read UserDoctor $UserDoctor + * @property-read Pharmacy|null $Pharmacy */ class OrderPrescription extends Model { @@ -55,10 +59,22 @@ class OrderPrescription extends Model /** * The attributes that are mass assignable. */ - protected array $fillable = ['order_prescription_id', 'order_inquiry_id', 'doctor_id', 'patient_id', 'family_id', 'pharmacist_id', 'prescription_status', 'pharmacist_audit_status', 'pharmacist_verify_time', 'pharmacist_fail_reason', 'platform_audit_status', 'platform_fail_time', 'platform_fail_reason', 'is_auto_phar_verify', 'doctor_created_time', 'expired_time', 'is_delete', 'prescription_code', 'doctor_name', 'patient_name', 'patient_sex', 'patient_age', 'doctor_advice', 'created_at', 'updated_at']; + protected array $fillable = ['order_prescription_id', 'order_inquiry_id', 'doctor_id', 'pharmacy_id', 'pharmacy_code', 'pharmacy_name', 'patient_id', 'family_id', 'pharmacist_id', 'prescription_status', 'pharmacist_audit_status', 'pharmacist_verify_time', 'pharmacist_fail_reason', 'platform_audit_status', 'platform_fail_time', 'platform_fail_reason', 'is_auto_phar_verify', 'doctor_created_time', 'expired_time', 'is_delete', 'prescription_code', 'doctor_name', 'patient_name', 'patient_sex', 'patient_age', 'doctor_advice', 'created_at', 'updated_at']; + + protected array $casts = [ + 'pharmacy_id' => 'string', + ]; protected string $primaryKey = "order_prescription_id"; + /** + * 关联药房表 + */ + public function Pharmacy(): HasOne + { + return $this->hasOne(Pharmacy::class, 'pharmacy_id', 'pharmacy_id'); + } + /** * 关联处方疾病表 */ diff --git a/app/Model/OrderProduct.php b/app/Model/OrderProduct.php index 56e31e9..4c75ab4 100644 --- a/app/Model/OrderProduct.php +++ b/app/Model/OrderProduct.php @@ -55,12 +55,17 @@ use Hyperf\Snowflake\Concern\Snowflake; * @property string $consignee_name_mask 收货人姓名(掩码) * @property string $consignee_tel 收货人电话 * @property string $consignee_tel_mask 收货人电话(掩码) + * @property int|string $pharmacy_id 发货药房id + * @property string $pharmacy_code 发货药房代码快照 + * @property string $pharmacy_name 发货药房名称快照 + * @property int $delivery_type 配送方式(1:自提 2:快递) * @property Carbon $created_at 创建时间 * @property Carbon $updated_at 修改时间 * @property-read Collection|OrderProductItem[]|null $OrderProductItem * @property-read OrderPrescription|null $OrderPrescription * @property-read PatientFamily|null $PatientFamily * @property-read Collection|OrderPrescriptionIcd[]|null $OrderPrescriptionIcd + * @property-read Pharmacy|null $Pharmacy */ class OrderProduct extends Model { @@ -74,10 +79,23 @@ class OrderProduct extends Model /** * The attributes that are mass assignable. */ - protected array $fillable = ['order_product_id', 'order_inquiry_id', 'order_prescription_id', 'order_id', 'doctor_id', 'patient_id', 'family_id', 'order_product_no', 'escrow_trade_no', 'order_product_status', 'pay_channel', 'pay_status', 'is_delete', 'cancel_reason', 'amount_total', 'coupon_amount_total', 'payment_amount_total', 'logistics_fee', 'logistics_no', 'logistics_company_code', 'sub_logistics_status', 'delivery_time', 'pay_time', 'remarks', 'refund_status', 'cancel_time', 'cancel_remarks', 'report_pre_status', 'report_pre_time', 'report_pre_fail_reason', 'province_id', 'province', 'city_id', 'city', 'county_id', 'county', 'address', 'address_mask', 'consignee_name', 'consignee_name_mask', 'consignee_tel', 'consignee_tel_mask', 'created_at', 'updated_at']; + protected array $fillable = ['order_product_id', 'order_inquiry_id', 'order_prescription_id', 'order_id', 'doctor_id', 'pharmacy_id', 'pharmacy_code', 'pharmacy_name', 'delivery_type', 'patient_id', 'family_id', 'order_product_no', 'escrow_trade_no', 'order_product_status', 'pay_channel', 'pay_status', 'is_delete', 'cancel_reason', 'amount_total', 'coupon_amount_total', 'payment_amount_total', 'logistics_fee', 'logistics_no', 'logistics_company_code', 'sub_logistics_status', 'delivery_time', 'pay_time', 'remarks', 'refund_status', 'cancel_time', 'cancel_remarks', 'report_pre_status', 'report_pre_time', 'report_pre_fail_reason', 'province_id', 'province', 'city_id', 'city', 'county_id', 'county', 'address', 'address_mask', 'consignee_name', 'consignee_name_mask', 'consignee_tel', 'consignee_tel_mask', 'created_at', 'updated_at']; + + protected array $casts = [ + 'pharmacy_id' => 'string', + 'delivery_type' => 'integer', + ]; protected string $primaryKey = "order_product_id"; + /** + * 关联药房表 + */ + public function Pharmacy(): HasOne + { + return $this->hasOne(Pharmacy::class, 'pharmacy_id', 'pharmacy_id'); + } + /** * 关联订单商品item表 */ diff --git a/app/Model/Pharmacy.php b/app/Model/Pharmacy.php new file mode 100644 index 0000000..cf95664 --- /dev/null +++ b/app/Model/Pharmacy.php @@ -0,0 +1,133 @@ + 'string', + 'postage' => 'string', + 'free_shipping_threshold' => 'string', + 'is_pickup' => 'integer', + 'province_id' => 'integer', + 'city_id' => 'integer', + 'county_id' => 'integer', + 'status' => 'integer', + 'created_at' => 'datetime', + 'updated_at' => 'datetime', + ]; + + /** + * 关联医生药房绑定表 + */ + public function DoctorPharmacy(): HasMany + { + return $this->hasMany(DoctorPharmacy::class, 'pharmacy_id', 'pharmacy_id'); + } + + /** + * 获取单条有效药房数据 + */ + public static function getValidPharmacy(int|string $pharmacyId): ?self + { + return self::where([ + 'pharmacy_id' => (string) $pharmacyId, + 'status' => 1, + ])->first(); + } + + /** + * 获取单条药房信息 + */ + public static function getOne(array $params, array $fields = ['*']): ?self + { + return self::where($params)->first($fields); + } + + /** + * 获取药房列表 + */ + public static function getList(array $params = [], array $fields = ['*']): Collection + { + return self::where($params)->get($fields); + } + + /** + * 分页查询药房 + */ + public static function getPage(array $params, array $fields = ['*'], ?int $page = null, ?int $per_page = 10): array + { + $raw = self::where($params)->paginate($per_page, $fields, 'page', $page); + return [ + 'current_page' => $raw->currentPage(), + 'total' => $raw->total(), + 'data' => $raw->items(), + 'per_page' => $raw->perPage(), + 'last_page' => $raw->lastPage(), + ]; + } +} diff --git a/app/Model/Product.php b/app/Model/Product.php index 42dfb44..269ed0e 100644 --- a/app/Model/Product.php +++ b/app/Model/Product.php @@ -23,6 +23,8 @@ use Hyperf\Snowflake\Concern\Snowflake; * @property int $product_type 药品类型(0:未知 1:中成药 2:西药) * @property string $product_platform_code 处方平台商品编码 * @property string $product_pharmacy_code 第三方药店商品编码 + * @property int|string $pharmacy_id 所属药房ID + * @property string $pharmacy_code 所属药房编码 * @property string $product_cover_img 商品封面图 * @property string $product_spec 商品规格 * @property string $license_number 批准文号 @@ -49,7 +51,11 @@ class Product extends Model /** * The attributes that are mass assignable. */ - protected array $fillable = ['product_id', 'product_platform_id', 'product_status', 'is_delete', 'prescription_num', 'product_name', 'common_name', 'product_price', 'mnemonic_code', 'product_type', 'product_platform_code', 'product_pharmacy_code', 'product_cover_img', 'product_spec', 'license_number', 'manufacturer', 'single_unit', 'single_use', 'packaging_unit', 'frequency_use', 'available_days', 'product_remarks', 'created_at', 'updated_at']; + protected array $fillable = ['product_id', 'product_platform_id', 'pharmacy_id', 'pharmacy_code', 'product_status', 'is_delete', 'prescription_num', 'product_name', 'common_name', 'product_price', 'mnemonic_code', 'product_type', 'product_platform_code', 'product_pharmacy_code', 'product_cover_img', 'product_spec', 'license_number', 'manufacturer', 'single_unit', 'single_use', 'packaging_unit', 'frequency_use', 'available_days', 'product_remarks', 'created_at', 'updated_at']; + + protected array $casts = [ + 'pharmacy_id' => 'string', + ]; protected string $primaryKey = "product_id"; diff --git a/app/Model/ProductPlatform.php b/app/Model/ProductPlatform.php index f5ddd8a..47466b5 100644 --- a/app/Model/ProductPlatform.php +++ b/app/Model/ProductPlatform.php @@ -16,6 +16,7 @@ use Hyperf\Snowflake\Concern\Snowflake; * @property int $product_type 药品类型(0:未知 1:中成药 2:西药) * @property string $product_platform_code 处方平台商品编码 * @property string $product_pharmacy_code 第三方药店商品编码 + * @property string $pharmacy_code 所属药房编码 * @property string $product_spec 商品规格 * @property string $license_number 批准文号 * @property string $manufacturer 生产厂家 @@ -38,7 +39,7 @@ class ProductPlatform extends Model /** * The attributes that are mass assignable. */ - protected array $fillable = ['product_platform_id', 'product_name', 'product_price', 'product_type', 'product_platform_code', 'product_pharmacy_code', 'product_spec', 'license_number', 'manufacturer', 'single_unit', 'packaging_unit', 'packaging_count', 'retail_unit', 'created_at', 'updated_at']; + protected array $fillable = ['product_platform_id', 'pharmacy_code', 'product_name', 'product_price', 'product_type', 'product_platform_code', 'product_pharmacy_code', 'product_spec', 'license_number', 'manufacturer', 'single_unit', 'packaging_unit', 'packaging_count', 'retail_unit', 'created_at', 'updated_at']; protected string $primaryKey = "product_platform_id"; diff --git a/app/Model/UserDoctor.php b/app/Model/UserDoctor.php index 286a9e1..ffd7586 100644 --- a/app/Model/UserDoctor.php +++ b/app/Model/UserDoctor.php @@ -121,6 +121,15 @@ class UserDoctor extends Model return $this->hasMany(OrderInquiry::class, "doctor_id", "doctor_id"); } + /** + * 关联医生药房绑定表 + * @return HasMany + */ + public function DoctorPharmacy(): HasMany + { + return $this->hasMany(DoctorPharmacy::class, "doctor_id", "doctor_id"); + } + /** * 获取医生信息-单条 * @param array $params diff --git a/app/Request/DoctorPharmacyRequest.php b/app/Request/DoctorPharmacyRequest.php new file mode 100644 index 0000000..4d64424 --- /dev/null +++ b/app/Request/DoctorPharmacyRequest.php @@ -0,0 +1,35 @@ + ['pharmacy_id'], + 'bindPharmacy' => ['pharmacy_id'], + ]; + + public function authorize(): bool + { + return true; + } + + public function rules(): array + { + return [ + 'pharmacy_id' => 'required|numeric', + ]; + } + + public function messages(): array + { + return [ + 'pharmacy_id.required' => '药房ID不能为空', + 'pharmacy_id.numeric' => '药房ID必须为数字', + ]; + } +} diff --git a/app/Request/PatientOrderRequest.php b/app/Request/PatientOrderRequest.php index d561500..437363f 100644 --- a/app/Request/PatientOrderRequest.php +++ b/app/Request/PatientOrderRequest.php @@ -32,6 +32,7 @@ class PatientOrderRequest extends FormRequest "address_id", "product_ids", "client_type", + "delivery_type", ], 'getPatientPrescriptionOrderList' => [ // 获取处方订单列表 ], @@ -84,7 +85,8 @@ class PatientOrderRequest extends FormRequest 'order_no' => 'required', 'order_prescription_id' => 'required', - 'address_id' => 'required', + 'address_id' => 'nullable', + 'delivery_type' => ['nullable', 'integer', Rule::in([1, 2])], 'product_ids' => 'required|array|min:1', 'detection_status' => 'required|integer|min:0|max:5', 'order_service_status' => 'required|integer|min:0|max:4', diff --git a/app/Request/UserDoctorRequest.php b/app/Request/UserDoctorRequest.php index 29d741d..79c9036 100644 --- a/app/Request/UserDoctorRequest.php +++ b/app/Request/UserDoctorRequest.php @@ -56,6 +56,7 @@ class UserDoctorRequest extends FormRequest 'prescription_icd',// 诊断疾病[] // 'doctor_advice', // 医嘱 'prescription_product',// 处方药品[] + 'pharmacy_id', // 药房id(可选,未传则取默认药房) ], 'getDoctorMessageList' => [ // 获取医生问诊消息列表 'message_inquiry_type',// 消息订单类型(1:专家问诊 2:快速问诊 3:公益问诊 4:问诊购药 5:结束) @@ -94,6 +95,7 @@ class UserDoctorRequest extends FormRequest 'prescription_icd' => 'required|array|min:1', 'doctor_advice' => 'required', 'prescription_product' => 'required|array|min:1|max:5', + 'pharmacy_id' => 'nullable|numeric', 'message_inquiry_type' => 'required|integer|min:1|max:5', ]; } diff --git a/app/Services/BasicDataService.php b/app/Services/BasicDataService.php index 04d6b0f..bd8e325 100644 --- a/app/Services/BasicDataService.php +++ b/app/Services/BasicDataService.php @@ -16,6 +16,8 @@ use App\Model\HospitalDepartmentCustom; use App\Model\HotSearchKeyword; use App\Model\OperationManual; use App\Model\Product; +use App\Model\DoctorPharmacy; +use App\Model\Pharmacy; use Hyperf\Redis\Redis; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; @@ -242,6 +244,24 @@ class BasicDataService extends BaseService $params = array(); $params['product_status'] = 1; $params['is_delete'] = 0; + + // 若是医生端,则只展示医生选定/默认药房下的药品 + if (isset($user_info['user_type']) && $user_info['user_type'] == 2) { + $doctor_id = $user_info['client_user_id'] ?? 0; + $pharmacy_id = $this->request->input('pharmacy_id'); + if (isset($pharmacy_id) && $pharmacy_id !== '') { + $pharmacy = Pharmacy::getValidPharmacy($pharmacy_id); + } else { + $defaultPharmacy = DoctorPharmacy::getDoctorDefaultPharmacy($doctor_id); + $pharmacy = ($defaultPharmacy && $defaultPharmacy->Pharmacy && $defaultPharmacy->Pharmacy->status == 1) ? $defaultPharmacy->Pharmacy : null; + } + + if (empty($pharmacy) || empty($pharmacy->pharmacy_code)) { + return success([]); + } + $params['pharmacy_code'] = $pharmacy->pharmacy_code; + } + $product = Product::getSearchKeywordList($params, $product_keyword,$fields); if (empty($product)) { return success(); @@ -449,6 +469,30 @@ class BasicDataService extends BaseService $params = array(); $params['product_status'] = 1; $params['is_delete'] = 0; + + // 若是医生端,则只展示医生选定/默认药房下的药品 + if (isset($user_info['user_type']) && $user_info['user_type'] == 2) { + $doctor_id = $user_info['client_user_id'] ?? 0; + $pharmacy_id = $this->request->input('pharmacy_id'); + if (isset($pharmacy_id) && $pharmacy_id !== '') { + $pharmacy = Pharmacy::getValidPharmacy($pharmacy_id); + } else { + $defaultPharmacy = DoctorPharmacy::getDoctorDefaultPharmacy($doctor_id); + $pharmacy = ($defaultPharmacy && $defaultPharmacy->Pharmacy && $defaultPharmacy->Pharmacy->status == 1) ? $defaultPharmacy->Pharmacy : null; + } + + if (empty($pharmacy) || empty($pharmacy->pharmacy_code)) { + return success([ + 'total' => 0, + 'per_page' => (int)$per_page, + 'current_page' => (int)$page, + 'last_page' => 0, + 'data' => [], + ]); + } + $params['pharmacy_code'] = $pharmacy->pharmacy_code; + } + $product = Product::getWithAmountPage($params, $keyword,$fields, $page, $per_page); if (empty($product['data'])) { return success($product); diff --git a/app/Services/DoctorPharmacyService.php b/app/Services/DoctorPharmacyService.php new file mode 100644 index 0000000..d729d0b --- /dev/null +++ b/app/Services/DoctorPharmacyService.php @@ -0,0 +1,137 @@ +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()); + } +} diff --git a/app/Services/OrderPrescriptionService.php b/app/Services/OrderPrescriptionService.php index 36e0213..bcb794e 100644 --- a/app/Services/OrderPrescriptionService.php +++ b/app/Services/OrderPrescriptionService.php @@ -405,7 +405,11 @@ class OrderPrescriptionService extends BaseService $arg['payDate'] = $order_product['pay_time']; // 支付时间 $arg['money'] = $order_product['payment_amount_total']; // 订单金额 $arg['freight'] = $order_product['logistics_fee']; // 运费(单位:元) - $arg['takeTypeCode'] = 2; // 取货方式 1 自提 2 快递,目前只支持快递,传固定值 2 + $arg['takeTypeCode'] = ($order_product['delivery_type'] == 1) ? 1 : 2; // 取货方式 1 自提 2 快递 + if (empty($order_product['pharmacy_code'])) { + throw new BusinessException("订单药房编码缺失,无法上报处方平台"); + } + $arg['pharmacyCode'] = $order_product['pharmacy_code']; $arg['buyerName'] = $order_product['consignee_name'];// 收货人姓名 $arg['buyerPhone'] = $order_product['consignee_tel'];// 收货人联系方式 $arg['buyerAddress'] = $order_product['address'];// 收货人地址 @@ -417,6 +421,7 @@ class OrderPrescriptionService extends BaseService $arg['districtName'] = $order_product['county']; // 收货地址(区 县)名称 $arg['presList'][0]['prescriptionNo'] = $order_prescription['prescription_code']; // 处方编号 + $arg['presList'][0]['pharmacyCode'] = $order_product['pharmacy_code']; $arg['presList'][0]['prescriptionSubType'] = 1; // 处方类型 0:无类型 1:普 通处方 2:儿科处 方 $arg['presList'][0]['patientName'] = $order_prescription['patient_name']; // 就诊人姓名 $arg['presList'][0]['patientPhone'] = $user['mobile']; // 就诊人联系方式 diff --git a/app/Services/PatientOrderService.php b/app/Services/PatientOrderService.php index 6cb7cff..af1e586 100644 --- a/app/Services/PatientOrderService.php +++ b/app/Services/PatientOrderService.php @@ -42,6 +42,7 @@ use App\Model\PatientFamily; use App\Model\PatientFamilyHealth; use App\Model\PatientFamilyPersonal; use App\Model\PatientFollow; +use App\Model\Pharmacy; use App\Model\Product; use App\Model\ProductPlatformAmount; use App\Model\SystemInquiryConfig; @@ -1482,6 +1483,7 @@ class PatientOrderService extends BaseService $address_id = $this->request->input('address_id'); $product_ids = $this->request->input('product_ids'); $client_type = $this->request->input('client_type'); + $delivery_type = (int)$this->request->input('delivery_type', 2); // 1: 自提 2: 快递 // 获取处方数据 $params = array(); @@ -1493,6 +1495,13 @@ class PatientOrderService extends BaseService return fail(); } + // 获取处方药房信息 + $pharmacy_id = $order_prescription['pharmacy_id'] ?? 0; + $pharmacy = Pharmacy::getValidPharmacy($pharmacy_id); + if (empty($pharmacy) || empty($pharmacy['pharmacy_code'])) { + return fail(HttpEnumCode::HTTP_ERROR, "处方所属药房异常,无法下单"); + } + // 验证处方状态 if ($order_prescription['prescription_status'] == 1) { return fail(HttpEnumCode::HTTP_ERROR, "处方未审核"); @@ -1516,13 +1525,28 @@ class PatientOrderService extends BaseService return fail(HttpEnumCode::HTTP_ERROR, "创建订单失败"); } - // 检测收货地址 - $params = array(); - $params['user_id'] = $user_info['user_id']; - $params['address_id'] = $address_id; - $user_ship_address = UserShipAddress::getOne($params); - if (empty($user_ship_address)) { - return fail(HttpEnumCode::HTTP_ERROR, "收货地址错误"); + // 检测收货方式及收货地址 + $user_ship_address = null; + if ($delivery_type == 1) { + // 自提模式,检查药房是否支持自提 + if ($pharmacy['is_pickup'] != 1) { + return fail(HttpEnumCode::HTTP_ERROR, "该药房不支持到店自提"); + } + if (!empty($address_id)) { + $params = array(); + $params['user_id'] = $user_info['user_id']; + $params['address_id'] = $address_id; + $user_ship_address = UserShipAddress::getOne($params); + } + } else { + // 快递模式,必须有收货地址 + $params = array(); + $params['user_id'] = $user_info['user_id']; + $params['address_id'] = $address_id; + $user_ship_address = UserShipAddress::getOne($params); + if (empty($user_ship_address)) { + return fail(HttpEnumCode::HTTP_ERROR, "收货地址错误"); + } } $not_enough_product_ids = []; @@ -1710,17 +1734,17 @@ class PatientOrderService extends BaseService // 处理运费数据 $app_env = config('app_env', 'prod'); - if (env("APP_ENV") == "prod") { - // $Prescription = new Prescription(); - // $result = $Prescription->getLogisticsFee(); - // if ($freight_calculation_amount < $result['drugCost']) { - // $logistics_fee = $result['freight']; - // } - //测试环境 运费 - $logistics_fee = 7; - }else{ - //测试环境 运费 - $logistics_fee = 0; + $logistics_fee = 0.00; + if ($delivery_type == 1) { + $logistics_fee = 0.00; + } else { + $postage = (float)($pharmacy['postage'] ?? 0.00); + $threshold = (float)($pharmacy['free_shipping_threshold'] ?? 0.00); + if ($threshold > 0 && (float)$freight_calculation_amount >= $threshold) { + $logistics_fee = 0.00; + } else { + $logistics_fee = $postage; + } } // 实际支付金额=商品总金额-优惠卷金额+运费金额 @@ -1730,7 +1754,7 @@ class PatientOrderService extends BaseService (string)$coupon_amount_total, 2 ), - $logistics_fee, + (string)$logistics_fee, 2 ); @@ -1792,18 +1816,37 @@ class PatientOrderService extends BaseService $data['coupon_amount_total'] = $coupon_amount_total; // 优惠卷总金额 $data['payment_amount_total'] = $payment_amount_total; // 实际付款金额 $data['logistics_fee'] = $logistics_fee; // 运费金额 - $data['province_id'] = $user_ship_address['province_id']; - $data['province'] = $user_ship_address['province']; - $data['city_id'] = $user_ship_address['city_id']; - $data['city'] = $user_ship_address['city']; - $data['county_id'] = $user_ship_address['county_id']; - $data['county'] = $user_ship_address['county']; - $data['address'] = $user_ship_address['address']; - $data['address_mask'] = $user_ship_address['address_mask']; - $data['consignee_name'] = $user_ship_address['consignee_name']; - $data['consignee_name_mask'] = $user_ship_address['consignee_name_mask']; - $data['consignee_tel'] = $user_ship_address['consignee_tel']; - $data['consignee_tel_mask'] = $user_ship_address['consignee_tel_mask']; + $data['pharmacy_id'] = $pharmacy['pharmacy_id']; + $data['pharmacy_code'] = $pharmacy['pharmacy_code']; + $data['pharmacy_name'] = $pharmacy['pharmacy_name']; + $data['delivery_type'] = $delivery_type; + if (!empty($user_ship_address)) { + $data['province_id'] = $user_ship_address['province_id']; + $data['province'] = $user_ship_address['province']; + $data['city_id'] = $user_ship_address['city_id']; + $data['city'] = $user_ship_address['city']; + $data['county_id'] = $user_ship_address['county_id']; + $data['county'] = $user_ship_address['county']; + $data['address'] = $user_ship_address['address']; + $data['address_mask'] = $user_ship_address['address_mask']; + $data['consignee_name'] = $user_ship_address['consignee_name']; + $data['consignee_name_mask'] = $user_ship_address['consignee_name_mask']; + $data['consignee_tel'] = $user_ship_address['consignee_tel']; + $data['consignee_tel_mask'] = $user_ship_address['consignee_tel_mask']; + } else { + $data['province_id'] = 0; + $data['province'] = ""; + $data['city_id'] = 0; + $data['city'] = ""; + $data['county_id'] = 0; + $data['county'] = ""; + $data['address'] = $pharmacy['pickup_address'] ?? "到店自提"; + $data['address_mask'] = $pharmacy['pickup_address'] ?? "到店自提"; + $data['consignee_name'] = $order_prescription['patient_name'] ?? ""; + $data['consignee_name_mask'] = $order_prescription['patient_name'] ?? ""; + $data['consignee_tel'] = ""; + $data['consignee_tel_mask'] = ""; + } $order_product = OrderProduct::addOrderProduct($data); if (empty($order_product)) { Db::rollBack(); @@ -2154,7 +2197,10 @@ class PatientOrderService extends BaseService $fields = [ "order_prescription_id", - "order_inquiry_id" + "order_inquiry_id", + "pharmacy_id", + "pharmacy_code", + "pharmacy_name", ]; $params = array(); @@ -2357,19 +2403,19 @@ class PatientOrderService extends BaseService // 获取可用优惠卷总金额 $coupon_amount_total = $userCouponService->getCouponTotalPrice($user_coupons); - // 获取运费金额 - $logistics_fee = 0; - if (env("APP_ENV") == "prod") { - // $Prescription = new Prescription(); - // $result = $Prescription->getLogisticsFee(); - // if ($freight_calculation_amount < $result['drugCost']) { - // $logistics_fee = $result['freight']; - // } - //测试环境 运费 - $logistics_fee = 7; - }else{ - //测试环境 运费 - $logistics_fee = 0; + // 获取运费金额及药房信息 + $pharmacy_id = $order_prescription['pharmacy_id'] ?? 0; + $pharmacy = Pharmacy::getValidPharmacy($pharmacy_id); + + $logistics_fee = 0.00; + if (!empty($pharmacy)) { + $postage = (float)($pharmacy['postage'] ?? 0.00); + $threshold = (float)($pharmacy['free_shipping_threshold'] ?? 0.00); + if ($threshold > 0 && (float)$freight_calculation_amount >= $threshold) { + $logistics_fee = 0.00; + } else { + $logistics_fee = $postage; + } } // 实际支付金额=商品总金额-优惠卷金额+运费金额 @@ -2399,6 +2445,16 @@ class PatientOrderService extends BaseService $result['logistics_fee'] = $logistics_fee; $result['user_ship_address'] = $user_ship_address; $result['order_prescription_product'] = $order_prescription_products; + $result['pharmacy_info'] = !empty($pharmacy) ? [ + 'pharmacy_id' => (string)$pharmacy['pharmacy_id'], + 'pharmacy_code' => $pharmacy['pharmacy_code'], + 'pharmacy_name' => $pharmacy['pharmacy_name'], + 'telephone' => $pharmacy['telephone'] ?? '', + 'is_pickup' => $pharmacy['is_pickup'] ?? 0, + 'postage' => (string)($pharmacy['postage'] ?? '0.00'), + 'free_shipping_threshold' => (string)($pharmacy['free_shipping_threshold'] ?? '0.00'), + 'pickup_address' => $pharmacy['pickup_address'] ?? '', + ] : null; return success($result); } diff --git a/app/Services/UserDoctorService.php b/app/Services/UserDoctorService.php index b321dee..5cf9ff3 100644 --- a/app/Services/UserDoctorService.php +++ b/app/Services/UserDoctorService.php @@ -40,6 +40,8 @@ use App\Model\OrderServicePackageInquiry; use App\Model\PatientFollow; use App\Model\PatientHistoryInquiry; use App\Model\PatientHistoryInquiry as PatientHistoryInquiryModel; +use App\Model\DoctorPharmacy; +use App\Model\Pharmacy; use App\Model\Popup; use App\Model\Product; use App\Model\ProductPlatformAmount; @@ -1199,9 +1201,24 @@ class UserDoctorService extends BaseService $result['message'] = "成功"; $result['data'] = []; + // 校验医生是否已配置有效的默认药房 + $doctor_id = $user_info['client_user_id'] ?? 0; + $default_pharmacy = DoctorPharmacy::getDoctorDefaultPharmacy($doctor_id); + if (empty($default_pharmacy) || empty($default_pharmacy->Pharmacy) || $default_pharmacy->Pharmacy->status != 1 || empty($default_pharmacy->Pharmacy->pharmacy_code)) { + $result['status'] = 2; + $result['message'] = "您尚未配置默认药房或药房已停用,请前往【我的-药房设置】完成绑定后再开具处方"; + return success($result); + } + + $result['data']['default_pharmacy'] = [ + 'pharmacy_id' => (string)$default_pharmacy->Pharmacy->pharmacy_id, + 'pharmacy_code' => $default_pharmacy->Pharmacy->pharmacy_code, + 'pharmacy_name' => $default_pharmacy->Pharmacy->pharmacy_name, + ]; + $params = array(); $params['order_inquiry_id'] = $order_inquiry_id; - $params['doctor_id'] = $user_info['client_user_id']; + $params['doctor_id'] = $doctor_id; $order_prescription = OrderPrescription::getOne($params); if (empty($order_prescription)){ return success($result); @@ -1462,6 +1479,22 @@ class UserDoctorService extends BaseService $doctor_advice = $this->request->input('doctor_advice'); $prescription_product = $this->request->input('prescription_product'); $disease_desc = $this->request->input('disease_desc'); // 病情主诉 + $input_pharmacy_id = $this->request->input('pharmacy_id'); + + // 获取开方药房 + $pharmacy = null; + if (isset($input_pharmacy_id) && $input_pharmacy_id !== '') { + $pharmacy = Pharmacy::getValidPharmacy($input_pharmacy_id); + } else { + $doctor_default = DoctorPharmacy::getDoctorDefaultPharmacy($user_info['client_user_id'] ?? 0); + if ($doctor_default && $doctor_default->Pharmacy && $doctor_default->Pharmacy->status == 1) { + $pharmacy = $doctor_default->Pharmacy; + } + } + + if (empty($pharmacy) || empty($pharmacy['pharmacy_code'])) { + return fail(HttpEnumCode::HTTP_ERROR, "未找到有效的开方药房或药房已被停用,无法开具处方"); + } // 获取医生信息 $params = array(); @@ -1635,6 +1668,9 @@ class UserDoctorService extends BaseService $data['patient_sex'] = $order_inquiry['patient_sex']; $data['patient_age'] = $order_inquiry['patient_age']; $data['doctor_advice'] = $doctor_advice; + $data['pharmacy_id'] = $pharmacy['pharmacy_id']; + $data['pharmacy_code'] = $pharmacy['pharmacy_code']; + $data['pharmacy_name'] = $pharmacy['pharmacy_name']; $order_prescription = OrderPrescription::addOrderPrescription($data); if (empty($order_prescription)) { Db::rollBack(); @@ -1689,6 +1725,12 @@ class UserDoctorService extends BaseService return fail(HttpEnumCode::HTTP_ERROR,"药品" . $product['product_name'] . "已被删除,无法开具"); } + // 校验药品所属药房是否与处方药房一致 + if (!empty($product['pharmacy_code']) && $product['pharmacy_code'] !== $pharmacy['pharmacy_code']) { + Db::rollBack(); + return fail(HttpEnumCode::HTTP_ERROR, "药品【" . $product['product_name'] . "】不属于当前药房"); + } + // 检测药品是否超出最大可开数 if ($item['prescription_product_num'] > $product['prescription_num']) { // 库存不足 diff --git a/config/routes.php b/config/routes.php index 1ffdbde..22bfc62 100644 --- a/config/routes.php +++ b/config/routes.php @@ -18,6 +18,7 @@ use App\Controller\DetectionController; use App\Controller\DoctorAccountController; use App\Controller\DoctorAuthController; use App\Controller\DoctorInquiryConfigController; +use App\Controller\DoctorPharmacyController; use App\Controller\IndexController; use App\Controller\InquiryController; use App\Controller\LoginController; @@ -259,6 +260,15 @@ Router::addGroup('/doctor', function () { Router::get('/check', [UserDoctorController::class, 'checkOpenPrescription']); }); + // 药房 + Router::addGroup('/pharmacy', function () { + // 获取医生绑定的药房列表 + Router::get('', [DoctorPharmacyController::class, 'getDoctorPharmacyList']); + + // 设置医生默认药房 + Router::put('/default', [DoctorPharmacyController::class, 'setDoctorDefaultPharmacy']); + }); + // 常用语 Router::addGroup('/words', function () { // 获取常用语列表 @@ -772,6 +782,9 @@ Router::addGroup('/basic', function () { // 获取检测疾病分类列表 Router::get('/detection/disease', [BasicDataController::class, 'getDetectionDiseaseList']); + + // 获取公共药房列表 + Router::get('/pharmacy', [DoctorPharmacyController::class, 'getPublicPharmacyList']); }); // 获取医生评价 diff --git a/extend/Prescription/Prescription.php b/extend/Prescription/Prescription.php index 75dd198..9291177 100644 --- a/extend/Prescription/Prescription.php +++ b/extend/Prescription/Prescription.php @@ -136,13 +136,15 @@ class Prescription /** * 获取商品库存 * @param string $product_platform_code 处方平台商品编码(此处使用第三方药店商品编码!) + * @param string $pharmacy_code 药房编码 * @return array */ - public function getProdStock(string $product_platform_code): array + public function getProdStock(string $product_platform_code, string $pharmacy_code = ""): array { + $code = !empty($pharmacy_code) ? $pharmacy_code : $this->pharmacy_code; $option = [ "json" => array( - "pharmacyCode" => $this->pharmacy_code, + "pharmacyCode" => $code, "drugCode" => $product_platform_code, ), ]; @@ -165,13 +167,15 @@ class Prescription /** * 获取运费 + * @param string $pharmacy_code 药房编码 * @return array */ - public function getLogisticsFee(): array + public function getLogisticsFee(string $pharmacy_code = ""): array { + $code = !empty($pharmacy_code) ? $pharmacy_code : $this->pharmacy_code; $option = [ "json" => array( - "pharmacyCode" => $this->pharmacy_code, + "pharmacyCode" => $code, ), ];