DoctorController.php 102 KB

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