PatientCardFacadeRepository.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Repositories\Eloquent;
  3. use App\Models\PatientCard as BaseTable;
  4. use App\Repositories\Contracts\PatientCardInterface;
  5. use App\Facades\HisApiFacade;
  6. use Illuminate\Support\Facades\DB;
  7. use App\Facades\ThirdConsultFacade;
  8. use App\Models\User;
  9. /**
  10. * PatientCard
  11. *
  12. */
  13. class PatientCardFacadeRepository extends BaseRepository implements PatientCardInterface
  14. {
  15. public function getList(array $conditions, array $fields, string $sort, int $page, int $limit)
  16. {
  17. $result = BaseTable::where(function($query) use($conditions){
  18. if (isset($conditions['uid'])){
  19. $query->where('uid', $conditions['uid']);
  20. }
  21. })->orderByRaw($sort)->paginate($limit, $fields);
  22. return $this->response($result);
  23. }
  24. public function findBy(array $conditions, array $fields){
  25. $result = BaseTable::where(function($query) use($conditions){
  26. if (isset($conditions['id'])){
  27. $query->where('id', $conditions['id']);
  28. }
  29. if (isset($conditions['uid'])){
  30. $query->where('uid', $conditions['uid']);
  31. }
  32. })->first($fields);
  33. return $result ? $this->response($result) : $this->error()->dataDoesNotExist();
  34. }
  35. public function create(array $data)
  36. {
  37. //此用户和身份证号是否存在绑定关系
  38. $hisData = BaseTable::where('uid', $data['uid'])->where('id_number', $data['id_number'])->first(['id']);
  39. if ($hisData) {
  40. $this->error()->dataDoesExist('已存在绑定关系');
  41. }
  42. //微信帐号信息
  43. $wxInfo = User::where('id', $data['uid'])->first(['id','weichat_uid','wx_headimgurl']);
  44. if (!$wxInfo) {
  45. $this->error()->userDoesNotExist();
  46. }
  47. //身份证信息
  48. $idCardInfo = idCardInfo($data['id_number']);
  49. try {
  50. $dbData = DB::transaction(function () use ($data, $wxInfo, $idCardInfo) {
  51. if ($data['is_chat'] == BaseTable::IS_CHAT_1) {
  52. BaseTable::where('uid', $data['uid'])->update(['is_chat'=>BaseTable::IS_CHAT_0]);
  53. }
  54. //在线咨询接口交互
  55. $ThirdConsult = ThirdConsultFacade::customerCreate('', $data['id_number'], $data['name'], $idCardInfo['sex'], 90, $idCardInfo['age'], $data['phone'], $wxInfo->weichat_uid, $wxInfo->wx_headimgurl);
  56. $data['third_customer_openid'] = $ThirdConsult->getData()->data->open_id;
  57. return BaseTable::create($data);
  58. //还未知HIS结构,停止向下
  59. //HIS接口交互
  60. $address = $data['city'].$data['addiress'];
  61. $hisData = HisApiFacade::patientRegister($data['name'], $data['sex']=='男'?1:2, $data['phone'], $address, $data['id_number']);
  62. if ($hisData->getData()->code == self::SUCCESS_CODE && $ThirdConsult->getData()->code == self::SUCCESS_CODE) {
  63. $data['third_customer_openid'] = $ThirdConsult->getData()->data->open_id;
  64. $data['yktid'] = isset($hisData->getData()->data->yktid)?$hisData->getData()->data->yktid:$hisData->getData()->data->ykt_id;
  65. $data['card'] = $hisData->getData()->data->yktbh;
  66. return BaseTable::create($data);
  67. }
  68. });
  69. return $this->response($dbData);
  70. } catch (\Exception $e) {
  71. $this->setLog(self::TYPENAME.'失败', $this->startTime(), [$e->getMessage()]);
  72. $this->error()->fail();
  73. }
  74. }
  75. public function updateBy(array $conditions, array $data)
  76. {
  77. //此用户和身份证号是否存在绑定关系
  78. $hisData = BaseTable::where('id', $conditions['id'])->first();
  79. if (!$hisData) {
  80. $this->error()->dataDoesNotExist('此就诊人不存在');
  81. }
  82. //微信帐号信息
  83. $wxInfo = User::where('id', $conditions['uid'])->first(['id','weichat_uid','wx_headimgurl']);
  84. if (!$wxInfo) {
  85. $this->error()->userDoesNotExist();
  86. }
  87. //身份证信息
  88. $idCardInfo = idCardInfo($data['id_number']);
  89. try {
  90. DB::transaction(function () use ($hisData, $wxInfo, $idCardInfo, $data, $conditions) {
  91. //在线咨询接口交互 更新患者信息
  92. ThirdConsultFacade::customerCreate($hisData->third_customer_openid, $data['id_number'], $data['name'], $idCardInfo['sex'], 90, $idCardInfo['age'], $data['phone'], $wxInfo->wechat_uid, $wxInfo->wx_headimgurl);
  93. if ($data['is_chat'] == BaseTable::IS_CHAT_1) {
  94. BaseTable::where('uid', $conditions['uid'])->update(['is_chat'=>BaseTable::IS_CHAT_0]);
  95. }
  96. BaseTable::where(function($query) use($conditions){
  97. if (isset($conditions['id'])){
  98. $query->where('id', $conditions['id']);
  99. }
  100. if (isset($conditions['uid'])){
  101. $query->where('uid', $conditions['uid']);
  102. }
  103. })->update($data);
  104. });
  105. return $this->response();
  106. } catch (\Exception $e) {
  107. $this->setLog(self::TYPENAME.'更新 失败', $this->startTime(), [$e->getMessage()]);
  108. $this->error()->fail();
  109. }
  110. }
  111. public function deleteBy(array $conditions)
  112. {
  113. $result = BaseTable::where(function($query) use($conditions){
  114. if (isset($conditions['id'])){
  115. $query->where('id', $conditions['id']);
  116. }
  117. if (isset($conditions['uid'])){
  118. $query->where('uid', $conditions['uid']);
  119. }
  120. })->delete();
  121. return $result ? $this->response($result) : $this->error()->fail();
  122. }
  123. }