DoctorController.php 101 KB

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