DROP PROCEDURE IF EXISTS `pro_stat_issue`;
delimiter ;;
CREATE PROCEDURE `pro_stat_issue`(IN `ls_type` varchar(32),IN `ls_start` varchar(20),IN `ls_end` varchar(20))
BEGIN
	DECLARE ls_stat_name VARCHAR(20);
	
	IF ls_type = 'issue_daily' OR ls_type = 'issue_monthly' THEN
		SET ls_stat_name = ls_start;
		IF ls_type = 'issue_monthly' THEN
			SET ls_stat_name = SUBSTR(ls_start FROM 1 FOR 7);
		END IF;
	
		# 先清除数据
		DELETE t FROM ts_stat_data t WHERE t.stat_type = ls_type AND t.`name` = ls_stat_name;
		
		# 再统计数据
		INSERT INTO ts_stat_data (stat_type, `name`, `value`)
		SELECT ls_type, ls_stat_name, COUNT(*)
		FROM ta_issue t
		WHERE t.`status` > -1
		AND TO_DAYS(t.create_date) >= TO_DAYS(ls_start)
		AND TO_DAYS(t.create_date) <= TO_DAYS(ls_end);
		
	END IF;	
	
END
;;
delimiter ;