$params['doctor_id'], 'date' => $params['date'], 'period' => $params['period'], 'status' => $params['status'], 'quota' => $params['quota'] ?? 0, 'max_patients' => $params['max_patients'] ?? 0, 'remark' => $params['remark'] ?? '', ]; if (isset($params['id']) && $params['id']) { // 更新 $data['update_time'] = time(); Roster::where('id', $params['id'])->update($data); return ['id' => $params['id']]; } else { // 新增 $data['create_time'] = time(); $data['update_time'] = time(); $roster = Roster::create($data); return ['id' => $roster->id]; } } catch (\Exception $e) { self::setError($e->getMessage()); return false; } } /** * @notes 删除排班 * @param array $params * @return bool */ public static function delete(array $params) { try { Roster::destroy($params['id']); return true; } catch (\Exception $e) { self::setError($e->getMessage()); return false; } } /** * @notes 排班详情 * @param array $params * @return array */ public static function detail(array $params) { return Roster::findOrEmpty($params['id'])->toArray(); } /** * @notes 批量保存排班 * @param array $params * @return array|bool */ public static function batchSave(array $params) { try { Db::startTrans(); $successCount = 0; $updateCount = 0; $createCount = 0; foreach ($params['rosters'] as $roster) { $data = [ 'doctor_id' => $roster['doctor_id'], 'date' => $roster['date'], 'period' => $roster['period'], 'status' => $roster['status'], 'quota' => $roster['quota'] ?? 0, 'max_patients' => $roster['max_patients'] ?? 0, 'remark' => $roster['remark'] ?? '', ]; // 检查是否已存在 $exists = Roster::where([ ['doctor_id', '=', $data['doctor_id']], ['date', '=', $data['date']], ['period', '=', $data['period']], ])->find(); if ($exists) { // 更新 $data['update_time'] = time(); $exists->save($data); $updateCount++; } else { // 新增 $data['create_time'] = time(); $data['update_time'] = time(); Roster::create($data); $createCount++; } $successCount++; } Db::commit(); return [ 'success_count' => $successCount, 'create_count' => $createCount, 'update_count' => $updateCount, ]; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); return false; } } /** * @notes 复制排班 * @param array $params * @return bool */ public static function copy(array $params) { try { Db::startTrans(); // 获取源排班数据 $where = [ ['date', 'between', [$params['source_start_date'], $params['source_end_date']]] ]; if (isset($params['doctor_id']) && $params['doctor_id']) { $where[] = ['doctor_id', '=', $params['doctor_id']]; } $sourceRosters = Roster::where($where)->select(); // 计算日期差 $sourceDays = (strtotime($params['source_end_date']) - strtotime($params['source_start_date'])) / 86400; $targetDays = (strtotime($params['target_start_date']) - strtotime($params['source_start_date'])) / 86400; foreach ($sourceRosters as $roster) { $newDate = date('Y-m-d', strtotime($roster->date) + ($targetDays * 86400)); $data = [ 'doctor_id' => $roster->doctor_id, 'date' => $newDate, 'period' => $roster->period, 'status' => $roster->status, 'quota' => $roster->quota, 'max_patients' => $roster->max_patients, 'remark' => $roster->remark, 'create_time' => time(), 'update_time' => time(), ]; // 检查目标日期是否已存在排班 $exists = Roster::where([ ['doctor_id', '=', $data['doctor_id']], ['date', '=', $data['date']], ['period', '=', $data['period']], ])->find(); if (!$exists) { Roster::create($data); } } Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); return false; } } }