1.存储过程内容
create or replace procedure proc_generate_demand_note ( totalTaxNo in varchar2, userId in varchar2, dpCode in varchar2, ifTaxation in varchar2, invalidFlag in varchar2, payCompany in varchar2, taxBillType in varchar2, personalEntryId in varchar2, taxNo in varchar2, logisticsNo in varchar2, ifPrint in varchar2, totalTaxBegin in number, totalTaxEnd in number, declareDateBegin in varchar2, declareDateEnd in varchar2, execMsg out varchar2 ) as select_sql varchar2(2000); v_cursor integer; v_count integer; v_tax_no varchar2(18); v_quantity number(10,5); v_dutypaid_price number(10,5); v_actual_tax number(10,5); v_decl_tm Date; v_head_id number(19); v_cbecbill_no varchar2(20); v_temp varchar2(100); some_kinds_of_err EXCEPTION; begin /* 判断必填的输入参数是否缺失 */ if totalTaxNo is null then v_temp := '缴款通知书编号不能为空!'; raise some_kinds_of_err; end if; if userId is null then v_temp := '操作人不能为空!'; raise some_kinds_of_err; end if; if dpCode is null then v_temp := '关区代码不能为空!'; raise some_kinds_of_err; end if; if ifTaxation is null then v_temp := '征免标识不能为空!'; raise some_kinds_of_err; end if; if invalidFlag is null then v_temp := '作废标识不能为空!'; raise some_kinds_of_err; end if; -- 打开游标 v_cursor := dbms_sql.open_cursor; /* 查询人工生成缴款通知书添加的税单信息 */ select_sql := 'select t1.TAX_NO, t2.QUALITYS, t2.DUTYPAID_PRICES, t1.TOTAL_TAX, t1.DECL_TM, t1.HEAD_ID, t1.CBECBILL_NO '; select_sql := select_sql || 'from TB_B_TAX t1, (select a.TAX_NO, sum(c.QUALITY) QUALITYS, sum(c.DUTYPAID_PRICE) DUTYPAID_PRICES '; select_sql := select_sql || 'from TB_B_TAX a, TB_IE_MINIFEST_HEAD b, TB_B_TAX_DETAIL c '; select_sql := select_sql || 'where a.HEAD_ID = b.ID and a.TAX_NO = c.TAX_NO '; -- 动态拼接SQL语句 select_sql := select_sql || 'and a.DP_CODE = :DP_CODE '; select_sql := select_sql || 'and a.IF_TAXATION = :IF_TAXATION '; select_sql := select_sql || 'and a.INVALID_FLAG = :INVALID_FLAG '; if payCompany is not null then select_sql := select_sql || 'and a.PAY_COMPANY = :PAY_COMPANY '; end if; if taxBillType is not null then select_sql := select_sql || 'and a.TAX_BILL_TYPE = :TAX_BILL_TYPE '; end if; if personalEntryId is not null then select_sql := select_sql || 'and b.PERSONAL_ENTRY_ID like :PERSONAL_ENTRY_ID '; end if; if taxNo is not null then select_sql := select_sql || 'and a.TAX_NO like :TAX_NO '; end if; if logisticsNo is not null then select_sql := select_sql || 'and b.LOGISTICS_NO like :LOGISTICS_NO '; end if; if ifPrint is not null then select_sql := select_sql || 'and a.IF_PRINT = :IF_PRINT '; end if; if totalTaxBegin is not null then select_sql := select_sql || 'and a.TOTAL_TAX >= :TOTAL_TAX_BEGIN '; end if; if totalTaxEnd is not null then select_sql := select_sql || 'and a.TOTAL_TAX <= :TOTAL_TAX_END '; end if; if declareDateBegin is not null then select_sql := select_sql || 'and b.DECLARE_DATE >= to_date(:DECLARE_DATE_BEGIN, ''yyyy-mm-dd hh24:mi:ss'') '; end if; if declareDateEnd is not null then select_sql := select_sql || 'and b.DECLARE_DATE <= to_date(:DECLARE_DATE_END, ''yyyy-mm-dd hh24:mi:ss'') '; end if; select_sql := select_sql || 'and (sysdate - b.discharge_date) > 14 '; select_sql := select_sql || 'group by a.TAX_NO) t2 where t1.TAX_NO = t2.TAX_NO '; -- 解析动态SQL语句 dbms_sql.parse(v_cursor, select_sql, dbms_sql.native); -- 动态绑定输入参数 dbms_sql.bind_variable(v_cursor, ':DP_CODE', dpCode); dbms_sql.bind_variable(v_cursor, ':IF_TAXATION', ifTaxation); dbms_sql.bind_variable(v_cursor, ':INVALID_FLAG', invalidFlag); if payCompany is not null then dbms_sql.bind_variable(v_cursor, ':PAY_COMPANY', payCompany); end if; if taxBillType is not null then dbms_sql.bind_variable(v_cursor, ':TAX_BILL_TYPE', taxBillType); end if; if personalEntryId is not null then dbms_sql.bind_variable(v_cursor, ':PERSONAL_ENTRY_ID', personalEntryId); end if; if taxNo is not null then dbms_sql.bind_variable(v_cursor, ':TAX_NO', taxNo); end if; if logisticsNo is not null then dbms_sql.bind_variable(v_cursor, ':LOGISTICS_NO', logisticsNo); end if; if ifPrint is not null then dbms_sql.bind_variable(v_cursor, ':IF_PRINT', ifPrint); end if; if totalTaxBegin is not null then dbms_sql.bind_variable(v_cursor, ':TOTAL_TAX_BEGIN', totalTaxBegin); end if; if totalTaxEnd is not null then dbms_sql.bind_variable(v_cursor, ':TOTAL_TAX_END', totalTaxEnd); end if; if declareDateBegin is not null then dbms_sql.bind_variable(v_cursor, ':DECLARE_DATE_BEGIN', declareDateBegin); end if; if declareDateEnd is not null then dbms_sql.bind_variable(v_cursor, ':DECLARE_DATE_END', declareDateEnd); end if; -- 定义列 v_cursor 对应 select 语句中的列 dbms_sql.define_column(v_cursor, 1, v_tax_no, 20); dbms_sql.define_column(v_cursor, 2, v_quantity); dbms_sql.define_column(v_cursor, 3, v_dutypaid_price); dbms_sql.define_column(v_cursor, 4, v_actual_tax); dbms_sql.define_column(v_cursor, 5, v_decl_tm); dbms_sql.define_column(v_cursor, 6, v_head_id); dbms_sql.define_column(v_cursor, 7, v_cbecbill_no, 20); -- 执行动态SQL语句 v_count := dbms_sql.execute(v_cursor); loop exit when dbms_sql.fetch_rows(v_cursor) <= 0; -- 将列的值读入临时变量中 dbms_sql.column_value(v_cursor, 1, v_tax_no); dbms_sql.column_value(v_cursor, 2, v_quantity); dbms_sql.column_value(v_cursor, 3, v_dutypaid_price); dbms_sql.column_value(v_cursor, 4, v_actual_tax); dbms_sql.column_value(v_cursor, 5, v_decl_tm); dbms_sql.column_value(v_cursor, 6, v_head_id); dbms_sql.column_value(v_cursor, 7, v_cbecbill_no); /* 生成缴款通知书明细 */ insert into tb_b_meger_tax_detail (ID, TOTAL_TAX_NO, TAX_NO, CBECBILL_NO, ACTUAL_TAX, DECL_TM, QUANTITY, DUTYPAID_PRICE, HEAD_ID) values (I_MEGER_TAX_DETAIL_SQC.nextval, totalTaxNo, v_tax_no, v_cbecbill_no, v_actual_tax, v_decl_tm, v_quantity, v_dutypaid_price, v_head_id); /* 修改税单状态 */ update TB_B_TAX t set t.TOTAL_TAX_NO = totalTaxNo, t.STATUS = '2' where t.TAX_NO = v_tax_no; /* 保存操作日志 */ insert into TB_B_TAX_OPERATE (ID, OPRATOR, OPRATE_TM, TAX_NO, OPERATE_TYPE, OPERATE_REASON) values (SEQ_B_TAX_OPERATE.nextval, userId, sysdate, v_tax_no, '4', '人工生成缴款通知书'); end loop; -- 关闭游标 dbms_sql.close_cursor(v_cursor); -- 提交事务 commit; execMsg := '操作成功!'; -- 异常回滚 exception when some_kinds_of_err then execMsg := '操作失败,' || v_temp; when others then rollback; execMsg := '操作失败!'; end proc_generate_demand_note;
2.JAVA程序调用的方法
@Autowired private JdbcTemplate jdbcTemplate; @Override public String generateDemandNote(final QueryCondition qc) { return jdbcTemplate.execute(new CallableStatementCreator() { @Override public CallableStatement createCallableStatement(Connection conn) throws SQLException { String storeProc = "{ call proc_generate_demand_note(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) }"; CallableStatement cs = conn.prepareCall(storeProc); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); cs.setString(1, qc.getTotalTaxNo()); cs.setString(2, StringUtils.isNotEmpty(qc.getOperator()) ? qc.getOperator() : null); cs.setString(3, StringUtils.isNotEmpty(qc.getDpCode()) ? qc.getDpCode() : null); cs.setString(4, qc.getIfTaxation() != null ? String.valueOf(qc.getIfTaxation()) : null); cs.setString(5, qc.getInvalidFlag() != null ? String.valueOf(qc.getInvalidFlag()) : null); cs.setString(6, StringUtils.isNotEmpty(qc.getPayCompany()) ? qc.getPayCompany() : null); cs.setString(7, StringUtils.isNotEmpty(qc.getTaxBillType()) ? qc.getTaxBillType() : null); cs.setString(8, StringUtils.isNotEmpty(qc.getCbecbillNo()) ? qc.getCbecbillNo() : null); cs.setString(9, StringUtils.isNotEmpty(qc.getTaxNo()) ? qc.getTaxNo() : null); cs.setString(10, StringUtils.isNotEmpty(qc.getBillNo()) ? qc.getBillNo() : null); cs.setString(11, qc.getIfPrint() != null ? String.valueOf(qc.getIfPrint()) : null); cs.setString(12, qc.getTotalTaxFrom() != null ? String.valueOf(qc.getTotalTaxFrom()) : null); cs.setString(13, qc.getTotalTaxTo() != null ? String.valueOf(qc.getTotalTaxTo()) : null); cs.setString(14, qc.getDeclareDateFrom() != null ? sdf.format(qc.getDeclareDateFrom()) : null); cs.setString(15, qc.getDeclareDateTo() != null ? sdf.format(qc.getDeclareDateTo()) : null); cs.registerOutParameter(16, OracleTypes.VARCHAR); return cs; } }, new CallableStatementCallback<String>() { @Override public String doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException { cs.execute(); return cs.getString(16); } }); }