package com.sf.dtx5.cache;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.zip.DeflaterOutputStream;
import com.sf.dtx5.Config;
import com.sf.dtx5.db.ByteLength;
import com.sf.dtx5.db.ConnectionPool;
import com.sf.dtx5.db.DbConnect;
import com.sf.dtx5.domain.Dtx5ForceUpdate;
import com.sf.dtx5.util.ExpOutUtils;
import net.sf.json.JSONObject;
/**
* dtx5版本号强制升级
*
*/
public class Dtx5ForceUpdateTask {
/**
* 新增运单信息查询SQL语句
*/
private String mSearchBillSql = " select t.version_number,t.deadline,sysdate from tm_dtx5_config t where id=(select max(id)from tm_dtx5_config where type='1' and status='0' and deal_flg=0)";
/**
* 从数据库中加载增量数据
*/
public byte[] loadIncrementData() {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = ConnectionPool.getInstance().getConnection();
if (con == null) {
Config.log.writeLog(2, "连接数据库失败");
return new byte[0];
}
pstmt = con.prepareStatement(mSearchBillSql);
rs = pstmt.executeQuery();
if (rs.next()) {
/** String str=rs.getString("version_number");
StringBuilder version = new StringBuilder();
for(int i=0;i<str.indexOf('.');++i){
if (Character.isDigit(str.charAt(i))){
version.append(str.charAt(i));
}
}
version.append(".");
for(int i=str.indexOf('.')+1;i<str.length();++i){
if (Character.isDigit(str.charAt(i))){
version.append(str.charAt(i));
}else{
break;
}
}*/
Dtx5ForceUpdate jsondao = new Dtx5ForceUpdate(rs.getString("version_number"),rs.getString("deadline"),rs.getString("sysdate"));
JSONObject json = JSONObject.fromObject(jsondao);
String jsonstr=json.toString();
byte[]datas=jsonstr.getBytes("UTF-8");
ByteBuffer buffer = ByteBuffer.allocate(4 + 2 + 12 + 12
+ ((datas != null) ? datas.length : 0));
buffer.put((byte)0);
buffer.put((byte)0);
buffer.put((byte)0);
buffer.put((byte)0);
buffer.put((byte)0);
buffer.put((byte)0);
buffer.put((byte)0);
buffer.put((byte)0);
buffer.put(datas);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DeflaterOutputStream dos = new DeflaterOutputStream(baos);
dos.write(buffer.array());
dos.finish();
int len = buffer.array().length;
ByteBuffer temp = ByteBuffer
.allocate(4 + baos.toByteArray().length);// 压缩前的数据长度
temp.putInt(Integer.reverseBytes(len));
temp.put(baos.toByteArray());
return temp.array();
}
} catch (Exception e) {
Config.log.writeLog(2, "巴枪版本强制升级>>查询运单失败:%s", ExpOutUtils.getStackTrace(e));
} finally {
DbConnect.closeOthers(pstmt, rs);
ConnectionPool.getInstance().putConnection(con);
}
ByteLength bytelength=new ByteLength();
return bytelength.length();
}
}