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

📄 machineconfigdaoimpl.java.svn-base

📁 自动导入文件到数据库,用于那些无法实时入库,定点时间入库的需求
💻 SVN-BASE
字号:
package com.onet.autobill.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.onet.autobill.dao.MachineConfigDao;
import com.onet.autobill.model.MachineConfig;

public class MachineConfigDaoImpl extends JdbcDaoSupport implements MachineConfigDao {
	
	private int beginTime = 8;
	
	private SimpleDateFormat simpledateformatdd= new SimpleDateFormat ("dd");
	
	private SimpleDateFormat simpledateformatHH= new SimpleDateFormat ("HH");
	
	private Calendar calendar = Calendar.getInstance();

	@Override
	public List<MachineConfig> getAllMachineConfig() {

		final List<MachineConfig> machineConfigList = new LinkedList<MachineConfig>();
		String sql = "SELECT MachineName,MachineUser,MachinePassword,config.* "
			+ " FROM MachineConfig config,Machine machine "
			+ " WHERE config.MachineId = machine.MachineId "
			+ "  and DATEADD(minute,config.RunSchedule,LastImportTime)<=GETDATE()"
			+ "  and config.ValidateFlg <>0";
		
		try {
			getJdbcTemplate().query(sql, new RowCallbackHandler() {
				public void processRow(ResultSet rs) throws SQLException {
					do {
						MachineConfig config = new MachineConfig();
						config.setMachineName(rs.getString("MachineName"));
						config.setMachineUser(rs.getString("MachineUser"));
						config.setMachinePassword(rs.getString("MachinePassword"));
						config.setMachineId(rs.getInt("MachineId"));
						config.setStatus(rs.getInt("Status"));
						config.setLogDir(rs.getString("LogDir"));
						config.setMachineConfigId(rs.getInt("Id"));
						config.setSndPre(rs.getString("SndPre"));
						config.setRecPre(rs.getString("RecPre"));
						config.setLogCode(rs.getInt("LogCode"));
						config.setLastImportTime(rs.getTimestamp("LastImportTime"));
						calendar.setTime(rs.getTimestamp("LastImportTime"));
						config.setValidateFlg(rs.getInt("ValidateFlg"));
						config.setLogVersion(rs.getInt("LogVersion"));
						config.setRunSchedule(rs.getInt("RunSchedule"));
						config.setAutobillNode(rs.getInt("AutobillNode"));
						String lastTime = simpledateformatHH.format(config.getLastImportTime());
						if(Integer.parseInt(lastTime)<beginTime){
							calendar.add(Calendar.MINUTE, config.getRunSchedule());
							String nowTime = simpledateformatHH.format(calendar.getTime());
							if(Integer.parseInt(nowTime)<beginTime){
								updateTime(config);
								continue;
							}
						}
						machineConfigList.add(config);
					} while (rs.next());
				}
			});
		} catch (Exception e) {
			logger.error("", e);
		}
		return machineConfigList;
	}

	@Override
	public boolean updateNodeAndTime(int node, MachineConfig machineConfig) {
		logger.debug("成功操作更新node"+node);
		String sql = "update MachineConfig set LastAutobillNode = AutobillNode, AutobillNode=? where Id =?";
		try{
			getJdbcTemplate().update(sql, new Object[] { node, machineConfig.getMachineConfigId()});
			updateTime(machineConfig);
		return true;
		}catch(Exception e){
			logger.error("machineConfig编号:"+machineConfig.getMachineConfigId()+"更新node及时间失败",e);
			return false;
		}
	}

	@Override
	public boolean updateNodeTo0ByMachineConfigId(MachineConfig machineConfig) {
		String sql = "update MachineConfig set AutobillNode =0 where Id =?";
		try{
			getJdbcTemplate().update(sql, new Object[] { machineConfig.getMachineConfigId() });
		return true;
		}catch(Exception e){
			logger.error("machineConfig编号:"+machineConfig.getMachineConfigId()+"更新node为0失败",e);
			return false;
		}
	}

	@Override
	public boolean updateLastNodeToNodeByFileErr(MachineConfig machineConfig) {
		String sql = "update MachineConfig set LastAutobillNode =AutobillNode  where Id =?";
		try{
			getJdbcTemplate().update(sql, new Object[] { machineConfig.getMachineConfigId()});
			updateTime(machineConfig);
		return true;
		}catch(Exception e){
			logger.error("因无新记录machineConfig编号:"+machineConfig.getMachineConfigId()+"更新LastAutobillNode为AutobillNode及时间失败",e);
			return false;
		}
	}

	@Override
	public boolean updateTwoNodeByImportToDBErr(int node, MachineConfig machineConfig) {
		logger.debug("失败操作更新node"+node);
		String sql = "update MachineConfig set AutobillNode=?,LastAutobillNode =?  where Id =?";
		try{
			getJdbcTemplate().update(sql, new Object[] { node, node ,machineConfig.getMachineConfigId()});
			updateTime(machineConfig);
		return true;
		}catch(Exception e){
			logger.error("因倒库错误machineConfig编号:"+machineConfig.getMachineConfigId()+"更新LastAutobillNode和AutobillNode及时间失败",e);
			return false;
		}
	}

	@Override
	public boolean newDayNodeInit(MachineConfig machineConfig) {
		String sql = "update MachineConfig set LastAutobillNode = AutobillNode,AutobillNode = 0 where Id =?";
		try {
			getJdbcTemplate().update(sql, new Object[] {machineConfig.getMachineConfigId()});
			return true;
		} catch (Exception e) {
			logger.error("初始化日期时间出错", e);
			return false;
		}
	}
	
	public boolean updateTime(MachineConfig machineConfig){
		try{
			calendar.setTime(machineConfig.getLastImportTime());
			calendar.add(Calendar.MINUTE, machineConfig.getRunSchedule());
			String newD = simpledateformatdd.format(calendar.getTime());
			String lastD = simpledateformatdd.format(machineConfig.getLastImportTime());
			if(!newD.equals(lastD)){
				newDayNodeInit(machineConfig);
			}
			String sql = "update MachineConfig set LastImportTime = DATEADD(minute,RunSchedule,LastImportTime) where Id =?";
			getJdbcTemplate().update(sql, new Object[] { machineConfig.getMachineConfigId() });

			return true;
		}catch(Exception e){
			logger.error("重置最后更新时间出错",e);
			return false;
		}
	}

}

⌨️ 快捷键说明

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