insertGetId($data); Log::write('成功添加绩效记录,ID:' . $id . ',员工ID:' . $data['staff_id'] . ',绩效类型:' . $data['performance_type']); return $id; } catch (\Exception $e) { Log::write('添加绩效记录失败:' . $e->getMessage()); throw $e; } } /** * 更新绩效记录 * @param int $id 记录ID * @param array $data 更新数据 * @return bool 是否成功 */ public function updatePerformance(int $id, array $data) { try { if (empty($id)) { throw new \Exception('缺少绩效记录ID'); } // 更新时间 if (!isset($data['updated_at'])) { $data['updated_at'] = time(); } // 更新数据 $result = Db::name('school_performance_summary')->where('id', $id)->update($data); Log::write('成功更新绩效记录,ID:' . $id); return $result !== false; } catch (\Exception $e) { Log::write('更新绩效记录失败:' . $e->getMessage()); throw $e; } } /** * 批量添加绩效记录 * @param array $dataList 绩效数据列表 * @return int 插入的记录数 */ public function addBatchPerformance(array $dataList) { try { if (empty($dataList)) { return 0; } // 设置默认值 foreach ($dataList as &$data) { if (!isset($data['order_status'])) { $data['order_status'] = self::ORDER_STATUS_PENDING; } if (!isset($data['created_at'])) { $data['created_at'] = time(); } if (!isset($data['updated_at'])) { $data['updated_at'] = time(); } } // 批量插入 $count = Db::name('school_performance_summary')->insertAll($dataList); Log::write('成功批量添加绩效记录,数量:' . $count); return $count; } catch (\Exception $e) { Log::write('批量添加绩效记录失败:' . $e->getMessage()); throw $e; } } /** * 获取绩效记录 * @param int $id 记录ID * @return array|null 绩效记录 */ public function getPerformance(int $id) { try { if (empty($id)) { throw new \Exception('缺少绩效记录ID'); } $record = Db::name('school_performance_summary')->where('id', $id)->find(); return $record; } catch (\Exception $e) { Log::write('获取绩效记录失败:' . $e->getMessage()); throw $e; } } /** * 获取员工绩效列表 * @param int $staffId 员工ID * @param string $performanceType 绩效类型 * @param string $orderStatus 订单状态 * @param string $startDate 开始日期 * @param string $endDate 结束日期 * @return array 绩效列表 */ public function getStaffPerformanceList(int $staffId, string $performanceType = '', string $orderStatus = '', string $startDate = '', string $endDate = '') { try { $query = Db::name('school_performance_summary')->where('staff_id', $staffId); if (!empty($performanceType)) { $query->where('performance_type', $performanceType); } if (!empty($orderStatus)) { $query->where('order_status', $orderStatus); } if (!empty($startDate)) { $startTime = strtotime($startDate . ' 00:00:00'); $query->where('created_at', '>=', $startTime); } if (!empty($endDate)) { $endTime = strtotime($endDate . ' 23:59:59'); $query->where('created_at', '<=', $endTime); } $list = $query->order('created_at', 'desc')->select()->toArray(); return $list; } catch (\Exception $e) { Log::write('获取员工绩效列表失败:' . $e->getMessage()); throw $e; } } /** * 计算员工绩效总额 * @param int $staffId 员工ID * @param string $performanceType 绩效类型 * @param string $orderStatus 订单状态 * @param string $startDate 开始日期 * @param string $endDate 结束日期 * @return float 绩效总额 */ public function calculateStaffPerformanceTotal(int $staffId, string $performanceType = '', string $orderStatus = '', string $startDate = '', string $endDate = '') { try { $query = Db::name('school_performance_summary')->where('staff_id', $staffId); if (!empty($performanceType)) { $query->where('performance_type', $performanceType); } if (!empty($orderStatus)) { $query->where('order_status', $orderStatus); } if (!empty($startDate)) { $startTime = strtotime($startDate . ' 00:00:00'); $query->where('created_at', '>=', $startTime); } if (!empty($endDate)) { $endTime = strtotime($endDate . ' 23:59:59'); $query->where('created_at', '<=', $endTime); } $total = $query->sum('performance_value'); return floatval($total); } catch (\Exception $e) { Log::write('计算员工绩效总额失败:' . $e->getMessage()); throw $e; } } }