diff --git a/app/Model/OrderPrescription.php b/app/Model/OrderPrescription.php index b7896ea..ff7bcb5 100644 --- a/app/Model/OrderPrescription.php +++ b/app/Model/OrderPrescription.php @@ -17,6 +17,7 @@ use Hyperf\Snowflake\Concern\Snowflake; * @property int $order_inquiry_id 订单-问诊id * @property int $doctor_id 医生id * @property int $patient_id 患者id + * @property int $family_id 家庭成员id(就诊用户) * @property int $pharmacist_id 药师id * @property int $prescription_status 处方状态(1:待审核 2:待使用 3:已失效 4:已使用) * @property int $pharmacist_audit_status 药师审核状态(0:审核中 1:审核成功 2:审核驳回) @@ -34,6 +35,8 @@ use Hyperf\Snowflake\Concern\Snowflake; * @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 */ class OrderPrescription extends Model { @@ -47,7 +50,7 @@ class OrderPrescription extends Model /** * The attributes that are mass assignable. */ - protected array $fillable = ['order_prescription_id', 'order_inquiry_id', 'doctor_id', 'patient_id', 'pharmacist_id', 'prescription_status', 'pharmacist_audit_status', 'pharmacist_fail_reason', 'platform_audit_status', 'platform_fail_reason', 'is_delete', 'prescription_code', 'doctor_name', 'patient_name', 'patient_sex', 'patient_age', 'prescription_img', 'doctor_advice', 'created_at', 'updated_at']; + protected array $fillable = ['order_prescription_id', 'order_inquiry_id', 'doctor_id', 'patient_id', 'family_id', 'pharmacist_id', 'prescription_status', 'pharmacist_audit_status', 'pharmacist_fail_reason', 'platform_audit_status', 'platform_fail_reason', 'is_delete', 'prescription_code', 'doctor_name', 'patient_name', 'patient_sex', 'patient_age', 'prescription_img', 'doctor_advice', 'created_at', 'updated_at']; protected string $primaryKey = "order_prescription_id"; diff --git a/app/Model/OrderProduct.php b/app/Model/OrderProduct.php index 6b5ad13..faad5eb 100644 --- a/app/Model/OrderProduct.php +++ b/app/Model/OrderProduct.php @@ -152,4 +152,14 @@ class OrderProduct extends Model { return self::where($params)->update($data); } + + /** + * 新增 + * @param array $data + * @return \Hyperf\Database\Model\Model|OrderProduct + */ + public static function addOrderProduct(array $data): \Hyperf\Database\Model\Model|OrderProduct + { + return self::create($data); + } } diff --git a/app/Model/OrderProductItem.php b/app/Model/OrderProductItem.php index a0edc9f..82e8712 100644 --- a/app/Model/OrderProductItem.php +++ b/app/Model/OrderProductItem.php @@ -97,4 +97,14 @@ class OrderProductItem extends Model ->where($params) ->get($fields); } + + /** + * 新增 + * @param array $data + * @return \Hyperf\Database\Model\Model|OrderProductItem + */ + public static function addOrderProductItem(array $data): \Hyperf\Database\Model\Model|OrderProductItem + { + return self::create($data); + } } diff --git a/app/Services/PatientOrderService.php b/app/Services/PatientOrderService.php index 1a7fc7f..eb3fc37 100644 --- a/app/Services/PatientOrderService.php +++ b/app/Services/PatientOrderService.php @@ -2,6 +2,7 @@ namespace App\Services; +use App\Amqp\Producer\CancelUnpayOrdersDelayDirectProducer; use App\Constants\DoctorTitleCode; use App\Constants\HttpEnumCode; use App\Model\Hospital; @@ -15,7 +16,9 @@ use App\Model\Product; use App\Model\UserDoctor; use App\Model\UserShipAddress; use Extend\Wechat\WechatPay; +use Hyperf\Amqp\Producer; use Hyperf\DbConnection\Db; +use Hyperf\Snowflake\IdGeneratorInterface; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; @@ -757,7 +760,21 @@ 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,"收货地址错误"); + } + + + $amount_total = 0; + $coupon_amount_total = 0; + $logistics_fee = 0; // 运费金额 + + // 检测药品是否存在,库存是否足够,获取订单金额 foreach ($order_prescription_product as $value){ $params = array(); $params['product_id'] = $value['product_id']; @@ -765,9 +782,19 @@ class PatientOrderService extends BaseService if (empty($product)){ return fail(HttpEnumCode::HTTP_ERROR,"处方存在未知药品"); } + + $amount_total += $product['product_price']; } + // 获取运费金额 + + // 实际支付金额 + $payment_amount_total = $amount_total + $logistics_fee; + Db::beginTransaction(); + + $generator = $this->container->get(IdGeneratorInterface::class); + try { // 新增药品订单 $data = array(); @@ -775,11 +802,80 @@ class PatientOrderService extends BaseService $data['order_prescription_id'] = $order_prescription['order_prescription_id']; $data['doctor_id'] = $order_prescription['doctor_id']; $data['patient_id'] = $order_prescription['patient_id']; + $data['family_id'] = $order_prescription['family_id']; + $data['order_product_no'] = $generator->generate(); + $data['order_product_status'] = 1; // 订单状态(1:待支付 2:待发货 3:已发货 4:已签收 5:已取消) + $data['amount_total'] = $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']; + $order_product = OrderProduct::addOrderProduct($data); + if (empty($order_product)){ + Db::rollBack(); + return fail(HttpEnumCode::SERVER_ERROR); + } // 新增药品订单详情 - // 修正处方使用状态 - // 新增取消订单队列 + foreach ($order_prescription_product as $item){ + $params = array(); + $params['product_id'] = $item['product_id']; + $product = Product::getOne($params); + if (empty($product)){ + Db::rollBack(); + return fail(HttpEnumCode::HTTP_ERROR,"处方存在未知药品"); + } + $data = array(); + $data['order_product_id'] = $order_product['order_product_id']; + $data['order_inquiry_id'] = $order_prescription['order_inquiry_id']; + $data['order_prescription_id'] = $order_prescription['orderorder_prescription_id_inquiry_id']; + $data['product_id'] = $item['product_id']; + $data['product_name'] = $item['product_name']; + $data['product_price'] = $product['product_price']; + $data['product_platform_code'] = $product['product_platform_code']; + $data['amount'] = $item['prescription_product_num']; + $data['manufacturer'] = $item['manufacturer']; + $data['product_cover_img'] = $product['product_cover_img']; + $data['product_spec'] = $item['product_spec']; + $order_product_item = OrderProductItem::addOrderProductItem($data); + if (!empty($order_product_item)){ + Db::rollBack(); + return fail(HttpEnumCode::SERVER_ERROR); + } + } + + // 修正处方使用状态 + $data = array(); + $data['prescription_status'] = 4; + + $params = array(); + $params['order_prescription_id'] = $order_prescription['order_prescription_id']; + OrderPrescription::edit($params,$data); + + // 增加至取消订单延迟队列 + $data = array(); + $data['order_no'] = $order_product['order_product_no']; + $data['order_type'] = 2; + + $message = new CancelUnpayOrdersDelayDirectProducer($data); + $message->setDelayMs(1000 * 60 * 30); + $producer = $this->container->get(Producer::class); + $res = $producer->produce($message); + if (!$res) { + Db::rollBack(); + return fail(HttpEnumCode::SERVER_ERROR, "订单创建失败"); + } Db::commit(); } catch (\Exception $e) { Db::rollBack();