DoctorController.php 97 KB

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