ScheDuleController.php 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\CdmsUsers;
  4. use App\Models\CommunitySchedule;
  5. use App\Models\Docter;
  6. use App\Models\DocterOrganization;
  7. use App\Models\DocterSetting;
  8. use App\Models\Order;
  9. use App\Models\Organization;
  10. use App\Models\Schedule;
  11. use App\Models\SchedulePeriod;
  12. use App\Models\SelfSchedule;
  13. use App\Models\SystemConfig;
  14. use App\Models\TimePeriod;
  15. use App\Models\WeekSchedule;
  16. use Carbon\Carbon;
  17. use Encore\Admin\Admin;
  18. use Illuminate\Support\Facades\DB;
  19. use Illuminate\Support\Facades\Log;
  20. use mysql_xdevapi\Exception;
  21. use phpDocumentor\Reflection\DocBlock\Description;
  22. class ScheDuleController extends Controller
  23. {
  24. public function __construct()
  25. {
  26. // 允许 $originarr 数组内的 域名跨域访问
  27. header('Access-Control-Allow-Origin:*');
  28. // 响应类型
  29. header('Access-Control-Allow-Methods:POST,GET');
  30. // 带 cookie 的跨域访问
  31. header('Access-Control-Allow-Credentials: true');
  32. // 响应头设置
  33. header('Access-Control-Allow-Headers:x-requested-with,Content-Type,X-CSRF-Token');
  34. }
  35. //获取排班页面
  36. public function getComScheduleback()
  37. {
  38. $set_time = request('time');//时间
  39. $org_id = request('org_id');//机构
  40. $docter_id = request('docter_id');//医生
  41. // $set_time = '2020-11-30至2020-12-06';
  42. // $org_id = 1;
  43. // $docter_id = 47;
  44. $role = 2;
  45. if(empty($set_time) || empty($org_id) || empty($docter_id)){
  46. return json_encode(['status'=>601,'msg'=>'缺少必要参数','data'=>'']);
  47. } else{
  48. // echo 'what happend';
  49. }
  50. $all_time = explode('至',$set_time);
  51. if(count($all_time) <2){
  52. return json_encode(['status'=>601,'msg'=>'缺少必要参数','data'=>'']);
  53. }
  54. $start_time = intval(str_replace('-','',$all_time[0]));
  55. $end_time = intval(str_replace('-','',$all_time[1]));
  56. $first_day = strtotime($all_time[0]);
  57. // DB::enableQueryLog();
  58. $sche_dule = Schedule::whereBetween('schedule_day',[$start_time,$end_time])
  59. ->where(['docter_id'=>$docter_id])
  60. ->pluck('id')
  61. ->toArray();
  62. // echo '<pre>';
  63. // print_r(DB::getQueryLog());
  64. // echo '</pre>';
  65. //dd($sche_dule);
  66. $sche_dule_arr =[
  67. [
  68. 'date'=>"上午",
  69. 'monday'=>false,
  70. 'tuesday'=>false,
  71. 'wednesday'=>false,
  72. 'thursday'=>false,
  73. 'friday'=>false,
  74. 'saturday'=>false,
  75. 'sunday'=>false,
  76. ],
  77. [
  78. 'date'=>"下午",
  79. 'monday'=>false,
  80. 'tuesday'=>false,
  81. 'wednesday'=>false,
  82. 'thursday'=>false,
  83. 'friday'=>false,
  84. 'saturday'=>false,
  85. 'sunday'=>false,
  86. ],
  87. [
  88. 'date'=>"晚上",
  89. 'monday'=>false,
  90. 'tuesday'=>false,
  91. 'wednesday'=>false,
  92. 'thursday'=>false,
  93. 'friday'=>false,
  94. 'saturday'=>false,
  95. 'sunday'=>false,
  96. ]
  97. ];
  98. //周字段映射
  99. $week_arr = ['monday','tuesday','wednesday','thursday','friday','saturday','sunday'];
  100. //时段区间id
  101. $schedule_config = SystemConfig::get('docter_config');
  102. $times[] = TimePeriod::where('start_time_period','>=',$schedule_config['morning_start'])->where('end_time_period','<=',$schedule_config['morning_end'])->pluck('id')->toArray();
  103. $times[] = TimePeriod::where('start_time_period','>=',$schedule_config['afternoon_start'])->where('end_time_period','<=',$schedule_config['afternoon_end'])->pluck('id')->toArray();
  104. $times[] = TimePeriod::where('start_time_period','>=',$schedule_config['evening_start'])->where('end_time_period','<=',$schedule_config['evening_end'])->pluck('id')->toArray();
  105. if(empty($sche_dule)){
  106. return ['status'=>200,'msg'=>'ok','data'=>['docter'=>$sche_dule_arr,'org_id'=>$org_id,'docter_id'=>$docter_id]];
  107. }
  108. $role = 2;
  109. if($role == 1){
  110. $ids = DocterOrganization::where(['organization_id'=>$org_id])->pluck('docter_id');
  111. $list = Docter::whereIn('id',$ids)->get(['id as value','name as label'])->toArray();
  112. $name = Organization::where('id',$docter_id)->value('name');
  113. $user_id = $org_id;
  114. for ($i = 1; $i<7; $i++){
  115. $str_time = date('Y-m-d',($first_day + $i * 86400));
  116. for($j=0;$j<3;$j++){
  117. //检测上午
  118. $is_mo_have = SchedulePeriod::whereIn('schedule_id',$sche_dule)->where('schedule_date',$str_time)->whereIn('time_period_id',$times[$j])
  119. ->where('organization_id','!=',$org_id)->count();
  120. ['schedule_id'=>$sche_dule,'schedule_date'=>$str_time,'organization_id'=>$org_id,'docter_id'=>$docter_id];
  121. $is_self = SchedulePeriod::whereIn('schedule_id',$sche_dule)->where('schedule_date',$str_time)->whereIn('time_period_id',$times[$j])
  122. ->where('organization_id',$org_id)->count();
  123. if($is_self){
  124. $sche_dule_arr[$j][$week_arr[$i]] = true;
  125. }
  126. if($is_mo_have){
  127. $sche_dule_arr[$j][$week_arr[$i]] = 'other';
  128. }
  129. }
  130. }
  131. } else {
  132. $ids = DocterOrganization::where(['docter_id'=>$docter_id])->pluck('organization_id');
  133. $list = Organization::whereIn('id',$ids)->get(['id as value','name as label'])->toArray();
  134. $name = Docter::where('id',$docter_id)->value('name');
  135. $user_id = $docter_id;
  136. for ($i=0; $i<=6; $i++){
  137. $str_time = date('Y-m-d',($first_day + $i * 86400));
  138. for($j=0;$j<=2;$j++){
  139. //检测上午
  140. $is_mo_have = SchedulePeriod::whereIn('schedule_id',$sche_dule)->where('schedule_date',$str_time)->whereIn('time_period_id',$times[$j])
  141. ->where('organization_id','!=',$org_id)->where('docter_id','=',$docter_id)->count();
  142. $is_mo_self = SchedulePeriod::whereIn('schedule_id',$sche_dule)->where('schedule_date',$str_time)->whereIn('time_period_id',$times[$j])
  143. ->where('organization_id',$org_id)->where('docter_id',$docter_id)->count();
  144. // echo $i.'--'.$j.'is_self---'.$is_mo_self.'<br>';
  145. if($is_mo_self){
  146. $sche_dule_arr[$j][$week_arr[$i]] = true;
  147. }
  148. if($is_mo_have){
  149. $sche_dule_arr[$j][$week_arr[$i]] = 'other';
  150. }
  151. }
  152. }
  153. }
  154. // "morning_start" => "09:00"
  155. // "morning_end" => "12:00"
  156. // "afternoon_start" => "13:00"
  157. // "afternoon_end" => "18:00"
  158. // "evening_start" => "19:00"
  159. // "evening_end" => "20:00"
  160. $data['docter'] = $sche_dule_arr;
  161. $data['name'] = $name;
  162. // $data['list'] = $list;
  163. $data['role'] = $role;
  164. $data['user_id'] = $user_id;
  165. return (json_encode(['code'=>200,'msg'=>'ok','data'=>$data]));
  166. }
  167. //获取周排班
  168. public function getComSchedule(){
  169. $docter_id = request('docter_id',1);
  170. $org_id = request('org_id',1);
  171. $type = request('type',2);
  172. $role = request('role',2);
  173. if(empty($docter_id) || empty($type)){
  174. return apiReturn(604,'缺少必要参数');
  175. }
  176. $docter_id = 1;
  177. $docter_setting = DocterSetting::where(['docter_id'=>$docter_id,'type'=>$type])->first();
  178. $times = [];
  179. if(!empty($docter_setting)){
  180. $time_period = object_array(json_decode($docter_setting->service_time));
  181. $times[] = TimePeriod::where(['docter_id'=>$docter_id])->whereBetween('end_time_period',[intval($time_period[0]['startTime']),intval($time_period[0]['endTime'])])->pluck('id')->toArray();
  182. $times[] = TimePeriod::where(['docter_id'=>$docter_id])->whereBetween('end_time_period',[intval($time_period[1]['startTime']),intval($time_period[1]['endTime'])])->pluck('id')->toArray();
  183. $times[] = TimePeriod::where(['docter_id'=>$docter_id])->whereBetween('end_time_period',[intval($time_period[2]['startTime']),intval($time_period[2]['endTime'])])->pluck('id')->toArray();
  184. }
  185. if(count($times) < 3){
  186. dd('no have times');
  187. }
  188. //周字段映射
  189. $week_arr = ['monday','tuesday','wednesday','thursday','friday','saturday','sunday'];
  190. $data = [
  191. [
  192. 'date'=>"上午",
  193. 'monday'=>false,
  194. 'tuesday'=>false,
  195. 'wednesday'=>false,
  196. 'thursday'=>false,
  197. 'friday'=>false,
  198. 'saturday'=>false,
  199. 'sunday'=>false,
  200. ],
  201. [
  202. 'date'=>"下午",
  203. 'monday'=>false,
  204. 'tuesday'=>false,
  205. 'wednesday'=>false,
  206. 'thursday'=>false,
  207. 'friday'=>false,
  208. 'saturday'=>false,
  209. 'sunday'=>false,
  210. ],
  211. [
  212. 'date'=>"晚上",
  213. 'monday'=>false,
  214. 'tuesday'=>false,
  215. 'wednesday'=>false,
  216. 'thursday'=>false,
  217. 'friday'=>false,
  218. 'saturday'=>false,
  219. 'sunday'=>false,
  220. ]
  221. ];
  222. if($role == 2){
  223. for ($i=0;$i<6;$i++){
  224. for($j=0;$j<=2;$j++){
  225. $is_self_have = WeekSchedule::where(['docter_id'=>$docter_id,'schedule_type'=>$type,'organization_id'=>$org_id,'week'=>$i+1])->whereIn('time_period_id',$times[$j])->count();
  226. $is_other_have = WeekSchedule::where(['docter_id'=>$docter_id,'schedule_type'=>$type,'week'=>$i+1])->where('organization_id','!=',$org_id)->whereIn('time_period_id',$times[$j])->count();
  227. if($is_self_have){
  228. $data[$j][$week_arr[$i]] = true;
  229. }
  230. if($is_other_have){
  231. $data[$j][$week_arr[$i]] = 'other';
  232. }
  233. }
  234. }
  235. } else {
  236. for ($i=0;$i<6;$i++){
  237. for($j=0;$j<=2;$j++){
  238. $is_self_have = WeekSchedule::where(['docter_id'=>$docter_id,'schedule_type'=>$type,'organization_id'=>$org_id,'week'=>$i+1])->whereIn('time_period_id',$times[$j])->count();
  239. $is_other_have = WeekSchedule::where(['docter_id'=>$docter_id,'schedule_type'=>$type,'week'=>$i+1])->where('docter_id','!=',$docter_id)->whereIn('time_period_id',$times[$j])->count();
  240. if($is_self_have){
  241. $data[$j][$week_arr[$i]] = true;
  242. }
  243. if($is_other_have){
  244. $data[$j][$week_arr[$i]] = 'other';
  245. }
  246. }
  247. }
  248. }
  249. $name = Docter::where('id',$docter_id)->value('name');
  250. $shcedule_data['docter'] = $data;
  251. $shcedule_data['name'] = $name;
  252. $shcedule_data['role'] = $role;
  253. $shcedule_data['user_id'] = $docter_id;
  254. return apiReturn(200,'ok',$shcedule_data);
  255. }
  256. //编辑排班
  257. public function setDocCheduleback()
  258. {
  259. $docter_id = request('docter_id');
  260. $org_id = request('org_id');
  261. $set_time = request('time');
  262. $data = request('schedulingtime');
  263. // $set_time = '2020-12-07至2020-12-13';
  264. if(empty($set_time) || empty($org_id) || empty($docter_id)){
  265. return json_encode(['status'=>601,'msg'=>'缺少必要参数','data'=>'']);
  266. }
  267. $all_time = explode('至',$set_time);
  268. $schedule_id = Schedule::where(['organization_id'=>$org_id,'docter_id'=>$docter_id,'schedule_date'=>$all_time[0]])->value('id');
  269. if(count($all_time) <2) {
  270. return json_encode(['status'=>601,'msg'=>'缺少必要参数','data'=>'']);
  271. }
  272. $start_time = intval(str_replace('-','',$all_time[0]));
  273. $end_time = intval(str_replace('-','',$all_time[1]));
  274. $first_day = strtotime($start_time);
  275. //周字段映射
  276. $week_arr = ['monday','tuesday','wednesday','thursday','friday','saturday','sunday'];
  277. // $data = [
  278. // [
  279. // 'date'=>"上午",
  280. // 'monday'=>false,
  281. // 'tuesday'=>false,
  282. // 'wednesday'=>false,
  283. // 'thursday'=>true,
  284. // 'friday'=>false,
  285. // 'saturday'=>false,
  286. // 'sunday'=>false,
  287. // ],
  288. // [
  289. // 'date'=>"下午",
  290. // 'monday'=>false,
  291. // 'tuesday'=>true,
  292. // 'wednesday'=>false,
  293. // 'thursday'=>false,
  294. // 'friday'=>false,
  295. // 'saturday'=>false,
  296. // 'sunday'=>false,
  297. // ],
  298. // [
  299. // 'date'=>"晚上",
  300. // 'monday'=>false,
  301. // 'tuesday'=>false,
  302. // 'wednesday'=>true,
  303. // 'thursday'=>false,
  304. // 'friday'=>false,
  305. // 'saturday'=>false,
  306. // 'sunday'=>false,
  307. // ]
  308. // ];
  309. //时段区间id
  310. $schedule_config = SystemConfig::get('docter_config');
  311. $times[1] = TimePeriod::where('start_time_period','>=',$schedule_config['morning_start'])
  312. ->where('end_time_period','<=',$schedule_config['morning_end'])
  313. ->pluck('id')->toArray();
  314. $times[2] = TimePeriod::where('start_time_period','>=',$schedule_config['afternoon_start'])
  315. ->where('end_time_period','<=',$schedule_config['afternoon_end'])
  316. ->pluck('id')->toArray();
  317. $times[3] = TimePeriod::where('start_time_period','>=',$schedule_config['evening_start'])
  318. ->where('end_time_period','<=',$schedule_config['evening_end'])
  319. ->pluck('id')->toArray();
  320. foreach ($times as $val){
  321. foreach ($val as $t){
  322. $time_period[] = $t;
  323. }
  324. }
  325. $data[0] = array_values(object_array(json_decode($data[0])));
  326. $data[1] = array_values(object_array(json_decode($data[1])));
  327. $data[2] = array_values(object_array(json_decode($data[2])));
  328. $mo_schedule = array_values($data[0]);
  329. $af_schedule = array_values($data[1]);
  330. $ev_schedule = array_values($data[2]);
  331. DB::beginTransaction();
  332. try {
  333. $schedule_insert = [
  334. 'organization_id'=>$org_id,
  335. 'docter_id'=>$docter_id,
  336. 'schedule_date'=>$all_time[0],
  337. 'schedule_day'=>$start_time,
  338. 'created_at'=>Carbon::now(),
  339. 'updated_at'=>Carbon::now(),
  340. ];
  341. if(!$schedule_id){
  342. $schedule_id = Schedule::insertGetId($schedule_insert);
  343. }
  344. unset($schedule_insert['schedule_day']);
  345. $schedule_insert['schedule_id'] =$schedule_id;
  346. $info = [];
  347. for ($i = 1 ;$i<=3;$i++){
  348. $mo_schedule = array_values($data[$i-1]);
  349. foreach ($mo_schedule as $key=>$val){
  350. if($key==0) continue;
  351. $schedule_date = $start_time + $key;
  352. $strtime = "$schedule_date";
  353. $time_insert = $schedule_insert;
  354. $time_insert['type'] = $i;
  355. $schedule_time = $times[$i];
  356. $real_time = date('Y-m-d',($first_day + ($key-1)*86400));
  357. $time_insert['schedule_date'] = $real_time;
  358. $time_insert['me_sure'] = 1;
  359. if($val == true){
  360. $data['schedule_date'] = $start_time ;
  361. foreach ($schedule_time as $time_id){
  362. $is_have = SchedulePeriod::where(['time_period_id'=>$time_id,'type'=>($i),'docter_id'=>$docter_id,'schedule_date'=>$real_time,'organization_id'=>$org_id,'docter_id'=>$docter_id,'schedule_id'=>$schedule_id])->value('id');
  363. if($is_have) break;
  364. $time_insert['time_period_id'] = $time_id;
  365. $info[] = $time_insert;
  366. SchedulePeriod::insert($time_insert);
  367. }
  368. } else {
  369. $data['schedule_date'] = $start_time;
  370. foreach ($schedule_time as $time_id){
  371. $is_have = SchedulePeriod::where(['time_period_id'=>$time_id,'type'=>($i),'docter_id'=>$docter_id,'schedule_date'=>$real_time,'organization_id'=>$org_id,'docter_id'=>$docter_id,'schedule_id'=>$schedule_id])->value('id');
  372. if($is_have) SchedulePeriod::where(['time_period_id'=>$time_id,'type'=>($i),'docter_id'=>$docter_id,'schedule_date'=>$real_time,'organization_id'=>$org_id,'docter_id'=>$docter_id,'schedule_id'=>$schedule_id])->delete();
  373. }
  374. }
  375. }
  376. }
  377. DB::commit();
  378. } catch (Exception $e){
  379. DB::rollBack();
  380. Log::info($e->getMessage());
  381. }
  382. return json_encode(['status'=>200,'msg'=>'插入成功','data'=>count($info)]);
  383. }
  384. public function setDocChedule()
  385. {
  386. $docter_id = request('docter_id',1);
  387. $org_id = request('org_id',1);
  388. $type = request('type',2);
  389. if(empty($docter_id) || empty($org_id) || empty($type)){
  390. return apiReturn(601,'缺少必要参数');
  391. }
  392. $data = request('schedulingtime');
  393. if(empty($org_id) || empty($docter_id) || $data){
  394. return json_encode(['status'=>601,'msg'=>'缺少必要参数','data'=>'']);
  395. }
  396. $docter_setting = DocterSetting::where(['docter_id'=>$docter_id])->first();
  397. // $data = [
  398. // [
  399. // 'date'=>"上午",
  400. // 'monday'=>false,
  401. // 'tuesday'=>true,
  402. // 'wednesday'=>false,
  403. // 'thursday'=>false,
  404. // 'friday'=>true,
  405. // 'saturday'=>false,
  406. // 'sunday'=>false,
  407. // ],
  408. // [
  409. // 'date'=>"下午",
  410. // 'monday'=>false,
  411. // 'tuesday'=>false,
  412. // 'wednesday'=>false,
  413. // 'thursday'=>false,
  414. // 'friday'=>false,
  415. // 'saturday'=>true,
  416. // 'sunday'=>false,
  417. // ],
  418. // [
  419. // 'date'=>"晚上",
  420. // 'monday'=>true,
  421. // 'tuesday'=>true,
  422. // 'wednesday'=>false,
  423. // 'thursday'=>false,
  424. // 'friday'=>false,
  425. // 'saturday'=>false,
  426. // 'sunday'=>false,
  427. // ]
  428. // ];
  429. if(!empty($docter_setting)){
  430. $time_period = object_array(json_decode($docter_setting->service_time));
  431. $times[] = TimePeriod::where(['docter_id'=>$docter_id])->whereBetween('end_time_period',[intval($time_period[0]['startTime']),intval($time_period[0]['endTime'])])->pluck('id')->toArray();
  432. $times[] = TimePeriod::where(['docter_id'=>$docter_id])->whereBetween('end_time_period',[intval($time_period[1]['startTime']),intval($time_period[1]['endTime'])])->pluck('id')->toArray();
  433. $times[] = TimePeriod::where(['docter_id'=>$docter_id])->whereBetween('end_time_period',[intval($time_period[2]['startTime']),intval($time_period[2]['endTime'])])->pluck('id')->toArray();
  434. }
  435. $week_data = [
  436. 'schedule_type'=>$type,
  437. 'organization_id'=>$org_id,
  438. 'docter_id'=>$docter_id
  439. ];
  440. $k = 0;
  441. DB::beginTransaction();
  442. $data[0] = array_values(object_array(json_decode($data[0])));
  443. $data[1] = array_values(object_array(json_decode($data[1])));
  444. $data[2] = array_values(object_array(json_decode($data[2])));
  445. // $data[0] = array_values($data[0]);
  446. // $data[1] = array_values($data[1]);
  447. // $data[2] = array_values($data[2]);
  448. try {
  449. for($i=0;$i<=2;$i++){
  450. $schedule_data = $data[$i];
  451. foreach ($schedule_data as $key=>$schedule){
  452. if($key == 0) continue;
  453. $week_data ['type'] = $i+1;
  454. $week_data ['week'] = $key;
  455. if($schedule == true){
  456. // echo '时间列类型'.($i+1).'----'.$key.'星期数'.'<br>';
  457. foreach ($times[$i] as $id){
  458. $week_data['time_period_id'] = $id;
  459. $is_have = WeekSchedule::where(['docter_id'=>$docter_id,'organization_id'=>$org_id,'schedule_type'=>$type,'type'=>$i+1,'week'=>$key])->where('time_period_id',$id)->value('id');
  460. $is = $is_have?'有':'沒有';
  461. // echo '时间段id'.$id.'----'.$is.'</br>';
  462. if($is_have) continue;
  463. $k++;
  464. WeekSchedule::insert($week_data);
  465. }
  466. } else {
  467. $is_have = WeekSchedule::where(['docter_id' => $docter_id, 'organization_id' => $org_id, 'schedule_type' => $type,'type'=>$i+1,'week'=>$key])->whereIn('time_period_id', $times[$i])->value('id');
  468. if($is_have) WeekSchedule::where(['docter_id' => $docter_id, 'organization_id' => $org_id, 'schedule_type' => $type,'type'=>$i+1,'week'=>$key])->whereIn('time_period_id', $times[$i])->delete();
  469. }
  470. }
  471. }
  472. DB::commit();
  473. } catch (\Exception $e){
  474. DB::rollBack();
  475. return apiReturn(602,$e->getLine().'行'.$e->getMessage());
  476. }
  477. return apiReturn(200,'ok',$k);
  478. }
  479. //设置自己排班
  480. public function setSelfSchedule()
  481. {
  482. $docter_id = request('docter_id');
  483. $org_id = request('org_id');
  484. $date = request('date');
  485. $type = request('type','1,2'); //1 上午,2 下午,3 晚上
  486. $schedule_type = request('schedule_type');
  487. $self_setting = DocterSetting::where(['docter_id'=>$docter_id])->first();
  488. $times = [];
  489. if(!empty($self_setting)){
  490. $time_period = object_array(json_decode($self_setting->service_time));
  491. $times[1] = TimePeriod::where(['docter_id'=>$docter_id])->whereBetween('end_time_period',[intval($time_period[0]['startTime']),intval($time_period[0]['endTime'])])->pluck('id')->toArray();
  492. $times[2] = TimePeriod::where(['docter_id'=>$docter_id])->whereBetween('end_time_period',[intval($time_period[1]['startTime']),intval($time_period[1]['endTime'])])->pluck('id')->toArray();
  493. $times[3] = TimePeriod::where(['docter_id'=>$docter_id])->whereBetween('end_time_period',[intval($time_period[2]['startTime']),intval($time_period[2]['endTime'])])->pluck('id')->toArray();
  494. }
  495. if(count($times) <3){
  496. return apiReturn(602,'请设置真确的时间段');
  497. }
  498. if(empty($docter_id) || empty($org_id) || empty('date') || empty($type) || empty($schedule_type)){
  499. return apiReturn(601,'缺少必要参数');
  500. }
  501. if(strtotime($date) < strtotime('tomorrow')){
  502. return apiReturn(602,'只能排明天以後的');
  503. }
  504. $week = date("w",strtotime($date));
  505. $types = explode(',',$type);
  506. if(!empty($types)){
  507. foreach ($types as $t){
  508. $self_schedule = [
  509. 'schedule_date'=>$date,
  510. 'week'=>intval($week),
  511. 'schedule_type'=>$schedule_type,
  512. 'organization_id'=>$org_id,
  513. 'docter_id'=>$docter_id,
  514. 'type'=>intval($t)
  515. ];
  516. foreach ($times[intval($t)] as $id){
  517. $self_schedule['time_period_id'] = $id;
  518. $is_have = SelfSchedule::where(['docter_id'=>$docter_id,'organization_id'=>$org_id,'schedule_date'=>$date,'type'=>intval($t),'week'=>$week,'time_period_id'=>$id])->value('id');
  519. if($is_have) continue;
  520. SelfSchedule::insert($self_schedule);
  521. }
  522. }
  523. $cha = array_diff([1,2,3],$types);
  524. foreach ($cha as $t){
  525. SelfSchedule::where(['docter_id'=>$docter_id,'organization_id'=>$org_id,'schedule_date'=>$date,'type'=>intval($t)])->delete();
  526. }
  527. }
  528. return apiReturn(200,'ok');
  529. }
  530. //更新用戶設置
  531. public function updateDocterSetting()
  532. {
  533. $docter_id = request('docter_id',1);
  534. $configData = request('configData');
  535. $type = request('type');
  536. if(empty($docter_id) || empty($configData)){
  537. return apiReturn(604,'参数错误');
  538. }
  539. $configData = object_array(json_decode($configData));
  540. // $configData = [
  541. // 'amEndTime'=>"11:30",
  542. // 'amNum'=>20,
  543. // 'amStartTime'=>"09:00",
  544. // 'nightEndTime'=>"19:30",
  545. // 'nightNum'=>8,
  546. // 'nightStartTime'=>"11:30",
  547. // 'pmEndTime'=>"16:00",
  548. // 'pmNum'=>20,
  549. // 'pmStartTime'=>"13:30",
  550. // 'serviceNum'=>2,
  551. // 'status'=>false,
  552. // 'timeSpan'=>15,
  553. // ];
  554. // configData: {
  555. //         amEndTime: "11:30", //上午结束时间 string
  556. //        amNum: 20, //上午号源 Num
  557. //        amStartTime: "09:00", //上午开始时间 string
  558. //        nightEndTime: "19:30", //晚上结束时间 string
  559. //        nightNum: 8, //晚上号源 num
  560. //        nightStartTime: "18:30", //晚上开始时间 staring
  561. //        pmEndTime: "16:00", //下午结束时间 string
  562. //        pmNum: 20, //下午号源 num
  563. //        pmStartTime: "13:30", //下午开始时间 string
  564. //        serviceNum: 2, //同时服务人数 Num
  565. //        showDayNumber: 7, //可预约服务天数 Num
  566. //        status: false, // 是否开启 true false
  567. //        timeSpan: "15", // 就诊时长 string
  568. //      },
  569. $setting = DocterSetting::where(['docter_id'=>$docter_id])->first()->toArray();
  570. $self_config = object_array(json_decode($setting['service_time']));
  571. $is_new = [];
  572. //
  573. // if(intval($configData['timeSpan']) != $setting['service_num']){
  574. // $is_new[] = 4 ;
  575. // }
  576. //
  577. // if($configData['amStartTime'] != $self_config[1]['amStartTime'] || $configData['amEndTime'] != $self_config[1]['amEndTime']){
  578. // $is_new[] = 1;
  579. // }
  580. //
  581. // if($configData['pmStartTime'] != $self_config[2]['pmStartTime'] || $configData['pmEndTime'] != $self_config[2]['pmEndTime']){
  582. // $is_new[] = 2;
  583. // }
  584. //
  585. // if($configData['nightStartTime'] != $self_config[3]['nightStartTime'] || $configData['nightEndTime'] != $self_config[3]['nightEndTime']){
  586. // $is_new[] = 3;
  587. // }
  588. //
  589. // configData: {
  590. //         amEndTime: "11:30", //上午结束时间 string
  591. //        amNum: 20, //上午号源 Num
  592. //        amStartTime: "09:00", //上午开始时间 string
  593. //        nightEndTime: "19:30", //晚上结束时间 string
  594. //        nightNum: 8, //晚上号源 num
  595. //        nightStartTime: "18:30", //晚上开始时间 staring
  596. //        pmEndTime: "16:00", //下午结束时间 string
  597. //        pmNum: 20, //下午号源 num
  598. //        pmStartTime: "13:30", //下午开始时间 string
  599. //        serviceNum: 2, //同时服务人数 Num
  600. //        showDayNumber: 7, //可预约服务天数 Num
  601. //        status: false, // 是否开启 true false
  602. //        timeSpan: "15", // 就诊时长 string
  603. //      },
  604. //添加时间段
  605. $is_new = [1];
  606. if(count($is_new) > 5){
  607. // $data = ['docter_id'=>$docter_id,'am_start'=>'8:30','am_end'=>'11:30','pm_start'=>'12:00','pm_end'=>'15:00','ev_start'=>'19:00','ev_end'=>'22:00','service_time'=>15];
  608. // $amtimes[] = explode(':',$configData['amStartTime']);
  609. // $amtimes[] = explode(':',$configData['amEndTime']);
  610. // $amcha = ($amtimes[1][0] - $amtimes[0][0])*60 +($amtimes[1][1] - $amtimes[0][1]);
  611. // $amCount = $amcha/$data['service_time'];
  612. // $amCount = intval(floor((strtotime($configData['amEndTime']) - strtotime($configData['amStartTime'])) / (60*$configData['timeSpan'])));
  613. // $pmtimes[] = explode(':',$configData['pmStartTime']);
  614. // $pmtimes[] = explode(':',$configData['pmEndTime']);
  615. // $pmCount =intval(floor((strtotime($configData['pmEndTime']) - strtotime($configData['pmStartTime'])) / (60*$configData['timeSpan'])));
  616. //
  617. //
  618. // $evtimes[] = explode(':',$configData['nightStartTime']);
  619. // $evtimes[] = explode(':',$configData['nightEndTime']);
  620. // $evcha = ($evtimes[1][0] - $evtimes[0][0])*60 +($evtimes[1][1] - $evtimes[0][1]);
  621. // $evCount = $evcha/$configData['service_time'];
  622. $amCount = $configData['amNum'];
  623. $pmCount = $configData['pmNum'];
  624. $evCount = $configData['nightNum'];
  625. $today_id = Schedule::where(['docter_id'=>$docter_id])->where('schedule_date','=',date('Y-m-d'))->value('id');
  626. if(in_array(4,$is_new)){
  627. TimePeriod::where(['docter_id'=>$docter_id])->delete();
  628. $schuedule_ids = Schedule::where(['docter_id'=>$docter_id])->where(['schedule_date','>',date('Y-m-d')])->pluck('id')->toArray();
  629. SchedulePeriod::where(['docter_id'=>$docter_id])->whereIn('schedule_id',$schuedule_ids)->delete();
  630. for($i=1;$i<=$amCount;$i++){
  631. $amdata = [
  632. 'docter_id'=>$docter_id,
  633. 'start_time_period'=> date('H:i',strtotime($configData['amStartTime']) + ($i-1)*$configData['timeSpan']*60),
  634. 'end_time_period'=> date('H:i',strtotime($configData['amStartTime']) + $i*$configData['timeSpan']*60),
  635. ];
  636. TimePeriod::insert($amdata);
  637. }
  638. for($i=1;$i<=$pmCount;$i++){
  639. $pmdata = [
  640. 'docter_id'=>$docter_id,
  641. 'start_time_period'=> date('H:i',strtotime($configData['pmStartTime']) + ($i-1)*$configData['timeSpan']*60),
  642. 'end_time_period'=> date('H:i',strtotime($configData['pmStartTime']) + $i*$configData['timeSpan']*60),
  643. ];
  644. TimePeriod::insert($pmdata);
  645. }
  646. for($i=1;$i<=$evCount;$i++){
  647. $evdata = [
  648. 'docter_id'=>$docter_id,
  649. 'start_time_period'=> date('H:i',strtotime($configData['nightStartTime']) + ($i-1)*$configData['timeSpan']*60),
  650. 'end_time_period'=> date('H:i',strtotime($configData['nightStartTime']) + $i*$configData['timeSpan']*60),
  651. ];
  652. TimePeriod::insert($evdata);
  653. }
  654. } else {
  655. if(in_array(1,$is_new)){
  656. TimePeriod::where(['docter_id'=>$docter_id,'type'=>1])->delete();
  657. $old_ids = SelfSchedule::where(['docter_id'=>$docter_id,'type'=>1])->distinct('week')->pluck('week','type')->toArray();
  658. $schedule_ids = Schedule::where(['docter_id'=>$docter_id])->where(['schedule_date','>',date('Y-m-d')])->pluck('id')->toArray();
  659. SelfSchedule::where(['docter_id'=>$docter_id,'type'=>1])->whereIn('schedule_id',$schedule_ids)->delete();
  660. SchedulePeriod::where(['docter_id'=>$docter_id])->whereIn('schedule_id',$schedule_ids)->delete();
  661. for($i=1;$i<=$amCount;$i++){
  662. $amdata = [
  663. 'docter_id'=>$docter_id,
  664. 'start_time_period'=> date('H:i',strtotime($configData['amStartTime']) + ($i-1)*$configData['timeSpan']*60),
  665. 'end_time_period'=> date('H:i',strtotime($configData['amStartTime']) + $i*$configData['timeSpan']*60),
  666. ];
  667. TimePeriod::insert($amdata);
  668. }
  669. }
  670. if(in_array(2,$is_new)){
  671. for($i=1;$i<=$pmCount;$i++){
  672. $pmdata = [
  673. 'docter_id'=>$docter_id,
  674. 'start_time_period'=> date('H:i',strtotime($configData['pmStartTime']) + ($i-1)*$configData['timeSpan']*60),
  675. 'end_time_period'=> date('H:i',strtotime($configData['pmStartTime']) + $i*$configData['timeSpan']*60),
  676. ];
  677. TimePeriod::insert($pmdata);
  678. }
  679. }
  680. if(in_array(2,$is_new)) {
  681. for($i=1;$i<=$evCount;$i++){
  682. $evdata = [
  683. 'docter_id'=>$docter_id,
  684. 'start_time_period'=> date('H:i',strtotime($configData['nightStartTime']) + ($i-1)*$configData['timeSpan']*60),
  685. 'end_time_period'=> date('H:i',strtotime($configData['nightStartTime']) + $i*$configData['timeSpan']*60),
  686. ];
  687. TimePeriod::insert($evdata);
  688. }
  689. }
  690. }
  691. }
  692. // {"1":{"amStartTime":"08:00","amEndTime":"12:00","amPerson":3},"2":{"pmStartTime":"14:00","pmEndTime":"18:00","pmPerson":5},"3":{"nightStartTime":"18:00","nightEndTime":"23:00","nightPerson":4}}
  693. $config = [
  694. 1=>['amStartTime'=>$configData['amStartTime'],"amEndTime"=>$configData['amEndTime'],'amPerson'=>$configData['amNum']],
  695. 2=>["pmStartTime"=>$configData['pmStartTime'],"pmEndTime"=>$configData['pmEndTime'],'pmPerson'=>$configData['pmNum']],
  696. 3=>["nightStartTime"=>$configData['nightStartTime'],"nightEndTime"=>$configData['nightEndTime'],'nightPerson'=>$configData['nightNum']]
  697. ];
  698. $setting = [
  699. 'docter_id'=>$docter_id,
  700. 'type'=>$type,
  701. 'status'=>$configData['status'],
  702. 'show_days'=>$configData['showDayNumber'],
  703. 'service_num'=>$configData['serviceNum'],
  704. 'service_times'=>$configData['timeSpan'],
  705. 'service_time'=>json_encode($config)
  706. ];
  707. $is_have = DocterSetting::where(['docter_id'=>$docter_id])->value('id');
  708. if($is_have){
  709. DocterSetting::where(['docter_id'=>$docter_id])->update($setting);
  710. } else {
  711. DocterSetting::insert($setting);
  712. }
  713. return apiReturn(200,'ok');
  714. }
  715. public function getDocterSetting()
  716. {
  717. $docter_id = request('docter_id',1);
  718. $setting = DocterSetting::where(['docter_id'=>$docter_id])->first()->toArray();
  719. $config = object_array(json_decode($setting['service_time']));
  720. $status = $setting['status'] ? true: false;
  721. $configData = [
  722. 'amEndTime'=>$config[1]['amEndTime'],
  723. 'amNum'=>$config[1]['amPerson'],
  724. 'amStartTime'=>$config[1]['amStartTime'],
  725. 'nightEndTime'=>$config[3]['nightEndTime'],
  726. 'nightNum'=>$config[3]['nightPerson'],
  727. 'nightStartTime'=>$config[3]['nightStartTime'],
  728. 'pmEndTime'=>$config[2]['pmEndTime'],
  729. 'pmNum'=>$config[2]['pmPerson'],
  730. 'pmStartTime'=>$config[2]['pmStartTime'],
  731. 'serviceNum'=>$setting['service_num'],
  732. 'showDayNumber'=>$setting['show_days'],
  733. 'status'=>$status,
  734. 'timeSpan'=>$setting['service_times'],
  735. ];
  736. // configData: {
  737. //         amEndTime: "11:30", //上午结束时间 string
  738. //        amNum: 20, //上午号源 Num
  739. //        amStartTime: "09:00", //上午开始时间 string
  740. //        nightEndTime: "19:30", //晚上结束时间 string
  741. //        nightNum: 8, //晚上号源 num
  742. //        nightStartTime: "18:30", //晚上开始时间 staring
  743. //        pmEndTime: "16:00", //下午结束时间 string
  744. //        pmNum: 20, //下午号源 num
  745. //        pmStartTime: "13:30", //下午开始时间 string
  746. //        serviceNum: 2, //同时服务人数 Num
  747. //        showDayNumber: 7, //可预约服务天数 Num
  748. //        status: false, // 是否开启 true false
  749. //        timeSpan: "15", // 就诊时长 string
  750. //      },
  751. return apiReturn(200,'ok',$configData);
  752. }
  753. //获取用户信息
  754. public function getScheduleInfo()
  755. {
  756. $docter_id = request('id');
  757. $org_id = request('org_id');
  758. $is_docter = CdmsUsers::where('docter_id',$docter_id)->first();
  759. if(empty($org_id) && empty($docter_id)){
  760. return json_encode(['status'=>601,'msg'=>'缺少必要参数','data'=>'']);
  761. }
  762. $role = 1;
  763. if($is_docter){
  764. $role = 2;
  765. }
  766. $lists =[];
  767. if($role == 1){
  768. $ids = DocterOrganization::where(['organization_id'=>$org_id])->pluck('docter_id')->toArray();
  769. $list = Docter::whereIn('id',$ids)->get(['id','name']);
  770. foreach ($list as $val){
  771. $lists[] = ['value'=>$val->id,'label'=>$val->name];
  772. }
  773. $name = Organization::where('id',$org_id)->value('name');
  774. $user_id = $org_id;
  775. } else {
  776. $ids = DocterOrganization::where(['docter_id'=>$docter_id])->pluck('organization_id');
  777. $list = Organization::whereIn('id',$ids)->get(['id','name']);
  778. foreach ($list as $val){
  779. $lists[] = ['value'=>$val->id,'label'=>$val->name];
  780. }
  781. $name = Docter::where('id',$docter_id)->value('name');
  782. $user_id = $docter_id;
  783. }
  784. $data['user_id'] = $user_id;
  785. $data['name'] = $name;
  786. $data['role'] = $role;
  787. $data['list'] = $lists;
  788. return (json_encode(['code'=>200,'msg'=>'ok','data'=>$data]));
  789. }
  790. //获取图文订单
  791. public function get_chat_order()
  792. {
  793. $org_id = 1;
  794. // $docter_id = 1;
  795. $docter_id = request('docter_id');
  796. if(empty($docter_id)){
  797. return json_encode(['status'=>601,'msg'=>'缺少必要参数','data'=>'']);
  798. }
  799. $list = Order::where(['organization_id'=>$org_id,'docter_id'=>$docter_id,'order_status'=>3])->with('orderUser')->distinct('user_id')->get('user_id');
  800. $user_list = [];
  801. foreach ($list as $val){
  802. $user_list[] = [
  803. 'id'=>$val->user_id,
  804. 'avatar'=>$val->orderUser->avatar,
  805. 'name'=>$val->orderUser->nickname,
  806. ];
  807. }
  808. $data['docter'] = Docter::where('id',$docter_id)->first(['id','avatar','name']);
  809. $data['order_list'] = $user_list;
  810. return (json_encode(['code'=>200,'msg'=>'ok','data'=>$data]));
  811. }
  812. public function get_month_schedule()
  813. {
  814. $date = request('date');
  815. $start_time = strtotime($date);
  816. //$start_time = strtotime(date('Y-m',time()));
  817. $month_day = date("t",$start_time);
  818. $types = [1=>1,2=>2,3=>3];
  819. $status = [1=>true,2=>'other',3=>false];
  820. for($i=0;$i<$month_day;$i++){
  821. $now_time = $start_time + $i*86400;
  822. $now_date = date('Y-m-d',$now_time);
  823. $week = date('N',$now_time);
  824. $arr = [
  825. ['status'=>false,'type'=>1],
  826. ['status'=>true,'type'=>2],
  827. ['status'=>false,'type'=>3],
  828. ];
  829. if($i%2 == 0){
  830. $arr = [
  831. ['status'=>false,'type'=>1],
  832. ['status'=>true,'type'=>2],
  833. ['status'=>true,'type'=>3],
  834. ];
  835. }
  836. if($i%3 == 0){
  837. $arr = [
  838. ['status'=>true,'type'=>1],
  839. ['status'=>'other','type'=>2],
  840. ['status'=>false,'type'=>3],
  841. ];
  842. }
  843. if($i%3 == 0){
  844. $arr = [
  845. ['status'=>'other','type'=>1],
  846. ['status'=>true,'type'=>2],
  847. ['status'=>false,'type'=>3],
  848. ];
  849. }
  850. $all_time[] = ['date'=>($i+1),'day'=>$now_date,'schedulelingList'=>$arr,'week'=>$week];
  851. }
  852. return apiReturn(200,'ok',$all_time);
  853. }
  854. //获取周排班模板
  855. public function getWeekSchedule()
  856. {
  857. $docter_id = request('docter_id',1);
  858. $org_id = request('org_id',1);
  859. $type = request('type',2);
  860. $role = request('role',2);
  861. if(empty($docter_id) || empty($type)){
  862. return apiReturn(604,'缺少必要参数');
  863. }
  864. $docter_id = 1;
  865. $docter_setting = DocterSetting::where(['docter_id'=>$docter_id,'type'=>$type])->first();
  866. //周字段映射
  867. $week_arr = ['monday','tuesday','wednesday','thursday','friday','saturday','sunday'];
  868. $data = [
  869. [
  870. 'date'=>"上午",
  871. 'monday'=>false,
  872. 'tuesday'=>false,
  873. 'wednesday'=>false,
  874. 'thursday'=>false,
  875. 'friday'=>false,
  876. 'saturday'=>false,
  877. 'sunday'=>false,
  878. ],
  879. [
  880. 'date'=>"下午",
  881. 'monday'=>false,
  882. 'tuesday'=>false,
  883. 'wednesday'=>false,
  884. 'thursday'=>false,
  885. 'friday'=>false,
  886. 'saturday'=>false,
  887. 'sunday'=>false,
  888. ],
  889. [
  890. 'date'=>"晚上",
  891. 'monday'=>false,
  892. 'tuesday'=>false,
  893. 'wednesday'=>false,
  894. 'thursday'=>false,
  895. 'friday'=>false,
  896. 'saturday'=>false,
  897. 'sunday'=>false,
  898. ]
  899. ];
  900. if($role == 2){
  901. for ($i=0;$i<6;$i++){
  902. for($j=0;$j<=2;$j++){
  903. $is_self_have = WeekSchedule::where(['docter_id'=>$docter_id,'schedule_type'=>$type,'organization_id'=>$org_id,'week'=>$i+1])->count();
  904. $is_other_have = WeekSchedule::where(['docter_id'=>$docter_id,'schedule_type'=>$type,'week'=>$i+1])->where('organization_id','!=',$org_id)->count();
  905. if($is_self_have){
  906. $data[$j][$week_arr[$i]] = true;
  907. }
  908. if($is_other_have){
  909. $data[$j][$week_arr[$i]] = 'other';
  910. }
  911. }
  912. }
  913. } else {
  914. for ($i=0;$i<6;$i++){
  915. for($j=0;$j<=2;$j++){
  916. $is_self_have = WeekSchedule::where(['docter_id'=>$docter_id,'schedule_type'=>$type,'organization_id'=>$org_id,'week'=>$i+1])->count();
  917. $is_other_have = WeekSchedule::where(['docter_id'=>$docter_id,'schedule_type'=>$type,'week'=>$i+1])->where('docter_id','!=',$docter_id)->count();
  918. if($is_self_have){
  919. $data[$j][$week_arr[$i]] = true;
  920. }
  921. if($is_other_have){
  922. $data[$j][$week_arr[$i]] = 'other';
  923. }
  924. }
  925. }
  926. }
  927. $name = Docter::where('id',$docter_id)->value('name');
  928. $shcedule_data['docter'] = $data;
  929. $shcedule_data['name'] = $name;
  930. $shcedule_data['role'] = $role;
  931. $shcedule_data['user_id'] = $docter_id;
  932. return apiReturn(200,'ok',$shcedule_data);
  933. }
  934. //设置周排班模板
  935. public function setWeekSchedule()
  936. {
  937. $docter_id = request('docter_id',1);
  938. $org_id = request('org_id',1);
  939. $type = request('type',2);
  940. if(empty($docter_id) || empty($org_id) || empty($type)){
  941. return apiReturn(601,'缺少必要参数');
  942. }
  943. $docter_setting = DocterSetting::where(['docter_id'=>$docter_id])->first();
  944. $data = [
  945. [
  946. 'date'=>"上午",
  947. 'monday'=>false,
  948. 'tuesday'=>true,
  949. 'wednesday'=>false,
  950. 'thursday'=>false,
  951. 'friday'=>true,
  952. 'saturday'=>false,
  953. 'sunday'=>false,
  954. ],
  955. [
  956. 'date'=>"下午",
  957. 'monday'=>false,
  958. 'tuesday'=>false,
  959. 'wednesday'=>false,
  960. 'thursday'=>false,
  961. 'friday'=>false,
  962. 'saturday'=>true,
  963. 'sunday'=>false,
  964. ],
  965. [
  966. 'date'=>"晚上",
  967. 'monday'=>true,
  968. 'tuesday'=>true,
  969. 'wednesday'=>false,
  970. 'thursday'=>false,
  971. 'friday'=>false,
  972. 'saturday'=>false,
  973. 'sunday'=>false,
  974. ]
  975. ];
  976. if(!empty($docter_setting)){
  977. $time_period = object_array(json_decode($docter_setting->service_time));
  978. $times[] = TimePeriod::where(['docter_id'=>$docter_id])->whereBetween('end_time_period',[intval($time_period[0]['startTime']),intval($time_period[0]['endTime'])])->pluck('id')->toArray();
  979. $times[] = TimePeriod::where(['docter_id'=>$docter_id])->whereBetween('end_time_period',[intval($time_period[1]['startTime']),intval($time_period[1]['endTime'])])->pluck('id')->toArray();
  980. $times[] = TimePeriod::where(['docter_id'=>$docter_id])->whereBetween('end_time_period',[intval($time_period[2]['startTime']),intval($time_period[2]['endTime'])])->pluck('id')->toArray();
  981. }
  982. $week_data = [
  983. 'schedule_type'=>$type,
  984. 'organization_id'=>$org_id,
  985. 'docter_id'=>$docter_id
  986. ];
  987. $k = 0;
  988. DB::beginTransaction();
  989. // $data[0] = array_values(object_array(json_decode($data[0])));
  990. // $data[1] = array_values(object_array(json_decode($data[1])));
  991. // $data[2] = array_values(object_array(json_decode($data[2])));
  992. $data[0] = array_values($data[0]);
  993. $data[1] = array_values($data[1]);
  994. $data[2] = array_values($data[2]);
  995. try {
  996. for($i=0;$i<=2;$i++){
  997. $schedule_data = $data[$i];
  998. foreach ($schedule_data as $key=>$schedule){
  999. if($key == 0) continue;
  1000. $week_data ['type'] = $i+1;
  1001. $week_data ['week'] = $key;
  1002. if($schedule == true){
  1003. // echo '时间列类型'.($i+1).'----'.$key.'星期数'.'<br>';
  1004. foreach ($times[$i] as $id){
  1005. $week_data['time_period_id'] = $id;
  1006. $is_have = WeekSchedule::where(['docter_id'=>$docter_id,'organization_id'=>$org_id,'schedule_type'=>$type,'type'=>$i+1,'week'=>$key])->where('time_period_id',$id)->value('id');
  1007. $is = $is_have?'有':'沒有';
  1008. // echo '时间段id'.$id.'----'.$is.'</br>';
  1009. if($is_have) continue;
  1010. $k++;
  1011. WeekSchedule::insert($week_data);
  1012. }
  1013. } else {
  1014. $is_have = WeekSchedule::where(['docter_id' => $docter_id, 'organization_id' => $org_id, 'schedule_type' => $type,'type'=>$i+1,'week'=>$key])->whereIn('time_period_id', $times[$i])->value('id');
  1015. if($is_have) WeekSchedule::where(['docter_id' => $docter_id, 'organization_id' => $org_id, 'schedule_type' => $type,'type'=>$i+1,'week'=>$key])->whereIn('time_period_id', $times[$i])->delete();
  1016. }
  1017. }
  1018. }
  1019. DB::commit();
  1020. } catch (\Exception $e){
  1021. DB::rollBack();
  1022. return apiReturn(602,$e->getLine().'行'.$e->getMessage());
  1023. }
  1024. return apiReturn(200,'ok',$k);
  1025. }
  1026. }