DoctorController.php 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Yuanhang Liu & Xiaoyun Liu
  5. * Date: 20-10-14
  6. * Time: 下午2:50
  7. */
  8. namespace App\Http\Controllers\Api\V2;
  9. use App\Http\Controllers\Api\V1\AuthController;
  10. use App\Models\Docter;
  11. use App\Models\Office;
  12. use App\Models\Order;
  13. use App\Models\OrderPack;
  14. use App\Models\OrderPatient;
  15. use App\Models\Patient;
  16. use App\Models\Qualification;
  17. use App\Models\Suggest;
  18. use App\Models\Areas;
  19. use App\Models\Collection as Collections;
  20. use App\Models\Organization;
  21. use App\Models\DocterTimes;
  22. use App\Models\DocterOrganization;
  23. use App\Http\Controllers\Api\V2\CommonController as Commons;
  24. use App\Models\Team;
  25. use App\Models\User;
  26. use Illuminate\Support\Facades\DB;
  27. use MongoDB\BSON\ObjectId;
  28. use PhpParser\Comment\Doc;
  29. use App\Models\DocterUser;
  30. use App\Models\Schedule;
  31. use App\Models\TimePeriod;
  32. use App\Models\SchedulePeriod;
  33. use App\Models\DocterMessage;
  34. use App\Models\Evaluate;
  35. use App\Models\UserDocter;
  36. use App\Models\ServiceApply;
  37. use App\Models\SystemConfig;
  38. use App\Models\WeekSchedule;
  39. use App\Models\SelfSchedule;
  40. use App\Models\DocterSetting;
  41. use App\Models\DocterServiceTimes;
  42. use App\Models\CallLog;
  43. use App\Models\ImMessage;
  44. use App\Models\DocterLable;
  45. use App\Models\SchedueFlags as Sflag;
  46. /**
  47. * 医生相关类
  48. * Auth Yuanhang Liu
  49. * Date 2020/10/14 15:06
  50. * Class DoctorController
  51. * @package App\Http\Controllers\Api\V2
  52. */
  53. class DoctorController extends AuthController
  54. {
  55. /**service_schedule
  56. * @return \Illuminate\Http\JsonResponse
  57. * 首页医生个人信息接口
  58. */
  59. public function doctorInfor()
  60. {
  61. $req = $this->user;
  62. $data = Docter::where('phone', $req['phone'])->first(['id', 'name', 'avatar', 'label', 'sign', 'intro', 'service_days', 'service_persons','password', 'eva_num', 'score', 'is_then', 'created_at']);
  63. if (!empty($data)) {
  64. // 获取标签
  65. $LabFind = [];
  66. if ($data['label']!='无'){
  67. if(count($data['label'])!=0){
  68. $labstr = $data['label'];
  69. $LabFind = DocterLable::whereIn('id',$labstr)->where('status',1)->select('label_name')->get();
  70. }
  71. }
  72. $data['label'] = $LabFind;
  73. // $counts = Evaluate::where('docter_id', '=', $req['id'])->count();
  74. $evalList = Evaluate::where('docter_id', '=', $req['id'])->get();
  75. $counts = count($evalList);
  76. $data['eva_num'] = $counts;
  77. $soc = 0;
  78. if ($counts != 0) {
  79. $evalList = $evalList->toArray();
  80. foreach ($evalList as $k => $v) {
  81. $soc += $v['score'];
  82. }
  83. $data['score'] = upDecimal($soc / $counts, 1);
  84. }
  85. $data['service_days'] = numDays(date('Y-m-d', strtotime($data['created_at'])));
  86. return out($data);
  87. } else {
  88. return exit_out($data, 1, '参数错误,数据不存在');
  89. }
  90. }
  91. /**
  92. * 医生端个人信息修改
  93. * @return mixed
  94. */
  95. public function doctorInfoEdit()
  96. {
  97. $user = $this->user;
  98. $doctor_id = $user['id'];
  99. if (request()->isMethod('post')) {
  100. $req = request()->post();
  101. $this->validate(request(), [
  102. 'avatar' => 'string',
  103. 'sign' => 'max:255',
  104. // 'phone|手机号不正确' => 'regex:/^1[3456789][0-9]{9}$/',
  105. 'intro' => 'max:2555'
  106. ]);
  107. $data = [];
  108. if (isset($req['avatar']) && !empty($req['avatar'])) {
  109. $data['avatar'] = $req['avatar'];
  110. }
  111. if (isset($req['sign']) && !empty($req['sign'])) {
  112. $data['sign'] = $req['sign'];
  113. }
  114. if (isset($req['intro']) && !empty($req['intro'])) {
  115. $data['intro'] = $req['intro'];
  116. }
  117. if (empty($data)) {
  118. return out();
  119. }
  120. if ($doctor_id) {
  121. $save = Docter::where('id', $doctor_id)->update($data);
  122. return out();
  123. }
  124. } else {
  125. // get提交哈
  126. $list = Docter::where('id', '=', $doctor_id)->first()->toArray();
  127. // 头像,个人标签,服务简介展示
  128. $data = [
  129. 'avatar' => !empty($list['avatar'])?$list['avatar']:'',
  130. 'sign' => !empty($list['sign'])?$list['sign']:'',
  131. 'intro' => !empty($list['intro'])?$list['intro']:'',
  132. ];
  133. return out($data);
  134. }
  135. }
  136. /**
  137. * 修改密码
  138. * @return \Illuminate\Http\JsonResponse
  139. * @author Liu-Yh
  140. * Create By 2020/11/4 18:19
  141. */
  142. public function passwordEdit()
  143. {
  144. $user = $this->user;
  145. $doctor_id = $user['id'];
  146. $req = request()->post();
  147. $this->validate(request(), [
  148. 'lis:原密码' => 'string',
  149. 'newpassword:新密码' => 'string',
  150. ]);
  151. $data = [];
  152. $data = Docter::where('id', '=', $doctor_id)->first()->toArray();
  153. $password = md5(md5(md5($req['newpassword'])));
  154. if ($data['password'] == '' || !$data['password']) {
  155. // 说明第一次直接跳过
  156. $save = Docter::where('id', $doctor_id)->update(['password' => $password]);
  157. return out();
  158. } else {
  159. if(isset($req['lis'])&&!empty($req['lis'])){
  160. $oldpassword = md5(md5(md5($req['lis'])));
  161. if ($oldpassword != $data['password']) {
  162. return out('', '401', '旧密码错误');
  163. } else {
  164. $save = Docter::where('id', $doctor_id)->update(['password' => $password]);
  165. return out();
  166. }
  167. }else{
  168. return out('', '401', '请输入旧密码');
  169. }
  170. }
  171. }
  172. /**
  173. * 获取基本设置
  174. * @return mixed
  175. */
  176. public function getSettingBase(){
  177. $user = $this->user;
  178. $doctor_id = (new Commons)->getUserIdByDoctorId($user['phone']);
  179. $finds = Docter::select(['is_chat', 'is_phone', 'is_appoint'])->where('id', $doctor_id)->first()->toArray();
  180. $finds['is_chat'] = $finds['is_chat'] == 0 ? false : true;
  181. $finds['is_phone'] = $finds['is_phone'] == 0 ? false : true;
  182. $finds['is_appoint'] = $finds['is_appoint'] == 0 ? false : true;
  183. $finds['is_chat_status'] = $this->getStyStatus(1);
  184. $finds['is_phone_status'] = $this->getStyStatus(2);
  185. $finds['is_appoint_status'] = $this->getStyStatus(3);
  186. return out($finds);
  187. }
  188. /**
  189. * 获取图文设置
  190. * @return mixed
  191. */
  192. public function getSettingChat(){
  193. $user = $this->user;
  194. $doctor_id = $user['id'];
  195. (array)$list = [];
  196. $finds = Docter::select(['is_chat', 'is_phone', 'is_appoint'])->where('id', $doctor_id)->first()->toArray();
  197. $list['is_chat'] = $finds['is_chat'] == 0 ? false : true;
  198. // 图文咨询时间
  199. $pic_fins = DocterTimes::where(['docter_id' => $doctor_id, 'type' => 2])->first();
  200. if ($pic_fins) {
  201. $pic_fins = $pic_fins->toArray();
  202. $pic_fins['service_time'] = json_decode($pic_fins['service_time']);
  203. } else {
  204. $pic_fins = [];
  205. }
  206. $list['list'] = $pic_fins;
  207. return out($list);
  208. }
  209. /**
  210. * 获取电话的设置
  211. * @return mixed
  212. */
  213. public function getSettingPhone(){
  214. $user = $this->user;
  215. $doctor_id = $user['id'];
  216. (array)$list = [];
  217. $finds = Docter::select(['is_chat', 'is_phone', 'is_appoint'])->where('id', $doctor_id)->first()->toArray();
  218. $list['is_phone'] = $finds['is_phone'] == 0 ? false : true;
  219. // 图文咨询时间
  220. $phone_fins = DocterTimes::where(['docter_id' => $doctor_id, 'type' => 1])->first();
  221. if ($phone_fins) {
  222. $phone_fins = $phone_fins->toArray();
  223. $phone_fins['service_time'] = json_decode($phone_fins['service_time']);
  224. } else {
  225. $phone_fins = [];
  226. }
  227. $list['list'] = $phone_fins;
  228. return out($list);
  229. }
  230. /**
  231. * 获取门诊的设置
  232. * @return mixed
  233. */
  234. public function getSettingSchedule(){
  235. $user = $this->user;
  236. $doctor_id = $user['id'];
  237. (array)$list = [];
  238. $finds = Docter::select(['is_chat', 'is_phone', 'is_appoint'])->where('id', $doctor_id)->first()->toArray();
  239. $list['is_schedule'] = $finds['is_appoint'] == 0 ? false : true;
  240. $newLists=[];
  241. $men_fins = DocterSetting::where(['docter_id' => $doctor_id, 'type' => 1])->orderBy('org_id','asc')->get();
  242. $org = DocterOrganization::with('organization')->where(['docter_id'=>$doctor_id,'state'=>1])->orderBy('organization_id','asc')->get();
  243. if ($org) {
  244. $org = $org->toArray();
  245. foreach ($org as $k => $v) {
  246. $org_pre[$k]['id'] = $v['organization']['id'];
  247. $org_pre[$k]['name'] = $v['organization']['name'];
  248. }
  249. }
  250. if ($men_fins) {
  251. $men_fins = $men_fins->toArray();
  252. foreach ($men_fins as $k => $v) {
  253. $men_fins[$k]['service_time'] = json_decode($v['service_time'],true);
  254. }
  255. $org_list=[];
  256. foreach ($org as $k=>$v){
  257. $org_list[$k]['docter_id'] = $doctor_id;
  258. $org_list[$k]['type'] = 1;
  259. $org_list[$k]['status'] = 2;
  260. $org_list[$k]['show_days'] = '';
  261. $org_list[$k]['service_times'] = '';
  262. $org_list[$k]['service_num'] = '';
  263. $org_list[$k]['org_id'] = $v['organization']['id'];
  264. $org_list[$k]['org_name'] = $v['organization']['name'];
  265. $org_list[$k]['appoint_price'] = '';
  266. $org_list[$k]['service_time'] = [
  267. 1=>['amStartTime'=>'','amEndTime'=>'','amPerson'=>''],
  268. 2=>['pmStartTime'=>'','pmEndTime'=>'','pmPerson'=>''],
  269. 3=>['nightStartTime'=>'','nightEndTime'=>'','nightPerson'=>''],
  270. ];
  271. }
  272. foreach ($org_list as $k=>$v){
  273. foreach ($men_fins as $kk=>$vv){
  274. if ($v['org_id']==$vv['org_id']){
  275. $org_list[$k]['docter_id'] = $doctor_id;
  276. $org_list[$k]['type'] = 1;
  277. $org_list[$k]['status'] = 2;
  278. $org_list[$k]['show_days'] = $vv['show_days'];
  279. $org_list[$k]['service_times'] = $vv['service_times'];
  280. $org_list[$k]['service_num'] = $vv['service_num'];
  281. $org_list[$k]['org_id'] = $vv['org_id'];
  282. $org_list[$k]['org_name'] = $v['org_name'];
  283. $org_list[$k]['appoint_price'] = $vv['appoint_price'];
  284. $org_list[$k]['service_time'] = [
  285. 1=>['amStartTime'=>$vv['service_time'][1]['amStartTime'],'amEndTime'=>$vv['service_time'][1]['amEndTime'],'amPerson'=>$vv['service_time'][1]['amPerson']],
  286. 2=>['pmStartTime'=>$vv['service_time'][2]['pmStartTime'],'pmEndTime'=>$vv['service_time'][2]['pmEndTime'],'pmPerson'=>$vv['service_time'][2]['pmPerson']],
  287. 3=>['nightStartTime'=>$vv['service_time'][3]['nightStartTime'],'nightEndTime'=>$vv['service_time'][3]['nightEndTime'],'nightPerson'=>$vv['service_time'][3]['nightPerson']],
  288. ];
  289. }
  290. }
  291. }
  292. $newLists = $org_list;
  293. } else {
  294. $men_fins = [];
  295. foreach ($org as $k=>$v){
  296. $newLists[$k]['docter_id'] = $doctor_id;
  297. $newLists[$k]['type'] = 1;
  298. $newLists[$k]['status'] = 2;
  299. $newLists[$k]['show_days'] = '';
  300. $newLists[$k]['service_times'] = '';
  301. $newLists[$k]['service_num'] = '';
  302. $newLists[$k]['org_id'] = $v['organization']['id'];
  303. $newLists[$k]['org_name'] = $v['organization']['name'];
  304. $newLists[$k]['appoint_price'] = '';
  305. $newLists[$k]['service_time'] = [
  306. 1=>['amStartTime'=>'','amEndTime'=>'','amPerson'=>''],
  307. 2=>['pmStartTime'=>'','pmEndTime'=>'','pmPerson'=>''],
  308. 3=>['nightStartTime'=>'','nightEndTime'=>'','nightPerson'=>''],
  309. ];
  310. }
  311. }
  312. $list['list'] = $newLists;
  313. return out($list);
  314. }
  315. /**
  316. * 获取审核状态
  317. * @param $type
  318. * @return bool
  319. */
  320. public function getStyStatus($type)
  321. {
  322. $user = $this->user;
  323. $doctor_id = $user['id'];
  324. $where['docter_id'] = $doctor_id;
  325. $where['service_type'] = $type;
  326. $statusD = ServiceApply::where($where)->first();
  327. if ($statusD) {
  328. return $statusD;
  329. } else {
  330. return false;
  331. }
  332. }
  333. /**
  334. * 修改基本设置
  335. * @return mixed
  336. */
  337. public function saveSettingBase(){
  338. $req = request()->post();
  339. $data = json_decode($req['data'], true);
  340. $user = $this->user;
  341. $doctor_id = $user['id'];
  342. $list = [];
  343. isset($data['is_chat']) ? $data['is_chat'] == true ? $list['is_chat'] = 1 : $list['is_chat'] = 0 : '';
  344. isset($data['is_phone']) ? $data['is_phone'] == true ? $list['is_phone'] = 1 : $list['is_phone'] = 0 : '';
  345. isset($data['is_appoint']) ? $data['is_appoint'] == true ? $list['is_appoint'] = 1 : $list['is_appoint'] = 0 : '';
  346. Docter::where('id', $doctor_id)->update($list);
  347. return out();
  348. }
  349. /**
  350. * 修改图文设置
  351. */
  352. public function saveSettingChat(){
  353. $req = request()->post();
  354. $data = json_decode($req['data'], true);
  355. $user = $this->user;
  356. $doctor_id = $user['id'];
  357. $finds = Docter::select(['is_chat', 'is_phone', 'is_appoint'])->where('id', $doctor_id)->first();
  358. Db::beginTransaction();
  359. try {
  360. // 修改更多信息
  361. if ($finds['is_chat'] == 0) {
  362. if (isset($data['is_chat']) && !empty($data['is_chat'])) {
  363. return out('', 500, '您未开启图文');
  364. unset($data['is_chat']);
  365. }
  366. } else {
  367. if (isset($data['is_chat']) && !empty($data['is_chat'])) {
  368. DocterServiceTimes::where(['docter_id'=>$doctor_id,'type'=>2])->delete();
  369. $save_data = $data['is_chat'];
  370. $newData = [];
  371. $serviceData = [];
  372. $newData['base_price'] = $save_data['base_price'];
  373. $newData['step_price'] = $save_data['step_price'];
  374. $service_times = [
  375. 1 => ['amStartTime' => $save_data['amStartTime'], 'amEndTime' => $save_data['amEndTime']],
  376. 2 => ['pmStartTime' => $save_data['pmStartTime'], 'pmEndTime' => $save_data['pmEndTime']],
  377. 3 => ['nightStartTime' => $save_data['nightStartTime'], 'nightEndTime' => $save_data['nightEndTime']],
  378. ];
  379. if($save_data['amStartTime']&&$save_data['amEndTime']){
  380. $serviceData[0]['docter_id']=$doctor_id;
  381. $serviceData[0]['type']=2;
  382. $serviceData[0]['start_time_period']=$save_data['amStartTime'];
  383. $serviceData[0]['end_time_period']=$save_data['amEndTime'];
  384. $serviceData[0]['start_time_line']=str_replace(':','',$save_data['amStartTime']);
  385. $serviceData[0]['end_time_line']=str_replace(':','',$save_data['amEndTime']);
  386. }else{
  387. unset($service_times[1]);
  388. }
  389. if($save_data['pmStartTime']&&$save_data['pmEndTime']){
  390. $serviceData[1]['docter_id']=$doctor_id;
  391. $serviceData[1]['type']=2;
  392. $serviceData[1]['start_time_period']=$save_data['pmStartTime'];
  393. $serviceData[1]['end_time_period']=$save_data['pmEndTime'];
  394. $serviceData[1]['start_time_line']=str_replace(':','',$save_data['pmStartTime']);
  395. $serviceData[1]['end_time_line']=str_replace(':','',$save_data['pmEndTime']);
  396. }else{
  397. unset($service_times[2]);
  398. }
  399. if($save_data['nightStartTime']&&$save_data['nightEndTime']){
  400. $serviceData[2]['docter_id']=$doctor_id;
  401. $serviceData[2]['type']=2;
  402. $serviceData[2]['start_time_period']=$save_data['nightStartTime'];
  403. $serviceData[2]['end_time_period']=$save_data['nightEndTime'];
  404. $serviceData[2]['start_time_line']=str_replace(':','',$save_data['nightStartTime']);
  405. $serviceData[2]['end_time_line']=str_replace(':','',$save_data['nightEndTime']);
  406. }else{
  407. unset($service_times[3]);
  408. }
  409. $newData['service_time'] = !empty($service_times)?json_encode($service_times, JSON_UNESCAPED_UNICODE):'';
  410. $newData['person'] = $save_data['person'];
  411. $newData['type'] = $save_data['type'];
  412. $pic_fins = DocterTimes::where(['docter_id' => $doctor_id, 'type' => 2])->first();
  413. DocterServiceTimes::insert($serviceData);
  414. if (empty($pic_fins)) {
  415. $newData['docter_id'] = $doctor_id;
  416. DocterTimes::create($newData);
  417. } else {
  418. $pic_fins = $pic_fins->toArray();
  419. DocterTimes::where(['id' => $pic_fins['id']])->update($newData);
  420. }
  421. }
  422. }
  423. DB::commit();
  424. return out();
  425. } catch (\Exception $e) {
  426. DB::rollBack();
  427. return out('', 500, $e->getMessage());
  428. } catch (\PDOException $e) {
  429. DB::rollBack();
  430. return out('', 500, $e->getMessage());
  431. }
  432. }
  433. /**
  434. * 电话设置修改
  435. * @return mixed
  436. * @throws \Exception
  437. */
  438. public function saveSettingPhone(){
  439. $req = request()->post();
  440. $data = json_decode($req['data'], true);
  441. $user = $this->user;
  442. $doctor_id = $user['id'];
  443. $finds = Docter::select(['is_chat', 'is_phone', 'is_appoint'])->where('id', $doctor_id)->first();
  444. Db::beginTransaction();
  445. try {
  446. if ($finds['is_phone'] == 0) {
  447. if (isset($data['is_phone']) && !empty($data['is_phone'])) {
  448. return out('', 500, '您未开启电话');
  449. unset($data['is_phone']);
  450. }
  451. } else {
  452. if (isset($data['is_phone']) && !empty($data['is_phone'])) {
  453. // 存入状态为1的操作
  454. DocterServiceTimes::where(['docter_id'=>$doctor_id,'type'=>1])->delete();
  455. $save_data = $data['is_phone'];
  456. $newData = [];
  457. $serviceData = [];
  458. $newData['base_price'] = $save_data['base_price'];
  459. $newData['step_price'] = $save_data['step_price'];
  460. $service_times = [
  461. 1 => ['amStartTime' => $save_data['amStartTime'], 'amEndTime' => $save_data['amEndTime']],
  462. 2 => ['pmStartTime' => $save_data['pmStartTime'], 'pmEndTime' => $save_data['pmEndTime']],
  463. 3 => ['nightStartTime' => $save_data['nightStartTime'], 'nightEndTime' => $save_data['nightEndTime']],
  464. ];
  465. if($save_data['amStartTime']&&$save_data['amEndTime']){
  466. $serviceData[0]['docter_id']=$doctor_id;
  467. $serviceData[0]['type']=2;
  468. $serviceData[0]['start_time_period']=$save_data['amStartTime'];
  469. $serviceData[0]['end_time_period']=$save_data['amEndTime'];
  470. $serviceData[0]['start_time_line']=str_replace(':','',$save_data['amStartTime']);
  471. $serviceData[0]['end_time_line']=str_replace(':','',$save_data['amEndTime']);
  472. }else{
  473. unset($service_times[1]);
  474. }
  475. if($save_data['pmStartTime']&&$save_data['pmEndTime']){
  476. $serviceData[1]['docter_id']=$doctor_id;
  477. $serviceData[1]['type']=2;
  478. $serviceData[1]['start_time_period']=$save_data['pmStartTime'];
  479. $serviceData[1]['end_time_period']=$save_data['pmEndTime'];
  480. $serviceData[1]['start_time_line']=str_replace(':','',$save_data['pmStartTime']);
  481. $serviceData[1]['end_time_line']=str_replace(':','',$save_data['pmEndTime']);
  482. }else{
  483. unset($service_times[2]);
  484. }
  485. if($save_data['nightStartTime']&&$save_data['nightEndTime']){
  486. $serviceData[2]['docter_id']=$doctor_id;
  487. $serviceData[2]['type']=2;
  488. $serviceData[2]['start_time_period']=$save_data['nightStartTime'];
  489. $serviceData[2]['end_time_period']=$save_data['nightEndTime'];
  490. $serviceData[2]['start_time_line']=str_replace(':','',$save_data['nightStartTime']);
  491. $serviceData[2]['end_time_line']=str_replace(':','',$save_data['nightEndTime']);
  492. }else{
  493. unset($service_times[3]);
  494. }
  495. $newData['service_time'] = !empty($service_times)?json_encode($service_times, JSON_UNESCAPED_UNICODE):'';
  496. $newData['person'] = $save_data['person'];
  497. $newData['type'] = $save_data['type'];
  498. // 图文咨询时间
  499. $phone_fins = DocterTimes::where(['docter_id' => $doctor_id, 'type' => 1])->first();
  500. DocterServiceTimes::insert($serviceData);
  501. if (empty($phone_fins)) {
  502. $newData['docter_id'] = $doctor_id;
  503. DocterTimes::create($newData);
  504. } else {
  505. $phone_fins = $phone_fins->toArray();
  506. DocterTimes::where(['id' => $phone_fins['id']])->update($newData);
  507. }
  508. }
  509. }
  510. DB::commit();
  511. return out();
  512. } catch (\Exception $e) {
  513. DB::rollBack();
  514. return out('', 500, $e->getMessage());
  515. } catch (\PDOException $e) {
  516. DB::rollBack();
  517. return out('', 500, $e->getMessage());
  518. }
  519. }
  520. /**
  521. * 门诊设置修改
  522. * @return mixed
  523. * @throws \Exception
  524. */
  525. public function saveSettingSchedule(){
  526. $req = request()->post();
  527. $data = json_decode($req['data'], true);
  528. $user = $this->user;
  529. $doctor_id = $user['id'];
  530. $finds = Docter::select(['is_chat', 'is_phone', 'is_appoint'])->where('id', $doctor_id)->first();
  531. $flag = false;
  532. Db::beginTransaction();
  533. try {
  534. if ($finds['is_appoint'] == 0) {
  535. if (isset($data['is_appoint']) && !empty($data['is_appoint'])) {
  536. return out('', 500, '您未开启门诊');
  537. unset($data['is_appoint']);
  538. }
  539. } else {
  540. // 此时可能有两个医院,为一个数组,一个一个入库
  541. if (isset($data['is_appoint']) && !empty($data['is_appoint'])) {
  542. $save_data = $data['is_appoint'];
  543. $setting_data = [];
  544. $times_data = [];
  545. // 查询除本地来
  546. $edutime = DocterSetting::where(['docter_id' => $doctor_id, 'type' => 1])->get();
  547. if ($edutime) {
  548. $newedu = [];
  549. foreach ($edutime as $k => $v) {
  550. $newedu[$k]['org_id'] = $v['org_id'];
  551. $newedu[$k]['service_times'] = $v['service_times'];
  552. }
  553. $flag_arr = [];
  554. foreach ($newedu as $k => $v) {
  555. foreach ($save_data as $kk => $vv) {
  556. if ($v['org_id'] == $vv['org_id']) {
  557. if ($v['service_times'] != $vv['service_times']) {
  558. $flag_arr[] = $vv['org_id'];
  559. }
  560. }
  561. }
  562. }
  563. $flag = !empty($flag_arr) ? $flag_arr : false;
  564. }
  565. foreach ($save_data as $k => $v) {
  566. if (!isset($v['service_times'])||empty($v['service_times'])){
  567. unset($save_data[$k]);
  568. }else{
  569. DocterSetting::where('docter_id', '=', $doctor_id)->where('org_id', $v['org_id'])->delete();
  570. $setting_data[$k]['docter_id'] = $doctor_id;
  571. $setting_data[$k]['type'] = 1;
  572. $setting_data[$k]['status'] = 2;
  573. $setting_data[$k]['appoint_price'] = $v['base_price'];
  574. $setting_data[$k]['show_days'] = $v['show_days'];
  575. $setting_data[$k]['service_times'] = $v['service_times'];
  576. $setting_data[$k]['service_num'] = $v['service_num'];
  577. $setting_data[$k]['org_id'] = $v['org_id'];
  578. $service_times = [
  579. 1 => ['amStartTime' => $v['amStartTime'], 'amEndTime' => $v['amEndTime'], 'amPerson' => $v['amPerson']],
  580. 2 => ['pmStartTime' => $v['pmStartTime'], 'pmEndTime' => $v['pmEndTime'], 'pmPerson' => $v['pmPerson']],
  581. 3 => ['nightStartTime' => $v['nightStartTime'], 'nightEndTime' => $v['nightEndTime'], 'nightPerson' => $v['nightPerson']],
  582. ];
  583. $setting_data[$k]['service_time'] = json_encode($service_times, JSON_UNESCAPED_UNICODE);
  584. $setting_data[$k]['created_at'] = date('Y-m-d H:i:s', time());
  585. $setting_data[$k]['updated_at'] = date('Y-m-d H:i:s', time());
  586. // 第二步, 根据时长去生成24小时的时间段,也要存id
  587. if ($flag != false) {
  588. TimePeriod::where('docter_id', '=', $doctor_id)->where('org_id', $v['org_id'])->delete();
  589. $timeList = $this->getTimeHoures($v['service_times']);
  590. foreach ($timeList as $kk => $vv) {
  591. $times_data[$k][$kk]['type'] = 1;
  592. $times_data[$k][$kk]['docter_id'] = $doctor_id;
  593. $times_data[$k][$kk]['start_time_period'] = $vv[0];
  594. $times_data[$k][$kk]['end_time_period'] = $vv[1];
  595. $times_data[$k][$kk]['org_id'] = $v['org_id'];
  596. }
  597. }
  598. }
  599. }
  600. if($setting_data){
  601. if ($flag != false) {
  602. foreach ($times_data as $k => $v) {
  603. TimePeriod::insert($v);
  604. }
  605. }
  606. DocterSetting::insert($setting_data);
  607. }
  608. }
  609. }
  610. DB::commit();
  611. } catch (\Exception $e) {
  612. DB::rollBack();
  613. return out('', 500, $e->getMessage());
  614. } catch (\PDOException $e) {
  615. DB::rollBack();
  616. return out('', 500, $e->getMessage());
  617. }
  618. if ($finds['is_appoint'] != 0 && isset($data['is_appoint']) && !empty($data['is_appoint'])) {
  619. /*去重新更新,自定义排班表,和排班详情表,因为时间段id发生了变化!*/
  620. if ($flag != false) {
  621. Db::beginTransaction();
  622. try {
  623. $weekList = WeekSchedule::where('docter_id', $doctor_id)->get();
  624. $moth_date = $this->date_bool();
  625. $Wheres['schedule_type'] = 1;
  626. $Wheres['docter_id'] = $doctor_id;
  627. $counts = Schedule::where($Wheres)->whereBetween('schedule_date',[date('Y-m-d',time()),end($moth_date)['date']])->get();
  628. if ($weekList && $counts) {
  629. $wheres['docter_id'] = $doctor_id;
  630. $wheres['schedule_type'] = 1;
  631. $wheres[] = ['schedule_date','>=',date('Y-m-d',time())];
  632. $list = SchedulePeriod::where($wheres)->whereIn('organization_id', $flag)->orderBy('schedule_date','asc')->get();
  633. SchedulePeriod::where($wheres)->delete();
  634. if ($list) {
  635. $list = $list->toArray();
  636. $selfList = [];
  637. foreach ($list as $k => $v) {
  638. if (!isset($selfList[$v['schedule_date']])) {
  639. $selfList[$v['schedule_date']] = $v;
  640. if ($v['type'] == 1) {
  641. $selfList[$v['schedule_date']]['ident']['zao_id'] = $v['organization_id'];
  642. $selfList[$v['schedule_date']]['ident']['xia_id'] = 0;
  643. $selfList[$v['schedule_date']]['ident']['wan_id'] = 0;
  644. } else if ($v['type'] == 2) {
  645. $selfList[$v['schedule_date']]['ident']['zao_id'] = 0;
  646. $selfList[$v['schedule_date']]['ident']['xia_id'] = $v['organization_id'];
  647. $selfList[$v['schedule_date']]['ident']['wan_id'] = 0;
  648. } else {
  649. $selfList[$v['schedule_date']]['ident']['zao_id'] = 0;
  650. $selfList[$v['schedule_date']]['ident']['xia_id'] = 0;
  651. $selfList[$v['schedule_date']]['ident']['wan_id'] = $v['organization_id'];
  652. }
  653. } else {
  654. if ($v['type'] == 1) {
  655. $selfList[$v['schedule_date']]['ident']['zao_id'] = $v['organization_id'];
  656. } else if ($v['type'] == 2) {
  657. $selfList[$v['schedule_date']]['ident']['xia_id'] = $v['organization_id'];
  658. } else {
  659. $selfList[$v['schedule_date']]['ident']['wan_id'] = $v['organization_id'];
  660. }
  661. }
  662. }
  663. // 占用效率
  664. $scheduleLists = [];
  665. foreach ($counts as $kk => $vv) {
  666. foreach ($selfList as $k => $v) {
  667. if ($vv['schedule_date'] == $v['schedule_date']) {
  668. if ($v['ident']['zao_id']) {
  669. $times = $this->num_time($v['ident']['zao_id'], 1);
  670. if (!$times) {
  671. return out('', 500, "请先在排班设置中填写本机构的时间段");
  672. }
  673. $maf = $this->getMaf($vv['id'], $times, ['organization_id' => $v['ident']['zao_id'],'me_sure'=>$v['me_sure'],'schedule_date' => $v['schedule_date'], 'type' => 1], $doctor_id);
  674. if ($maf) {
  675. foreach ($maf as $os) {
  676. $scheduleLists[] = $os;
  677. }
  678. }
  679. }
  680. if ($v['ident']['xia_id']) {
  681. $times = $this->num_time($v['ident']['xia_id'], 2);
  682. if (!$times) {
  683. return out('', 500, "请先在排班设置中填写本机构的时间段");
  684. }
  685. $maf = $this->getMaf($vv['id'], $times, ['organization_id' => $v['ident']['xia_id'],'me_sure'=>$v['me_sure'],'schedule_date' => $v['schedule_date'], 'type' => 2], $doctor_id);
  686. if ($maf) {
  687. foreach ($maf as $os) {
  688. $scheduleLists[] = $os;
  689. }
  690. }
  691. }
  692. if ($v['ident']['wan_id']) {
  693. $times = $this->num_time($v['ident']['wan_id'], 3);
  694. if (!$times) {
  695. return out('', 500, "请先在排班设置中填写本机构的时间段");
  696. }
  697. $maf = $this->getMaf($vv['id'], $times, ['organization_id' => $v['ident']['wan_id'], 'me_sure'=>$v['me_sure'],'schedule_date' => $v['schedule_date'], 'type' => 3], $doctor_id);
  698. if ($maf) {
  699. foreach ($maf as $os) {
  700. $scheduleLists[] = $os;
  701. }
  702. }
  703. }
  704. }
  705. }
  706. }
  707. SchedulePeriod::insert($scheduleLists);
  708. }
  709. }
  710. DB::commit();
  711. } catch (\Exception $e) {
  712. DB::rollBack();
  713. return out('', 500, '2' . $$e->getMessage());
  714. } catch (\PDOException $e) {
  715. DB::rollBack();
  716. return out('', 500, '2' . $e->getMessage());
  717. }
  718. }
  719. }
  720. return out();
  721. }
  722. /**
  723. * 计算24小时的时间区间段
  724. * @param $minute 分钟比如:15
  725. * @return array
  726. */
  727. public function getTimeHoures($minute)
  728. {
  729. // 可选10分钟,15分钟,20分钟,30分钟,1个小时
  730. $start = strtotime(date("Y-m-d 00:00:00"));
  731. $end = strtotime(date("Y-m-d 23:59:59"));
  732. $returnArr = [];
  733. for ($time = $start; $time <= $end; $time = $time + $minute * 60) {
  734. array_push($returnArr, date("H:i", $time));
  735. }
  736. $newList = [];
  737. // for($i=0;$i<count($returnArr)-1;$i++){
  738. // $newList[$i]['start']=$returnArr[$i];
  739. // $newList[$i]['end']=$returnArr[$i+1];
  740. // }
  741. $k = 0;
  742. for ($i = 0; $i < (count($returnArr) * 2) - 1; $i++) {
  743. if ($i % 2 != 0) {
  744. if ($i != 0) {
  745. $k++;
  746. $newList[$k][0] = $returnArr[$k];
  747. }
  748. } else {
  749. if ($i != 0) {
  750. $newList[$k - 1][1] = $returnArr[$k];
  751. } else {
  752. $newList[$k][0] = $returnArr[$k];
  753. }
  754. }
  755. }
  756. $newList[count($newList) - 1][1] = $returnArr[0];
  757. return $newList;
  758. }
  759. /**
  760. * 提交认证
  761. * Auth:Yuanhang-Liu
  762. * Date:2020/10/14 15:08 *
  763. * @return \Illuminate\Http\JsonResponse
  764. */
  765. public function addthen()
  766. {
  767. $req = request()->post();
  768. $this->validate(request(), [
  769. 'name|医生姓名' => 'required|max:50',
  770. 'sex|性别' => 'required|in:男,女',
  771. 'ID|身份证号' => 'required|min:16|max:18',
  772. 'works|医院信息' => 'required',
  773. 'certificate|医生照片和执业证书' => 'required|string',
  774. 'idPhoto|身份证正反面' => 'required|string',
  775. 'doctorQualification|照片和资格证书' => 'required|string',
  776. ]);
  777. if ($req['sex'] == '男') {
  778. $req['sex'] = 1;
  779. } else {
  780. $req['sex'] = 2;
  781. }
  782. // 首先获取医生姓名,性别,身份证号,照片,身份证,资格证去补全医生表
  783. // 入库organization_ids这个数据是一条字符串,先分割成数组,如果只有一条则医生只选择了一个医院,机构id和医生id入库docter_organization(医生机构关联表)
  784. $user = $this->user;
  785. $doctor_id = $user['id'];
  786. if (!$doctor_id) {
  787. return out(null, 500, '医生信息错误!');
  788. }
  789. $data = [];
  790. $data['name'] = $req['name'];
  791. $data['sex'] = $req['sex'];
  792. $data['card_id'] = $req['ID'];
  793. $data['practice'] = $req['certificate'];
  794. $data['card_photo'] = $req['idPhoto'];
  795. $data['is_quail'] = $req['doctorQualification'];
  796. //处理医院机构id
  797. $work = json_decode($req['works'], true);
  798. if (!$work) {
  799. return out('', '', '缺少医院');
  800. }
  801. $off_then = DocterOrganization::where('docter_id', '=', $doctor_id)->get('id')->toArray();
  802. $is_del = false;
  803. if ($off_then) {
  804. $newids = [];
  805. foreach ($off_then as $k => $v) {
  806. $newids[$k] = $v['id'];
  807. }
  808. $is_del = true;
  809. }
  810. $org_list = [];
  811. foreach ($work as $k => $v) {
  812. if ($v['hospital']) {
  813. $org_list[$k]['docter_id'] = $doctor_id;
  814. $org_list[$k]['organization_id'] = $v['hospital'];
  815. $org_list[$k]['offices_id'] = $v['department'];
  816. $org_list[$k]['qualifications_id'] = $v['qualifications'];
  817. $org_list[$k]['state'] = $v['state'];
  818. }
  819. }
  820. DB::beginTransaction();//开启事务
  821. try {
  822. //补全医生表
  823. Docter::where('id', $doctor_id)->update($data);
  824. if ($is_del) {
  825. foreach ($newids as $k => $v) {
  826. DocterOrganization::where('id', $v)->delete();
  827. }
  828. }
  829. // 提交消息;
  830. DocterMessage::create([
  831. 'docter_id' => $doctor_id,
  832. 'status' => 1,
  833. 'type' => 8,
  834. 'content' => '您已提交医生资质认证审核材料 '
  835. ]);
  836. DocterOrganization::insertOrIgnore($org_list);
  837. DB::commit();//提交至数据库
  838. return out();
  839. } catch (\Exception $e) {
  840. DB::rollback();
  841. return out(null, 500, $e->getMessage());
  842. }
  843. }
  844. /**
  845. * Auth:Yuanhang-Liu
  846. * Date:2020/10/14 17:17 *
  847. * @return \Illuminate\Http\JsonResponse
  848. */
  849. public function get_city_id()
  850. {
  851. // 查询机构!
  852. $req = request()->post();
  853. $this->validate(request(), [
  854. 'level_type' => 'required|in:4',
  855. 'province|省或直辖市id' => 'required_if:level_type,==,4',
  856. 'city|市' => 'required_if:level_type,==,4',
  857. 'area|区' => 'required_if:level_type,==,4',
  858. ]);
  859. // 查找下医院
  860. // $ids_string = explode(',',$req['organizations_ids']);
  861. $province = trim($req['province']);
  862. $city = trim($req['city']);
  863. $area = trim($req['area']);
  864. $where['province_id'] = $province;
  865. $where['city_id'] = $city;
  866. $where['area_id'] = $area;
  867. $where['type'] = 1;
  868. $Organization = Organization::with('offices')->where($where)->get()->toArray();
  869. $qulire = Qualification::where('status', 1)->get();
  870. $newList = [];
  871. if ($Organization) {
  872. foreach ($Organization as $k => $v) {
  873. $newList[$k]['value'] = $v['id'];
  874. $newList[$k]['label'] = $v['name'];
  875. if ($v['offices']) {
  876. foreach ($v['offices'] as $kk => $vv) {
  877. $newList[$k]['children'][$kk]['value'] = $vv['id'];
  878. $newList[$k]['children'][$kk]['label'] = $vv['name'];
  879. if ($qulire) {
  880. foreach ($qulire as $p => $c) {
  881. $newList[$k]['children'][$kk]['children'][$p]['value'] = $c['id'];
  882. $newList[$k]['children'][$kk]['children'][$p]['label'] = $c['name'];
  883. }
  884. } else {
  885. unset($Organization[$k], $newList[$k]);
  886. }
  887. }
  888. } else {
  889. unset($Organization[$k], $newList[$k]);
  890. }
  891. }
  892. }
  893. return out($newList);
  894. }
  895. /**
  896. * 获取城市联动
  897. * @return \Illuminate\Http\JsonResponse
  898. * @author Liu-Yh
  899. * Create By 2020/11/4 19:35
  900. */
  901. public function getCityList()
  902. {
  903. // 查询机构!
  904. $req = request()->post();
  905. $where['status'] = 1;
  906. $data = Areas::where($where)->get(['id', 'name', 'level', 'parent_id'])->toArray();
  907. $newData = [];
  908. foreach ($data as $k => $v) {
  909. $newData[$k]['value'] = $v['id'];
  910. $newData[$k]['label'] = $v['name'];
  911. $newData[$k]['pid'] = $v['parent_id'];
  912. }
  913. $list = $this->getTrees($newData);
  914. $list = $this->getTree($list);
  915. return out($list);
  916. }
  917. /**
  918. * 获取城市树
  919. * @param $data
  920. * @return array
  921. */
  922. protected function getTrees($arr, $pid = 0)
  923. {
  924. static $list = [];
  925. foreach ($arr as $key => $value) {
  926. if ($value["pid"] == $pid) {
  927. $list[] = $value;
  928. unset($arr[$key]); //删除已经排好的数据为了减少遍历的次数,当然递归本身就很费神就是了
  929. $this->getTrees($arr, $value["value"]);
  930. }
  931. }
  932. return $list;
  933. }
  934. /**
  935. * 整合树🌲
  936. * @param $data
  937. * @param $pId
  938. * @return string
  939. */
  940. protected function getTree($data, $pId = 0)
  941. {
  942. $tree = [];
  943. foreach ($data as $k => $v) {
  944. if ($v['pid'] == $pId) { //父亲找到儿子
  945. $chid = $this->getTree($data, $v['value']);
  946. if ($chid) {
  947. $v['children'] = $chid;
  948. }
  949. $tree[] = $v;
  950. unset($data[$k]);
  951. } else {
  952. unset($data[$k]);
  953. }
  954. }
  955. return $tree;
  956. }
  957. /**
  958. * 获取科室和资质
  959. * @return mixed
  960. */
  961. public function getOfferList()
  962. {
  963. // 查询机构!
  964. $req = request()->post();
  965. $this->validate(request(), [
  966. 'type' => 'required|in:1,2',
  967. ]);//1=科室,2=资质
  968. $type = $req['type'];
  969. if ($type == 1) {
  970. $List = Office::get(['id', 'name'])->toArray();
  971. } else if ($type == 2) {
  972. $List = Qualification::get(['id', 'name'])->toArray();
  973. } else {
  974. return out('', '', '错误请求');
  975. }
  976. return out($List);
  977. }
  978. /**
  979. * 获取我的患者列表
  980. * Auth:Yuanhang-Liu
  981. * Date:2020/10/17 13:21
  982. * @return \Illuminate\Http\JsonResponse
  983. */
  984. public function userList()
  985. {
  986. $req = request()->post();
  987. $this->validate(request(), [
  988. 'userId' => 'integer',
  989. 'userType|用户类型' => 'integer|in:1,2,3,4,5,6,7',
  990. ]);
  991. $user = $this->user;
  992. $doctor_id = $user['id'];
  993. if (!$doctor_id) {
  994. return out(null, 500, '医生信息错误!');
  995. }
  996. $returnList = [];
  997. $orderList = Order::with(['user'])->where('docter_id', '=', $doctor_id)->groupBy('id', 'product_type', 'user_id')->get()->toArray();
  998. $collections = DocterUser::with('user')->where('docter_id', '=', $doctor_id)->groupBy('user_id')->get()->toArray();
  999. $newList = [];
  1000. foreach ($orderList as $k => $v) {
  1001. $newList[$k]['order_id'] = $v['id'];
  1002. $newList[$k]['user_id'] = $v['user_id'];
  1003. $newList[$k]['order_sn'] = $v['order_sn'];
  1004. $newList[$k]['nickname'] = $v['user']['nickname'];
  1005. $save = UserDocter::where('user_id', '=', $v['user']['id'])->first();
  1006. if ($save) {
  1007. $newList[$k]['remark'] = $save['remark'];
  1008. } else {
  1009. $newList[$k]['remark'] = '';
  1010. }
  1011. $newList[$k]['avatar'] = $v['user']['avatar'];
  1012. $newList[$k]['product_type'] = $v['product_type'];
  1013. }
  1014. $newcoll = [];
  1015. foreach ($collections as $k => $v) {
  1016. $newcoll[$k]['id'] = $v['id'];
  1017. $newcoll[$k]['user_id'] = $v['user_id'];
  1018. $newcoll[$k]['nickname'] = $v['user']['nickname'];
  1019. $save = UserDocter::where('user_id', '=', $v['user_id'])->first();
  1020. if ($save) {
  1021. $newList[$k]['remark'] = $save['remark'];
  1022. } else {
  1023. $newList[$k]['remark'] = '';
  1024. }
  1025. $newcoll[$k]['avatar'] = $v['user']['avatar'];
  1026. }
  1027. $user_pic = [];
  1028. $user_relation = [];
  1029. $user_vaccines = [];
  1030. $user_guar = [];
  1031. $user_pack = [];
  1032. foreach ($newList as $k => $v) {
  1033. if ($v['product_type'] == 1 || $v['product_type'] == 2) {
  1034. // 咨询患者
  1035. if (!$this->putusers($user_pic, $v['user_id'])) {
  1036. array_push($user_pic, $v);
  1037. }
  1038. }
  1039. if ($v['product_type'] == 3) {
  1040. // 门诊患者
  1041. if (!$this->putusers($user_relation, $v['user_id'])) {
  1042. array_push($user_relation, $v);
  1043. }
  1044. }
  1045. if ($v['product_type'] == 4) {
  1046. // 疫苗患者
  1047. if (!$this->putusers($user_vaccines, $v['user_id'])) {
  1048. array_push($user_vaccines, $v);
  1049. }
  1050. }
  1051. if ($v['product_type'] == 5) {
  1052. // 儿保预约
  1053. if (!$this->putusers($user_guar, $v['user_id'])) {
  1054. array_push($user_guar, $v);
  1055. }
  1056. }
  1057. if ($v['product_type'] == 6) {
  1058. // 服务包
  1059. $OrderPacks = OrderPack::where('order_id', '=', $v['order_id'])->first();
  1060. if ($OrderPacks) {
  1061. $teams = Team::with('docter')->where('id', '=', $OrderPacks['team_id'])->first();
  1062. if ($teams) {
  1063. $teams = $teams->toArray();
  1064. if ($teams['docter']) {
  1065. foreach ($teams['docter'] as $kk => $vv) {
  1066. if ($vv['id'] == $doctor_id) {
  1067. array_push($user_pack, $v);
  1068. }
  1069. }
  1070. }
  1071. }
  1072. }
  1073. }
  1074. }
  1075. $returnList['user_pic']['data'] = $user_pic;
  1076. $returnList['user_relation']['data'] = $user_relation;
  1077. $returnList['user_vaccines']['data'] = $user_vaccines;
  1078. $returnList['user_guar']['data'] = $user_guar;
  1079. $returnList['user_pack']['data'] = $user_pack;
  1080. if (empty($newcoll)) {
  1081. // 关注患者
  1082. $returnList['user_collection']['data'] = [];
  1083. } else {
  1084. $returnList['user_collection']['data'] = $newcoll;
  1085. }
  1086. $returnList['user_pic']['count'] = count($user_pic);
  1087. $returnList['user_relation']['count'] = count($user_relation);
  1088. $returnList['user_vaccines']['count'] = count($user_vaccines);
  1089. $returnList['user_guar']['count'] = count($user_guar);
  1090. $returnList['user_pack']['count'] = count($user_pack);
  1091. $returnList['user_collection']['count'] = count($returnList['user_collection']['data']);
  1092. return out($returnList);
  1093. }
  1094. /**
  1095. * 升级版 in_array
  1096. * @param $data1
  1097. * @param $data2
  1098. * @return bool
  1099. * @author Liu-Yh
  1100. * Create By 2020/11/27 16:18
  1101. */
  1102. protected function putusers($data1, $data2)
  1103. {
  1104. if (empty($data1)) {
  1105. return false;
  1106. }
  1107. $list = [];
  1108. foreach ($data1 as $k => $v) {
  1109. array_push($list, $v['user_id']);
  1110. }
  1111. if (in_array($data2, $list)) {
  1112. return true;
  1113. } else {
  1114. return false;
  1115. }
  1116. }
  1117. /**
  1118. * 上备注
  1119. * @return \Illuminate\Http\JsonResponse
  1120. * @author Liu-Yh
  1121. * Create By 2020/11/27 16:16
  1122. */
  1123. public function remacks()
  1124. {
  1125. $req = request()->post();
  1126. $this->validate(request(), [
  1127. 'remak|备注' => 'required',
  1128. 'id|id' => 'required',
  1129. ]);
  1130. $user = $this->user;
  1131. $user_id = $user['id'];
  1132. $wheres['docter_id'] = $user_id;
  1133. $wheres['user_id'] = $req['id'];
  1134. $save = UserDocter::where($wheres)->first();
  1135. if ($save) {
  1136. UserDocter::where('id', '=', $save['id'])->update([
  1137. 'remark' => $req['remak']
  1138. ]);
  1139. } else {
  1140. UserDocter::create([
  1141. 'docter_id' => $user_id,
  1142. 'user_id' => $req['id'],
  1143. 'remark' => $req['remak']
  1144. ]);
  1145. }
  1146. return out([]);
  1147. }
  1148. /**
  1149. * 评价列表
  1150. * @return \Illuminate\Http\JsonResponse
  1151. * @author Liu-Yh
  1152. * Create By 2020/11/17 17:41
  1153. */
  1154. public function evaluate()
  1155. {
  1156. $req = request()->post();
  1157. $this->validate(request(), [
  1158. 'curPage' => 'required|integer',
  1159. 'pageSize' => 'required|integer',
  1160. ]);
  1161. $pageSize = ($req['curPage'] - 1) * $req['pageSize'];
  1162. $user = $this->user;
  1163. $doctor_id = $user['id'];
  1164. $wheres['docter_id'] = $doctor_id;
  1165. $wheres['status'] = 2;
  1166. $data = Evaluate::with(['order', 'user'])->where($wheres)->skip($pageSize)->take($req['pageSize'])->groupBy('id')->get()->toArray();;
  1167. if ($data) {
  1168. $newList = [];
  1169. foreach ($data as $k => $v) {
  1170. $newList[$k]['order_id'] = $v['order']['order_sn'];
  1171. $newList[$k]['user'] = $v['user']['nickname'];
  1172. $newList[$k]['lv'] = $v['score'];
  1173. $newList[$k]['time'] = $v['created_at'];
  1174. $newList[$k]['evaluate'] = $v['content'];
  1175. }
  1176. return out($newList);
  1177. } else {
  1178. return out([]);
  1179. }
  1180. }
  1181. /**
  1182. * 我的列表进入详情
  1183. * Auth:Yuanhang-Liu
  1184. * Date:2020/10/18 15:21
  1185. * @return \Illuminate\Http\JsonResponse
  1186. */
  1187. public function userListDetile()
  1188. {
  1189. $req = request()->post();
  1190. $this->validate(request(), [
  1191. 'user_id|用户ID' => 'required|integer',
  1192. 'userType|用户类型' => 'integer|in:1,2,3,4,5,6,7',
  1193. ]);
  1194. $user = $this->user;
  1195. // 查询详情;
  1196. $finds_patient = User::with('patients')->where('id', '=', $req['user_id'])->first();
  1197. $remarks = UserDocter::where('user_id', '=', $req['user_id'])->first();
  1198. $finds_order = Order::with('orderPatient')->where('user_id', '=', $req['user_id'])->orderBy('created_at', 'desc')->limit(3)->get();
  1199. if ($finds_order) {
  1200. $finds_order = $finds_order->toArray();
  1201. }
  1202. $userArr = $patientsArr = $orderArr = $fuwArr = [];
  1203. $patientsArr = [];
  1204. $ws['user_id'] = $req['user_id'];
  1205. $ws['product_type'] = 6;
  1206. $fuw_order = Order::with('orderPack')->where($ws)->orderBy('created_at', 'desc')->get();
  1207. if ($fuw_order) {
  1208. $fuw_order = $fuw_order->toArray();
  1209. // var_dump($fuw_order);
  1210. foreach ($fuw_order as $kk => $vv) {
  1211. $fuwArr[$kk]['order_id'] = $vv['id'];
  1212. if ($vv['order_pack']) {
  1213. // foreach ($vv['order_pack'] as $k => $v) {
  1214. $fuwArr[$kk]['id'] = $vv['order_pack']['id'];
  1215. $fuwArr[$kk]['pack_name'] = $vv['order_pack']['pack_name'];
  1216. $fuwArr[$kk]['pack_intro'] = $vv['order_pack']['pack_intro'];
  1217. $fuwArr[$kk]['pack_price'] = $vv['order_pack']['pack_price'];
  1218. $fuwArr[$kk]['effective_days'] = $vv['order_pack']['effective_days'];
  1219. $fuwArr[$kk]['start_time'] = date('Y-m-d,H:i', $vv['order_pack']['start_time']);
  1220. $fuwArr[$kk]['end_time'] = date('Y-m-d,H:i', $vv['order_pack']['end_time']);
  1221. // }
  1222. }
  1223. }
  1224. }
  1225. if ($finds_patient) {
  1226. $finds_patient = $finds_patient->toArray();
  1227. $userArr['id'] = $finds_patient['id'];
  1228. if ($remarks) {
  1229. $userArr['remark'] = $remarks['remark']; //用户备注
  1230. } else {
  1231. $userArr['remark'] = ''; //用户备注
  1232. }
  1233. $userArr['nickname'] = $finds_patient['nickname'];
  1234. $userArr['avatar'] = $finds_patient['avatar'];
  1235. if ($finds_patient['patients']) {
  1236. foreach ($finds_patient['patients'] as $kk => $vv) {
  1237. $patientsArr[$kk]['id'] = $vv['id'];
  1238. $patientsArr[$kk]['avatar'] = $vv['avatar'];
  1239. $patientsArr[$kk]['name'] = $vv['name'];
  1240. if ($vv['sex'] == 0) {
  1241. $patientsArr[$kk]['sex'] = '未知';
  1242. } elseif ($vv['sex'] == 1) {
  1243. $patientsArr[$kk]['sex'] = '男';
  1244. } else {
  1245. $patientsArr[$kk]['sex'] = '女';
  1246. }
  1247. $patientsArr[$kk]['age'] = numBirthday($vv['birthday']);
  1248. }
  1249. }
  1250. }
  1251. if ($finds_order) {
  1252. foreach ($finds_order as $k => $v) {
  1253. $orderArr[$k]['id'] = $v['id'];
  1254. $orderArr[$k]['order_sn'] = $v['order_sn'];
  1255. if ($v['order_status'] >= 4) {
  1256. $orderArr[$k]['receiving_time'] = date('Y-m-d H:i', $v['receiving_time']);
  1257. $orderArr[$k]['outtime'] = date('Y-m-d H:i', $v['end_time']);
  1258. } else {
  1259. $orderArr[$k]['receiving_time'] = '';
  1260. $orderArr[$k]['outtime'] = '';
  1261. }
  1262. $orderArr[$k]['order_status'] = $v['order_status'];
  1263. $orderArr[$k]['product_type'] = $v['product_type'];
  1264. $orderArr[$k]['created_at'] = $v['created_at'];
  1265. $orderArr[$k]['name'] = $v['order_patient']['name'];
  1266. $orderArr[$k]['birthday'] = numBirthday($v['order_patient']['birthday']);
  1267. }
  1268. }
  1269. // 拼接服务消息
  1270. $wh['user_id'] = $req['user_id'];
  1271. $wh['order_status'] = ['>', 3];
  1272. $wh['product_type'] = 6;
  1273. $fuwuList = [];
  1274. $fuwuInfo = Order::where($wh)->get();
  1275. $fuwuList['fuwu_num'] = count($fuwuInfo);
  1276. $whs['user_id'] = $req['user_id'];
  1277. $whs['order_status'] = ['>', 3];
  1278. $order_nums = Order::where($whs)->get();
  1279. $fuwuList['order_num'] = count($order_nums);
  1280. $pati_nums = Patient::where('user_id', '=', $req['user_id'])->get();
  1281. $fuwuList['pati_num'] = count($pati_nums);
  1282. $returnArr = [
  1283. 'user_arr' => $userArr,
  1284. 'patients_arr' => $patientsArr,//患者档案
  1285. 'order_arr' => $orderArr, //历史问诊
  1286. 'fuwu_arr' => $fuwArr, //已购服务
  1287. 'nums' => $fuwuList,
  1288. ];
  1289. return out($returnArr);
  1290. }
  1291. /**
  1292. * 我的列表进入详情
  1293. * Auth:Yuanhang-Liu
  1294. * Date:2020/10/20 23:21
  1295. * @return \Illuminate\Http\JsonResponse
  1296. */
  1297. public function patientsDetile()
  1298. {
  1299. $req = request()->post();
  1300. $this->validate(request(), [
  1301. 'patient_id|患者ID' => 'required|integer',
  1302. ]);
  1303. $user = $this->user;
  1304. $finds = Patient::where('id', '=', $req['patient_id'])->first();
  1305. if ($finds) {
  1306. $finds = $finds->toArray();
  1307. }
  1308. if ($finds['sex'] == 0) {
  1309. $finds['sex'] = '中性';
  1310. } elseif ($finds['sex'] == 1) {
  1311. $finds['sex'] = '男';
  1312. } else {
  1313. $finds['sex'] = '女';
  1314. }
  1315. switch ($finds['relationship_type']) {
  1316. case 1:
  1317. $finds['relationship_type'] = '父亲';
  1318. break;
  1319. case 2:
  1320. $finds['relationship_type'] = '母亲';
  1321. break;
  1322. case 3:
  1323. $finds['relationship_type'] = '祖父';
  1324. break;
  1325. case 4:
  1326. $finds['relationship_type'] = '祖母';
  1327. break;
  1328. case 5:
  1329. $finds['relationship_type'] = '外祖父';
  1330. break;
  1331. case 6:
  1332. $finds['relationship_type'] = '外祖母';
  1333. break;
  1334. case 7:
  1335. $finds['relationship_type'] = '叔侄';
  1336. break;
  1337. default:
  1338. $finds['relationship_type'] = '其他';
  1339. }
  1340. $finds['age'] = numBirthday($finds['birthday']);
  1341. // 患者单子
  1342. $patient_list = OrderPatient::with('order')->where('patient_id', '=', $req['patient_id'])->groupBy('id', 'order_id')->get();
  1343. $patient_arr = [];
  1344. if ($patient_list) {
  1345. $patient_list = $patient_list->toArray();
  1346. foreach ($patient_list as $k => $v) {
  1347. switch ($v['order']['product_type']) {
  1348. case 1:
  1349. $patient_arr[$k]['product_type'] = '电话咨询';
  1350. break;
  1351. case 2:
  1352. $patient_arr[$k]['product_type'] = '图文咨询';
  1353. break;
  1354. case 3:
  1355. $patient_arr[$k]['product_type'] = '门诊';
  1356. break;
  1357. case 4:
  1358. $patient_arr[$k]['product_type'] = '疫苗接种预约';
  1359. break;
  1360. case 5:
  1361. $patient_arr[$k]['product_type'] = '儿保预约';
  1362. break;
  1363. case 6:
  1364. $patient_arr[$k]['product_type'] = '服务包';
  1365. break;
  1366. case 7:
  1367. $patient_arr[$k]['product_type'] = '充值';
  1368. break;
  1369. default:
  1370. $patient_arr[$k]['product_type'] = '错误';
  1371. }
  1372. switch ($v['order']['order_status']) {
  1373. case 1:
  1374. $patient_arr[$k]['order_status'] = '未支付';
  1375. break;
  1376. case 2:
  1377. $patient_arr[$k]['order_status'] = '待接单';
  1378. break;
  1379. case 3:
  1380. $patient_arr[$k]['order_status'] = '进行中';
  1381. break;
  1382. case 4:
  1383. $patient_arr[$k]['order_status'] = '已完成';
  1384. break;
  1385. case 5:
  1386. $patient_arr[$k]['order_status'] = '已取消';
  1387. break;
  1388. default:
  1389. $patient_arr[$k]['order_status'] = '未知';
  1390. }
  1391. $patient_arr[$k]['created_at'] = $v['created_at'];
  1392. }
  1393. }
  1394. $returnArr = [
  1395. 'patient' => $finds,
  1396. 'patientOrder' => $patient_arr,
  1397. ];
  1398. return out($returnArr);
  1399. }
  1400. /**
  1401. * 关注用户
  1402. * Auth:Yuanhang-Liu
  1403. * Date:2020/10/21 00:21
  1404. * @return \Illuminate\Http\JsonResponse
  1405. */
  1406. public function userFollow()
  1407. {
  1408. $req = request()->post();
  1409. $this->validate(request(), [
  1410. 'user_id|用户ID' => 'required|integer',
  1411. 'is_ley' => 'required',
  1412. ]);
  1413. // 如果ley==false说明要取消关注,反之添加关注
  1414. $user = $this->user;
  1415. $docter_id = $user['id'];
  1416. if ($req['is_ley'] == 1) {
  1417. DocterUser::where('user_id', '=', $req['user_id'])->delete();
  1418. } else {
  1419. DocterUser::create([
  1420. 'user_id' => $req['user_id'],
  1421. 'docter_id' => $user['id'],
  1422. ]);
  1423. }
  1424. return out();
  1425. }
  1426. /**
  1427. * 是否关注用户
  1428. * Auth:Yuanhang-Liu
  1429. * Date:2020/10/21 00:21
  1430. * @return \Illuminate\Http\JsonResponse
  1431. */
  1432. public function getFollows()
  1433. {
  1434. $req = request()->post();
  1435. $this->validate(request(), [
  1436. 'user_id|用户ID' => 'required|integer',
  1437. ]);
  1438. $user = $this->user;
  1439. $docter_id = $user['id'];
  1440. $where['docter_id'] = $docter_id;
  1441. $where['user_id'] = $req['user_id'];
  1442. $find = DocterUser::where($where)->first();
  1443. if ($find) {
  1444. $find = $find->toArray();
  1445. return out(true);
  1446. } else {
  1447. return out(false);
  1448. }
  1449. }
  1450. /**
  1451. * 获取医生所在机构列表
  1452. * Auth:Yuanhang-Liu
  1453. * Date:2020/10/21 00:21
  1454. * @return \Illuminate\Http\JsonResponse
  1455. */
  1456. public function organizationList()
  1457. {
  1458. $user = $this->user;
  1459. $doctor_id = $user['id'];
  1460. $where['docter_id'] = $doctor_id;
  1461. $where['state'] = 1;
  1462. $orgList = DocterOrganization::with('organization')->where($where)->get()->toArray();
  1463. $newList = [];
  1464. foreach ($orgList as $k => $v) {
  1465. $newList[$k]['value'] = $v['organization']['id'];
  1466. $newList[$k]['label'] = $v['organization']['name'];
  1467. }
  1468. return out($newList);
  1469. }
  1470. /**
  1471. * 获取医院的服务时间
  1472. * @return mixed
  1473. */
  1474. public function getOrgTimes()
  1475. {
  1476. $req = request()->post();
  1477. $this->validate(request(), [
  1478. 'org_id|医院id' => 'required',
  1479. ]);
  1480. $user = $this->user;
  1481. $doctor_id = $user['id'];
  1482. $where['docter_id'] = $doctor_id;
  1483. $orgList = DocterSetting::where(['docter_id'=>$doctor_id,'type'=>1,'org_id'=>$req['org_id']])->first();
  1484. $newList = [];
  1485. if($orgList){
  1486. $time = json_decode($orgList['service_time'],true);
  1487. $newList['zao']['amStartTime'] = isset($time[1])?$time[1]['amStartTime']:'';
  1488. $newList['zao']['amEndTime'] = isset($time[1])?$time[1]['amEndTime']:'';
  1489. $newList['zao']['amPerson'] = isset($time[1])?$time[1]['amPerson']:'';
  1490. $newList['zhong']['pmStartTime'] = isset($time[2])?$time[2]['pmStartTime']:'';
  1491. $newList['zhong']['pmEndTime'] = isset($time[2])?$time[2]['pmEndTime']:'';
  1492. $newList['zhong']['pmPerson'] = isset($time[2])?$time[2]['pmPerson']:'';
  1493. $newList['wan']['nightStartTime'] = isset($time[3])?$time[3]['nightStartTime']:'';
  1494. $newList['wan']['nightEndTime'] = isset($time[3])?$time[3]['nightEndTime']:'';
  1495. $newList['wan']['nightPerson'] = isset($time[3])?$time[3]['nightPerson']:'';
  1496. }
  1497. return out($newList);
  1498. }
  1499. /**
  1500. * 获取历史排班时间列表
  1501. */
  1502. public function getLinsList()
  1503. {
  1504. $user = $this->user;
  1505. $doctor_id = $user['id'];
  1506. $newData = [];
  1507. $strim = date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600));
  1508. $wheres['docter_id'] = $doctor_id;
  1509. $data = Schedule::select(['id', 'schedule_date'])->where($wheres)->whereDate('schedule_date', '>', $strim)->orderBy('schedule_date', 'desc')->get();
  1510. if ($data) {
  1511. $data = $data->toArray();
  1512. } else {
  1513. return out($newData);
  1514. }
  1515. $newData = array_values($data);
  1516. $lab = [];
  1517. $p = 0;
  1518. for ($i = count($newData); $i > 0; $i--) {
  1519. // if ($i%7==0||$i==1){
  1520. //
  1521. // if ($p>=1){
  1522. // $lab[$p-1]['end_time'] = $arr[$i-1];
  1523. // $lab[$p]['start_time'] = $arr[$i-1];
  1524. // if(isset($arr[$i-7])){
  1525. // $lab[$p]['end_time'] = isset($arr[$i-7])?$arr[$i-7]:'error';
  1526. //
  1527. // }else{
  1528. // unset($lab[$p]);
  1529. // }
  1530. //// $lab[$p]['end_time'] = isset($arr[$i-7])?$arr[$i-7]:'error';
  1531. // }else{
  1532. // $lab[$p]['start_time'] = $arr[$i-1];
  1533. // }
  1534. // ++$p;
  1535. // }else{
  1536. // continue;
  1537. // }
  1538. if ($i % 7 == 0 || $i == 1) {
  1539. if ($p >= 1) {
  1540. $lab[$p - 1]['end_time'] = $newData[$i - 1];
  1541. if (isset($newData[$i - 7])) {
  1542. $lab[$p]['start_time'] = $newData[$i - 1];
  1543. $lab[$p]['end_time'] = $newData[$i - 7];
  1544. } else {
  1545. unset($lab[$p]);
  1546. }
  1547. } else {
  1548. $lab[$p]['start_time'] = $newData[$i - 1];
  1549. }
  1550. ++$p;
  1551. } else {
  1552. continue;
  1553. }
  1554. }
  1555. // 审核通过时间拿出来
  1556. $wh['docter_id'] = $doctor_id;
  1557. $wh['service_type'] = 3;
  1558. $wh['status'] = 2;
  1559. $service = ServiceApply::where($wh)->first();
  1560. if ($service) {
  1561. $service = $service;
  1562. } else {
  1563. $service = 0;
  1564. }
  1565. $rtuenList = [
  1566. 'list' => $lab,
  1567. 'service' => $service,
  1568. ];
  1569. return out($rtuenList);
  1570. }
  1571. /**
  1572. * 获取周模板详情
  1573. * @return \Illuminate\Http\JsonResponse
  1574. * @author Liu-Yh
  1575. * Create By 2020/12/16 12:09
  1576. */
  1577. public function getLinsDetailnew($flag = false)
  1578. {
  1579. $user = $this->user;
  1580. $doctor_id = $user['id'];
  1581. $newData = [];
  1582. $data = WeekSchedule::with('organization')->where('docter_id', '=', $doctor_id)->groupBy('type', 'week')->orderBy('week', 'asc')->get();
  1583. if ($data) {
  1584. $data = $data->toArray();
  1585. } else {
  1586. return out($newData);
  1587. }
  1588. $lab = [];
  1589. foreach ($data as $k => $v) {
  1590. $lab[$k]['id'] = $v['id'];
  1591. $lab[$k]['type'] = $v['type']; // 1=早上,2=下午,3=晚上
  1592. $lab[$k]['organization_id'] = $v['organization_id']; // 1=早上,2=下午,3=晚上
  1593. $lab[$k]['flag'] = $v['week'];
  1594. switch ($v['week']) {
  1595. case "7":
  1596. $lab[$k]['week'] = '周日';
  1597. break;
  1598. case "1":
  1599. $lab[$k]['week'] = '周一';
  1600. break;
  1601. case "2":
  1602. $lab[$k]['week'] = '周二';
  1603. break;
  1604. case "3":
  1605. $lab[$k]['week'] = '周三';
  1606. break;
  1607. case "4":
  1608. $lab[$k]['week'] = '周四';
  1609. break;
  1610. case "5":
  1611. $lab[$k]['week'] = '周五';
  1612. break;
  1613. default:
  1614. $lab[$k]['week'] = '周六';
  1615. }
  1616. $lab[$k]['name'] = $v['organization']; // 所在医院
  1617. }
  1618. $weekList = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
  1619. try {
  1620. $newReturn = [];
  1621. foreach ($lab as $k => $v) {
  1622. if (!isset($newReturn[$v['flag']])) {
  1623. $newReturn[$v['flag']] = $v;
  1624. if ($v['type'] == 1) {
  1625. $newReturn[$v['flag']]['ident']['zao_id'] = $v['name']['id'];
  1626. $newReturn[$v['flag']]['ident']['xia_id'] = 0;
  1627. $newReturn[$v['flag']]['ident']['wan_id'] = 0;
  1628. } else if ($v['type'] == 2) {
  1629. $newReturn[$v['flag']]['ident']['zao_id'] = 0;
  1630. $newReturn[$v['flag']]['ident']['xia_id'] = $v['name']['id'];
  1631. $newReturn[$v['flag']]['ident']['wan_id'] = 0;
  1632. } else {
  1633. $newReturn[$v['flag']]['ident']['zao_id'] = 0;
  1634. $newReturn[$v['flag']]['ident']['xia_id'] = 0;
  1635. $newReturn[$v['flag']]['ident']['wan_id'] = $v['name']['id'];
  1636. }
  1637. $newReturn[$v['flag']]['week'] = $v['week'];
  1638. $newReturn[$v['flag']]['name'] = $v['name'];
  1639. } else {
  1640. if ($v['type'] == 1) {
  1641. $newReturn[$v['flag']]['ident']['zao_id'] = $v['name']['id'];
  1642. } else if ($v['type'] == 2) {
  1643. $newReturn[$v['flag']]['ident']['xia_id'] = $v['name']['id'];
  1644. } else {
  1645. $newReturn[$v['flag']]['ident']['wan_id'] = $v['name']['id'];
  1646. }
  1647. }
  1648. unset($newReturn[$v['flag']]['name']);
  1649. }
  1650. } catch (\Exception $e) {
  1651. var_dump($e->getMessage() . $e->getFile() . $e->getLine());
  1652. }
  1653. $returnLists['list'] = array_values($newReturn);
  1654. return out($returnLists);
  1655. }
  1656. /**
  1657. * 生成时间段
  1658. * @param int $org_id 机构id
  1659. * @param int $type 时间段,1=早上,2=下午,3=晚上
  1660. * @return array|bool
  1661. * @author Liu-Yh
  1662. * Create By 2020/12/15 18:47
  1663. */
  1664. protected function num_time($org_id = 0, $type = 1)
  1665. {
  1666. $user = $this->user;
  1667. $doctor_id = $user['id'];
  1668. $zaoTimes = [];
  1669. $zhongTimes = [];
  1670. $wanTimes = [];
  1671. $where['docter_id'] = $doctor_id;
  1672. $where['type'] = 1;
  1673. $where['org_id'] = $org_id;
  1674. $configs = DocterSetting::where($where)->first();
  1675. if ($configs) {
  1676. $list = json_decode($configs['service_time'], true);
  1677. if (isset($list[1]['amStartTime']) && !empty($list[1]['amStartTime'])) {
  1678. $zaoTimes[0] = $list[1]['amStartTime'];
  1679. } else {
  1680. $zaoTimes[0] = "08:00";
  1681. }
  1682. if (isset($list[1]['amEndTime']) && !empty($list[1]['amEndTime'])) {
  1683. $zaoTimes[1] = $list[1]['amEndTime'];
  1684. } else {
  1685. $zaoTimes[1] = "12:00";
  1686. }
  1687. if (isset($list[2]['pmStartTime']) && !empty($list[2]['pmStartTime'])) {
  1688. $zhongTimes[0] = $list[2]['pmStartTime'];
  1689. } else {
  1690. $zhongTimes[0] = "13:00";
  1691. }
  1692. if (isset($list[2]['pmEndTime']) && !empty($list[2]['pmEndTime'])) {
  1693. $zhongTimes[1] = $list[2]['pmEndTime'];
  1694. } else {
  1695. $zhongTimes[1] = "18:00";
  1696. }
  1697. if (isset($list[3]['nightStartTime']) && !empty($list[3]['nightStartTime'])) {
  1698. $wanTimes[0] = $list[3]['nightStartTime'];
  1699. } else {
  1700. $wanTimes[0] = "19:00";
  1701. }
  1702. if (isset($list[3]['nightEndTime']) && !empty($list[3]['nightEndTime'])) {
  1703. $wanTimes[1] = $list[3]['nightEndTime'];
  1704. } else {
  1705. $wanTimes[1] = "21:00";
  1706. }
  1707. } else {
  1708. return false;
  1709. }
  1710. if ($type == 1) {
  1711. $morningWhere[] = ['start_time_period', '>=', $zaoTimes[0]];
  1712. $morningWhere[] = ['end_time_period', '<=', $zaoTimes[1]];
  1713. $morningWhere['docter_id'] = $doctor_id;
  1714. $morningWhere['org_id'] = $org_id;
  1715. $morningd = TimePeriod::where($morningWhere)->get()->toArray();
  1716. if (count($morningd)>1){
  1717. array_pop($morningd);
  1718. }
  1719. $morning = array_column($morningd, 'id');
  1720. return $morning;
  1721. } elseif ($type == 2) {
  1722. $afternoondWhere[] = ['start_time_period', '>=', $zhongTimes[0]];
  1723. $afternoondWhere[] = ['end_time_period', '<=', $zhongTimes[1]];
  1724. $afternoondWhere['docter_id'] = $doctor_id;
  1725. $afternoondWhere['org_id'] = $org_id;
  1726. $afternoond = TimePeriod::where($afternoondWhere)->get()->toArray();
  1727. if (count($afternoond)>1){
  1728. array_pop($afternoond);
  1729. }
  1730. $afternoon = array_column($afternoond, 'id');
  1731. return $afternoon;
  1732. } elseif ($type == 3) {
  1733. $nightWhere[] = ['start_time_period', '>=', $wanTimes[0]];
  1734. $nightWhere[] = ['end_time_period', '<=', $wanTimes[1]];
  1735. $nightWhere['docter_id'] = $doctor_id;
  1736. $nightWhere['org_id'] = $org_id;
  1737. $nightd = TimePeriod::where($nightWhere)->get()->toArray();
  1738. if (count($nightd)>1){
  1739. array_pop($nightd);
  1740. }
  1741. $night = array_column($nightd, 'id');
  1742. return $night;
  1743. } else {
  1744. $morningWhere[] = ['start_time_period', '>=', $zaoTimes[0]];
  1745. $morningWhere[] = ['end_time_period', '<=', $zaoTimes[1]];
  1746. $morningWhere['docter_id'] = $doctor_id;
  1747. $morningWhere['org_id'] = $org_id;
  1748. $morningd = TimePeriod::where($morningWhere)->get()->toArray();
  1749. if (count($morningd)>1){
  1750. array_pop($morningd);
  1751. }
  1752. $morning = array_column($morningd, 'id');
  1753. $afternoondWhere[] = ['start_time_period', '>=', $zhongTimes[0]];
  1754. $afternoondWhere[] = ['end_time_period', '<=', $zhongTimes[1]];
  1755. $afternoondWhere['docter_id'] = $doctor_id;
  1756. $afternoondWhere['org_id'] = $org_id;
  1757. $afternoond = TimePeriod::where($afternoondWhere)->get()->toArray();
  1758. if (count($afternoond)>1){
  1759. array_pop($afternoond);
  1760. }
  1761. $afternoon = array_column($afternoond, 'id');
  1762. $nightWhere[] = ['start_time_period', '>=', $wanTimes[0]];
  1763. $nightWhere[] = ['end_time_period', '<=', $wanTimes[1]];
  1764. $nightWhere['docter_id'] = $doctor_id;
  1765. $nightWhere['org_id'] = $org_id;
  1766. $nightd = TimePeriod::where($nightWhere)->get()->toArray();
  1767. if (count($nightd)>1){
  1768. array_pop($nightd);
  1769. }
  1770. $night = array_column($nightd, 'id');
  1771. return [$morning, $afternoon, $night];
  1772. }
  1773. }
  1774. /**
  1775. * 获取俩月日期
  1776. * @return array
  1777. */
  1778. protected function date_bool($start_time = false, $end_time = false)
  1779. {
  1780. $weekarray = array(7, 1, 2, 3, 4, 5, 6);
  1781. $monthDays = [];
  1782. if ($start_time && $end_time) {
  1783. $firstDay = $start_time;
  1784. $lastDay = $end_time;
  1785. } else {
  1786. $firstDay = date('Y-m-01', time());
  1787. $lastDay = date('Y-m-d', strtotime("$firstDay +2 month -1 day"));
  1788. }
  1789. $i = 0;
  1790. while (date('Y-m-d', strtotime("$firstDay +$i days")) <= $lastDay) {
  1791. $monthDays[$i]['date'] = date('Y-m-d', strtotime("$firstDay +$i days"));
  1792. $monthDays[$i]['week'] = $weekarray[date("w", strtotime("$firstDay +$i days"))];
  1793. $i++;
  1794. }
  1795. return $monthDays;
  1796. }
  1797. /**
  1798. * 新版门诊排班(排班周模板)
  1799. * @return mixed
  1800. * @throws \Exception
  1801. */
  1802. public function week_scheduling()
  1803. {
  1804. $req = request()->post();
  1805. $this->validate(request(), [
  1806. 'data|数据' => 'required',
  1807. ]);
  1808. $user = $this->user;
  1809. $doctor_id = $user['id'];
  1810. $data = json_decode($req['data'], true);
  1811. // 查询自定义排班
  1812. $Wheres['docter_id'] = $doctor_id;
  1813. $Wheres['me_sure'] = 1;
  1814. $Wheres['schedule_type'] = 1;
  1815. $Wheres[] = ['schedule_date','>=',date('Y-m-d',time())];
  1816. $meSureList = SchedulePeriod::where($Wheres)->get();
  1817. $moth_date = $this->date_bool();
  1818. $delDate = [];
  1819. $delDate2 = [];
  1820. $schedule_ids = [];
  1821. $schedule = [];
  1822. foreach ($data as $k=>$v){
  1823. if ($v['identifi']['zao']==0&&$v['identifi']['zhong']==0&&$v['identifi']['wan']==0){
  1824. $delDate[] = $v['week'];
  1825. }
  1826. }
  1827. if ($meSureList){
  1828. foreach ($meSureList as $k=>$v){
  1829. $delDate2[] = $v['schedule_date'];
  1830. }
  1831. }
  1832. foreach ($moth_date as $k=>$v){
  1833. if (in_array($v['week'],$delDate)){
  1834. unset($moth_date[$k]);
  1835. }
  1836. if (in_array($v['date'],$delDate2)){
  1837. unset($moth_date[$k]);
  1838. }
  1839. }
  1840. DB::beginTransaction();
  1841. try {
  1842. WeekSchedule::where('docter_id', '=', $doctor_id)->where('schedule_type', '=', 1)->delete();
  1843. $delWhere['docter_id'] = $doctor_id;
  1844. $delWhere['me_sure'] = 0;
  1845. $delWhere['schedule_type'] = 1;
  1846. $delWhere[] = ['schedule_date','>=',date('Y-m-d',time())];
  1847. SchedulePeriod::where($delWhere)->delete();
  1848. Schedule::where(['docter_id'=>$doctor_id,'schedule_type'=>1])->whereNotIn('schedule_date',$delDate2)->delete();
  1849. foreach ($moth_date as $kk => $vv) {
  1850. $schedule[$kk]['schedule_type'] = 1;
  1851. $schedule[$kk]['schedule_date'] = $vv['date'];
  1852. $schedule[$kk]['schedule_day'] = str_replace('-', '', $vv['date']);
  1853. $schedule[$kk]['docter_id'] = $doctor_id;
  1854. $schedule[$kk]['organization_id'] = 0;
  1855. $schedule[$kk]['week'] = $vv['week'];
  1856. $schedule[$kk]['per_time_num'] = 0;
  1857. $schedule_ids[$kk] = Schedule::insertGetId($schedule[$kk]);
  1858. }
  1859. foreach ($data as $k => $v) {
  1860. // 插入详情
  1861. if ($v['identifi']['zao']!=0) {
  1862. $maf[$k]['schedule_type'] = 1;
  1863. $maf[$k]['docter_id'] = $doctor_id;
  1864. $maf[$k]['week'] = $v['week'];
  1865. $maf[$k]['type'] = 1;
  1866. $maf[$k]['organization_id'] = $v['identifi']['zao'];
  1867. $weekList[] = $maf[$k];
  1868. }
  1869. if ($v['identifi']['zhong']!=0) {
  1870. $maf[$k]['schedule_type'] = 1;
  1871. $maf[$k]['docter_id'] = $doctor_id;
  1872. $maf[$k]['week'] = $v['week'];
  1873. $maf[$k]['type'] = 2;
  1874. $maf[$k]['organization_id'] = $v['identifi']['zhong'];
  1875. $weekList[] = $maf[$k];
  1876. }
  1877. if ($v['identifi']['wan']!=0) {
  1878. $maf[$k]['schedule_type'] = 1;
  1879. $maf[$k]['docter_id'] = $doctor_id;
  1880. $maf[$k]['week'] = $v['week'];
  1881. $maf[$k]['type'] = 3;
  1882. $maf[$k]['organization_id'] = $v['identifi']['wan'];
  1883. $weekList[] = $maf[$k];
  1884. }
  1885. }
  1886. $scheduleLists = [];
  1887. foreach ($schedule_ids as $kk => $vv) {
  1888. foreach ($data as $k => $v) {
  1889. if ($schedule[$kk]['week'] == $v['week']) {
  1890. //检查有没有设置时间
  1891. if ($v['identifi']['zao']) {
  1892. $times = $this->num_time($v['identifi']['zao'], 1);
  1893. if (!$times) {
  1894. return out('', 500, "请先在排班设置中填写本机构的早上时间段");
  1895. }
  1896. $maf = $this->getMaf($vv, $times, ['organization_id' => $v['identifi']['zao'],'me_sure'=>0, 'schedule_date' => $schedule[$kk]['schedule_date'], 'type' => 1], $doctor_id);
  1897. if ($maf) {
  1898. foreach ($maf as $os) {
  1899. $scheduleLists[] = $os;
  1900. }
  1901. }
  1902. }
  1903. if ($v['identifi']['zhong']) {
  1904. $times = $this->num_time($v['identifi']['zhong'], 2);
  1905. if (!$times) {
  1906. return out('', 500, "请先在排班设置中填写本机构的下午时间段");
  1907. }
  1908. $maf = $this->getMaf($vv, $times, ['organization_id' => $v['identifi']['zhong'],'me_sure'=>0, 'schedule_date' => $schedule[$kk]['schedule_date'], 'type' => 2], $doctor_id);
  1909. if ($maf) {
  1910. foreach ($maf as $os) {
  1911. $scheduleLists[] = $os;
  1912. }
  1913. }
  1914. }
  1915. if ($v['identifi']['wan']) {
  1916. $times = $this->num_time($v['identifi']['wan'], 3);
  1917. if (!$times) {
  1918. return out('', 500, "请先在排班设置中填写本机构的晚上时间段");
  1919. }
  1920. $maf = $this->getMaf($vv, $times, ['organization_id' => $v['identifi']['wan'],'me_sure'=>0, 'schedule_date' => $schedule[$kk]['schedule_date'], 'type' => 3], $doctor_id);
  1921. if ($maf) {
  1922. foreach ($maf as $os) {
  1923. $scheduleLists[] = $os;
  1924. }
  1925. }
  1926. }
  1927. }
  1928. }
  1929. }
  1930. SchedulePeriod::insert($scheduleLists);
  1931. WeekSchedule::insert($weekList);
  1932. DB::commit();
  1933. return out();
  1934. } catch (\Exception $e) {
  1935. DB::rollBack();
  1936. return out('', 500, $e->getFile() . '中的第:' . $e->getLine() . '行报错:' . $e->getCode(), $e->getMessage());
  1937. } catch (\PDOException $e) {
  1938. DB::rollBack();
  1939. return out('', 500, $e->getFile() . '中的第:' . $e->getLine() . '行报错:' . $e->getCode(), $e->getMessage());
  1940. }
  1941. }
  1942. /**
  1943. * 修改某一天的数据
  1944. * @author Liu-Yh
  1945. * Create By 2020/12/16 18:46
  1946. */
  1947. public function saveDay($data = [])
  1948. {
  1949. $flag = false;
  1950. if (empty($data)) {
  1951. $req = request()->post();
  1952. $this->validate(request(), [
  1953. 'schedule_date|日期' => 'required',
  1954. 'week|周' => 'required',
  1955. 'type|类型' => 'required',
  1956. 'organization_id|日期' => 'required',
  1957. ]);
  1958. $req['schedule_date'] = date('Y-m-d',strtotime($req['schedule_date']));
  1959. } else {
  1960. $flag = true;
  1961. $req = $data;
  1962. }
  1963. $user = $this->user;
  1964. $doctor_id = $user['id'];
  1965. $save_data = [];
  1966. DB::beginTransaction();
  1967. try {
  1968. $type_data = json_decode($req['type'],true);
  1969. $where = [];
  1970. $where['schedule_date'] = $req['schedule_date'];
  1971. $where['docter_id'] = $doctor_id;
  1972. $where['schedule_type'] = 1;
  1973. $where['organization_id'] = $req['organization_id'];
  1974. if ($type_data['mon']==0&&$type_data['aft']==0&&$type_data['nig']==0){
  1975. // 进行删除
  1976. unset($where['organization_id']);
  1977. SchedulePeriod::where($where)->delete();
  1978. Schedule::where($where)->delete();
  1979. return out();
  1980. }
  1981. SchedulePeriod::where($where)->delete();
  1982. $save_data=[];
  1983. if($type_data['mon']!=0) {
  1984. $mon_times = $this->num_time($type_data['mon'], 1);
  1985. $mf1 = $this->arrangeDay($mon_times, ['organization_id' => $type_data['mon'], 'week' => $req['week'], 'schedule_date' => $req['schedule_date'], 'type' => 1], $doctor_id);
  1986. if($mf1){
  1987. foreach ($mf1 as $v){
  1988. $save_data[] = $v;
  1989. }
  1990. }
  1991. }
  1992. if($type_data['aft']!=0){
  1993. $aft_times = $this->num_time($type_data['aft'], 2);
  1994. $mf2 = $this->arrangeDay($aft_times, ['organization_id' => $type_data['aft'], 'week' => $req['week'], 'schedule_date' => $req['schedule_date'], 'type' => 2], $doctor_id);
  1995. if($mf2){
  1996. foreach ($mf2 as $v){
  1997. $save_data[] = $v;
  1998. }
  1999. }
  2000. }
  2001. if($type_data['nig']!=0) {
  2002. $nig_times = $this->num_time($type_data['nig'], 3);
  2003. $mf3 = $this->arrangeDay($nig_times, ['organization_id' => $type_data['nig'], 'week' => $req['week'], 'schedule_date' => $req['schedule_date'], 'type' => 3], $doctor_id);
  2004. if($mf3){
  2005. foreach ($mf3 as $v){
  2006. $save_data[] = $v;
  2007. }
  2008. }
  2009. }
  2010. if (!$flag){
  2011. $lists = Schedule::where(['docter_id'=>$doctor_id,'schedule_date'=>$req['schedule_date']])->first();
  2012. if($lists){
  2013. if($save_data){
  2014. $chong = [];
  2015. foreach ($save_data as $k => $v) {
  2016. $chong[$k]['schedule_id'] = $lists['id'];
  2017. $chong[$k]['docter_id'] = $v['docter_id'];
  2018. $chong[$k]['order_num'] = 0;
  2019. $chong[$k]['organization_id'] = $v['organization_id'];
  2020. $chong[$k]['time_period_id'] = $v['time_period_id'];
  2021. $chong[$k]['type'] = $v['type'];
  2022. $chong[$k]['me_sure'] = 1;
  2023. $chong[$k]['schedule_date'] = $v['schedule_date'];
  2024. }
  2025. SchedulePeriod::insert($chong);
  2026. }
  2027. }
  2028. }
  2029. DB::commit();
  2030. if ($flag) {
  2031. return true;
  2032. } else {
  2033. return out();
  2034. }
  2035. } catch (\Exception $e) {
  2036. DB::rollBack();
  2037. if ($flag) {
  2038. return false;
  2039. } else {
  2040. return out('', 500, $e->getFile() . '中的第:' . $e->getLine() . '行报错:' . $e->getCode(), $e->getMessage());
  2041. }
  2042. } catch (\PDOException $e) {
  2043. DB::rollBack();
  2044. if ($flag) {
  2045. return false;
  2046. } else {
  2047. return out('', 500, $e->getFile() . '中的第:' . $e->getLine() . '行报错:' . $e->getCode(), $e->getMessage());
  2048. }
  2049. }
  2050. }
  2051. /**
  2052. * 整理单个修改排班的数据
  2053. * @param $time
  2054. * @param $req
  2055. * @param $doctor_id
  2056. * @return array
  2057. * @author Liu-Yh
  2058. * Create By 2020/12/16 19:00
  2059. */
  2060. protected function arrangeDay($time, $req, $doctor_id)
  2061. {
  2062. $save_data = [];
  2063. foreach ($time as $k => $v) {
  2064. $save_data[$k]['schedule_date'] = $req['schedule_date'];
  2065. $save_data[$k]['week'] = $req['week'];
  2066. $save_data[$k]['schedule_type'] = 1;
  2067. $save_data[$k]['type'] = $req['type'];
  2068. $save_data[$k]['organization_id'] = $req['organization_id'];
  2069. $save_data[$k]['docter_id'] = $doctor_id;
  2070. $save_data[$k]['time_period_id'] = $v;
  2071. }
  2072. return $save_data;
  2073. }
  2074. /**
  2075. * 查看月视图
  2076. * @author Liu-Yh
  2077. * Create By 2020/12/16 19:20
  2078. */
  2079. public function monthDetail()
  2080. {
  2081. $req = request()->post();
  2082. $this->validate(request(), [
  2083. 'start_day|日期' => 'required',
  2084. 'end_day|日期' => 'required',
  2085. ]);
  2086. //格式化日期
  2087. $req['start_day'] = date('Y-m-d',strtotime($req['start_day']));
  2088. $req['end_day'] = date('Y-m-d',strtotime($req['end_day']));
  2089. $user = $this->user;
  2090. $doctor_id = $user['id'];
  2091. $Wheres['docter_id'] = $doctor_id;
  2092. $Wheres['schedule_type'] = 1;
  2093. $list = SchedulePeriod::where($Wheres)->whereBetween('schedule_date',[$req['start_day'],$req['end_day']])->select(['schedule_date','id','organization_id','type'])->orderBy('schedule_date','asc')->get();
  2094. // 定义数组整合数据
  2095. if ($list){
  2096. $list = $list->toArray();
  2097. }else{
  2098. return out([]);
  2099. }
  2100. $dateFlag = [];
  2101. foreach ($list as $k=>$v){
  2102. if (!isset($dateFlag[$v['schedule_date']])) {
  2103. $dateFlag[$v['schedule_date']] = $v;
  2104. if ($v['type'] == 1) {
  2105. $dateFlag[$v['schedule_date']]['ident']['zao'] = $v['organization_id'];
  2106. $dateFlag[$v['schedule_date']]['ident']['xia'] = 0;
  2107. $dateFlag[$v['schedule_date']]['ident']['wan'] = 0;
  2108. } else if ($v['type'] == 2) {
  2109. $dateFlag[$v['schedule_date']]['ident']['zao'] = 0;
  2110. $dateFlag[$v['schedule_date']]['ident']['xia'] = $v['organization_id'];
  2111. $dateFlag[$v['schedule_date']]['ident']['wan'] = 0;
  2112. } else {
  2113. $dateFlag[$v['schedule_date']]['ident']['zao'] = 0;
  2114. $dateFlag[$v['schedule_date']]['ident']['xia'] = 0;
  2115. $dateFlag[$v['schedule_date']]['ident']['wan'] = $v['organization_id'];
  2116. }
  2117. $dateFlag[$v['schedule_date']]['date'] = $v['schedule_date'];
  2118. } else {
  2119. if ($v['type'] == 1) {
  2120. $dateFlag[$v['schedule_date']]['ident']['zao'] = $v['organization_id'];
  2121. }
  2122. if ($v['type'] == 2) {
  2123. $dateFlag[$v['schedule_date']]['ident']['xia'] = $v['organization_id'];
  2124. }
  2125. if ($v['type'] == 3){
  2126. $dateFlag[$v['schedule_date']]['ident']['wan'] = $v['organization_id'];
  2127. }
  2128. }
  2129. }
  2130. return out(array_values($dateFlag));
  2131. }
  2132. /**
  2133. * 获取早中下午的id
  2134. * @param $sid
  2135. * @param $time
  2136. * @param $req
  2137. * @param $doctor_id
  2138. * @return array
  2139. * @author Liu-Yh
  2140. * Create By 2020/11/6 16:27
  2141. */
  2142. protected function getMaf($sid, $time, $req, $doctor_id)
  2143. {
  2144. $maf = [];
  2145. foreach ($time as $k => $v) {
  2146. $maf[$k]['schedule_id'] = $sid;
  2147. $maf[$k]['organization_id'] = $req['organization_id'];
  2148. $maf[$k]['time_period_id'] = $v;
  2149. $maf[$k]['schedule_date'] = $req['schedule_date'];
  2150. $maf[$k]['docter_id'] = $doctor_id;
  2151. $maf[$k]['type'] = $req['type'];
  2152. $maf[$k]['order_num'] = 0;
  2153. $maf[$k]['me_sure'] = $req['me_sure'];
  2154. }
  2155. return $maf;
  2156. }
  2157. /**
  2158. * 周模板添加数据
  2159. * @param $time
  2160. * @param $req
  2161. * @param $doctor_id
  2162. * @return array
  2163. */
  2164. protected function week_getMaf($time, $req, $doctor_id)
  2165. {
  2166. $maf = [];
  2167. foreach ($time as $k => $v) {
  2168. // $maf[$k]['schedule_id']=$sid;
  2169. // $maf[$k]['schedule_date']=$req['schedule_date'];
  2170. $maf[$k]['type'] = $req['type'];
  2171. $maf[$k]['schedule_type'] = 1;
  2172. $maf[$k]['organization_id'] = $req['organization_id'];
  2173. $maf[$k]['docter_id'] = $doctor_id;
  2174. $maf[$k]['time_period_id'] = $v;
  2175. $maf[$k]['week'] = $req['week'];
  2176. }
  2177. return $maf;
  2178. }
  2179. /**
  2180. * 获取会话列表
  2181. * @return \Illuminate\Http\JsonResponse
  2182. * @author Liu-Yh
  2183. * Create By 2020/11/6 17:11
  2184. */
  2185. public function getUserListMsg()
  2186. {
  2187. $req = request()->post();
  2188. $user = $this->user;
  2189. $doctor_id = $user['id'];
  2190. $where[] = ['product_type', 2];
  2191. $where[] = ['order_status', '>', 2];
  2192. $where[] = ['payment_status', 2];
  2193. $where[] = ['docter_id', $doctor_id];
  2194. $list = Order::with('user')->where($where)->orderBy('receiving_time', 'desc')->get();
  2195. $newList = [];
  2196. $isflag = [];
  2197. if ($list) {
  2198. $list = $list->toArray();
  2199. foreach ($list as $k => $v) {
  2200. $messages = ImMessage::where(['senderId'=>'doctor_'.$doctor_id,'receiverId'=>'member_'.$v['user_id']])->orWhere(function ($query) use ($req,$doctor_id,$v){
  2201. $query->where(['senderId'=>'member_'.$v['user_id'],'receiverId'=>'doctor_'.$doctor_id]);
  2202. })->orderBy('id','asc')->first();
  2203. if($messages){
  2204. if($messages['type']=='text'){
  2205. $newList[$v['user_id']]['out_message'] = $messages['payload'];
  2206. }else if ($messages['type'] == 'audio'){
  2207. $newList[$v['user_id']]['out_message'] = "[语音消息]";
  2208. }else{
  2209. $newList[$v['user_id']]['out_message'] = "[图片消息]";
  2210. }
  2211. if (empty($messages['create_time'])){
  2212. $newList[$v['user_id']]['out_time'] = "";
  2213. }else{
  2214. $newList[$v['user_id']]['out_time'] = date('m-d',$messages['create_time']);
  2215. }
  2216. }else{
  2217. $newList[$v['user_id']]['out_message'] = '';
  2218. $newList[$v['user_id']]['out_time'] = '';
  2219. }
  2220. $newList[$v['user_id']]['flag'] = 'member_' . $v['user_id'];
  2221. $newList[$v['user_id']]['user_id'] = $v['user_id'];
  2222. if ($v['order_status']==3){
  2223. $isflag[$k] =$v['user_id'];
  2224. }
  2225. $newList[$v['user_id']]['order_status'] = $v['order_status'];
  2226. $newList[$v['user_id']]['nickname'] = $v['user']['nickname'];
  2227. $newList[$v['user_id']]['avatar'] = $v['user']['avatar'];
  2228. $newList[$v['user_id']]['remark'] = !empty($v['user']['remark'])?$v['user']['remark']:'';
  2229. }
  2230. $newList = array_values($newList);
  2231. foreach ($newList as $k=>$v){
  2232. if (in_array($v['user_id'],$isflag)){
  2233. $newList[$k]['order_status'] = 3;
  2234. }
  2235. }
  2236. }
  2237. return out($newList);
  2238. }
  2239. /**
  2240. * 获取认证信息
  2241. * @return mixed
  2242. */
  2243. public function getRenInfo()
  2244. {
  2245. $req = request()->post();
  2246. $user = $this->user;
  2247. $doctor_id = $user['id'];
  2248. // 获取本人认证信息
  2249. $returnRes = [];
  2250. $docter = Docter::where('id', '=', $doctor_id)->first();
  2251. if ($docter) {
  2252. $docter = $docter->toArray();
  2253. } else {
  2254. return $docter;
  2255. }
  2256. $returnRes['info'] = $docter;
  2257. // 获取申请的认证机构
  2258. $org = DocterOrganization::with(['organization'])->where('docter_id', '=', $doctor_id)->get();
  2259. $oqf = DocterOrganization::with(['office', 'qualification'])->where('docter_id', '=', $doctor_id)->get();
  2260. foreach ($org as $k => $v) {
  2261. $org[$k]['province'] = Areas::where('id', '=', $v['organization']['province_id'])->select('name')->first()['name'];
  2262. $org[$k]['city'] = Areas::where('id', '=', $v['organization']['city_id'])->select('name')->first()['name'];
  2263. $org[$k]['area'] = Areas::where('id', '=', $v['organization']['area_id'])->select('name')->first()['name'];
  2264. $org[$k]['name'] = $v['organization']['name'];
  2265. }
  2266. $returnRes['orgList'] = $org;
  2267. $returnRes['oqf'] = $oqf;
  2268. return out($returnRes);
  2269. }
  2270. /**
  2271. * 获取通话记录
  2272. * @return mixed
  2273. */
  2274. public function callLog(){
  2275. $req = request()->post();
  2276. $user = $this->user;
  2277. $doctor_id = $user['id'];
  2278. $where =[];
  2279. if(isset($req['order_id'])&&!empty($req['order_id'])){
  2280. $where['order_id'] = $req['order_id'];
  2281. }
  2282. $where['docter_id'] = $doctor_id;
  2283. $list = CallLog::where($where)->get();
  2284. return out($list);
  2285. }
  2286. /**
  2287. * 通话列表
  2288. * @return mixed
  2289. */
  2290. public function user_call(){
  2291. $req = request()->post();
  2292. $user = $this->user;
  2293. $doctor_id = $user['id'];
  2294. $list=[];
  2295. // var_dump($doctor_id);
  2296. //die();
  2297. if(isset($req['user_id'])&&!empty($req['user_id'])){
  2298. $ids = [];
  2299. $orderList = Order::where(['user_id'=>$req['user_id'],'product_type'=>1,'payment_status'=>2])->select('id')->get();
  2300. if($orderList){
  2301. foreach ($orderList as $k=>$v){
  2302. $ids[$k] = $v['id'];
  2303. }
  2304. }
  2305. $lists = CallLog::with('orders')->whereIn('order_id',$ids)->orderBy('call_time', 'desc')->get();
  2306. if ($lists){
  2307. foreach ($lists as $k=>$v){
  2308. $list[$k]['time'] = $v['call_time'];
  2309. $list[$k]['talk_time'] = $v['talk_time'];
  2310. $list[$k]['order_sn'] = $v['orders']['order_sn'];
  2311. }
  2312. }
  2313. }else{
  2314. $orderList = Order::with(['user','calllog'])->where(['docter_id'=>$doctor_id,'product_type'=>1,'payment_status'=>2])->groupBy(['user_id'])->get();
  2315. if ($orderList){
  2316. foreach ($orderList as $k=>$v){
  2317. $list[$k]['user'] = $v['user'];
  2318. $calls = CallLog::where('docter_id',$doctor_id)->orderBy('id','desc')->first();
  2319. if ($calls){
  2320. $list[$k]['user']['dates'] = $calls['call_time'];
  2321. }else{
  2322. $list[$k]['user']['dates'] = '无';
  2323. }
  2324. $save = UserDocter::where(['user_id'=>$v['user']['id'],'docter_id'=>$doctor_id])->first();
  2325. if ($save) {
  2326. $list[$k]['user']['remark'] = $save['remark'];
  2327. } else {
  2328. $list[$k]['user']['remark'] = '';
  2329. }
  2330. $list[$k]['user']['log'] = $v['calllog'];
  2331. // $list[$k]['log']['date'] = date('m/d',strtotime(end($v['calllog'])['call_time']));
  2332. }
  2333. }
  2334. }
  2335. return out($list);
  2336. }
  2337. /**
  2338. * 获取聊天记录
  2339. * @return mixed
  2340. */
  2341. public function getImList(){
  2342. $req = request()->post();
  2343. $user = $this->user;
  2344. $doctor_id = $user['id'];
  2345. $where =[];
  2346. if(!isset($req['flag'])||empty($req['flag'])){
  2347. return out("",500,'用户flag不能为空');
  2348. }
  2349. $list = ImMessage::where(['senderId'=>'doctor_'.$doctor_id,'receiverId'=>$req['flag']])->orWhere(function ($query) use ($req,$doctor_id){
  2350. $query->where(['senderId'=>$req['flag'],'receiverId'=>'doctor_'.$doctor_id]);
  2351. })->get();
  2352. $newList=[];
  2353. if ($list){
  2354. foreach ($list as $k=>$v){
  2355. $decode = json_decode($v['text'],true);
  2356. // $newList[$k]['id']=$decode['id'];
  2357. $newList[$k]['messageId']=$decode[0]['messageId'];
  2358. $newList[$k]['type']=$decode[0]['type'];
  2359. $newList[$k]['senderId']=$decode[0]['senderId'];
  2360. $newList[$k]['receiverId']=$decode[0]['receiverId'];
  2361. $newList[$k]['timestamp']=$decode[0]['timestamp'];
  2362. $newList[$k]['payload']= json_decode($decode[0]['payload'],true);
  2363. }
  2364. }
  2365. return out($newList);
  2366. }
  2367. }