123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace App\Repositories\Eloquent;
- use App\Models\PatientCard as BaseTable;
- use App\Repositories\Contracts\PatientCardInterface;
- use App\Facades\HisApiFacade;
- use Illuminate\Support\Facades\DB;
- use App\Facades\ThirdConsultFacade;
- use App\Models\User;
- /**
- * PatientCard
- *
- */
- class PatientCardFacadeRepository extends BaseRepository implements PatientCardInterface
- {
- public function getList(array $conditions, array $fields, string $sort, int $page, int $limit)
- {
- $result = BaseTable::where(function($query) use($conditions){
- if (isset($conditions['uid'])){
- $query->where('uid', $conditions['uid']);
- }
- })->orderByRaw($sort)->paginate($limit, $fields);
-
- return $this->response($result);
- }
-
- public function findBy(array $conditions, array $fields){
- $result = BaseTable::where(function($query) use($conditions){
- if (isset($conditions['id'])){
- $query->where('id', $conditions['id']);
- }
- if (isset($conditions['uid'])){
- $query->where('uid', $conditions['uid']);
- }
- })->first($fields);
- return $result ? $this->response($result) : $this->error()->dataDoesNotExist();
- }
-
- public function create(array $data)
- {
- //此用户和身份证号是否存在绑定关系
- $hisData = BaseTable::where('uid', $data['uid'])->where('id_number', $data['id_number'])->first(['id']);
- if ($hisData) {
- $this->error()->dataDoesExist('已存在绑定关系');
- }
-
- //微信帐号信息
- $wxInfo = User::where('id', $data['uid'])->first(['id','weichat_uid','wx_headimgurl']);
- if (!$wxInfo) {
- $this->error()->userDoesNotExist();
- }
-
- //身份证信息
- $idCardInfo = idCardInfo($data['id_number']);
-
- try {
- $dbData = DB::transaction(function () use ($data, $wxInfo, $idCardInfo) {
-
- if ($data['is_chat'] == BaseTable::IS_CHAT_1) {
- BaseTable::where('uid', $data['uid'])->update(['is_chat'=>BaseTable::IS_CHAT_0]);
- }
-
- //在线咨询接口交互
- $ThirdConsult = ThirdConsultFacade::customerCreate('', $data['id_number'], $data['name'], $idCardInfo['sex'], 90, $idCardInfo['age'], $data['phone'], $wxInfo->weichat_uid, $wxInfo->wx_headimgurl);
-
- $data['third_customer_openid'] = $ThirdConsult->getData()->data->open_id;
- return BaseTable::create($data);
- //还未知HIS结构,停止向下
- //HIS接口交互
- $address = $data['city'].$data['addiress'];
- $hisData = HisApiFacade::patientRegister($data['name'], $data['sex']=='男'?1:2, $data['phone'], $address, $data['id_number']);
-
- if ($hisData->getData()->code == self::SUCCESS_CODE && $ThirdConsult->getData()->code == self::SUCCESS_CODE) {
- $data['third_customer_openid'] = $ThirdConsult->getData()->data->open_id;
- $data['yktid'] = isset($hisData->getData()->data->yktid)?$hisData->getData()->data->yktid:$hisData->getData()->data->ykt_id;
- $data['card'] = $hisData->getData()->data->yktbh;
- return BaseTable::create($data);
- }
- });
- return $this->response($dbData);
- } catch (\Exception $e) {
- $this->setLog(self::TYPENAME.'失败', $this->startTime(), [$e->getMessage()]);
- $this->error()->fail();
- }
- }
-
- public function updateBy(array $conditions, array $data)
- {
- //此用户和身份证号是否存在绑定关系
- $hisData = BaseTable::where('id', $conditions['id'])->first();
- if (!$hisData) {
- $this->error()->dataDoesNotExist('此就诊人不存在');
- }
-
- //微信帐号信息
- $wxInfo = User::where('id', $conditions['uid'])->first(['id','weichat_uid','wx_headimgurl']);
- if (!$wxInfo) {
- $this->error()->userDoesNotExist();
- }
-
- //身份证信息
- $idCardInfo = idCardInfo($data['id_number']);
-
- try {
- DB::transaction(function () use ($hisData, $wxInfo, $idCardInfo, $data, $conditions) {
- //在线咨询接口交互 更新患者信息
- ThirdConsultFacade::customerCreate($hisData->third_customer_openid, $data['id_number'], $data['name'], $idCardInfo['sex'], 90, $idCardInfo['age'], $data['phone'], $wxInfo->wechat_uid, $wxInfo->wx_headimgurl);
-
- if ($data['is_chat'] == BaseTable::IS_CHAT_1) {
- BaseTable::where('uid', $conditions['uid'])->update(['is_chat'=>BaseTable::IS_CHAT_0]);
- }
-
- BaseTable::where(function($query) use($conditions){
- if (isset($conditions['id'])){
- $query->where('id', $conditions['id']);
- }
- if (isset($conditions['uid'])){
- $query->where('uid', $conditions['uid']);
- }
- })->update($data);
- });
- return $this->response();
- } catch (\Exception $e) {
- $this->setLog(self::TYPENAME.'更新 失败', $this->startTime(), [$e->getMessage()]);
- $this->error()->fail();
- }
- }
-
- public function deleteBy(array $conditions)
- {
- $result = BaseTable::where(function($query) use($conditions){
- if (isset($conditions['id'])){
- $query->where('id', $conditions['id']);
- }
- if (isset($conditions['uid'])){
- $query->where('uid', $conditions['uid']);
- }
- })->delete();
- return $result ? $this->response($result) : $this->error()->fail();
- }
- }
|