DoctorController.php 97 KB

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