package com.task.recomm;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import com.util.ConfigManager;
import com.util.Constant;
import com.util.DBConnection;
import com.util.DateLib;
/**
* 厘佣金返还类
*
* 文件名 ReCommTask.java
* 作者: linj
* 日期: 2010-5-11
*
*/
public class ReCommDAO {
private Logger log = Logger.getLogger(this.getClass());
private Connection cn=null;
private Statement st = null;
private CallableStatement cs = null;
private List<DataForm> datalist = null;
private int seqNum;
private ConfigManager config = new ConfigManager();
private final String DATEFORMAT="yyMMddHHmmss";
public void do_return_Comm(){
try {
datalist = getDate();
if (datalist.size() <=0 ) {
log.debug("***** No data... *****");
return;
}
if ( return_Comm(datalist) ){
log.debug("***** Success deal with [" + datalist.size() + "] data... *****");
}else{
log.debug("***** Fail... *****");
}
} catch (Exception e) {
log.error(e, e);
e.printStackTrace();
}
}
/**
* 商户返厘佣金
*/
private boolean return_Comm(List<DataForm> datalist) throws Exception{
int i = 0,u_ba,tel_ba,uni_ba;
boolean flag = true ;
int dsize = datalist.size();
try{
cn = DBConnection.getConnection();
cn.setAutoCommit(false);
st = cn.createStatement();
for (i=0;i<dsize;i++){
DataForm df = datalist.get(i);
u_ba = df.getBalance();
tel_ba = df.getT_balance();
uni_ba = df.getUni_balance();
if ( u_ba >= 10 ) return_Comm_opt(df,1) ;
if ( uni_ba >= 10 ) return_Comm_opt(df,2);
if ( tel_ba >= 10 ) return_Comm_opt(df,3);
System.out.println("==第 ["+ (i+1) +"]条, ID为: [" + df.getU_id()+ "] 的数据处理完成, 剩余["+ (dsize-i-1)+"]条未处理==");
}
st.close();
}catch(Exception e){
flag = false;
System.out.println("==第 ["+ (i+1) +"]条,执行出错!==");
}
finally {
cn.close();
}
return flag;
}
/**
* 商户返厘佣金操作
*/
private boolean return_Comm_opt(DataForm df,int doType) throws Exception {
boolean flag = true;
String balName="";
int balext; //记录日志使用(原金额)
String bal1,opType;
// String db_type = ConfigManager.getPvalue(Constant.DB_PROPERTIES,Constant.DB_TYPE);
int wait_time = Integer.valueOf(ConfigManager.getPvalue(Constant.SYSTEM_PROPERTIES,Constant.WAIT_TIME)); //取出等待毫秒数
//取出开始序列号
String seqNumStr = ConfigManager.getPvalue(Constant.GLOBAL_PROPERTIES,Constant.SEQ_NUM);
if (seqNumStr == null || "".equals(seqNumStr)) seqNumStr="0";
seqNum = Integer.parseInt(seqNumStr);
System.out.println("--doType---"+doType);
//开始处理记录
String serial = getNextSerial(); //生成流水号
String uid = df.getU_id().trim(); // 取ID
switch(doType){
case 1:
if ( uid.startsWith("9") ) opType="21";
else opType="44";
balext = df.getBalance();
bal1 = String.valueOf(df.getBalance()/10*10 );
balName = "u_balance_ext";
break;
case 2:
opType = "40";
balext = df.getUni_balance();
bal1 = String.valueOf(df.getUni_balance()/10*10 );
balName = "uni_balance_ext";
break;
default:
opType = "60";
balext = df.getT_balance();
bal1 = String.valueOf(df.getT_balance()/10*10 );
balName = "tel_balance_ext";
break;
}
// System.out.println("---------bal="+bal1);
double bal2 = Double.parseDouble(bal1)/1000.0;
try {
if ( cn == null ) {
cn = DBConnection.getConnection();
cn.setAutoCommit(false);
}
/*Statement st = cn.createStatement();
CallableStatement cs = null;*/
//更新记录
String updateSql = "update tb_sys_wallet_ext " +
"set "+ balName +" = " + balName + " - " + bal1 + " where u_id='"+ uid +"'";
st.addBatch(updateSql);
//执行存储过程
String sql = "exec proc_sys_wallet_base_notran ?,?,?,?,?,?,?,?,?";
cs = cn.prepareCall(sql);
cs.setDouble(1, bal2); //金额
cs.setString(2, "0");
cs.setString(3, uid); //ID
cs.setString(4, "-");
cs.setString(5, serial); //流水号
cs.setString(6, "当日佣金返还");
cs.setString(7, opType); // 操作类型
cs.setString(8, "-");
cs.registerOutParameter(9, Types.INTEGER);
cs.executeUpdate();
int result = cs.getInt(9);
cs.close();
if (result == 1) { //0: 成功 1:余额不足
throw new Exception(" ID="+uid+",colName="+ balName +" 余额不足...");
}
st.executeBatch();//--------全部执行
cn.commit();//--------------提交
String newSeqNumStr = getSeqNumStr(seqNum);
//将最后使用的序列号保存回配置文件中
config.setSeqValue(newSeqNumStr);
log.debug("== id:[" + uid + "], " + balName + ":["+ balext +" - "+ bal1 +"] , money:["+ bal2 +"] 处理完成==");
Thread.sleep(wait_time); //执行成功,停止毫秒数
} catch (SQLException e) {
cn.rollback();
flag = false;
log.error(e, e);
log.error("Exception... id=[" + df.getU_id() + "], " + balName + "=["+ df.getBalance() + ": error");
log.debug("Exception... id=[" + df.getU_id() + "], " + balName + "=["+ df.getBalance() + ": error");
throw e;
}
return flag;
}
/**
* 生成下一个流水号
*/
private String getNextSerial(){
seqNum++;
if (seqNum >= 9999) seqNum=0;
String seqNumStr = getSeqNumStr(seqNum);
String serial = DateLib.today(DATEFORMAT) + seqNumStr;
return (serial);
}
/**
* 将传入的序列数转换成四位字符串
* @param seqNum
* @return
*
*/
private String getSeqNumStr(int SeqNum){
int num = 0;
StringBuffer seqNumStr = new StringBuffer();
for (int n=3;n>=0;n--){
switch(n){
case 3:
num = (int) Math.floor(SeqNum/1000);
SeqNum = SeqNum - num * 1000;
break;
case 2:
num = (int) Math.floor(SeqNum/100);
SeqNum = SeqNum - num * 100;
break;
case 1:
num = (int) Math.floor(SeqNum/10);
SeqNum = SeqNum - num * 10;
break;
case 0:
num = (int) Math.floor(SeqNum/1);
SeqNum = SeqNum - num * 1;
break;
}
seqNumStr.append(String.valueOf(num));
}
return seqNumStr.toString();
}
/**
* 取出金额大于等于1分的数据
* @return List
* @throws Exception
*
*/
private List<DataForm> getDate() throws Exception{
// String db_type = ConfigManager.getPvalue(Constant.DB_PROPERTIES,Constant.DB_TYPE);
cn = DBConnection.getConnection();
Statement st = null;
ResultSet rs = null;
List<DataForm> dataList = new ArrayList<DataForm>();
String sql = "select u_id,u_balance_ext,tel_balance_ext,uni_balance_ext from tb_sys_wallet_ext " +
"where u_balance_ext >= 10 or tel_balance_ext >=10 or uni_balance_ext >=10 ";
try {
st = cn.createStatement();
rs = st.executeQuery(sql);
while(rs.next())
{
DataForm df = new DataForm();
df.setU_id(rs.getString(1));
df.setBalance(rs.getInt(2));
df.setT_balance(rs.getInt(3));
df.setUni_balance(rs.getInt(4));
dataList.add(df);
}
rs.close();
st.close();
} catch (SQLException e) {
throw e;
} finally {
cn.close();
}
return dataList;
}
}