2025-04-03 15:25:40 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\api\controller\backend;
|
|
|
|
|
|
|
|
use app\common\controller\Api;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
|
|
use think\Db;
|
|
|
|
|
2025-04-03 15:50:14 +08:00
|
|
|
|
2025-04-03 15:25:40 +08:00
|
|
|
/**
|
|
|
|
* 评价
|
|
|
|
*/
|
|
|
|
class AutomaticPublicity extends Api
|
|
|
|
{
|
|
|
|
protected $noNeedLogin = ['*'];
|
|
|
|
protected $noNeedRight = ['*'];
|
2025-04-03 15:50:14 +08:00
|
|
|
|
|
|
|
public function _initialize()
|
|
|
|
{
|
|
|
|
parent::_initialize();
|
|
|
|
$id = $this->request->header('Token');
|
|
|
|
if(!$id){
|
|
|
|
return $this->error('缺少参数');
|
|
|
|
}
|
|
|
|
$user = Db::name('user')->where('token', $id)->find();
|
|
|
|
if(!$user){
|
|
|
|
return $this->error('用户不存在','',99998);
|
|
|
|
}
|
|
|
|
}
|
2025-04-03 15:25:40 +08:00
|
|
|
|
|
|
|
public function index (){
|
|
|
|
$lr_evaluation_schedule = Db::name('evaluation_schedule')
|
|
|
|
->field('id,title,start_time')
|
|
|
|
->where('evaluation_type', 1)
|
|
|
|
->order('id', 'desc')
|
|
|
|
->select();
|
|
|
|
|
|
|
|
foreach ($lr_evaluation_schedule as $key => $val) {
|
|
|
|
// 使用 DISTINCT 去重 user_id 后计算数量
|
|
|
|
$lr_evaluation_schedule[$key]['yikaopingrenshu'] = Db::name('scoringrecord')
|
|
|
|
->where('evaluation_schedule_id', $val['id'])
|
|
|
|
->distinct(true) // 添加 distinct(true) 去重
|
|
|
|
->column('user_id'); // 获取去重后的 user_id 列
|
|
|
|
$lr_evaluation_schedule[$key]['yikaopingrenshu'] = count($lr_evaluation_schedule[$key]['yikaopingrenshu']); // 计算数量
|
|
|
|
$lr_evaluation_schedule[$key]['kaopingrenshu'] = Db::name('user')
|
|
|
|
->distinct(true) // 添加 distinct(true) 去重
|
|
|
|
->column('id'); // 获取去重后的 user_id 列
|
|
|
|
$lr_evaluation_schedule[$key]['kaopingrenshu'] = count($lr_evaluation_schedule[$key]['kaopingrenshu']); // 计算数量
|
|
|
|
// $lr_evaluation_schedule[$key]['niandu'] = Db::name('scoringrecord')
|
|
|
|
// ->where('evaluation_schedule_id', $val['id'])
|
|
|
|
// ->value('term');
|
|
|
|
|
|
|
|
$lr_evaluation_schedule[$key]['niandu'] = date('Y', strtotime($val['start_time']));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success('添加成功', $lr_evaluation_schedule);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dailyexport()
|
|
|
|
{
|
|
|
|
|
|
|
|
$id = $this->request->get('id');
|
|
|
|
if(empty($id)) {
|
|
|
|
return $this->error('缺少用户ID参数');
|
|
|
|
}
|
|
|
|
// 获取用户数据
|
|
|
|
$users = Db::name('user')->select();
|
|
|
|
|
|
|
|
// 获取评价计划
|
|
|
|
$lr_evaluation_schedule = Db::name('evaluation_schedule')
|
|
|
|
->field('id, title, start_time')
|
|
|
|
->where('id', $id)
|
|
|
|
->order('id', 'desc')
|
|
|
|
->select();
|
|
|
|
|
|
|
|
// 提前加载用户组名称
|
|
|
|
$groupIds = array_column($users, 'group_id');
|
|
|
|
$groups = Db::name('user_group')
|
|
|
|
->whereIn('id', $groupIds)
|
|
|
|
->column('name', 'id');
|
|
|
|
|
|
|
|
// 遍历评价计划,减少重复查询
|
|
|
|
foreach ($lr_evaluation_schedule as $k => $v) {
|
|
|
|
$year = date('Y', strtotime($v['start_time']));
|
|
|
|
|
|
|
|
// 批量查询用户的评分记录和加减分记录
|
|
|
|
$scoringRecords = Db::name('scoringrecord')
|
|
|
|
->whereIn('user_id', array_column($users, 'id'))
|
|
|
|
->where('term', $year)
|
|
|
|
->where('evaluation_schedule_id', $v['id'])
|
|
|
|
->select();
|
|
|
|
|
|
|
|
$additionRecords = Db::name('addition_and_subtraction_records')
|
|
|
|
->whereIn('user_id', array_column($users, 'id'))
|
|
|
|
->where('YEAR(createtime)', $year)
|
|
|
|
->where('status', 2)
|
|
|
|
->select();
|
|
|
|
|
|
|
|
// 构建以用户ID为键的快捷查询数组
|
|
|
|
$userScoring = [];
|
|
|
|
$userAdditionSubtraction = [];
|
|
|
|
foreach ($scoringRecords as $record) {
|
|
|
|
$userId = $record['user_id'];
|
|
|
|
if (!isset($userScoring[$userId])) {
|
|
|
|
$userScoring[$userId] = [
|
|
|
|
'user_scoringrecord' => 0,
|
|
|
|
'department_score_scoringrecord' => 0,
|
|
|
|
'hospital_score_scoringrecord' => 0,
|
|
|
|
'party_score_scoringrecord' => 0,
|
|
|
|
'overall_score_scoringrecord' => 0,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
$userScoring[$userId]['user_scoringrecord'] += $record['self_score'];
|
|
|
|
$userScoring[$userId]['department_score_scoringrecord'] += $record['department_score'];
|
|
|
|
$userScoring[$userId]['hospital_score_scoringrecord'] += $record['hospital_score'];
|
|
|
|
$userScoring[$userId]['party_score_scoringrecord'] += $record['party_branch_score'];
|
|
|
|
$userScoring[$userId]['overall_score_scoringrecord'] += $record['overall_party_score'];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($additionRecords as $record) {
|
|
|
|
$userId = $record['user_id'];
|
|
|
|
if (!isset($userAdditionSubtraction[$userId])) {
|
|
|
|
$userAdditionSubtraction[$userId] = [
|
|
|
|
'zongjiafenfenzhi' => 0,
|
|
|
|
'zongjianfenfenzhi' => 0,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
if ($record['assessment_type'] == 1) {
|
|
|
|
$userAdditionSubtraction[$userId]['zongjiafenfenzhi'] += $record['score_value'];
|
|
|
|
} elseif ($record['assessment_type'] == 2) {
|
|
|
|
$userAdditionSubtraction[$userId]['zongjianfenfenzhi'] += $record['score_value'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 处理用户数据
|
|
|
|
foreach ($users as $key => $value) {
|
|
|
|
$groupId = $value['group_id'];
|
|
|
|
$userId = $value['id'];
|
|
|
|
|
|
|
|
// 设置组名称
|
|
|
|
$users[$key]['group_name'] = $groups[$groupId] ?? '';
|
|
|
|
|
|
|
|
// 设置评分和加减分值
|
|
|
|
$users[$key]['user_scoringrecord'] = $userScoring[$userId]['user_scoringrecord'] ?? 0;
|
|
|
|
$users[$key]['department_score_scoringrecord'] = $userScoring[$userId]['department_score_scoringrecord'] ?? 0;
|
|
|
|
$users[$key]['hospital_score_scoringrecord'] = $userScoring[$userId]['hospital_score_scoringrecord'] ?? 0;
|
|
|
|
$users[$key]['party_score_scoringrecord'] = $userScoring[$userId]['party_score_scoringrecord'] ?? 0;
|
|
|
|
$users[$key]['overall_score_scoringrecord'] = $userScoring[$userId]['overall_score_scoringrecord'] ?? 0;
|
|
|
|
$users[$key]['zongjiafenfenzhi'] = $userAdditionSubtraction[$userId]['zongjiafenfenzhi'] ?? 0;
|
|
|
|
$users[$key]['zongjianfenfenzhi'] = $userAdditionSubtraction[$userId]['zongjianfenfenzhi'] ?? 0;
|
|
|
|
|
|
|
|
// 设置年份
|
|
|
|
$users[$key]['time'] = $year;
|
|
|
|
|
|
|
|
// 计算总分
|
|
|
|
$users[$key]['zongjiafen'] = 0; // 如果需要计算其他值,可以在这里添加逻辑
|
|
|
|
$users[$key]['total_score'] =
|
|
|
|
$users[$key]['user_scoringrecord'] * 0.2 +
|
|
|
|
$users[$key]['department_score_scoringrecord'] * 0.4 +
|
|
|
|
$users[$key]['party_score_scoringrecord'] * 0.4 +
|
|
|
|
$users[$key]['zongjiafenfenzhi'] -
|
|
|
|
$users[$key]['zongjianfenfenzhi'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return $this->success('查询成功', $users);
|
|
|
|
// $this->success('返回成功', $date);
|
|
|
|
// 创建一个新的 Excel 文件
|
|
|
|
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
|
|
|
|
|
|
// 设置表头
|
|
|
|
$headers = ['年度', '考评对象', '工号', '所属科室', '自评总分', '科室评分', '党支部评分', '总党支评分', '医院评分', '总加分', '总减分', '总得分']; // 根据你的 member 表字段进行调整
|
|
|
|
$columnIndex = 1; // A = 1, B = 2, ...
|
|
|
|
foreach ($headers as $header) {
|
|
|
|
$sheet->setCellValueByColumnAndRow($columnIndex, 1, $header);
|
|
|
|
$columnIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 填充数据
|
|
|
|
$rowNumber = 2; // 从第二行开始填充数据
|
|
|
|
foreach ($users as $member) {
|
|
|
|
$columnIndex = 1;
|
|
|
|
$sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['time']);
|
|
|
|
$sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['nickname']);
|
|
|
|
$sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['code']);
|
|
|
|
$sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['group_name']);
|
|
|
|
$sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['user_scoringrecord']);
|
|
|
|
$sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['department_score_scoringrecord']);
|
|
|
|
$sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['party_score_scoringrecord']);
|
|
|
|
$sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['overall_score_scoringrecord']);
|
|
|
|
$sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['hospital_score_scoringrecord']);
|
|
|
|
$sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['zongjiafenfenzhi']);
|
|
|
|
$sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['zongjianfenfenzhi']);
|
|
|
|
$sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['zongjiafen']);
|
|
|
|
$rowNumber++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 保存到 PHP 输出流
|
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
|
|
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
|
|
header('Content-Disposition: attachment; filename="' . '加减分数据' . date('YmdHis') . '.xlsx"');
|
|
|
|
// header('Content-Disposition: attachment;filename="活动报名列表' . date('YmdHis') . '.xlsx"');
|
|
|
|
header('Cache-Control: max-age=0');
|
|
|
|
|
|
|
|
// 输出到浏览器供用户下载
|
|
|
|
$writer->save('php://output');
|
|
|
|
|
|
|
|
// 清理并退出
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|