55 lines
2.2 KiB
SQL
55 lines
2.2 KiB
SQL
-- 按完成时间修正部门月度积分(earned_score / completed_task_count)
|
||
-- 注意:本脚本不会修改 deducted_score / 延误统计,仅重算 earned_score 与 net_score
|
||
|
||
-- 1) 设定需要修正的年月
|
||
SET @year = 2026;
|
||
SET @month = 2;
|
||
SET @start = STR_TO_DATE(CONCAT(@year, '-', LPAD(@month, 2, '0'), '-01 00:00:00'), '%Y-%m-%d %H:%i:%s');
|
||
SET @end = DATE_ADD(@start, INTERVAL 1 MONTH);
|
||
|
||
-- 2) 汇总该月完成的主任务/子任务积分(主任务排除流水线任务)
|
||
CREATE TEMPORARY TABLE tmp_monthly_earned AS
|
||
SELECT dept_id, SUM(score) AS earned_score, COUNT(*) AS completed_task_count
|
||
FROM (
|
||
SELECT t.receiver_dept_id AS dept_id, t.score_reward AS score
|
||
FROM task t
|
||
WHERE t.status = 'completed'
|
||
AND t.receiver_dept_id IS NOT NULL
|
||
AND t.score_reward IS NOT NULL
|
||
AND t.score_reward > 0
|
||
AND (t.is_pipeline IS NULL OR t.is_pipeline = 0)
|
||
AND t.complete_time >= @start AND t.complete_time < @end
|
||
UNION ALL
|
||
SELECT ti.receiver_dept_id AS dept_id, ti.total_score AS score
|
||
FROM task_item ti
|
||
WHERE ti.status = 'completed'
|
||
AND ti.receiver_dept_id IS NOT NULL
|
||
AND ti.total_score IS NOT NULL
|
||
AND ti.total_score > 0
|
||
AND ti.complete_time >= @start AND ti.complete_time < @end
|
||
) u
|
||
GROUP BY dept_id;
|
||
|
||
-- 3) 更新已有记录
|
||
UPDATE dept_monthly_score d
|
||
JOIN tmp_monthly_earned e
|
||
ON d.dept_id = e.dept_id AND d.year = @year AND d.month = @month
|
||
SET d.earned_score = e.earned_score,
|
||
d.completed_task_count = e.completed_task_count,
|
||
d.net_score = e.earned_score - d.deducted_score,
|
||
d.update_time = NOW();
|
||
|
||
-- 4) 插入缺失记录
|
||
INSERT INTO dept_monthly_score
|
||
(dept_id, dept_name, year, month, earned_score, deducted_score, net_score,
|
||
completed_task_count, delayed_task_count, total_delay_hours, create_time, update_time)
|
||
SELECT e.dept_id, dept.dept_name, @year, @month, e.earned_score, 0,
|
||
e.earned_score, e.completed_task_count, 0, 0, NOW(), NOW()
|
||
FROM tmp_monthly_earned e
|
||
JOIN sys_dept dept ON dept.id = e.dept_id
|
||
LEFT JOIN dept_monthly_score d
|
||
ON d.dept_id = e.dept_id AND d.year = @year AND d.month = @month
|
||
WHERE d.id IS NULL;
|
||
|
||
DROP TEMPORARY TABLE tmp_monthly_earned;
|