package com.im.component.ptc.trade.auction;
import java.net.*;
import java.io.*;
import java.util.*;
import com.im.component.ptc.config.model.*;
import com.im.component.ptc.trade.auction.model.*;
import com.im.component.ptc.*;
import com.im.util.encrypt.DesEncrypt;
import com.im.util.Debug;
import com.im.util.utils.*;
import com.im.util.Debug;
import com.im.util.model.*;
import com.im.util.log.LogContentModel;
import com.im.component.ptc.report.model.SumModel;
public class AuctionEngine extends Thread {
private List processors = new ArrayList();
public void run() {
ModelList auctions = null;
PageRequest pageRequest = new PageRequest();
pageRequest.setRecordsPerPage(-1);
pageRequest.setCurrentPage(-1);
while (true) {
try {
sleep(30000);
/// 当前系统日期和时间
java.util.Date currentDate = new java.util.Date(System.currentTimeMillis());
/// 上午交易开始时间:09:30 /
java.util.Date AMBidTimeBeginDate = new java.util.Date(System.currentTimeMillis());
ConfigModel AMConf = ConfigBO.getConfig("TRD", "AMBidTimeBegin");//上午交易开始时间'093000'
String AMBidTimeBegin = AMConf.getValue();
int AMhour = Integer.parseInt(AMBidTimeBegin.substring(0, 2));
int AMminutes = Integer.parseInt(AMBidTimeBegin.substring(2, 4));
int AMsecond = Integer.parseInt(AMBidTimeBegin.substring(4));
AMBidTimeBeginDate.setHours(AMhour);
AMBidTimeBeginDate.setMinutes(AMminutes);
AMBidTimeBeginDate.setSeconds(AMsecond);
/ 下午交易结束时间:15:00 /
java.util.Date PMBidTimeEndDate = new java.util.Date(System.currentTimeMillis());
ConfigModel PMConf = ConfigBO.getConfig("TRD", "PMBidTimeEnd");//下午交易结束时间'150000'
String PMBidTimeEnd = PMConf.getValue();
int PMhour = Integer.parseInt(PMBidTimeEnd.substring(0, 2));
int PMminutes = Integer.parseInt(PMBidTimeEnd.substring(2, 4));
int PMsecond = Integer.parseInt(PMBidTimeEnd.substring(4));
PMBidTimeEndDate.setHours(PMhour);
PMBidTimeEndDate.setMinutes(PMminutes);
PMBidTimeEndDate.setSeconds(PMsecond);
前台拍卖处理必须在开市时间( 9:30--15:00)期间 2006-04-18 ///
if ( currentDate.after(AMBidTimeBeginDate) && currentDate.before(PMBidTimeEndDate) ) {
// Debug.println("开市时间,允许处理拍卖 "+AMBidTimeBeginDate+" < "+currentDate +" <"+PMBidTimeEndDate) ;
//处理已经审核的拍卖项目 // A20-已经审核产权冻结完成
auctions = AuctionBO.getAuctionList(AuctionModel.AUCTION_STATUS_STOCK_FROZEN, pageRequest);
ProcessStockFrozen(auctions);
//处理正在进行的拍卖项目 // A30-正在拍卖
auctions = AuctionBO.getAuctionList(AuctionModel.AUCTION_STATUS_EXCUTING, pageRequest);
ProcessExcuting(auctions);
//处理已经结束的拍卖项目 // A40-拍卖结束
auctions = AuctionBO.getAuctionList(AuctionModel.AUCTION_STATUS_AUCTION_FINISHED, pageRequest);
ProcessFinished(auctions);
//处理已经结束的拍卖项目
//auctions = AuctionBO.getAuctionList(AuctionModel.AUCTION_STATUS_CLEARING, pageRequest);
//startAuctionProcessors(auctions);
//处理支付余款失败的项目
//auctions = AuctionBO.getAuctionList(AuctionModel.AUCTION_STATUS_PAY_REMAIN_FAILED, pageRequest);
//startAuctionProcessors(auctions);
}
// else{
// Debug.println("笔市时间,不允许处理拍卖 "+AMBidTimeBeginDate+" 》"+currentDate +" 》"+PMBidTimeEndDate) ;
// }
} catch (Exception e) {
}
this.yield();
}
}
public void destroy() {
}
/**
* 1.处理已经审核的拍卖项目 A20
* @param auctions
*/
private synchronized void ProcessStockFrozen(ModelList auctions) {
if (auctions == null || auctions.size() <= 0) return;
for (int i = 0; i < auctions.size(); i++) {
AuctionModel auction = (AuctionModel) auctions.get(i);
if (auction.getAuctionNo()==null || auction.getAuctionNo().trim().equals("")) continue;
LogContentModel log = new LogContentModel();
Date currentDate = new Date(System.currentTimeMillis());
try {
if (auction.getStartDate().before(currentDate)) {
auction.setStatus(auction.AUCTION_STATUS_EXCUTING); // A30-正在拍卖
AuctionBO.updateAuction(auction, log);
}
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
/**
* 2.处理正在拍卖的项目 A30
* @param auctions
*/
private synchronized void ProcessExcuting(ModelList auctions) {
if (auctions == null || auctions.size() <= 0) return;
for (int i = 0; i < auctions.size(); i++) {
AuctionModel auction = (AuctionModel) auctions.get(i);
if (auction.getAuctionNo()==null || auction.getAuctionNo().trim().equals("")) continue;
LogContentModel log = new LogContentModel();
Date currentDate = new Date(System.currentTimeMillis()); //当前系统时间
try {
Date expiredDate = new Date(auction.getExpiredDate().getTime()); //拍卖截止时间
Date currentFinishDate = new Date(System.currentTimeMillis()); //当天下午拍卖结束时间
ConfigModel conf = ConfigBO.getConfig("TRD", "PMBidTimeEnd"); //下午交易结束时间
String finishTime= conf.getValue();
int hour = Integer.parseInt(finishTime.substring(0, 2));
int minutes = Integer.parseInt(finishTime.substring(2, 4));
int second = Integer.parseInt(finishTime.substring(4));
currentFinishDate.setHours(hour);
currentFinishDate.setMinutes(minutes);
currentFinishDate.setSeconds(second);
expiredDate.setHours(hour);
expiredDate.setMinutes(minutes);
expiredDate.setSeconds(second);
//拍卖截止时间
long lExpiredDate = expiredDate.getTime() + auction.AUCTION_DELAY_INTERVAL; //30s
if (currentDate.before(expiredDate)) continue; //还没到拍卖结束时间
//-------------解冻抵押金和产权已经有单独的操作界面,但是需要判断是流拍还是有人竟买,一此改拍卖状态-----------
// 2006-06-02 yhxie
ModelList orders = AuctionBO.getBidOrderList(auction);//取所有出价人(biddorder表)
long delaytmp = (86279999+auction.AUCTION_DELAY_INTERVAL)/1000;//一天,即24小时
if (orders == null || orders.size() == 0) { //没有出价人--流拍,修改状态为 A81
//start
//没有竞标纪录,解冻抵押金,解冻股票
//if (AuctionBO.rollgagy(auction) == -1) {
/*
//如果没有抵押金需要解冻则直接解冻股票
AuctionBO.unfreezeStock(auction);
*/
// }
//AuctionBO.unfreezeStock(auction); //新版本2006-05-09,从上面拿到该处
//end
auction.setStatus(auction.AUCTION_STATUS_FAILED); // A81-流拍(处理完成、解冻产权完成)
AuctionBO.updateAuction(auction, log);
//当天下午交易结束时间-拍卖截止时间>24小时,拍卖结束,有人竟标
} else if (((currentFinishDate.getTime()-lExpiredDate)/1000) > delaytmp) {
//如果最后期限超过1天,则拍卖结束
auction.setStatus(auction.AUCTION_STATUS_AUCTION_FINISHED); // A40-拍卖结束
AuctionBO.updateAuction(auction, log);
} else if (currentDate.after(currentFinishDate)){
//如果当前时间大于当天结束时间,根据最后竟标时间判断是否结束拍卖
long lCurrentDate = System.currentTimeMillis(); //当前系统时间
long lLastBidTime = 0;
BidOrderModel order = (BidOrderModel) orders.get(0); //最后一标出价时间
Date lastBidTime = new Date(0);
String sLastBidTime = order.getOrderTime();
lastBidTime.setYear(Integer.parseInt(sLastBidTime.substring(0,4))-1900);
lastBidTime.setMonth(Integer.parseInt(sLastBidTime.substring(5, 7))-1);
lastBidTime.setDate(Integer.parseInt(sLastBidTime.substring(8,10)));
lastBidTime.setHours(Integer.parseInt(sLastBidTime.substring(11,13)));
lastBidTime.setMinutes(Integer.parseInt(sLastBidTime.substring(14, 16)));
lastBidTime.setSeconds(Integer.parseInt(sLastBidTime.substring(17)));
lLastBidTime = lastBidTime.getTime();
//如果当前时间减最后一标的时间当大于50秒,则结束拍卖
//Debug.print("currentFinishDate.getTime()-lExpiredDate"+Long.toString(lCurrentDate-lLastBidTime));
//
if ((lCurrentDate-lLastBidTime)>auction.AUCTION_DELAY_INTERVAL) {
auction.setStatus(auction.AUCTION_STATUS_AUCTION_FINISHED); // A40-拍卖结束
AuctionBO.updateAuction(auction, log);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 3.处理已经结束的拍卖项目
* @param auctions
*/
private synchronized void ProcessFinished(ModelList auctions) {
if (auctions == null || auctions.size() <= 0) return;
for (int i = 0; i < auctions.size(); i++) {
AuctionModel auction = (AuctionModel) auctions.get(i);
if (auction.getAuctionNo()==null || auction.getAuctionNo().trim().equals("")) continue;
LogContentModel log = new LogContentModel();
Date currentDate = new Date(System.currentTimeMillis());
try {
//计算手续费
if (auction.getCharge()<=0) {
ChargesModel charge = null;
double bestBid = 0.0;
double dCharge = 0.0;
String auctionType = (StringUtil.addZero(auction.getAuctionType(), 2)).trim();
charge = ConfigBO.getCharge(auction.getRegCode(), auction.getStockType(), auctionType);
//权益拍卖的话,数量按1计算
long orderVolume = auction.getOrderVolume();
if (auction.getAuctionType().equals("3"))
orderVolume = 1;
bestBid = (auction.getBestPrice()*orderVolume);
dCharge = charge.calculateCharge(bestBid, orderVolume); //手续费等
auction.setCharge(dCharge);
AuctionBO.updateAuction(auction, log);
}
//处理余款未付情况
if (auction.getAllowPay().equals("N")) {
//start //没有投标人或者唯一投标人,解冻股票
// if (AuctionBO.rollgagy(auction) == -1) {
/*
//如果没有抵押金需要解冻则直接解冻股票
AuctionBO.unfreezeStock(auction);
*/
// }
// AuctionBO.unfreezeStock(auction);新版本2006-05-09,从上面拿到该处
///end
auction.setStatus(auction.AUCTION_STATUS_PAY_REMAIN_FAILED); // A71-余款支付失败
AuctionBO.updateAuction(auction, log);
}
else
{
SumModel sm = new SumModel();
FundBO fund =new FundBO();
try{
sm=fund.getSumCharges(auction.getAuctionNo(),"sell");
if(sm.equals("FAILED") || sm.equals(null)){
FundBO.saveCharge(auction,log);
}
}catch(Exception e){
FundBO.saveCharge(auction,log);
}
// if(fund.getSumCharges(auction.getAuctionNo(),"buy").equals(null) && fund.getSumCharges(auction.getAuctionNo(),"sell").equals(null))
// FundBO.saveCharge(auction,log);
}
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
}