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.
117 lines
4.1 KiB
117 lines
4.1 KiB
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | Niucloud-admin 企业快速开发的多应用管理平台
|
|
// +----------------------------------------------------------------------
|
|
// | 官方网址:https://www.niucloud.com
|
|
// +----------------------------------------------------------------------
|
|
// | niucloud团队 版权所有 开源版本可自由商用
|
|
// +----------------------------------------------------------------------
|
|
// | Author: Niucloud Team
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace app\service\admin\salary;
|
|
|
|
use app\model\salary\Salary;
|
|
use core\base\BaseAdminService;
|
|
|
|
/**
|
|
* 工资统计分析服务类
|
|
* Class StatisticsService
|
|
* @package app\service\admin\salary
|
|
*/
|
|
class StatisticsService extends BaseAdminService
|
|
{
|
|
/**
|
|
* 获取工资统计摘要
|
|
* @param array $where
|
|
* @return array
|
|
*/
|
|
public function getSummary(array $where)
|
|
{
|
|
$query = Salary::alias('s')
|
|
->leftJoin('school_campus_person_role cpr', 's.staff_id = cpr.person_id')
|
|
->leftJoin('school_campus c', 'cpr.campus_id = c.id');
|
|
|
|
// 筛选条件
|
|
if (!empty($where['campus_id'])) {
|
|
$query->where('cpr.campus_id', $where['campus_id']);
|
|
}
|
|
if (!empty($where['salary_month'])) {
|
|
$query->where('s.salary_month', 'like', $where['salary_month'] . '%');
|
|
}
|
|
|
|
// 总体统计
|
|
$summary = $query->field([
|
|
'COUNT(s.id) as total_employees',
|
|
'SUM(s.net_salary) as total_amount',
|
|
'AVG(s.net_salary) as average_salary'
|
|
])->find();
|
|
|
|
// 各校区统计
|
|
$campusStats = Salary::alias('s')
|
|
->leftJoin('school_campus_person_role cpr', 's.staff_id = cpr.person_id')
|
|
->leftJoin('school_campus c', 'cpr.campus_id = c.id')
|
|
->field([
|
|
'IFNULL(cpr.campus_id, 0) as campus_id',
|
|
'CASE WHEN cpr.campus_id IS NULL OR cpr.campus_id = 0 THEN "总部" ELSE c.campus_name END as campus_name',
|
|
'COUNT(s.id) as employee_count',
|
|
'SUM(s.net_salary) as total_amount'
|
|
]);
|
|
|
|
// 筛选条件
|
|
if (!empty($where['campus_id'])) {
|
|
$campusStats->where('cpr.campus_id', $where['campus_id']);
|
|
}
|
|
if (!empty($where['salary_month'])) {
|
|
$campusStats->where('s.salary_month', 'like', $where['salary_month'] . '%');
|
|
}
|
|
|
|
$campusStats = $campusStats->group('cpr.campus_id')->select();
|
|
|
|
return [
|
|
'total_employees' => $summary['total_employees'] ?? 0,
|
|
'total_amount' => $summary['total_amount'] ?? 0,
|
|
'average_salary' => round($summary['average_salary'] ?? 0, 2),
|
|
'cost_rate' => 65.2, // 这里需要根据实际业务计算
|
|
'campus_stats' => $campusStats
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 获取工资趋势数据
|
|
* @param array $where
|
|
* @return array
|
|
*/
|
|
public function getTrend(array $where)
|
|
{
|
|
$query = Salary::alias('s')
|
|
->leftJoin('school_campus_person_role cpr', 's.staff_id = cpr.person_id');
|
|
|
|
if (!empty($where['campus_id'])) {
|
|
$query->where('cpr.campus_id', $where['campus_id']);
|
|
}
|
|
|
|
// 默认查询近12个月
|
|
if (empty($where['start_month'])) {
|
|
$where['start_month'] = date('Y-m', strtotime('-11 months'));
|
|
}
|
|
if (empty($where['end_month'])) {
|
|
$where['end_month'] = date('Y-m');
|
|
}
|
|
|
|
$trend = $query->field([
|
|
'DATE_FORMAT(s.salary_month, "%Y-%m") as month',
|
|
'SUM(s.net_salary) as total_amount',
|
|
'COUNT(s.id) as employee_count'
|
|
])
|
|
->where('s.salary_month', 'between', [
|
|
$where['start_month'] . '-01',
|
|
$where['end_month'] . '-31'
|
|
])
|
|
->group('DATE_FORMAT(s.salary_month, "%Y-%m")')
|
|
->order('month')
|
|
->select();
|
|
|
|
return $trend;
|
|
}
|
|
}
|