ParamsValidate.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace common\logic\order\validate;
  3. use api\services\MedicationRecipientService;
  4. use common\enums\AddonsEnum;
  5. use common\enums\GoodsTypeEnum;
  6. use common\logic\order\BaseOrderForm;
  7. use common\models\mall\MallAddons;
  8. use Exception;
  9. use RuntimeException;
  10. /**
  11. * 其他参数验证,例如:某个场景才是必填的,某种场景需要用的参数
  12. */
  13. class ParamsValidate implements OrderValidateInterface
  14. {
  15. /**
  16. * @param BaseOrderForm $form
  17. *
  18. * @return void
  19. * @throws Exception
  20. */
  21. public function valid(BaseOrderForm $form)
  22. {
  23. $oneExecFlag = false;
  24. $checkedGoodsIds = [];
  25. $symptom = array_column($form['data']['symptom'] ?? [], 'items', 'goods_id');
  26. foreach ($form->sku as $sku) {
  27. $this->prescriptionCheck($form, $sku, $symptom, $checkedGoodsIds, $oneExecFlag);
  28. }
  29. }
  30. /**
  31. * 处方药类型验证
  32. *
  33. * @param $sku
  34. * @param $symptom
  35. * @param $checkedGoodsIds
  36. * @param $oneExecFlag
  37. *
  38. * @return void
  39. */
  40. private function prescriptionCheck($form, $sku, $symptom, &$checkedGoodsIds, &$oneExecFlag)
  41. {
  42. // 如果是处方药需要验证必填参数
  43. if ($sku['goods']['goods_subtype'] == GoodsTypeEnum::GOODS_SUBTYPE_PRESCRIPTION) {
  44. if (isset($checkedGoodsIds[$sku['goods_id']])) {
  45. return;
  46. }
  47. if (!$oneExecFlag) {
  48. $cloudPrescriptionSettingService = new \addons\CloudPrescription\common\services\SettingService(['mall_id' => $form->mall_id]);
  49. if (
  50. !MallAddons::isInstall($form->mall_id, AddonsEnum::ADDONS_NAME_CLOUD_PRESCRIPTION) ||
  51. !$cloudPrescriptionSettingService->isEnable()
  52. ) {
  53. throw new RuntimeException('暂不支持购买处方药');
  54. }
  55. if (empty($form['data']['medication_recipient_id'])) {
  56. throw new RuntimeException('用药人必选');
  57. }
  58. // 查询用药人信息
  59. $medicationRecipientService = new MedicationRecipientService(['mall_id' => $form->mall_id, 'user_id' => $form->user_id]);
  60. $medicationRecipient = $medicationRecipientService->detail($form['data']['medication_recipient_id']);
  61. if (empty($medicationRecipient)) {
  62. throw new RuntimeException('用药人信息不存在');
  63. }
  64. // 记录用药人信息
  65. $form->process_extra_data['medication_recipient'] = $medicationRecipient;
  66. $oneExecFlag = true;
  67. }
  68. if (empty($symptom[$sku['goods_id']])) {
  69. throw new RuntimeException('疾病必选');
  70. }
  71. if (array_diff($symptom[$sku['goods_id']], json_decode($sku['goods']['extra']['symptom_list'], true))) {
  72. throw new RuntimeException('疾病选择有误');
  73. }
  74. $checkedGoodsIds[$sku['goods_id']] = true;
  75. }
  76. }
  77. }