| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- namespace common\logic\order\validate;
- use api\services\MedicationRecipientService;
- use common\enums\AddonsEnum;
- use common\enums\GoodsTypeEnum;
- use common\logic\order\BaseOrderForm;
- use common\models\mall\MallAddons;
- use Exception;
- use RuntimeException;
- /**
- * 其他参数验证,例如:某个场景才是必填的,某种场景需要用的参数
- */
- class ParamsValidate implements OrderValidateInterface
- {
- /**
- * @param BaseOrderForm $form
- *
- * @return void
- * @throws Exception
- */
- public function valid(BaseOrderForm $form)
- {
- $oneExecFlag = false;
- $checkedGoodsIds = [];
- $symptom = array_column($form['data']['symptom'] ?? [], 'items', 'goods_id');
- foreach ($form->sku as $sku) {
- $this->prescriptionCheck($form, $sku, $symptom, $checkedGoodsIds, $oneExecFlag);
- }
- }
- /**
- * 处方药类型验证
- *
- * @param $sku
- * @param $symptom
- * @param $checkedGoodsIds
- * @param $oneExecFlag
- *
- * @return void
- */
- private function prescriptionCheck($form, $sku, $symptom, &$checkedGoodsIds, &$oneExecFlag)
- {
- // 如果是处方药需要验证必填参数
- if ($sku['goods']['goods_subtype'] == GoodsTypeEnum::GOODS_SUBTYPE_PRESCRIPTION) {
- if (isset($checkedGoodsIds[$sku['goods_id']])) {
- return;
- }
- if (!$oneExecFlag) {
- $cloudPrescriptionSettingService = new \addons\CloudPrescription\common\services\SettingService(['mall_id' => $form->mall_id]);
- if (
- !MallAddons::isInstall($form->mall_id, AddonsEnum::ADDONS_NAME_CLOUD_PRESCRIPTION) ||
- !$cloudPrescriptionSettingService->isEnable()
- ) {
- throw new RuntimeException('暂不支持购买处方药');
- }
- if (empty($form['data']['medication_recipient_id'])) {
- throw new RuntimeException('用药人必选');
- }
- // 查询用药人信息
- $medicationRecipientService = new MedicationRecipientService(['mall_id' => $form->mall_id, 'user_id' => $form->user_id]);
- $medicationRecipient = $medicationRecipientService->detail($form['data']['medication_recipient_id']);
- if (empty($medicationRecipient)) {
- throw new RuntimeException('用药人信息不存在');
- }
- // 记录用药人信息
- $form->process_extra_data['medication_recipient'] = $medicationRecipient;
- $oneExecFlag = true;
- }
- if (empty($symptom[$sku['goods_id']])) {
- throw new RuntimeException('疾病必选');
- }
- if (array_diff($symptom[$sku['goods_id']], json_decode($sku['goods']['extra']['symptom_list'], true))) {
- throw new RuntimeException('疾病选择有误');
- }
- $checkedGoodsIds[$sku['goods_id']] = true;
- }
- }
- }
|