⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shouwendjtask.java

📁 一个真实项目的源代码。有一个比较优秀的时间类
💻 JAVA
字号:
package com.zx.gwgl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.TimerTask;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.work.db.DbConnection;
import com.work.db.DbUtil;
import com.work.exception.OurException;
/**
 * 测试发现,这个驱动有问题,不支持该有的jdbc标准。PreparedStatement无法循环增加参数
 * 而且事务支持的也有问题。
 * @author wangmj
 *
 */
public class ShouWenDjTask extends TimerTask{
	private static Log log = LogFactory.getLog(ShouWenDjTask.class);
	/**
	 * 为了实现收文流程中的数据自动登记;<br>
	 * 实现思路:首先判断此收文流程是否结束。将发文流程中的数据从zx_shouwen表中查询出来,
	 * 然后插入到表zx_shouwendj中去,同时将zx_shouwen表中符合条件记录的sfygd设置为1
	 * 
	 * @return void
	 */
	public void shouWenDj() throws OurException{

		String shouWenSql = "select swsj as rq," + // 日期 收文时间
				"wenhao as lwzh," + // 来文字号 文号
				"lwjg ," + // 来文机关 来文机关
				"cwrq," + // 成文日期 成文日期
				"ys," + // 页数 页数
				"wjbt," + // 文件标题 文件标题
				"bgqx," + // 保管期限,保管期限  已经被去掉了,替换成了保管期限。
				"'' as sjr," + // 收件人 手工录入,默认为空字符串
				"id as sw_id " + // 系统id 对应一个流程实例编号(就是zx_shouwen的id)
				"from zx_shouwen " + "where sfygd=0";
		List srcList = DbUtil.executeQueryList(shouWenSql);
		if(srcList == null ){
			log.info("没有需要归档的收文流程记录。");
			return ;
		}
		int LEN = srcList.size(); //需要登记的收文流程记录数;
		log.debug("总共有"+LEN+"条收文记录。");
		List needGdList = new ArrayList();
		//=====================判断流程是否结束,然后来决定对哪些记录进行自动登记===========
		for(int i=0;i<LEN;i++){
			HashMap map = (HashMap) srcList.get(i);
			String id = (String)map.get("sw_id");
			String tempSql = "select step_closedtype from enrol_flow where process_inst_id='"+id+"' order by id desc";
			
			List tempList = DbUtil.executeQueryList(tempSql);
			if(tempList == null) break;//跳出循环
		    int tempLen = tempList.size();
		    //Finished
		    boolean tempFlag = false; //用来判断此流程过程中是否结束,如果有一条记录包含Finished,那么说明此流程已经结束,可以登记了。
		    for(int j=0;j<tempLen;j++){
		    	if(((String)((HashMap)tempList.get(j)).get("step_closedtype")).equals("Finished")){
		    		tempFlag = true;
		    		break;
		    	}
		    }
		    if(tempFlag) needGdList.add(map);					
		}
		
		//=================================================
		LEN = needGdList.size();
		if(LEN<1 ){
			log.info("没有需要归档的收文流程记录。");
			return ;
		}
		log.debug("能够归档收文流程的记录数为:"+LEN+"条。");
		String djSql = "insert into zx_shouwendj "
				+ "(rq,lwzh,lwjg,cwrq,ys,wjbt,sjbh,sjr,sw_id,bgqx) values (?,?,?,?,?,?,?,?,?,?)";
		String sfygdSql = "update zx_shouwen set sfygd=1 where id =? "; //更新发文流程中对应的记录数
		String rq = "";
		String lwzh ="";
		String lwjg ="";
		String cwrq ="";
		int ys =0;
		
		String wjbt ="";		
		String sjbh = "";
		String sjr="";
		String sw_id = "";		
		
		String bgqx= "";
		
		Connection conn = DbConnection.getConn();
		if(conn == null ) 
			throw new OurException("获取数据库连接失败!");
		PreparedStatement pst = null;
		PreparedStatement pst2 = null;
		int result = 0;
		int result2 = 0; //这个sqlserver驱动程序竟然不支持PreparedStatement循环传递参数。		
		try{
			conn.setAutoCommit(false);
			
			pst  = conn.prepareStatement(djSql);
			pst2  = conn.prepareStatement(sfygdSql);
			for(int i=0;i<LEN;i++){
				HashMap map = (HashMap)needGdList.get(i);
				rq = (String)map.get("rq");
				lwzh = (String)map.get("lwzh");
				lwjg = (String)map.get("lwjg");
				cwrq = (String)map.get("cwrq");				
				ys = ((Integer)map.get("ys")).intValue(); 
	
				
				wjbt = (String)map.get("wjbt");				
				
				//sjbh = (String)map.get("swbh"); //收文编号已经被去掉了。
				sjbh = "";
				sjr = (String)map.get("sjr");
				sw_id = (String)map.get("sw_id");
				bgqx = (String)map.get("bgqx");
				
				
				pst.setString(1,rq);
				pst.setString(2,lwzh);
				pst.setString(3,lwjg);
				pst.setString(4,cwrq);
				pst.setInt(5,ys);
				
				pst.setString(6,wjbt);
				pst.setString(7,sjbh);
				pst.setString(8,sjr);
				pst.setString(9,sw_id);
				pst.setString(10,bgqx);
				
				pst2.setString(1,sw_id);
				result += pst.executeUpdate();
				result2 += pst2.executeUpdate();
				//将已经归档的zx_shouwen中对应的记录设置sfygd为1
			
			}
			conn.commit();			
						
			log.debug("共"+result+"条收文流程记录登记成功!共"+result2+"条收文流程记录设置登记标志成功!");
		
		}catch(SQLException e){
			log.error("出现异常!由于登记了"+result+"记录,更新了"+result2+"条记录!所以登记失败!" +
			"估计是记录数目太多,超出了PreparedStatement的缓存数!");
			
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
//			String temp =" select swsj as rq,wenhao as lwzh,lwjg ,cwrq,ys,wjbt,sjbh,'' as sjr," +
//					"id as sw_id ,0 as sfygd from zx_shouwen where sfygd=0";
			throw new OurException("收文流程自动登记执行失败!请通知管理员!",e);
			
		}finally{
			DbUtil.closeStatement(pst2);
			DbUtil.closeStatement(pst);
			DbUtil.closeConnection(conn);
		}
	}
	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		try {
			shouWenDj();
		} catch (OurException e) {
			System.out.println(com.work.exception.ExceptionUtil
					.getDetailMessage(e));

		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -