MedicationRecipientService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace api\services;
  3. use addons\RealAuth\common\logic\AuthLogic;
  4. use addons\RealAuth\common\models\RealAuthUser;
  5. use common\enums\AddonsEnum;
  6. use common\enums\StatusEnum;
  7. use common\models\mall\MallAddons;
  8. use common\models\user\MedicationRecipient;
  9. use Exception;
  10. use Yii;
  11. class MedicationRecipientService extends BaseService
  12. {
  13. public $user_id;
  14. /**
  15. * 用药人列表
  16. *
  17. * @return array
  18. */
  19. public function list(): array
  20. {
  21. return MedicationRecipient::listPage([
  22. 'where' => [
  23. [
  24. 'mall_id' => $this->mall_id,
  25. 'status' => StatusEnum::ENABLED,
  26. 'user_id' => $this->user_id
  27. ]
  28. ],
  29. 'select' => 'id, name, mobile, id_card, sex, age',
  30. 'order' => 'id desc'
  31. ]);
  32. }
  33. /**
  34. * 用药人详情
  35. *
  36. * @param int $id
  37. *
  38. * @return array
  39. */
  40. public function detail(int $id): array
  41. {
  42. $find = MedicationRecipient::find();
  43. $select = 'id, name, mobile, id_card, sex, age, is_allergy_history, allergy_history_details, is_medical_history, ' .
  44. 'sickness_detail, liver_function_status, kidney_function_status, pregnancy_breastfeeding_status';
  45. MedicationRecipient::paramPack([
  46. 'where' => [
  47. [
  48. 'mall_id' => $this->mall_id,
  49. 'status' => StatusEnum::ENABLED,
  50. 'user_id' => $this->user_id,
  51. 'id' => $id
  52. ]
  53. ],
  54. 'select' => $select,
  55. ], $find);
  56. return $find->asArray()->one() ?: [];
  57. }
  58. /**
  59. * 编辑/保存 用药人
  60. *
  61. * @param array $data
  62. *
  63. * @return bool
  64. */
  65. public function edit(array $data): bool
  66. {
  67. if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_REAL_AUTH)) {
  68. $config = Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_REAL_AUTH, 'basic');
  69. if (!empty($config['status'])) {
  70. try {
  71. if (!(new AuthLogic)->auth([
  72. 'user_id' => $this->user_id,
  73. 'mall_id' => $this->mall_id,
  74. 'real_name' => $data['name'],
  75. 'id_no' => $data['id_card'],
  76. 'auth_type' => RealAuthUser::USER,
  77. 'check_pass_mode' => 1
  78. ])) {
  79. $this->error = '实名验证失败';
  80. return false;
  81. }
  82. }catch (Exception $e) {
  83. if ($e->getMessage() !== '您已通过实名认证') {
  84. $this->error = $e->getMessage();
  85. return false;
  86. }
  87. }
  88. }
  89. }
  90. $data['user_id'] = $this->user_id;
  91. $data['mall_id'] = $this->mall_id;
  92. if (MedicationRecipient::setData($data) === false) {
  93. $this->error = MedicationRecipient::getStaticError();
  94. return false;
  95. }
  96. return true;
  97. }
  98. /**
  99. * 删除用药人
  100. *
  101. * @param int $id
  102. *
  103. * @return void
  104. */
  105. public function del(int $id): bool
  106. {
  107. return (bool)MedicationRecipient::updateAll(
  108. [
  109. 'updated_at' => time(),
  110. 'status' => StatusEnum::DELETE
  111. ],
  112. [
  113. 'id' => $id,
  114. 'mall_id' => $this->mall_id,
  115. 'status' => StatusEnum::ENABLED,
  116. 'user_id' => $this->user_id
  117. ]
  118. );
  119. }
  120. }