DoctorController.php 99 KB

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