DoctorController.php 100 KB

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