本来项目中下用quartz定时任务来做数据报表的定时计算插入,后面考虑性能改为mysql中写存储过程加定时任务。
定时每周日计算所有员工相关工作量,插入统计数据表中
BEGIN
declare sumwl FLOAT;/*完成*/
declare removewl FLOAT;/*扣除*/
declare addwl FLOAT;/*加成*/
declare shouldwl FLOAT;/*应完成*/
declare researchwl FLOAT;/*研发*/
declare testwl FLOAT;/*测试*/
declare reviewwl FLOAT;/*review*/
declare tags INT;/*标识*/
declare startTime DATE DEFAULT DATE(DATE_SUB(NOW(),interval 1 week));
declare endTime DATE DEFAULT date_sub(curdate(),interval 1 day) ;
declare uid Long;
-- 创建结束标志变量
declare tmpName varchar(20)default '' ;
declare cur1 CURSOR FOR SELECT user_id from pro_set_workload WHERE valid = 1 GROUP BY user_id;
-- 指定游标循环结束时的返回值
declare continue HANDLER for not found set tmpname = null;
OPEN cur1;
FETCH cur1 INTO uid;
WHILE ( tmpname is not null) DO
--查询是否存在
select COUNT(*) INTO tags from pro_workload_week where start_time = startTime and end_time = endTime and user_id = uid limit 1;
IF (tags = 0) THEN
-- 研发工作量
select IFNULL(sum(w.workload),0) INTO researchwl from pro_claimwork c INNER JOIN pro_work w ON c.work_id=w.work_id WHERE actual_end_time BETWEEN startTime AND endTime and c.valida = 1 and w.status = 4 and w.valid = 1 and c.user_designer_id= uid;
-- 测试工作量
select IFNULL(sum(w.workload),0) INTO testwl from pro_claimwork c INNER JOIN pro_work w ON
c .work_id =w.work_id WHERE actual_end_time BETWEEN startTime AND endTime and c.valida = 1 and w.status = 4 and w.valid = 1 and c.user_test_id= uid;
-- 测试工作量
select IFNULL(sum(w.workload),0) INTO reviewwl from pro_claimwork c INNER JOIN pro_work w ON c.work_id=w.work_id WHERE actual_end_time BETWEEN startTime AND endTime
and c.valida= 1 and w.status = 4 and w.valid = 1 and c.user_review_id= uid;
-- 应完成工作量
SELECT IFNULL(workload*(select COUNT(*) from pro_data_set WHERE data_time BETWEEN startTime and endTime and type=1), 0) INTO shouldwl from pro_set_workload WHERE valid = 1 and user_id = uid;
-- 完成工作量
SELECT IFNULL(sum(p.workload), 0) INTO sumwl from pro_claimwork c
INNER JOIN pro_work p on p.work_id = c.work_id
where c.valida = 1 and p.status = 4 and p.valid = 1
and p.actual_end_time BETWEEN startTime and endTime AND c.user_designer_id = uid;
-- 扣除工作量
SELECT IFNULL(sum(workload), 0) INTO removewl from pro_workload where workload<0 AND valid = 1 and workload_end_time BETWEEN startTime and endTime and user_id=uid;
-- 加成工作量
SELECT IFNULL(sum(workload), 0) INTO addwl from pro_workload where workload>0 AND valid = 1 and workload_end_time BETWEEN startTime and endTime and user_id=uid;
-- 插入
INSERT INTO pro_workload_week (id, user_id, start_time, end_time, sumwl, removewl, shouldwl, addwl,research,test,review) VALUES (CONCAT(DATE_FORMAT(NOW(), '%Y%m%d%H%m%s%S'),FLOOR(round(rand(),4)*10000)), uid, startTime, endTime , sumwl, removewl, shouldwl, addwl,researchwl,testwl,reviewwl);
END IF;
SELECT CONCAT('20180524165609549',FLOOR(RAND() * 1000)),tags ,startTime,endTime,sumwl,shouldwl, addwl,removewl,uid;
-- SELECT CONCAT('20180524165609549',FLOOR(RAND() * 1000)) ,startTime,endTime,sumwl,shouldwl, addwl,removewl,uid;
FETCH cur1 INTO uid;
END WHILE;
-- 关闭游标
close cur1;
END
定时任务使用mysql工具navicat配置事件,不会的可以百度