12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Yuanhang Liu
- * Date: 20-10-15
- * Time: 下午8:46
- */
- namespace App\Http\Controllers\Api\V2;
- use App\Models\Docter;
- use App\Models\Order;
- use App\Models\Patient;
- use App\Models\CallLog;
- use App\Models\OrderPatient;
- use App\Models\DocterOrganization;
- use App\Models\SchedulePeriod;
- use App\Models\Suggest;
- use App\Models\User;
- use App\Models\UserMessages;
- use App\Models\Organization;
- use App\Http\Controllers\Api\V2\CommonController as Commons;
- use Illuminate\Support\Facades\DB;
- use App\Models\Axb;
- use App\Models\SystemConfig;
- use App\Models\CallPhone;
- use EasyWeChat\Factory;
- /**
- * 咨询订单用户类
- * Class PatientController
- * @package App\Http\Controllers\Api\V2
- */
- class PatientController extends AuthController
- {
- protected $relationSearch = true;
- /**
- * @return mixed
- * 患者咨询列表
- * @author Yuanhang Liu & Xiaoyun Liu
- */
- public function orderPatientList()
- {
- $req = request()->post();
- $this->validate(request(), [
- 'curPage' => 'required|integer',
- 'pageSize' => 'required|integer',
- ]);
- $pageSize = ($req['curPage'] - 1) * $req['pageSize'];
- $user = $this->user;
- $doctor_id = $user['id'];
- $where = [];
- $order = ['created_at', 'desc'];
- $where['docter_id'] = $doctor_id;
- if (isset($req['appointment']) && $req['appointment'] == 1) {
- // 门诊预约
- $where['product_type'] = 3;
- if (isset($req['timeSort']) && $req['timeSort'] == '0') {
- $order = ['order_patients.appoint_start_time', 'asc']; //预约时间正序
- }
- if (isset($req['timeSort']) && $req['timeSort'] == '1') {
- $order = ['order_patients.appoint_start_time', 'desc']; //预约时间倒叙
- }
- if (isset($req['timeSort']) && $req['timeSort'] == '2') {
- $order = ['created_at', 'asc']; //下单时间正序
- }
- if (isset($req['timeSort']) && $req['timeSort'] == '3') {
- $order = ['created_at', 'desc']; //下单时间倒叙
- }
- if (isset($req['comStatus']) && $req['comStatus'] == '0') {
- $comstatus = [ 2, 3,7];
- $where[] = [function ($query) use ($comstatus) {
- $query->whereIn('order_status', $comstatus);//处理中
- }];
- }
- if (isset($req['comStatus']) && $req['comStatus'] == '1') {
- $where['order_status'] = 4; //已完成
- }
- } else {
- // 图文和电话的
- $ids = [1, 2];
- $where[] = [function ($query) use ($ids) {
- $query->whereIn('product_type', $ids);
- }];
- if (isset($req['typeAll']) && $req['typeAll'] == 1) {
- $where['product_type'] = 2;//图文咨询
- }
- if (isset($req['typeAll']) && $req['typeAll'] == 2) {
- $where['product_type'] = 1;//电话咨询
- }
- if (isset($req['processing']) && $req['processing'] == 0) {
- $status = [2, 3];
- $where[] = [function ($query) use ($status) {
- $query->whereIn('order_status', $status);//处理中
- }];
- }
- if (isset($req['processing']) && $req['processing'] == 1) {
- $where['order_status'] = 4; //历史记录,已完成订单
- }
- }
- // $data = Order::With(['orderPatient'=>function($query) use ($orders){
- // $query->orderBy($orders[0],$orders[1]);
- // }])->where($where)->orderBy($order[0],$order[1])->skip($pageSize)->take($req['pageSize'])->groupBy('id')->get()->toArray();
- $data = DB::table('orders')
- ->leftJoin('order_patients', 'order_patients.order_id', '=', 'orders.id')
- ->select(['orders.*', 'order_patients.birthday as obirthday', 'order_patients.appoint_start_time', 'order_patients.name as oanme'])
- ->where($where)->orderBy($order[0], $order[1])
- ->skip($pageSize)->take($req['pageSize'])
- ->groupBy('id')
- ->get()->toArray();
- // $data = Order::with('orderPatient')->where($where)->skip($pageSize)->take($req['pageSize'])->groupBy('id')->toSql();
- // $data = OrderPatient::with("order")->where('id', $user['id'])->orderBy('id', 'desc')->limit($pageSize,$req['pageSize'])->get()->toarray();
- $list = [];
- try {
- foreach ($data as $k => $v) {
- $v = get_object_vars($v);
- if (!$v['obirthday']) {
- unset($list[$k]);
- } else {
- $list[$k]['id'] = $v['id'];
- $list[$k]['user_id'] = $v['user_id'];
- $list[$k]['order_sn'] = $v['order_sn'];
- $list[$k]['zl'] = $v['product_type'];
- $list[$k]['zt'] = $v['order_status'];
- unset($list[$k]['order_status'], $list[$k]['product_type']);
- $list[$k]['created_at'] = $v['created_at'];
- $list[$k]['appoint_time'] = empty($v['appoint_start_time']) ? '---' : date('Y-m-d H:i:s', $v['appoint_start_time']);
- $list[$k]['name'] = $v['oanme'];
- $list[$k]['birthday'] = numBirthday($v['obirthday']);
- }
- }
- return out($list);
- } catch (\Exception $e) {
- return out($e->getFile() . '中第 ' . $e->getLine() . '行发生了 ' . $e->getMessage() . '错误');
- }
- return out($list);
- }
- public function mzPutOrder()
- {
- }
- /**
- * @return mixed
- * 完成订单
- * @author Yuanhang Liu & Xiaoyun Liu
- */
- public function orderPatientok()
- {
- $req = request()->post();
- $this->validate(request(), [
- 'order_id|订单id' => 'required|integer',
- 'pathogen|病因' => 'required',
- 'suggest|建议' => 'required',
- 'patient_id|患者ID' => 'required',
- ]);
- $user = $this->user;
- $find = Order::where('id', '=', $req['order_id'])->first()->toArray();
- if (!$find) {
- return out('', 500, '订单错误!');
- }
- if ($find['order_status'] > 3) {
- return out('', 500, '此订单不可操作!');
- }
- $lable = '';
- switch ($find['product_type']) {
- case 1:
- $lable = '电话咨询';
- break;
- case 2:
- $lable = '图文咨询';
- break;
- case 3:
- $lable = '门诊预约';
- break;
- case 4:
- $lable = '疫苗接种预约';
- break;
- case 5:
- $lable = '儿保预约';
- break;
- case 6:
- $lable = '服务包';
- break;
- default:
- $lable = '参数错误!';
- }
- $relation_id = '';
- if ($find['product_type'] == 1 || $find['product_type'] == 2) {
- // 需要有意见单的
- $suggests = Suggest::create([
- 'order_id' => $find['id'],
- 'user_id' => $find['user_id'],
- 'symptoms' => $req['symptoms'],
- 'pathogen' => $req['pathogen'],
- 'suggest' => $req['suggest'],
- 'patient_id' => $req['patient_id']
- ]);
- $suggests = $suggests->toArray();
- $relation_id = $suggests['id'];
- } else {
- $relation_id = 0;
- }
- $status = $find['product_type'];
- $lab = '';
- $mesid = $relation_id;
- if ($status == 1) {
- $lab = '电话咨询';
- } else if ($status == 2) {
- $lab = '图文咨询';
- } else if ($status == 3) {
- $mesid = $find['id'];
- $lab = '门诊预约';
- }
- $order_sn = $find['order_sn'];
- $doctername = $user['name'];
- DB::beginTransaction();
- try {
- Order::where('id', '=', $req['order_id'])->update(['order_status' => 4, 'updated_at' => date('Y-m-d H:i:s', time()), 'end_time' => time()]);
- Docter::where('id', '=', $user['id'])->increment('service_persons');
- // 添加到用户记录中!
- UserMessages::create([
- 'user_id' => $find['user_id'],
- 'docter_id' => $user['id'],
- 'status' => 1,
- 'type' => 2,
- 'relation_id' => $mesid,
- 'content' => "您的 " . $lab . " 订单,订单号" . $order_sn . ",医生" . $doctername . "已经确认完成(点击查看意见单)",
- ]);
- DB::commit();
- } catch (\Exception $e) {
- DB::rollBack();
- return out('', 500, $e->getMessage());
- } catch (\PDOException $e) {
- DB::rollBack();
- return out('', 500, $e->getMessage());
- }
- $this->ReceivingReminderOK($find['id']);
- $axbwhere['docter_id'] = $user['id'];
- $axbwhere['user_id'] = $find['user_id'];
- $finds = Axb::where($axbwhere)->orderBy('id', 'desc')->first();
- if ($status == 1 && $finds) {
- $unlok = (new Commons())->unLokPhone($finds['xphone'], $finds['subs_id']);
- Axb::where(['subs_id' => $finds['subs_id']])->delete();
- }
- return out();
- }
- /**
- * 门诊预约完成订单
- * @return mixed
- * @throws \Exception
- */
- public function mzPatientok()
- {
- $req = request()->post();
- $this->validate(request(), [
- 'order_id|订单id' => 'required|integer',
- ]);
- $user = $this->user;
- $find = Order::where('id', '=', $req['order_id'])->first()->toArray();
- if (!$find) {
- return out('', 500, '订单错误!');
- }
- if ($find['order_status'] > 3) {
- return out('', 500, '此订单不可操作!');
- }
- $lable = '';
- switch ($find['product_type']) {
- case 1:
- $lable = '电话咨询';
- break;
- case 2:
- $lable = '图文咨询';
- break;
- case 3:
- $lable = '门诊预约';
- break;
- case 4:
- $lable = '疫苗接种预约';
- break;
- case 5:
- $lable = '儿保预约';
- break;
- case 6:
- $lable = '服务包';
- break;
- default:
- $lable = '参数错误!';
- }
- $relation_id = 0;
- $status = $find['product_type'];
- $lab = '';
- $mesid = $relation_id;
- if ($status == 1) {
- $lab = '电话咨询';
- } else if ($status == 2) {
- $lab = '图文咨询';
- } else if ($status == 3) {
- $mesid = $find['id'];
- $lab = '门诊预约';
- }
- $order_sn = $find['order_sn'];
- $doctername = $user['name'];
- DB::beginTransaction();
- try {
- Order::where('id', '=', $req['order_id'])->update(['order_status' => 4, 'updated_at' => date('Y-m-d H:i:s', time()), 'end_time' => time()]);
- Docter::where('id', '=', $user['id'])->increment('service_persons');
- // 添加到用户记录中!
- UserMessages::create([
- 'user_id' => $find['user_id'],
- 'docter_id' => $user['id'],
- 'status' => 1,
- 'type' => 2,
- 'relation_id' => $mesid,
- 'content' => "您的 " . $lab . " 订单,订单号" . $order_sn . ",医生" . $doctername . "已经确认完成(点击查看订单)",
- ]);
- DB::commit();
- } catch (\Exception $e) {
- DB::rollBack();
- return out('', 500, $e->getMessage());
- } catch (\PDOException $e) {
- DB::rollBack();
- return out('', 500, $e->getMessage());
- }
- $this->ReceivingReminderOK($find['id']);
- return out();
- }
- /**
- * @return mixed
- * 接单
- * @author Yuanhang Liu & Xiaoyun Liu
- */
- public function putOrderPatient()
- {
- $req = request()->post();
- $this->validate(request(), [
- 'order_id|订单id' => 'required|integer',
- ]);
- $user = $this->user;
- $find = Order::where('id', '=', $req['order_id'])->first()->toArray();
- $status = $find['product_type'];
- $lab = '';
- if ($status == 1) {
- $lab = '电话咨询';
- } else if ($status == 2) {
- $lab = '图文咨询';
- } else if ($status == 3) {
- $lab = '门诊预约';
- }
- $order_sn = $find['order_sn'];
- $order_id = $find['id'];
- $doctername = $user['name'];
- if (!$find) {
- return out('', 500, '订单错误!');
- }
- if ($find['order_status'] != 2) {
- return out('', 500, '此订单已被接单或未付款!');
- }
- if ($find['payment_status'] >= 4) {
- return out('', 500, '此订单可能已完成!');
- }
- DB::beginTransaction();
- try {
- Order::where('id', '=', $req['order_id'])->update(['order_status' => 3, 'updated_at' => date('Y-m-d H:i:s', time()), 'receiving_time' => time()]);
- // 添加到用户记录中!
- UserMessages::create([
- 'user_id' => $find['user_id'],
- 'docter_id' => $user['id'],
- 'status' => 1,
- 'type' => 1,
- 'relation_id' => $order_id,
- 'content' => "您的 " . $lab . " 订单,订单号" . $order_sn . ",医生" . $doctername . "已经确认接单(点击查看订单详情)",
- ]);
- DB::commit();
- $this->ReceivingReminder($req['order_id']);
- return out();
- } catch (\Exception $e) {
- DB::rollBack();
- return out('', 500, $e->getMessage());
- } catch (\PDOException $e) {
- DB::rollBack();
- return out('', 500, $e->getMessage());
- }
- }
- /**
- * @return mixed
- * 患者咨询详情
- * @author Yuanhang Liu & Xiaoyun Liu
- */
- public function orderPatientDetail()
- {
- $req = request()->post();
- $this->validate(request(), [
- 'patient_id' => 'required|integer',
- ]);
- $data = [];
- // 订单查询
- $data = Order::with(['orderPatient', 'user', 'calllog'])->where('id', $req['patient_id'])->first();
- // 通话记录查询
- $datas = CallLog::where('order_id', $req['patient_id'])->get();
- // 机构查询
- $user = $this->user;
- $doctor_id = (new Commons)->getUserIdByDoctorId($user['phone']);
- // 返回数组
- $res_patient = [];
- //电话咨询
- //电话咨询
- if ($data){
- $data=$data->toArray();
- }
- if ($data['product_type'] == 1) {
- $res_patient['patient_id'] = $data['order_patient']['id'];
- $res_patient['user_id'] = $data['user_id'];
- $res_patient['order_sn'] = $data['order_sn'];
- $res_patient['patient_id'] = $data['patient_id'];
- $res_patient['product_type'] = $data['product_type'];
- $res_patient['name'] = $data['order_patient']['name'];//患者姓名
- $res_patient['numbirthday'] = numBirthday($data['order_patient']['birthday']);//年龄
- $res_patient['card_number'] = $data['order_patient']['card_number'];//身份证号
- $res_patient['created_at'] = $data['created_at'];//下单时间
- $res_patient['nickname'] = $data['user']['nickname'];//下单用户
- $res_patient['receiving_time'] = !empty($data['receiving_time']) ? date('Y-m-d H:i:s', $data['receiving_time']) : '---';//接单时间
- $res_patient['order_status'] = $data['order_status'];//订单状态
- $res_patient['call_list'] = [];//通话记录
- $res_patient['one_call'] = '';
- if ($data['calllog']) {
- $res_patient['one_call'] = $data['calllog'][0]['call_time'];
- $res_patient['secret_no'] = $data['calllog'][count($data['calllog']) - 1]['secret_no'];//X号码
- foreach ($data['calllog'] as $ks => $vs) {
- $res_patient['call_list'][$ks]['frequency'] = '通话' . ($ks += 1);//拨打电话开始时间
- $res_patient['call_list'][$ks]['start'] = $vs['call_time'];//拨打电话开始时间
- $res_patient['call_list'][$ks]['end'] = $vs['ring_time'];//拨打电话结束时间
- $res_patient['call_list'][$ks]['duration'] = gmdate('i:s', $vs['talk_time']);//拨打电话结束时间
- }
- }
- }
- //图文咨询
- if ($data['product_type'] == 2) {
- $res_patient['order_sn'] = $data['order_sn'];
- $res_patient['patient_id'] = $data['patient_id'];
- $res_patient['user_id'] = $data['user_id'];
- $res_patient['user_name'] = $data['user']['nickname'];
- $res_patient['user_avatar'] = $data['user']['avatar'];
- $res_patient['sex'] = $data['order_patient']['sex'];
- $res_patient['product_type'] = $data['product_type'];
- $res_patient['name'] = $data['order_patient']['name'];
- $res_patient['numbirthday'] = numBirthday($data['order_patient']['birthday']);
- $res_patient['card_number'] = $data['order_patient']['card_number'];
- $res_patient['symptoms'] = $data['order_patient']['symptoms'];//病情描述
- $res_patient['medical_imgs'] = json_decode($data['order_patient']['medical_imgs'], true);//病情照片
- $res_patient['created_at'] = $data['created_at'];//下单时间
- $res_patient['nickname'] = $data['user']['nickname'];//下单用户
- $res_patient['receiving_time'] = !empty($data['receiving_time']) ? date('Y-m-d H:i:s', $data['receiving_time']) : '---'; //接单时间
- $res_patient['order_status'] = $data['order_status'];//接单时间
- }
- //门诊预约
- if ($data['product_type'] == 3) {
- $res_patient['order_sn'] = $data['order_sn'];
- $res_patient['patient_id'] = $data['patient_id'];
- $res_patient['product_type'] = $data['product_type'];
- $res_patient['name'] = $data['order_patient']['name']; // 患者
- $res_patient['numbirthday'] = numBirthday($data['order_patient']['birthday']); //年龄
- $res_patient['card_number'] = $data['order_patient']['card_number']; // 证件号
- $res_patient['appoint_time'] = date('Y年m月d日 H:i', $data['order_patient']['appoint_start_time']);// 预约时间
- $res_patient['order_status'] = $data['order_status'];// 订单状态
- $organization = Organization::where('id', '=', $data['organization_id'])->first();
- if ($organization) {
- $organization = $organization->toArray();
- $res_patient['organization'] = $organization['name']; // 门诊机构
- } else {
- $res_patient['organization'] = ''; // 门诊机构
- }
- $res_patient['order_sn'] = $data['order_sn'];//订单号
- $res_patient['created_at'] = $data['created_at'];//下单时间
- $res_patient['nickname'] = $data['user']['nickname'];//下单用户
- }
- return out($res_patient);
- }
- /**
- * 拨打电话/绑定电话
- * @return \Illuminate\Http\JsonResponse
- * @author Liu-Yh
- * Create By 2020/11/24 19:19
- */
- public function callPhones()
- {
- $req = request()->post();
- $user = $this->user;
- $this->validate(request(), [
- 'order_id' => 'required',
- 'user_id' => 'required|integer',
- ]);
- $docter_id = $user['id'];
- $docter_phone = $user['phone'];
- if (!$docter_phone) {
- return out('', 500, '医生电话不存在!');
- }
- $find = Order::with('orderPatient')->where('order_sn', $req['order_id'])->first()->toArray();
- if (empty($find['order_patient'])) {
- return out('', 500, '患者电话不存在');
- }
- $phone = $find['order_patient']['phone'];
- if ($docter_phone == $phone) {
- return out('', 500, '医生和患者电话号不能一样!');
- }
- $wheres['docter_id'] = $docter_id;
- $wheres['user_id'] = $req['user_id'];
- $commons = new Commons();
- $finds = Axb::where($wheres)->orderBy('id', 'desc')->first();
- if ($finds) {
- $querylok = $commons->QuerySubsId($finds['xphone']);
- if ($querylok['Code'] == 'OK') {
- // 可能是数组
- $new_arr = explode(',', $querylok['SubsId']);
- foreach ($new_arr as $v) {
- $queryCallStatus = $commons->QuerySubscriptionDetail($finds['xphone'], $v);
- if ($queryCallStatus['Code'] == "OK") {
- if ($queryCallStatus['SecretBindDetailDTO']['PhoneNoA'] == $docter_phone && $queryCallStatus['SecretBindDetailDTO']['PhoneNoB'] == $phone) {
- return out($finds['xphone']);
- } else {
- if ($phone) {
- Axb::where('id', $finds['id'])->delete();
- return $this->createCall($phone, $docter_phone, $docter_id, $req['user_id']);
- } else {
- return out('', 500, '患者电话不存在');
- }
- }
- } else {
- if ($phone) {
- Axb::where('id', $finds['id'])->delete();
- return $this->createCall($phone, $docter_phone, $docter_id, $req['user_id']);
- } else {
- return out('', 500, '患者电话不存在');
- }
- }
- }
- } else {
- if ($phone) {
- Axb::where('id', $finds['id'])->delete();
- return $this->createCall($phone, $docter_phone, $docter_id, $req['user_id']);
- } else {
- return out('', 500, '患者电话不存在');
- }
- }
- } else {
- if ($phone) {
- return $this->createCall($phone, $docter_phone, $docter_id, $req['user_id']);
- } else {
- return out('', 500, '患者电话不存在');
- }
- }
- }
- /**
- * 创建电话
- * @param $phone
- * @param $docter_phone
- * @param $docter_id
- * @param $user_id
- * @return \Illuminate\Http\JsonResponse
- */
- protected function createCall($phone, $docter_phone, $docter_id, $user_id)
- {
- $commons = new Commons();
- $callModel = $commons->BindAxb($docter_phone, $phone);
- if ($callModel['Code'] == "OK") {
- Axb::create([
- 'docter_id' => $docter_id,
- 'user_id' => $user_id,
- 'xphone' => $callModel['SecretBindDTO']['SecretNo'],
- 'subs_id' => $callModel['SecretBindDTO']['SubsId'],
- 'createtime' => time(),
- ]);
- return out($callModel['SecretBindDTO']['SecretNo']);
- } else {
- return out($callModel);
- }
- }
- /**
- * 电话随访
- * @return \Illuminate\Http\JsonResponse
- * @author Liu-Yh
- * Create By 2020/11/24 19:19
- */
- public function callPhoneSure()
- {
- $req = request()->post();
- $user = $this->user;
- $this->validate(request(), [
- 'user_id' => 'required|integer',
- ]);
- $docter_id = $user['id'];
- $docter_phone = $user['phone'];
- if (!$docter_phone) {
- return out('', 500, '医生电话不存在!');
- }
- $find = User::where('id', $req['user_id'])->first()->toArray();
- $phone = $find['phone'];
- $wheres['docter_id'] = $docter_id;
- $wheres['user_id'] = $find['id'];
- $commons = new Commons();
- $finds = Axb::where($wheres)->orderBy('id', 'desc')->first();
- if ($finds) {
- $querylok = $commons->QuerySubsId($finds['xphone']);
- if ($querylok['Code'] == 'OK') {
- $queryCallStatus = $commons->QueryCallStatus($phone, $querylok['SubsId']);
- if ($queryCallStatus['Code'] == 'OK') {
- if ($queryCallStatus['SecretCallStatusDTO'] != 4) {
- return out($finds['xphone']);
- } else {
- if ($phone) {
- $callModel = $commons->BindAxb($docter_phone, $phone);
- if ($callModel['Code'] == "OK") {
- return out($callModel['SecretBindDTO']['SecretNo']);
- }
- } else {
- return out('', 500, '患者电话不存在');
- }
- }
- }
- }
- } else {
- if ($phone) {
- $callModel = $commons->BindAxb($docter_phone, $phone);
- if ($callModel['Code'] == "OK") {
- Axb::create([
- 'docter_id' => $docter_id,
- 'user_id' => $req['user_id'],
- 'xphone' => $callModel['SecretBindDTO']['SecretNo'],
- 'createtime' => time(),
- ]);
- return out($callModel['SecretBindDTO']['SecretNo']);
- } else {
- return out($callModel);
- }
- } else {
- return out('', 500, '患者电话不存在');
- }
- }
- }
- /**
- * 取消订单接口
- * @throws \Illuminate\Validation\ValidationException
- */
- public function cancelOrder()
- {
- $req = request()->post();
- $this->validate(request(), [
- 'order_id' => 'required|integer',
- ]);
- $order = Order::with(['orderPatient'])->where('id', $req['order_id'])->first();
- if ($order['order_status'] == 2 && $order['payment_status'] == 2) {
- DB::beginTransaction();
- try {
- if ($order['product_type'] == 3) {
- Order::where('id',$req['order_id'])->update(['order_status'=>5,'payment_status'=>5,'order_notes'=>'医生拒绝接单']);
- }else{
- //退钱到余额
- if (!empty($order['payment_amount'])) {
- User::changeBalance($order['user_id'], $order['payment_amount'], 4, $order['id'], '医生拒绝接单');
- }
- Order::where('id', $req['order_id'])->update(['order_status' => 5, 'order_notes' => '医生拒绝接单', 'payment_status' => 4]);
- }
- DB::commit();
- $this->CancelReminder($req['order_id']);
- return out('', 200, '订单取消成功');
- } catch (\Exception $e) {
- DB::rollBack();
- return out('', 500, $e->getMessage());
- } catch (\PDOException $e) {
- DB::rollBack();
- return out('', 500, $e->getMessage());
- }
- } else {
- return out('', 500, '订单不可取消');
- }
- }
- /**
- * 订单超时自动完成(定时)
- */
- public function overTimeOrers(){
- // $user = $this->user;
- // $docter_id = $user['id'];
- $config_chat = SystemConfig::get('docter_config','chat_complete_time');
- $config_phone = SystemConfig::get('docter_config','phone_complete_time');
- // 换算为秒
- $config_chat = $config_chat*60;
- $config_phone = $config_phone*60;
- $inOrder = Order::with('orderPatient')->where(['order_status'=>3,'payment_status'=>2])->get();
- $catNewIds = [];
- $menNewIds = [];
- foreach ($inOrder as $k=>$v){
- if ($v['product_type']==1){
- if ((time()-$v['receiving_time'])>=$config_chat){
- $catNewIds[$k] = $v['id'];
- $this->ReceivingReminderOK($v['id']);
- }
- }else if($v['product_type']==2){
- if ((time()-$v['receiving_time'])>=$config_phone){
- $catNewIds[$k] = $v['id'];
- $this->ReceivingReminderOK($v['id']);
- }
- }else if($v['product_type']==3){
- if ((time()-$v['receiving_time'])>=(1*60*60*24)){
- $menNewIds[$k] = $v['id'];
- $this->AppointReminder($v['id']);
- }
- }
- }
- if ($catNewIds || $menNewIds){
- // 操作图文和电话订单为已完成
- Order::whereIn('id',$catNewIds)->update(['order_status'=>4]);
- // 操作门诊订单为已超时
- Order::whereIn('id',$menNewIds)->update(['order_status'=>6]);
- }
- }
- /**
- * 获取病例信息接口!
- * @author Liu
- * @return \Illuminate\Http\JsonResponse
- * @throws \Illuminate\Validation\ValidationException
- */
- public function CaseAcquisition()
- {
- $req = request()->post();
- $this->validate(request(), [
- 'user_id' => 'required|integer',
- 'order_id',
- // 'is_showAll'=>'required|integer'
- ]);
- $user = $this->user;
- $docter_id = $user['id'];
- // 订单内点击
- if (isset($req['order_id'])&&!empty($req['order_id'])){
- $data = Order::with(['orderPatient','user'])->where('id',$req['order_id'])->first();
- }else{
- $data = Order::with(['orderPatient','user'])->where(['user_id'=>$req['user_id'],'docter_id'=>$docter_id,'product_type'=>2,'order_status'=>3])->orderBy('id','desc')->first();
- }
- // 返回数组
- $res_patient = [];
- // if ($req['is_showAll']==1){
- // $list = Order::with(['orderPatient','user'])->where(['user_id'=>$req['user_id'],'docter_id'=>$docter_id,'product_type'=>2])->orderBy('id','asc')->get();
- // if ($list){
- // $list = $list->toArray();
- // foreach ($list as $k=>$v){
- // $res_patient['list'][$k]['order_sn'] = $v['order_sn'];
- // $res_patient['list'][$k]['user_id'] = $v['user_id'];
- // $res_patient['list'][$k]['user_name'] = $v['user']['nickname'];
- // $res_patient['list'][$k]['user_avatar'] = $v['user']['avatar'];
- // $res_patient['list'][$k]['sex'] = $v['order_patient']['sex'];
- // $res_patient['list'][$k]['name'] = $v['order_patient']['name'];
- // $res_patient['list'][$k]['numbirthday'] = numBirthday($v['order_patient']['birthday']);
- // $res_patient['list'][$k]['symptoms'] = $v['order_patient']['symptoms'];//病情描述
- // $res_patient['list'][$k]['medical_imgs'] = json_decode($v['order_patient']['medical_imgs'], true);//病情照片
- // }
- // }
- // }
- if ($data) {
- $data = $data->toArray();
- $res_patient['order_sn'] = $data['order_sn'];
- $res_patient['user_id'] = $data['user_id'];
- $res_patient['user_name'] = $data['user']['nickname'];
- $res_patient['user_avatar'] = $data['user']['avatar'];
- $res_patient['sex'] = $data['order_patient']['sex'];
- $res_patient['name'] = $data['order_patient']['name'];
- $res_patient['numbirthday'] = numBirthday($data['order_patient']['birthday']);
- $res_patient['symptoms'] = $data['order_patient']['symptoms'];//病情描述
- $res_patient['medical_imgs'] = json_decode($data['order_patient']['medical_imgs'], true);//病情照片
- }
- return out($res_patient);
- }
- /**
- * 修改关闭状态
- * $type 1=开启小程序,2=关闭小程序
- * @return \Illuminate\Http\JsonResponse
- * @throws \Illuminate\Validation\ValidationException
- */
- public function docter_open(){
- $req = request()->post();
- $this->validate(request(), [
- 'type' => 'required|integer|in:1,2',
- ]);
- $user = $this->user;
- $docter_id = $user['id'];
- Docter::where('id',$docter_id)->update(['is_open'=>$req['type']]);
- return out();
- }
- /**
- * 确认超时提醒 当天的预约订单,医生还未点击完成的订单,23:00给医生发送提醒
- */
- public function AppointReminder($id=''){
- if (!empty($id)){
- $Order = Order::with(['orderPatient','docter','user'])->where(['id'=>$id,'order_status'=>3,'payment_status'=>2])->first();
- $send = send_wechatSubscription_message('appoint_reminder',[$Order['docter']['openid'], "pages/index/index",$Order['order_sn'],$Order['user']['nickname'],$Order['user']['phone'],$Order['created_at']]);
- }else{
- $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
- $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
- $Order = Order::with(['orderPatient','docter','user'])->where(['order_status'=>3,'payment_status'=>2,'product_type'=>3])->whereBetween('receiving_time',[$beginToday,$endToday])->get();
- foreach ($Order as $k=>$v){
- if ($v['docter']){
- if ($v['docter']['openid']){
- $send = send_wechatSubscription_message('appoint_reminder',[$v['docter']['openid'], "pages/index/index",$v['order_sn'],$v['user']['nickname'],$v['user']['phone'],$v['created_at']]);
- }
- }
- }
- }
- }
- /**
- * 取消订单提醒
- * @param $order_id
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function CancelReminder($order_id){
- $Order = Order::with(['orderPatient','docter','user'])->where(['id'=>$order_id])->first();
- $type = '';
- if ($Order['product_type']==1){
- $type = '电话咨询';
- }elseif ($Order['product_type']==2){
- $type = '图文咨询 ';
- }elseif ($Order['product_type']==3){
- $type = '门诊预约';
- }
- if ($Order){
- if ($Order['docter']['openid']){
- $send = send_wechatSubscription_message('cancel_reminder',[
- $Order['docter']['openid'],
- "pages/index/index",
- $Order['docter']['name'],
- $Order['order_sn'],
- $type,
- $Order['total_amount'],
- $Order['created_at'],
- '医生取消订单',
- ]);
- }
- }
- }
- /**
- * 咨询订单接单提醒
- * @param $order_id
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function ReceivingReminder($order_id){
- $Order = Order::with(['orderPatient','user'])->where(['id'=>$order_id])->first()->toArray();
- $type = '';
- if ($Order['product_type']==1){
- $type = '电话咨询';
- }elseif ($Order['product_type']==2){
- $type = '图文咨询 ';
- }elseif ($Order['product_type']==3){
- $type = '门诊预约';
- }
- if ($Order){
- if ($Order['user']['openid']){
- $send = send_wechatSubscription_message('receiving_reminder',[
- $Order['user']['openid'],
- "pages/index/index",
- $Order['order_sn'],
- $type,
- date('Y-m-d H:i',$Order['receiving_time']),
- $Order['order_patient']['phone'],
- ],'wechat_small_program');
- }
- }
- }
- /**
- * 消息回复提醒(医生)
- * @param $order_id
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function ReplyReminder($docter_id,$user_id){
- $list = Docter::with(['DocterOrganization','organization'])->where(['id'=>$docter_id,'is_open'=>2])->first();
- $user = User::where('id',$user_id)->first();
- if ($list){
- if ($user['openid']){
- $send = send_wechatSubscription_message('reply_reminder',[
- $user['openid'],
- "pages/index/index",
- $list['organization'][0]['name'],
- $list['name'],
- $user['nickname'],
- ],'wechat_small_program');
- }
- }
- }
- /**
- * 排班变更提醒
- * @param $docter_id 医生id
- * @param int $type 类型0=周排班 1=月排班
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function ScheduleReminder($docter_id=81,$type=0){
- $list = Docter::where(['id'=>$docter_id])->first();
- $type = '';
- $openid = $list['openid'];
- if ($type==0){
- $type = '周排班变动';
- }elseif ($type==1){
- $type = '月排班变动 ';
- }
- if ($list){
- if ($openid){
- $send = send_wechatSubscription_message('schedule_reminder',[$openid, "pages/index/index", $type, '社区']);
- }
- }
- }
- /**
- * 审核认证提醒
- * @param $docter_id 医生id
- * @param int $msg 消息
- * @param $time 时间
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function ThenReminder($docter_id,$msg='',$time){
- $list = Docter::where(['id'=>$docter_id])->first();
- $openid = $list['openid'];
- if ($list){
- if ($openid){
- $send = send_wechatSubscription_message('then_reminder',[$openid, "pages/index/index", $list['name'],$msg,$time]);
- }
- }
- }
- /**
- * 认证到期提醒
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function OutThenReminder(){
- $list = DocterOrganization::with('docter','organization')->get();
- if($list){
- foreach ($list as $k=>$v){
- if ($v['docter']['openid']&& (strtotime($v['authentication_end_time'])-strtotime($v['authentication_time']))<=(1*60*60*24)){
- $send = send_wechatSubscription_message('out_then_reminder',[$v['docter']['openid'], "pages/index/index", $v['organization']['name'],date('Y-m-d',strtotime($v['authentication_end_time']))]);
- }
- }
- }
- }
- /**
- * 签约失效提醒
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function InvalidThenReminder(){
- $list = DocterOrganization::with('docter','organization')->get();
- if($list){
- foreach ($list as $k=>$v){
- if ($v['docter']['openid']&& time()>=strtotime($v['authentication_end_time'])){
- $send = send_wechatSubscription_message('out_then_reminder',[$v['docter']['openid'], "pages/index/index", $v['organization']['name'],date('Y-m-d',strtotime($v['authentication_end_time']))]);
- }
- }
- }
- }
- /**
- * 明日预约提醒 8点跑定时
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function TodayReminder(){
- $list = Order::with('docter','orderPatient','organization')->get();
- if($list){
- foreach ($list as $k=>$v){
- if ($v['docter']['openid']&& date('Y-m-d',$v['order_patient']['appoint_start_time'])==date("Y-m-d",strtotime("+1 day"))){
- $send = send_wechatSubscription_message('today_reminder', [
- $v['docter']['openid'],
- "pages/index/index",
- $v['docter']['name'],
- date("Y-m-d",strtotime("+1 day")),
- "门诊预约",
- count($list),
- !empty($v['organization']['name'])?$v['organization']['name']:'',
- ]);
- }
- }
- }
- }
- /**
- * 完成订单
- * @param $order_id
- */
- public function ReceivingReminderOK($order_id){
- $Order = Order::with(['orderPatient','user'])->where(['id'=>$order_id])->first();
- $type = '';
- if ($Order['product_type']==1){
- $type = '电话咨询已结束';
- }elseif ($Order['product_type']==2){
- $type = '图文咨询已结束 ';
- }elseif ($Order['product_type']==3){
- $type = '门诊预约';
- }
- if ($Order){
- if ($Order['user']['openid']){
- $send = send_wechatSubscription_message('receiving_reminderok',[
- $Order['user']['openid'],
- "pages/index",
- $Order['order_sn'],
- $type,
- $Order['payment_amount'],
- ],'wechat_small_program');
- }
- }
- }
- }
|