智慧教务系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

257 lines
8.2 KiB

<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的多应用管理平台
// +----------------------------------------------------------------------
// | 官方网址:https://www.niucloud.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace app\service\admin\performance;
use core\base\BaseService;
use think\facade\Db;
use think\facade\Log;
/**
* 绩效服务类
* Class PerformanceService
* @package app\service\admin\performance
*/
class PerformanceService extends BaseService
{
/**
* 绩效类型:销售绩效
*/
const PERFORMANCE_TYPE_SALES = 'sales';
/**
* 绩效类型:市场绩效
*/
const PERFORMANCE_TYPE_MARKETING = 'marketing';
/**
* 订单状态:待处理
*/
const ORDER_STATUS_PENDING = 'pending';
/**
* 订单状态:已完成
*/
const ORDER_STATUS_COMPLETED = 'completed';
/**
* 添加绩效记录
* @param array $data 绩效数据
* @return int|string 插入的ID
*/
public function addPerformance(array $data)
{
try {
// 必填字段验证
$requiredFields = ['staff_id', 'resource_id', 'performance_type', 'performance_value'];
foreach ($requiredFields as $field) {
if (!isset($data[$field]) || empty($data[$field])) {
throw new \Exception('缺少必填字段:' . $field);
}
}
// 默认值设置
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();
}
// 插入数据
$id = Db::name('school_performance_summary')->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;
}
}
}