新增取消药品订单队列,新增创建药品订单

This commit is contained in:
wucongxing 2023-03-14 14:00:13 +08:00
parent a801c11121
commit e97538a34a
5 changed files with 337 additions and 218 deletions

View File

@ -6,7 +6,12 @@ namespace App\Amqp\Consumer;
use App\Model\OrderInquiry;
use App\Model\OrderInquiryCoupon;
use App\Model\OrderPrescription;
use App\Model\OrderPrescriptionProduct;
use App\Model\OrderProduct;
use App\Model\OrderProductItem;
use App\Model\Product;
use App\Model\ProductPlatformAmount;
use App\Model\UserCoupon;
use App\Utils\Log;
use Hyperf\Amqp\Message\ConsumerDelayedMessageTrait;
@ -48,6 +53,14 @@ class CancelUnpayOrdersDelayDirectConsumer extends ConsumerMessage
if ($data['order_type'] == 1){
// 问诊订单取消
$result = $this->cancelUnpayInquiryOrder($data['order_no']);
}elseif ($data['order_type'] == 2){
// 药品订单取消
$result = $this->cancelUnpayProductOrder($data['order_no']);
}else{
Log::getInstance()->error("取消未支付订单失败order_type类型错误");
return Result::DROP;// 销毁
}
if ($result['status'] == 0){
Db::rollBack();
Log::getInstance()->error("取消未支付订单失败:" . $result['message']);
@ -57,22 +70,6 @@ class CancelUnpayOrdersDelayDirectConsumer extends ConsumerMessage
Log::getInstance()->error("取消未支付订单失败:" . $result['message']);
return Result::ACK;// 销毁
}
}elseif ($data['order_type'] == 2){
// 药品订单取消
}
Db::commit();
Log::getInstance()->error("取消未支付订单 队列执行成功");
@ -80,16 +77,16 @@ class CancelUnpayOrdersDelayDirectConsumer extends ConsumerMessage
} catch (\Exception $e) {
Db::rollBack();
Log::getInstance()->error("取消未支付订单执行失败:" . $e->getMessage());
return Result::REQUEUE; // 重回队列
return Result::ACK; // 重回队列
}
}
/**
* 取消未支付的问诊订单
* @param string $order_no 系统订单编号
* @param string|int $order_no 系统订单编号
* @return array
*/
public function cancelUnpayInquiryOrder(string $order_no): array
public function cancelUnpayInquiryOrder(string|int $order_no): array
{
$result = array();
$result['status'] = 1;
@ -175,7 +172,12 @@ class CancelUnpayOrdersDelayDirectConsumer extends ConsumerMessage
return $result;
}
public function cancelUnpayProductOrder(string $order_no): array
/**
* 取消未支付的药品订单
* @param string|int $order_no 系统订单编号
* @return array
*/
public function cancelUnpayProductOrder(string|int $order_no): array
{
$result = array();
$result['status'] = 1;
@ -184,7 +186,7 @@ class CancelUnpayOrdersDelayDirectConsumer extends ConsumerMessage
// 获取药品订单数据
$params = array();
$params['order_product_no'] = $order_no;
$order_product = OrderInquiry::getOne($params);
$order_product = OrderProduct::getOne($params);
if (empty($order_product)){
$result['status'] = 0;
$result['message'] = "取消未支付的问诊订单失败:未查询到对应订单数据";
@ -232,6 +234,76 @@ class CancelUnpayOrdersDelayDirectConsumer extends ConsumerMessage
$params['order_inquiry_id'] = $order_product['order_inquiry_id'];
OrderProduct::edit($params,$data);
//
// 获取订单商品订单列表
$params = array();
$params['order_product_id'] = $order_product['order_product_id'];
$order_product_item = OrderProductItem::getList($params);
if (empty($order_product_item)){
$result['status'] = 0;
$result['message'] = "取消未支付的问诊订单失败:未查询到对应订单商品订单列表";
return $result;
}
foreach ($order_product_item as $item){
// 释放锁定库存
$params = array();
$params['product_id'] = $item['product_id'];
$product = Product::getWithAmountOne($params);
if (empty($product)){
$result['status'] = 0;
$result['message'] = "取消未支付的问诊订单失败:未查询到对应订单商品订单列表";
return $result;
}
// 库存+1
$params = array();
$params['amount_id'] = $product['ProductPlatformAmount']['amount_id'];
ProductPlatformAmount::inc($params, 'stock', (float)$item['amount']);
// 锁定库存-1
ProductPlatformAmount::dec($params, 'lock_stock', (float)$item['amount']);
}
// 获取处方数据
$params = array();
$params['order_prescription_id'] = $order_product['order_prescription_id'];
$order_prescription = OrderPrescription::getOne($params);
if (empty($order_prescription)){
$result['status'] = 0;
$result['message'] = "取消未支付的问诊订单失败:未查询到对应订单处方";
return $result;
}
// 修改处方状态为未使用
if ($order_prescription['prescription_status'] == 4){
$data = array();
$data['prescription_status'] = 2;
$params = array();
$params['order_prescription_id'] = $order_prescription['order_prescription_id'];
OrderPrescription::edit($params, $data);
}
// 获取处方商品数据
$params = array();
$params['order_prescription_id'] = $order_prescription['order_prescription_id'];
$order_prescription_product = OrderPrescriptionProduct::getList($params);
if (empty($order_prescription_product)) {
$result['status'] = 0;
$result['message'] = "取消未支付的问诊订单失败:未查询到对应订单处方商品数据";
return $result;
}
// 修改处方商品为未使用
foreach ($order_prescription_product as $item){
$data = array();
$data['use_status'] = 0;
$params = array();
$params['prescription_product_id'] = $item['prescription_product_id'];
OrderPrescriptionProduct::edit($params, $data);
}
return $result;
}
}

View File

@ -15,6 +15,7 @@ use Hyperf\Snowflake\Concern\Snowflake;
* @property int $prescription_product_id 主键id
* @property int $order_prescription_id 订单-处方id
* @property int $product_id 商品id
* @property int $use_status 使用状态1:已使用 0:未使用)
* @property int $prescription_product_num 商品数量
* @property string $product_name 商品名称
* @property string $product_spec 商品规格
@ -41,7 +42,7 @@ class OrderPrescriptionProduct extends Model
/**
* The attributes that are mass assignable.
*/
protected array $fillable = ['prescription_product_id', 'order_prescription_id', 'product_id', 'prescription_product_num', 'product_name', 'product_spec', 'license_number', 'manufacturer', 'single_unit', 'single_use', 'packaging_unit', 'frequency_use', 'available_days', 'created_at', 'updated_at'];
protected array $fillable = ['prescription_product_id', 'order_prescription_id', 'product_id', 'use_status', 'prescription_product_num', 'product_name', 'product_spec', 'license_number', 'manufacturer', 'single_unit', 'single_use', 'packaging_unit', 'frequency_use', 'available_days', 'created_at', 'updated_at'];
protected string $primaryKey = "prescription_product_id";
/**
@ -149,4 +150,9 @@ class OrderPrescriptionProduct extends Model
->where($params)->first($fields);
}
public static function edit(array $params = [], array $data = []): int
{
return self::where($params)->update($data);
}
}

View File

@ -25,6 +25,8 @@ class PatientOrderRequest extends FormRequest
'addPatientProductOrder' => [ // 创建药品订单
"order_prescription_id",
"address_id",
"product_ids",
"client_type",
],
'getPatientPrescriptionOrderList' => [ // 获取处方订单列表
],
@ -69,6 +71,7 @@ class PatientOrderRequest extends FormRequest
'order_prescription_id' => 'required',
'address_id' => 'required',
'product_ids' => 'required|array|min:1',
];
}
@ -118,6 +121,9 @@ class PatientOrderRequest extends FormRequest
'order_no.required' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR),
'order_prescription_id.required' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR),
'address_id.required' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR),
'product_ids.required' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR),
'product_ids.array' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR),
'product_ids.min' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR),
];
}
}

View File

@ -212,15 +212,6 @@ class InquiryService extends BaseService
return fail(HttpEnumCode::HTTP_ERROR, "意向药品错误");
}
// 检测商品库存
// if (!empty($product['ProductPlatformAmount'])) {
// if ($item['product_num'] > $product['ProductPlatformAmount']['real_stock']) {
// // 此处是否需要特殊返回
// Db::rollBack();
// return fail(HttpEnumCode::HTTP_ERROR, "意向药品库存不足");
// }
// }
// 用药意向是否和过敏史重叠
if (!empty($request_params['allergy_history'])) {
$res = strpos($request_params['allergy_history'], $product['product_name']);
@ -240,15 +231,6 @@ class InquiryService extends BaseService
Db::rollBack();
return fail(HttpEnumCode::SERVER_ERROR, "订单创建失败");
}
// 锁定库存
// 实际库存-1
// $params = array();
// $params['amount_id'] = $product['ProductPlatformAmount']['amount_id'];
// ProductPlatformAmount::dec($params,'real_stock',$item['product_num']);
//
// // 锁定库存+1
// ProductPlatformAmount::inc($params,'lock_stock',$item['product_num']);
}
}
@ -284,7 +266,7 @@ class InquiryService extends BaseService
}
$result = array();
$result['inquiry_no'] = $order_inquiry['inquiry_no']; // 订单编号
$result['inquiry_no'] = (string)$order_inquiry['inquiry_no']; // 订单编号
$result['order_inquiry_id'] = $order_inquiry['order_inquiry_id']; // 订单主键id
return success($result);

View File

@ -13,6 +13,7 @@ use App\Model\OrderPrescriptionProduct;
use App\Model\OrderProduct;
use App\Model\OrderProductItem;
use App\Model\Product;
use App\Model\ProductPlatformAmount;
use App\Model\UserDoctor;
use App\Model\UserShipAddress;
use Extend\Wechat\WechatPay;
@ -715,6 +716,8 @@ class PatientOrderService extends BaseService
$order_prescription_id = $this->request->input('order_prescription_id');
$address_id = $this->request->input('address_id');
$product_ids = $this->request->input('product_ids');
$client_type = $this->request->input('client_type');
// 获取处方数据
$params = array();
@ -732,10 +735,10 @@ class PatientOrderService extends BaseService
}
if ($order_prescription['prescription_status'] == 3) {
return fail(HttpEnumCode::HTTP_ERROR,"已失效");
return fail(HttpEnumCode::HTTP_ERROR, "处方已失效");
}
if ($order_prescription['prescription_status'] == 4) {
return fail(HttpEnumCode::HTTP_ERROR,"已使用");
return fail(HttpEnumCode::HTTP_ERROR, "处方已使用");
}
if ($order_prescription['pharmacist_audit_status'] != 1) {
@ -752,14 +755,6 @@ class PatientOrderService extends BaseService
return fail(HttpEnumCode::HTTP_ERROR, "处方已使用");
}
// 获取处方药品数据
$params = array();
$params['order_prescription_id'] = $order_prescription['order_prescription_id'];
$order_prescription_product = OrderPrescriptionProduct::getList($params);
if (empty($order_prescription_product)){
return fail(HttpEnumCode::HTTP_ERROR,"处方药品错误");
}
// 检测收货地址
$params = array();
$params['user_id'] = $user_info['user_id'];
@ -769,36 +764,57 @@ class PatientOrderService extends BaseService
return fail(HttpEnumCode::HTTP_ERROR, "收货地址错误");
}
$not_enough_product_ids = [];
$amount_total = 0;
$logistics_fee = 0; // 运费金额
// 检测药品是否存在,库存是否足够,获取订单金额
foreach ($order_prescription_product as $value){
foreach ($product_ids as $value) {
// 检测药品是否存在于处方中
$params = array();
$params['product_id'] = $value['product_id'];
$params['order_prescription_id'] = $order_prescription['order_prescription_id'];
$params['product_id'] = $value;
$order_prescription_product = OrderPrescriptionProduct::getOne($params);
if (empty($order_prescription_product)) {
return fail(HttpEnumCode::HTTP_ERROR, "创建订单失败");
}
$params = array();
$params['product_id'] = $value;
$product = Product::getWithAmountOne($params);
if (empty($product)) {
Db::rollBack();
return fail(HttpEnumCode::HTTP_ERROR, "处方存在未知药品");
}
// 检测商品库存
if (!empty($product['ProductPlatformAmount'])) {
if ($value['prescription_product_num'] > $product['ProductPlatformAmount']['real_stock']) {
// 此处是否需要特殊返回
Db::rollBack();
return fail(HttpEnumCode::HTTP_ERROR, "意向药品库存不足");
if ($order_prescription_product['prescription_product_num'] > $product['ProductPlatformAmount']['stock']) {
// 库存不足
$not_enough_product_ids[] = $value;
continue;
}
}
$amount_total += $product['product_price'];
// 获取订单金额
$amount_total += $product['product_price'] * $order_prescription_product['prescription_product_num'];
}
if (!empty($not_enough_product_ids)) {
return fail(HttpEnumCode::HTTP_ERROR, "存在库存不足商品", $not_enough_product_ids);
}
// 获取运费金额
$logistics_fee = 0;
// 实际支付金额
$payment_amount_total = $amount_total + $logistics_fee;
// 确定支付渠道
// 支付渠道1:小程序支付 2:微信扫码支付)
if ($client_type == 1) {
$pay_channel = 1;
} elseif ($client_type == 2) {
$pay_channel = 2;
}
Db::beginTransaction();
$generator = $this->container->get(IdGeneratorInterface::class);
@ -813,6 +829,7 @@ class PatientOrderService extends BaseService
$data['family_id'] = $order_prescription['family_id'];
$data['order_product_no'] = $generator->generate();
$data['order_product_status'] = 1; // 订单状态1:待支付 2:待发货 3:已发货 4:已签收 5:已取消)
$data['pay_channel'] = $pay_channel; // 支付渠道1:小程序支付 2:微信扫码支付)
$data['amount_total'] = $amount_total; // 订单金额
$data['payment_amount_total'] = $payment_amount_total; // 实际付款金额
$data['logistics_fee'] = $logistics_fee; // 运费金额
@ -835,10 +852,19 @@ class PatientOrderService extends BaseService
}
// 新增药品订单详情
foreach ($order_prescription_product as $item){
foreach ($product_ids as $product_id) {
$params = array();
$params['product_id'] = $item['product_id'];
$product = Product::getOne($params);
$params['order_prescription_id'] = $order_prescription['order_prescription_id'];
$params['product_id'] = $product_id;
$order_prescription_product = OrderPrescriptionProduct::getOne($params);
if (empty($order_prescription_product)) {
Db::rollBack();
return fail(HttpEnumCode::HTTP_ERROR, "创建订单失败");
}
$params = array();
$params['product_id'] = $product_id;
$product = Product::getWithAmountOne($params);
if (empty($product)) {
Db::rollBack();
return fail(HttpEnumCode::HTTP_ERROR, "处方存在未知药品");
@ -847,23 +873,40 @@ class PatientOrderService extends BaseService
$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['order_prescription_id'] = $order_prescription['order_prescription_id'];
$data['product_id'] = $product_id;
$data['product_name'] = $order_prescription_product['product_name'];
$data['product_price'] = $product['product_price'] * $order_prescription_product['prescription_product_num'];
$data['product_platform_code'] = $product['product_platform_code'];
$data['amount'] = $item['prescription_product_num'];
$data['manufacturer'] = $item['manufacturer'];
$data['amount'] = $order_prescription_product['prescription_product_num'];
$data['manufacturer'] = $order_prescription_product['manufacturer'];
$data['product_cover_img'] = $product['product_cover_img'];
$data['product_spec'] = $item['product_spec'];
$data['product_spec'] = $order_prescription_product['product_spec'];
$order_product_item = OrderProductItem::addOrderProductItem($data);
if (!empty($order_product_item)){
if (empty($order_product_item)) {
Db::rollBack();
return fail(HttpEnumCode::SERVER_ERROR);
}
// 处方药品表标记为已使用
$data = array();
$data['use_status'] = 1;
$params = array();
$params['prescription_product_id'] = $order_prescription_product['prescription_product_id'];
OrderPrescriptionProduct::edit($params, $data);
// 锁定库存
// 库存-1
$params = array();
$params['amount_id'] = $product['ProductPlatformAmount']['amount_id'];
ProductPlatformAmount::dec($params, 'stock', $order_prescription_product['prescription_product_num']);
// 锁定库存+1
ProductPlatformAmount::inc($params, 'lock_stock', $order_prescription_product['prescription_product_num']);
}
// 修正处方使用状态
// 修改处方为已使用
$data = array();
$data['prescription_status'] = 4;
@ -873,24 +916,29 @@ class PatientOrderService extends BaseService
// 增加至取消订单延迟队列
$data = array();
$data['order_no'] = $order_product['order_product_no'];
$data['order_no'] = (string)$order_product['order_product_no'];
$data['order_type'] = 2;
$message = new CancelUnpayOrdersDelayDirectProducer($data);
$message->setDelayMs(1000 * 60 * 30);
$message->setDelayMs(1000 * 30 * 1);
$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();
return fail(HttpEnumCode::SERVER_ERROR, $e->getMessage());
}
return success();
$result = array();
$result['inquiry_no'] = $order_product['order_product_no']; // 订单编号
$result['order_product_id'] = $order_product['order_product_id']; // 订单主键id
return success($result);
}
/**
@ -1000,7 +1048,7 @@ class PatientOrderService extends BaseService
$amount_total = 0;
$coupon_amount_total = 0;
$logistics_fee = 0; // 运费金额
foreach ($order_prescription_product as $item){
foreach ($order_prescription_product as &$item) {
$params = array();
$params['product_id'] = $item['product_id'];
$product = Product::getWithAmountOne($params);
@ -1008,15 +1056,18 @@ class PatientOrderService extends BaseService
return fail(HttpEnumCode::HTTP_ERROR, "处方存在未知药品");
}
$item['status'] = 1; // 正常
// 检测商品库存
if (!empty($product['ProductPlatformAmount'])) {
if ($item['prescription_product_num'] > $product['ProductPlatformAmount']['real_stock']) {
if ($item['prescription_product_num'] > $product['ProductPlatformAmount']['stock']) {
// 库存不足
$item['status'] = 2;
continue;
}
}
$amount_total += $product['product_price'];
$amount_total += $product['product_price'] * $item['prescription_product_num'];
}
// 获取运费金额
@ -1029,6 +1080,7 @@ class PatientOrderService extends BaseService
$params['user_id'] = $user_info['user_id'];
$user_ship_addresss = UserShipAddress::getList($params);
unset($item);
foreach ($user_ship_addresss as $item) {
if ($item['is_default'] == 1) {
$user_ship_address = $item;
@ -1046,6 +1098,7 @@ class PatientOrderService extends BaseService
$result['payment_amount_total'] = $payment_amount_total;
$result['logistics_fee'] = $logistics_fee;
$result['user_ship_address'] = $user_ship_address;
$result['order_prescription_product'] = $order_prescription_product;
return success($result);
}