mysql 存储过程

阅读更多
DELIMITER $$

USE `taxms`$$

DROP PROCEDURE IF EXISTS `proc_queryPrePayTaxReport`$$

CREATE DEFINER=`taxms`@`10.%` PROCEDURE `proc_queryPrePayTaxReport`(
	IN p_zywlx VARCHAR(120),    	-- 行业类型
	IN p_zheadnum VARCHAR(120),	-- 经营本部
	IN p_zdisnum VARCHAR(30),   	-- 地区
	IN p_zbukrs VARCHAR(30),   	-- 法人
	IN p_bukrs VARCHAR(10),    	-- 公司代码
	IN p_tax_pay_sub VARCHAR(20),   -- 纳税主体
	IN p_warn_value VARCHAR(2),     -- 预警值
	IN p_emp_code VARCHAR(10),      -- 登录工号
	IN p_fisrDayOfJanuary VARCHAR(12), -- 当前年一月第一天
	IN p_periodStart VARCHAR(12),	-- 当前月第一天
	IN p_query_type VARCHAR(2),	-- 查询类型(0:查询数量;1:查询记录)
	IN p_pagestart INT(7),		-- 起始记录
	IN p_pageend INT(5)		-- 结束记录
)
BEGIN 
DECLARE not_found INT DEFAULT 0 ;-- 是否未找到数据 标记 
DECLARE v_tax_pay_sub VARCHAR(20) DEFAULT '';
DECLARE v_tax_pay_name VARCHAR(200) DEFAULT '';
DECLARE v_bukrs VARCHAR(30) DEFAULT '';
DECLARE v_znslx  VARCHAR(30) DEFAULT '';	        	
DECLARE v_zywlx VARCHAR(30) DEFAULT '';
DECLARE v_zbukrs VARCHAR(30) DEFAULT '';
DECLARE v_zbranch VARCHAR(30) DEFAULT '';
DECLARE v_zdisnum VARCHAR(30) DEFAULT '';
DECLARE v_datab DATE DEFAULT NULL;
DECLARE v_zheadnum VARCHAR(30) DEFAULT '';
DECLARE v_ywlxmc VARCHAR(100) DEFAULT '';
DECLARE v_zheadname VARCHAR(120) DEFAULT '';
DECLARE v_zdisname VARCHAR(120) DEFAULT '';
DECLARE v_butxt VARCHAR(100) DEFAULT '';
DECLARE v_zbutxt VARCHAR(100) DEFAULT '';
DECLARE v_tax_amount VARCHAR(512) DEFAULT '';
DECLARE v_apply_taxobject VARCHAR(100) DEFAULT '';
DECLARE v_period_start VARCHAR(12) DEFAULT '';
DECLARE v_period_end VARCHAR(12) DEFAULT '';
DECLARE v_apply_id VARCHAR(32) DEFAULT '';
DECLARE v_tax_type VARCHAR(32) DEFAULT '';
DECLARE v_apply_status VARCHAR(16) DEFAULT '';
DECLARE v_apply_userid VARCHAR(10) DEFAULT '';
DECLARE v_tps_count INT(10) DEFAULT 0;
DECLARE v_M1 VARCHAR(20) DEFAULT '';
DECLARE v_M2 VARCHAR(20) DEFAULT '';
DECLARE v_M3 VARCHAR(20) DEFAULT '';
DECLARE v_M4 VARCHAR(20) DEFAULT '';
DECLARE v_M5 VARCHAR(20) DEFAULT '';
DECLARE v_M6 VARCHAR(20) DEFAULT '';
DECLARE v_M7 VARCHAR(20) DEFAULT '';
DECLARE v_M8 VARCHAR(20) DEFAULT '';
DECLARE v_M9 VARCHAR(20) DEFAULT '';
DECLARE v_M10 VARCHAR(20) DEFAULT '';
DECLARE v_M11 VARCHAR(20) DEFAULT '';
DECLARE v_M12 VARCHAR(20) DEFAULT '';
DECLARE v_M13 VARCHAR(20) DEFAULT '';
DECLARE v_fillValue DECIMAL(16,2) DEFAULT 0;
DECLARE v_valueType VARCHAR(240) DEFAULT '';
DECLARE v_MONAT VARCHAR(20) DEFAULT '';
DECLARE v_GJAHR VARCHAR(20) DEFAULT '';
DECLARE cursor_prePayTax CURSOR FOR
SELECT
	DISTINCT
	p.TAX_PAY_SUB,
	p.FILL_VALUE,
	p.VALUE_TYPE,
	r.TAX_PAY_NAME,
	r.BUKRS,
	r.ZBUKRS,
	r.ZYWLX,
	r.ZHEADNUM,
	r.ZHEADNAME,
	r.ZDISNUM,
	r.ZDISNAME,
	r.YWLXMC,
	r.BUTXT,
	p.GJAHR,
	p.MONAT
FROM
  report_original_pick p 
  JOIN report_custom_info r 
    ON p.TAX_PAY_SUB = r.TAX_PAY_SUB 
     AND p.GJAHR < '2016' -- 2016以前的历史数据
WHERE 1 = 1
-- AND r.END_TIME >= p_periodStart 
AND p.GJAHR = YEAR(p_periodStart)
AND p.MONAT <= LPAD(MONTH(p_periodStart),2,'0')
AND p.VALUE_TYPE = 'YJSK'
AND IF(p_zywlx = '' OR p_zywlx IS  NULL,1=1,r.ZYWLX = p_zywlx)
AND IF(p_zheadnum = '' OR p_zheadnum IS  NULL,1=1,r.ZHEADNUM = p_zheadnum)
AND IF(p_zdisnum = '' OR p_zdisnum IS  NULL,1=1,r.ZDISNUM = p_zdisnum)
AND IF(p_zbukrs = '' OR p_zbukrs IS  NULL,1=1,r.ZBUKRS = p_zbukrs)
AND IF(p_bukrs = '' OR p_bukrs IS  NULL,1=1,r.BUKRS = p_bukrs)    
AND IF(p_tax_pay_sub = '' OR p_tax_pay_sub IS  NULL,1=1,p.TAX_PAY_SUB = p_tax_pay_sub)
AND r.BUKRS IN (SELECT b.BUKRS AS DEPT_CODE FROM e_user_role t 
			JOIN crop_infos b 
			  ON t.DEPT_CODE = b.BUKRS 
			JOIN um_user u 
			  ON t.USER_ID = u.ID WHERE u.EMP_CODE = p_emp_code)
UNION -- 2016年以后的数据
SELECT
	DISTINCT
	p.TAX_PAY_SUB,
	p.FILL_VALUE,
	p.VALUE_TYPE,
	t2.TAX_PAY_NAME,
	t2.BUKRS,
	t3.ZBUKRS,
	t3.ZYWLX,
	t4.ZHEADNUM,
	t4.ZHEADNAME,
	t4.ZDISNUM,
	t4.ZDISNAME,
	l.YWLXMC,
	t5.BUTXT,
	p.GJAHR,
	p.MONAT
FROM
  report_original_pick p
JOIN 
  b_ztfi_tax_company t2 
  ON p.TAX_PAY_SUB = t2.TAX_PAY_SUB
  AND t2.END_TIME >= p_periodStart AND 
  p.GJAHR > '2015'
  JOIN crop_infos t5 
    ON t5.BUKRS = t2.BUKRS 
  JOIN b_ztfi_t001_ext t3 
    ON t2.BUKRS = t3.BUKRS 
  JOIN b_ztfi_ext_are_map t4 
    ON t4.ZDISNUM = t3.ZDISNUM 
  JOIN a_ztfi_ywlx l 
    ON t3.ZYWLX = l.ZYWLX	
WHERE 1 = 1 
		AND p.GJAHR = YEAR(p_periodStart)
		AND p.MONAT = LPAD(MONTH(p_periodStart),2,'0')
		AND p.VALUE_TYPE = 'YJSK'
		AND IF(p_zywlx = '' OR p_zywlx IS  NULL,1=1,t3.ZYWLX = p_zywlx)
		AND IF(p_zheadnum = '' OR p_zheadnum IS  NULL,1=1,t4.ZHEADNUM = p_zheadnum)
		AND IF(p_zdisnum = '' OR p_zdisnum IS  NULL,1=1,t4.ZDISNUM = p_zdisnum)
		AND IF(p_zbukrs = '' OR p_zbukrs IS  NULL,1=1,t3.ZBUKRS = p_zbukrs)
		AND IF(p_bukrs = '' OR p_bukrs IS  NULL,1=1,t2.BUKRS = p_bukrs)    
		AND IF(p_tax_pay_sub = '' OR p_tax_pay_sub IS  NULL,1=1,p.TAX_PAY_SUB = p_tax_pay_sub)
		AND t2.BUKRS IN (SELECT b.BUKRS AS DEPT_CODE FROM e_user_role t 
					JOIN crop_infos b 
					  ON t.DEPT_CODE = b.BUKRS 
					JOIN um_user u 
					  ON t.USER_ID = u.ID WHERE u.EMP_CODE = p_emp_code);     		  
-- end 2016			  
DECLARE CONTINUE HANDLER FOR NOT FOUND
BEGIN SET not_found = 1; -- 如果未找到数据 将标记改为不可用
END;
DROP TEMPORARY TABLE IF EXISTS tmp_prePayTax;		
CREATE TEMPORARY TABLE	tmp_prePayTax(
  ZYWLX VARCHAR(128), -- 行业类型代码
  YWLXMC VARCHAR(256), -- 行业类型名称
  ZHEADNUM VARCHAR(30), -- 本部代码
  ZHEADNAME VARCHAR(120), -- 本部名称
  ZDISNUM VARCHAR(30),  -- 区部代码
  ZDISNAME VARCHAR(120), -- 区部名称
  ZBUKRS VARCHAR(30), -- 法人
  ZBUTXT VARCHAR(30), -- 公司名称
  BUKRS VARCHAR(30),  -- 公司代码
  BUTXT VARCHAR(200), -- 公司名称
  TAX_PAY_SUB VARCHAR(20), -- 纳税主体代码
  TAX_PAY_NAME VARCHAR(200), -- 纳税主体名称
  TAX_AMOUNT VARCHAR(512),
  M1 DECIMAL(16,2) DEFAULT 0,  
M2 DECIMAL(16,2) DEFAULT 0,
M3 DECIMAL(16,2) DEFAULT 0,
M4 DECIMAL(16,2) DEFAULT 0,
M5 DECIMAL(16,2) DEFAULT 0,
M6 DECIMAL(16,2) DEFAULT 0,
M7 DECIMAL(16,2) DEFAULT 0,
M8 DECIMAL(16,2) DEFAULT 0,
M9 DECIMAL(16,2) DEFAULT 0,
M10 DECIMAL(16,2) DEFAULT 0,
M11 DECIMAL(16,2) DEFAULT 0,
M12 DECIMAL(16,2) DEFAULT 0,	
  M13 DECIMAL(16,2) DEFAULT 0
); 	
-- 打开游标
OPEN cursor_prePayTax;
looplabel:
LOOP
	FETCH cursor_prePayTax INTO  v_tax_pay_sub,v_fillValue,v_valueType,v_tax_pay_name,v_bukrs,v_zbukrs,v_zywlx,v_zheadnum,v_zheadname,
	v_zdisnum,v_zdisname,v_ywlxmc,v_butxt,v_GJAHR,v_MONAT;
	IF not_found = 1  THEN 
		LEAVE looplabel ;
	END IF ;
			-- 判断临时表中是否有该纳税主体
			SELECT COUNT(1) FROM tmp_prePayTax WHERE TAX_PAY_SUB = v_tax_pay_sub INTO v_tps_count;
			-- v_tps_count = 0 插入
			IF(v_tps_count = 0) THEN
				INSERT INTO tmp_prePayTax(ZYWLX,ZHEADNUM,ZHEADNAME,ZDISNUM,ZDISNAME,ZBUKRS,BUKRS,BUTXT,TAX_PAY_SUB,TAX_PAY_NAME,YWLXMC) 
					VALUES(v_zywlx,v_zheadnum,v_zheadname,v_zdisnum,v_zdisname,v_zbukrs,v_bukrs,v_butxt,v_tax_pay_sub,v_tax_pay_name,v_ywlxmc);
			ELSE
				-- 处理业务数据 1-12月数据更新
				IF (v_MONAT = '01')
					THEN UPDATE tmp_prePayTax SET M1 = IFNULL(v_fillValue,0) WHERE TAX_PAY_SUB = v_tax_pay_sub;
				ELSEIF (v_MONAT = '02') 
					THEN UPDATE tmp_prePayTax SET M2 = IFNULL(v_fillValue,0) WHERE TAX_PAY_SUB = v_tax_pay_sub;
				ELSEIF (v_MONAT = '03') 
					THEN UPDATE tmp_prePayTax SET M3 = IFNULL(v_fillValue,0) WHERE TAX_PAY_SUB = v_tax_pay_sub;
				ELSEIF (v_MONAT = '04') 
					THEN UPDATE tmp_prePayTax SET M4 = IFNULL(v_fillValue,0) WHERE TAX_PAY_SUB = v_tax_pay_sub;	
				ELSEIF (v_MONAT = '05' ) 
					THEN UPDATE tmp_prePayTax SET M5 = IFNULL(v_fillValue,0) WHERE TAX_PAY_SUB = v_tax_pay_sub;	
				ELSEIF (v_MONAT = '06') 
					THEN UPDATE tmp_prePayTax SET M6 = IFNULL(v_fillValue,0) WHERE TAX_PAY_SUB = v_tax_pay_sub;
				ELSEIF (v_MONAT = '07' ) 
					THEN UPDATE tmp_prePayTax SET M7 = IFNULL(v_fillValue,0) WHERE TAX_PAY_SUB = v_tax_pay_sub;	
				ELSEIF (v_MONAT = '08') 
					THEN UPDATE tmp_prePayTax SET M8 = IFNULL(v_fillValue,0) WHERE TAX_PAY_SUB = v_tax_pay_sub;
				ELSEIF (v_MONAT = '09') 
					THEN UPDATE tmp_prePayTax SET M9 = IFNULL(v_fillValue,0) WHERE TAX_PAY_SUB = v_tax_pay_sub;
				ELSEIF (v_MONAT = '10') 
					THEN UPDATE tmp_prePayTax SET M10 = IFNULL(v_fillValue,0) WHERE TAX_PAY_SUB = v_tax_pay_sub;	
				ELSEIF (v_MONAT = '11') 
					THEN UPDATE tmp_prePayTax SET M11 = IFNULL(v_fillValue,0) WHERE TAX_PAY_SUB = v_tax_pay_sub;
				ELSEIF (v_MONAT = '12') 
					THEN UPDATE tmp_prePayTax SET M12 = IFNULL(v_fillValue,0) WHERE TAX_PAY_SUB = v_tax_pay_sub;
				END IF;
			END IF;
	SET not_found = 0;
END LOOP looplabel;			
CLOSE cursor_prePayTax;
-- 返回结果
IF(p_query_type = 0) THEN 
	SELECT COUNT(1) FROM tmp_prePayTax;
ELSEIF(p_query_type = 1) THEN 
	SELECT * FROM tmp_prePayTax LIMIT p_pagestart,p_pageend;
END IF;
END$$

DELIMITER ;


/////

// mybatis 调用

你可能感兴趣的:(存储过程)