diff --git a/app/Controller/TestController.php b/app/Controller/TestController.php index 137a70f..a752847 100644 --- a/app/Controller/TestController.php +++ b/app/Controller/TestController.php @@ -327,7 +327,7 @@ class TestController extends AbstractController // } // dump(111); -// // 测试药师 + // 测试药师 // $data = array(); // $data['user_id'] = "534534546"; // $data['mobile'] = "18812345678"; @@ -351,25 +351,27 @@ class TestController extends AbstractController // } // 测试医院 - $data = array(); - $data['user_id'] = "5345345461"; - $data['mobile'] = "18221234167"; - $data['org_name'] = "成都金牛欣欣相照互联网医院有限公司"; - $data['org_number'] = "91510106MABTJY4K9R"; - - $result = $CaOnline->getCloudCert($data,'Organizational'); - - $data = array(); - $data['is_system'] = 1; - $data['type'] = 2; - $data['cert_base64'] = $result['certBase64']; - $data['cert_chain_p7'] = $result['certP7']; - $data['cert_serial_number'] = $result['certSerialnumber']; - $data['ca_pin'] = "5345345461"; - $doctor_pharmacist_cert = UserCaCert::addUserCaCert($data); - if (empty($doctor_pharmacist_cert)){ - return fail(); - } +// $data = array(); +// $data['user_id'] = "5345345461"; +// $data['mobile'] = "18221234167"; +// $data['card_name'] = "吴从兴"; +// $data['card_num'] = "372929199610075412"; +// $data['org_name'] = "成都金牛欣欣相照互联网医院有限公司"; +// $data['org_number'] = "91510106MABTJY4K9R"; +// +// $result = $CaOnline->getCloudCert($data,'Organizational'); +// +// $data = array(); +// $data['is_system'] = 1; +// $data['type'] = 2; +// $data['cert_base64'] = $result['certBase64']; +// $data['cert_chain_p7'] = $result['certP7']; +// $data['cert_serial_number'] = $result['certSerialnumber']; +// $data['ca_pin'] = "5345345461"; +// $doctor_pharmacist_cert = UserCaCert::addUserCaCert($data); +// if (empty($doctor_pharmacist_cert)){ +// return fail(); +// } return 111; } @@ -467,7 +469,44 @@ class TestController extends AbstractController } public function test_17(){ - + // 测试计算药品订单明细实际金额 +// $product_datas = array( +// array( +// "product_id" => 1, +// "product_price" => 10, +// "actual_product_price" => 10, +// "actual_quantity" => 2, +// ), +// array( +// "product_id" => 2, +// "product_price" => 20, +// "actual_product_price" => 20, +// "actual_quantity" => 2, +// ), +// array( +// "product_id" => 3, +// "product_price" => 5, +// "actual_product_price" => 5, +// "actual_quantity" => 2, +// ), +// ); +// +// $coupons = array( +// array( +// "application_scope" => 1, +// "coupon_price" => 3, +// "product_id" => "", +// ), +// array( +// "application_scope" => 5, +// "coupon_price" => 1, +// "product_id" => "1", +// ) +// ); +// +// $PatientOrderService = new PatientOrderService(); +// $product_datas = $PatientOrderService->calculateProductOrderItemActualPrice($product_datas,$coupons); +// dump($product_datas); } // 退款 diff --git a/app/Model/OrderProductCoupon.php b/app/Model/OrderProductCoupon.php index f0d2753..6dfb2b8 100644 --- a/app/Model/OrderProductCoupon.php +++ b/app/Model/OrderProductCoupon.php @@ -6,6 +6,8 @@ namespace App\Model; +use Carbon\Carbon; +use Hyperf\Database\Model\Relations\HasOne; use Hyperf\Snowflake\Concern\Snowflake; /** @@ -14,8 +16,8 @@ use Hyperf\Snowflake\Concern\Snowflake; * @property int $user_coupon_id 用户优惠卷表id * @property string $coupon_name 优惠卷名称 * @property string $coupon_use_price 优惠卷使用金额 - * @property \Carbon\Carbon $created_at 创建时间 - * @property \Carbon\Carbon $updated_at 修改时间 + * @property Carbon $created_at 创建时间 + * @property Carbon $updated_at 修改时间 */ class OrderProductCoupon extends Model { @@ -33,6 +35,14 @@ class OrderProductCoupon extends Model protected string $primaryKey = "order_coupon_id"; + /** + * 关联优惠券表 + */ + public function UserCoupon(): HasOne + { + return $this->hasOne(Coupon::class, 'user_coupon_id', 'user_coupon_id'); + } + /** * 获取信息-单条 * @param array $params diff --git a/app/Model/OrderProductItem.php b/app/Model/OrderProductItem.php index dd6b378..178d17e 100644 --- a/app/Model/OrderProductItem.php +++ b/app/Model/OrderProductItem.php @@ -18,6 +18,7 @@ use Hyperf\Snowflake\Concern\Snowflake; * @property int $product_id 商品id * @property string $product_name 商品名称 * @property string $product_price 商品价格 + * @property string $actual_product_price 实际商品价格 * @property string $product_platform_code 商品处方平台编码 * @property int $amount 数量 * @property string $manufacturer 生产厂家 @@ -25,7 +26,7 @@ use Hyperf\Snowflake\Concern\Snowflake; * @property string $product_spec 商品规格 * @property \Carbon\Carbon $created_at 创建时间 * @property \Carbon\Carbon $updated_at 修改时间 - * @property-read Product $Product + * @property-read Product|null $Product */ class OrderProductItem extends Model { @@ -39,7 +40,7 @@ class OrderProductItem extends Model /** * The attributes that are mass assignable. */ - protected array $fillable = ['product_item_id', 'order_product_id', 'order_inquiry_id', 'order_prescription_id', 'product_id', 'product_name', 'product_price', 'product_platform_code', 'amount', 'manufacturer', 'product_cover_img', 'product_spec', 'created_at', 'updated_at']; + protected array $fillable = ['product_item_id', 'order_product_id', 'order_inquiry_id', 'order_prescription_id', 'product_id', 'product_name', 'product_price', 'actual_product_price', 'product_platform_code', 'amount', 'manufacturer', 'product_cover_img', 'product_spec', 'created_at', 'updated_at']; protected string $primaryKey = "product_item_id"; diff --git a/app/Model/UserCoupon.php b/app/Model/UserCoupon.php index 1825afe..8a6eee7 100644 --- a/app/Model/UserCoupon.php +++ b/app/Model/UserCoupon.php @@ -41,7 +41,7 @@ class UserCoupon extends Model protected string $primaryKey = "user_coupon_id"; /** - * 关联医院表 + * 关联优惠券表 */ public function Coupon(): HasOne { diff --git a/app/Services/OrderPrescriptionService.php b/app/Services/OrderPrescriptionService.php index 8b19e87..d21c7c9 100644 --- a/app/Services/OrderPrescriptionService.php +++ b/app/Services/OrderPrescriptionService.php @@ -4,9 +4,11 @@ namespace App\Services; use App\Constants\HttpEnumCode; use App\Exception\BusinessException; +use App\Model\Coupon; use App\Model\OrderInquiry; use App\Model\OrderPrescriptionFile; use App\Model\OrderProduct; +use App\Model\OrderProductCoupon; use App\Model\PatientFamily; use App\Model\Product; use App\Model\UserCaCert; @@ -17,6 +19,7 @@ use App\Model\OrderPrescriptionIcd; use App\Model\OrderPrescriptionProduct; use App\Model\OrderProductItem; use App\Model\User; +use App\Model\UserCoupon; use App\Model\UserDoctor; use App\Model\UserDoctorInfo; use App\Model\UserPharmacist; @@ -28,11 +31,12 @@ use Extend\Ca\CaOffline; use Extend\Ca\CaOnline; use Extend\Prescription\Prescription; use Hyperf\Contract\LengthAwarePaginatorInterface; -use Hyperf\Utils\WaitGroup; +use \Hyperf\Coroutine\WaitGroup; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Swoole\Coroutine\Channel; use TCPDF; +use Hyperf\Coroutine; class OrderPrescriptionService extends BaseService { @@ -176,6 +180,8 @@ class OrderPrescriptionService extends BaseService * 上报处方平台 * @param string $order_product_id 商品订单id * @return bool + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ public function reportPrescription(string $order_product_id): bool { @@ -212,7 +218,7 @@ class OrderPrescriptionService extends BaseService } $wg = new WaitGroup(); - $wg->add(8); + $wg->add(9); $user = []; // 就诊患者用户数据 $patient_family = []; // 家庭成员-基本信息 @@ -222,15 +228,17 @@ class OrderPrescriptionService extends BaseService $user_pharmacist = []; // 药师数据 $user_pharmacist_info = []; // 药师详数据 $order_inquiry_case = []; // 病例数据 + $product_coupons = []; // 药品优惠券数据 $user_id = $order_inquiry['user_id']; $doctor_id = $order_prescription['doctor_id']; $family_id = $order_inquiry['family_id']; $pharmacist_id = $order_prescription['pharmacist_id']; $order_inquiry_id = $order_inquiry['order_inquiry_id']; + $order_product_id = $order_product['order_product_id']; // 获取就诊患者用户数据 - co(function () use ($wg, &$user, $user_id) { + Coroutine\co(function () use ($wg, &$user, $user_id) { $params = array(); $params['user_id'] = $user_id; $user = User::getOne($params)->toArray(); @@ -239,7 +247,7 @@ class OrderPrescriptionService extends BaseService }); // 获取家庭成员-基本信息 - co(function () use ($wg, &$patient_family, $family_id) { + Coroutine\co(function () use ($wg, &$patient_family, $family_id) { $params = array(); $params['family_id'] = $family_id; $patient_family = PatientFamily::getOne($params); @@ -250,7 +258,7 @@ class OrderPrescriptionService extends BaseService // 获取处方关联疾病数据 $order_prescription_id = $order_prescription['order_prescription_id']; - co(function () use ($wg, &$order_prescription_icd, $order_prescription_id) { + Coroutine\co(function () use ($wg, &$order_prescription_icd, $order_prescription_id) { $params = array(); $params['order_prescription_id'] = $order_prescription_id; $order_prescription_icd = OrderPrescriptionIcd::getList($params); @@ -260,7 +268,7 @@ class OrderPrescriptionService extends BaseService }); // 获取医生数据 - co(function () use ($wg, &$user_doctor, $doctor_id) { + Coroutine\co(function () use ($wg, &$user_doctor, $doctor_id) { $params = array(); $params['doctor_id'] = $doctor_id; $user_doctor = UserDoctor::getOne($params); @@ -270,7 +278,7 @@ class OrderPrescriptionService extends BaseService }); // 获取医生详情数据 - co(function () use ($wg, &$user_doctor_info, $doctor_id) { + Coroutine\co(function () use ($wg, &$user_doctor_info, $doctor_id) { $params = array(); $params['doctor_id'] = $doctor_id; $user_doctor_info = UserDoctorInfo::getOne($params); @@ -280,7 +288,7 @@ class OrderPrescriptionService extends BaseService }); // 获取药师数据 - co(function () use ($wg, &$user_pharmacist, $pharmacist_id) { + Coroutine\co(function () use ($wg, &$user_pharmacist, $pharmacist_id) { $params = array(); $params['pharmacist_id'] = $pharmacist_id; $user_pharmacist = UserPharmacist::getOne($params); @@ -290,7 +298,7 @@ class OrderPrescriptionService extends BaseService }); // 获取药师详情数据 - co(function () use ($wg, &$user_pharmacist_info, $pharmacist_id) { + Coroutine\co(function () use ($wg, &$user_pharmacist_info, $pharmacist_id) { $params = array(); $params['pharmacist_id'] = $pharmacist_id; $user_pharmacist_info = UserPharmacistInfo::getOne($params); @@ -300,7 +308,7 @@ class OrderPrescriptionService extends BaseService }); // 获取病例数据 - co(function () use ($wg, &$order_inquiry_case, $order_inquiry_id) { + Coroutine\co(function () use ($wg, &$order_inquiry_case, $order_inquiry_id) { $params = array(); $params['order_inquiry_id'] = $order_inquiry_id; $params['status'] = 1; @@ -310,6 +318,29 @@ class OrderPrescriptionService extends BaseService $wg->done(); }); + // 获取药品订单优惠券数据 + Coroutine\co(function () use ($wg, &$product_coupons, $order_product_id) { + $params = array(); + $params['order_product_id'] = $order_product_id; + $order_product_coupons = OrderProductCoupon::getList($params); + foreach ($order_product_coupons as $order_product_coupon){ + $params = array(); + $params['user_coupon_id'] = $order_product_coupon['user_coupon_id']; + $user_coupon = UserCoupon::getOne($params); + if (!empty($user_coupon)){ + $params = array(); + $params['coupon_id'] = $user_coupon['coupon_id']; + $coupon = Coupon::getOne($params); + if (!empty($coupon)){ + $product_coupons[] = $coupon->toArray(); + } + } + } + + // 计数器减一 + $wg->done(); + }); + $wg->wait(); if (empty($user)) { @@ -436,8 +467,12 @@ class OrderPrescriptionService extends BaseService $arg['presList'][0]['orderDrugList'][$key]['price'] = $product['product_price']; // 药品单价 $arg['presList'][0]['orderDrugList'][$key]['drugCount'] = $item['amount']; // 药品数量 $arg['presList'][0]['orderDrugList'][$key]['packingUnit'] = $product['packaging_unit']; // 药品单位 + $arg['presList'][0]['orderDrugList'][$key]['actualSellingPrice'] = $item['actual_product_price']; // 药品实际销售单价 } + // 处理上报处方平台药品优惠券相关数据 + $arg['promotion'] = $this->handleReportPrescriptionProductCouponData($product_coupons,$order_product_item,$order_product['order_product_no']); + $Prescription = new Prescription(); $result = $Prescription->reportPrescription($arg); if ($result['resultCode'] != "1000"){ @@ -487,4 +522,50 @@ class OrderPrescriptionService extends BaseService throw new BusinessException($e->getMessage()); } } + + /** + * 处理上报处方平台药品优惠券相关数据 + * @param array $product_coupons 药品订单优惠券,为coupon数据 + * @param array|object $order_product_item 药品订单明细 + * @param string $orderNo 药品订单编号 + * @return array + */ + public function handleReportPrescriptionProductCouponData(array $product_coupons,array|object $order_product_item,string $orderNo): array + { + $promotion = array(); + + if (empty($product_coupons)){ + return $promotion; + } + + foreach ($product_coupons as $product_coupon){ + // 适用范围(1:全场通用 2:问诊 3:按品牌适用 4:按类别适用 5:单品使用 6:全品类药品) + if ($product_coupon["application_scope"] == 1 || $product_coupon["application_scope"] == 6){ + $result = array(); + $result["type"] = "orderCoupons"; + $result["objectId"] = $orderNo; + $result["amount"] = $product_coupon["coupon_price"]; // 优惠金额 + $result["count"] = 1; + + $promotion[] = $result; + } + + if ($product_coupon["application_scope"] == 5){ + $product_ids = explode(',',$product_coupon['product_id']); + foreach ($order_product_item as $value){ + if (in_array($value['product_id'],$product_ids)){ + $result = array(); + $result["type"] = "productCoupons"; + $result["objectId"] = $value['product_platform_code']; + $result["amount"] = bcsub($value["product_price"],$value["actual_product_price"],2); // 优惠金额 + $result["count"] = 1; + + $promotion = array_push($promotion,$result); + } + } + } + } + + return $promotion; + } } \ No newline at end of file diff --git a/app/Services/PatientOrderService.php b/app/Services/PatientOrderService.php index c3b3da8..017347e 100644 --- a/app/Services/PatientOrderService.php +++ b/app/Services/PatientOrderService.php @@ -1647,6 +1647,9 @@ class PatientOrderService extends BaseService // 获取可用优惠卷总金额 $coupon_amount_total = $userCouponService->getCouponTotalPrice($user_coupons); + // 计算药品订单明细实际金额 + $product_datas = $this->calculateProductOrderItemActualPrice($product_datas,$user_coupons); + // 处理运费数据 $app_env = config('app_env', 'prod'); if ($app_env != "dev") { @@ -1767,6 +1770,7 @@ class PatientOrderService extends BaseService $data['product_id'] = $product_data['product_id']; $data['product_name'] = $product_data['product_name']; $data['product_price'] = bcmul((string)$product_data['product_price'], (string)$product_data['actual_quantity'], 3); + $data['actual_product_price'] = $product_data['actual_product_price']; $data['product_platform_code'] = $product_data['product_platform_code']; $data['amount'] = $product_data['product_num']; $data['manufacturer'] = $product_data['manufacturer']; @@ -3404,4 +3408,98 @@ class PatientOrderService extends BaseService return ""; } + + /** + * 计算药品订单明细实际金额 + * 此方法暂不考虑按品牌,按类别使用情况 + * @param array $product_datas 药品数据 + * @param array $coupons 优惠券数据 + * @return array + */ + public function calculateProductOrderItemActualPrice(array &$product_datas, array $coupons): array + { + if (empty($coupons)){ + return $product_datas; + } + + foreach ($product_datas as &$product_data){ + $product_data["actual_product_price"] = bcmul( + $product_data['actual_product_price'], + $product_data['actual_quantity'], + 3 + ); + } + + // 处理单一商品使用优惠券 + foreach ($coupons as $coupon){ + // 适用范围(1:全场通用 2:问诊 3:按品牌适用 4:按类别适用 5:单品使用 6:全品类药品) + if ($coupon["application_scope"] == 5){ + $product_ids = explode(',',$coupon['product_id']); + foreach ($product_datas as &$product_data){ + if (in_array($product_data['product_id'],$product_ids)){ + $product_data["actual_product_price"] = bcsub( + $product_data['actual_product_price'], + $coupon['coupon_price'], + 2 + ); + } + } + } + } + + // 处理订单通用优惠券 + $coupon_total_amount = 0; // 订单通用优惠券总金额 + foreach ($coupons as $coupon){ + // 适用范围(1:全场通用 2:问诊 3:按品牌适用 4:按类别适用 5:单品使用 6:全品类药品) + if ($coupon["application_scope"] == 6 || $coupon["application_scope"] == 1){ + $coupon_total_amount = bcadd($coupon_total_amount,$coupon['coupon_price'],2); + } + } + + if ($coupon_total_amount != 0){ + $product_total_amount = 0; // 订单明细总金额 + foreach ($product_datas as &$product_data){ + $product_total_amount = bcadd( + $product_total_amount, + $product_data['actual_product_price'], + 3 + ); + } + + $used_coupon_total_amount = 0; // 已使用的优惠券金额 + foreach ($product_datas as &$product_data){ + // 本商品可优惠的金额 = (商品价格 / 商品总价格) * 优惠券总金额 + $amount = bcmul( + bcdiv( + $product_data['actual_product_price'], + $product_total_amount, + 2 + ), + $coupon_total_amount, + 2 + ); + + // 分配当前商品的优惠金额 = 原金额 - 本商品可优惠的金额 + $product_data["actual_product_price"] = bcsub( + $product_data['actual_product_price'], + $amount, + 2 + ); + + $used_coupon_total_amount = bcadd($used_coupon_total_amount,$amount,2); + } + + // 处理浮点误差,将剩余金额分配到第一个商品 + $remaining_amount = bcsub($coupon_total_amount,$used_coupon_total_amount,2); + if ($remaining_amount != 0){ + $product_datas[0]['actual_product_price'] = bcsub( + $product_datas[0]["actual_product_price"], + $remaining_amount, + 2 + ); + } + } + + return $product_datas; + } } \ No newline at end of file diff --git a/extend/Ca/Ca.php b/extend/Ca/Ca.php index e621b5c..851ebc9 100644 --- a/extend/Ca/Ca.php +++ b/extend/Ca/Ca.php @@ -366,7 +366,6 @@ abstract class Ca throw new BusinessException($response->getBody()->getContents()); } $body = json_decode($response->getBody(), true); - dump($body); if (empty($body)) { // 返回值为空 throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));