package cs.mina.client.handler;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import cs.base.IBaseService;
import cs.entity.Admin;
import cs.entity.Announce;
import cs.entity.FreezeRecord;
import cs.entity.FundLog;
import cs.entity.Member;
import cs.entity.Order;
import cs.entity.Product;
import cs.entity.StockHold;
import cs.mina.ExecIDSet;
import cs.mina.MessageMap;
import cs.mina.SeqNumAndDatabaseIdMap;
import cs.mina.codec.msg.BaseMsg;
import cs.mina.codec.msg.BusinessMessageRejectMsg;
import cs.mina.codec.msg.ExecutionReportMsg;
import cs.mina.codec.msg.MsgType;
import cs.mina.codec.msg.NewOrderSingleMsg;
import cs.mina.codec.msg.OrderCancelReplaceRequestMsg;
import cs.service.IAdminService;
import cs.service.IAnnounceService;
import cs.service.IFreezeRecordService;
import cs.service.IFundLogService;
import cs.service.IMemberService;
import cs.service.IOrderService;
import cs.service.IProductService;
import cs.service.IStockHoldService;
import cs.util.ApplicationContextUtil;
import cs.util.Constant;
import csulog.CSULog;
/*
*@author(huangxiaoping)
*@date 2013-12-2
*/
public class BusinessMessageRejectMessageHandler implements
ClientMessageHandler {
private static final Logger logger=Logger.getLogger(BusinessMessageRejectMessageHandler.class);
private static final CSULog csuLog=CSULog.getInstance();
@Override
public void doHandler(IoSession session, BaseMsg message) {
DataSourceTransactionManager txManager=(DataSourceTransactionManager)ApplicationContextUtil.getApplicationContext().getBean("transactionManager");
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = txManager.getTransaction(def);
try {
BusinessMessageRejectMsg msg=(BusinessMessageRejectMsg)message;
csuLog.log_INFO("收到业务消息被拒消息[msgType=j],[refMsgType="+msg.getRefMsgType().getTagValue()+"],[refSeqNum="+msg.getRefSeqNum().getTagValue()+"],[reason="+msg.getBusinessRejectReason().getTagValue()+"]");
//获取service
IAnnounceService announceServiceImpl=(IAnnounceService)ApplicationContextUtil.getApplicationContext().getBean("announceServiceImpl");
IOrderService orderServiceImpl=(IOrderService)ApplicationContextUtil.getApplicationContext().getBean("orderServiceImpl");
IAdminService adminServiceImpl=(IAdminService)ApplicationContextUtil.getApplicationContext().getBean("adminServiceImpl");
IMemberService memberServiceImpl=(IMemberService)ApplicationContextUtil.getApplicationContext().getBean("memberServiceImpl");
IFundLogService fundLogServiceImpl=(IFundLogService)ApplicationContextUtil.getApplicationContext().getBean("fundLogServiceImpl");
IProductService productServiceImpl=(IProductService)ApplicationContextUtil.getApplicationContext().getBean("productServiceImpl");
IStockHoldService stockHoldServiceImpl=(IStockHoldService)ApplicationContextUtil.getApplicationContext().getBean("stockHoldServiceImpl");
IFreezeRecordService freezeRecordServiceImpl=(IFreezeRecordService)ApplicationContextUtil.getApplicationContext().getBean("freezeRecordServiceImpl");
String dbid=SeqNumAndDatabaseIdMap.get(msg.getRefSeqNum().getTagValue());
Order order=new Order();
order.setId(Integer.parseInt(dbid));
//BaseMsg baseMsg=MessageMap.getMessageBySeqNum(msg.getRefSeqNum().getTagValue());
order=((IBaseService<Order>)orderServiceImpl).get(order);
Member member=order.getMember();
Product product =productServiceImpl.findByCode(order.getProduct().getCode());
StockHold stockHold=stockHoldServiceImpl.getByProductIdAndUserId(product.getId(), member.getId());
if(msg.getRefMsgType().getTagValue().equals(MsgType.NEW_ORDER_SINGLE)){
//NewOrderSingleMsg orderMsg=(NewOrderSingleMsg)baseMsg;
//String side=orderMsg.getSide().getTagValue();
String transType=order.getTransType();
String side="";
if(transType.equals(Order.TRANSTYPE_BUY)){
side="1";
}else if(transType.equals(Order.TRANSTYPE_SELL)){
side="2";
}else{
csuLog.log_ERROR("BusinessMessageRejectMessageHandler逻辑transType错误[tt="+transType+"]");
}
order.setState(Constant.ORDER_STATUS_REJECT);
if(side.equals(Constant.FIX_SIDE_BUY+"")){
csuLog.log_INFO("修改前的用户总冻结资金和余额[lgid="+member.getLgid()+"],[orderId="+order.getId()+"],[freezeAmt="+member.getAmtFrozen()+"],[balanceAmt="+member.getBalance()+"]");
member.setBalance(member.getBalance()+order.getPrice()*order.getWaitTrans());
FreezeRecord freezeRecord=freezeRecordServiceImpl.getByOrderIdAndUserId(order.getId(), member.getId());
member.setAmtFrozen(member.getAmtFrozen()-freezeRecord.getAmt());
((IBaseService<Member>)memberServiceImpl).modify(member);//修改用户冻结资金
csuLog.log_INFO("修改后用户总冻结资金和余额[lgid="+member.getLgid()+"],[orderId="+order.getId()+"],[freezeAmt="+member.getAmtFrozen()+"],[balanceAmt="+member.getBalance()+"]");
((IBaseService<FreezeRecord>)freezeRecordServiceImpl).delete(freezeRecord);//删除冻结资金
csuLog.log_INFO("删除冻结资金记录[lgid="+freezeRecord.getUser().getLgid()+"],[orderId="+order.getId()+"],[id="+freezeRecord.getId()+"],[orderId="+freezeRecord.getOrder().getId()+"],[amt="+freezeRecord.getAmt()+"]");
//记录资金明细
FundLog fundLog=new FundLog();
fundLog.setAmount(order.getPrice()*order.getWaitTrans());
fundLog.setBalance(member.getBalance());
fundLog.setIp("127.0.0.1");
fundLog.setLaType(FundLog.LATYPE_TD_TAD);
fundLog.setLog("订单新增业务层被拒[id="+order.getId()+"]退回资金");
fundLog.setLpType(FundLog.LATYPE_TD_TDD);
fundLog.setMember(member);
fundLog.setTime(new Date());
((IBaseService<FundLog>)fundLogServiceImpl).add(fundLog);
csuLog.log_INFO("新增资金明细[lgid="+member.getLgid()+"],[orderId="+order.getId()+"],[amt="+fundLog.getAmount()+"],[balance="+fundLog.getBalance()+"]");
}else if(side.equals(Constant.FIX_SIDE_SELL+"")){
stockHold.setAmt(stockHold.getAmt()+order.getQuantity());
((IBaseService<StockHold>)stockHoldServiceImpl).modify(stockHold);
csuLog.log_INFO("修改持股变动[lgid="+member.getLgid()+"],[orderId="+order.getId()+"],[product="+order.getProduct().getName()+"],[amt="+order.getQuantity()+"],[total="+stockHold.getAmt()+"]");
}else{
csuLog.log_ERROR("BusinessMessageRejectMessageHandler逻辑side错误[side="+side+"]");
}
}else if(msg.getRefMsgType().getTagValue().equals(MsgType.ORDER_CANCEL_REQUEST)){
order.setState(order.getLastState());
}else if(msg.getRefMsgType().getTagValue().equals(MsgType.ORDER_CANCEL_REPLACE_REQUEST)){
//OrderCancelReplaceRequestMsg crMsg=(OrderCancelReplaceRequestMsg)baseMsg;
order.setState(order.getLastState());
order.setPrice(order.getLastPrice());
order.setQuantity(order.getQuantity()+order.getLastAmt()-order.getWaitTrans());
order.setWaitTrans(order.getLastAmt());
//String side=crMsg.getSide().getTagValue();
String transType=order.getTransType();
String side="";
if(transType.equals(Order.TRANSTYPE_BUY)){
side="1";
}else if(transType.equals(Order.TRANSTYPE_SELL)){
side="2";
}else{
csuLog.log_ERROR("BusinessMessageRejectMessageHandler逻辑transType错误[tt="+transType+"]");
}
if(side.equals(Constant.FIX_SIDE_BUY+"")){
csuLog.log_INFO("修改前的用户总冻结资金和余额[lgid="+member.getLgid()+"],[orderId="+order.getId()+"],[freezeAmt="+member.getAmtFrozen()+"],[balanceAmt="+member.getBalance()+"]");
double money=order.getPrice()*order.getWaitTrans()-order.getLastAmt()*order.getLastPrice();
member.setBalance(member.getBalance()+money);
FreezeRecord freezeRecord=freezeRecordServiceImpl.getByOrderIdAndUserId(order.getId(), member.getId());
member.setAmtFrozen(member.getAmtFrozen()-money);
((IBaseService<Member>)memberServiceImpl).modify(member);//修改用户冻结资金
csuLog.log_INFO("修改后用户总冻结资金和余额[lgid="+member.getLgid()+"],[orderId="+order.getId()+"],[freezeAmt="+member.getAmtFrozen()+"],[balanceAmt="+member.getBalance()+"]");
freezeRecord.setAmt(freezeRecord.getAmt()-money);
((IBaseService<FreezeRecord>)freezeRecordServiceImpl).modify(freezeRecord);
csuLog.log_INFO("修改资金冻结[lgid="+member.getLgid()+"],[orderId="+order.getId()+"],[amt="+(-money)+"],[leaveTotal="+freezeRecord.getAmt()+"]");
//记录资金明细
FundLog fundLog=new FundLog();
fundLog.setAmount(money);
fundLog.setBalance(member.getBalance());
fundLog.setIp("127.0.0.1");
fundLog.setLaType(FundLog.LATYPE_TD_TAD);
fundLog.setLog("订单修改业务层被拒[id="+order.getId()+"]回退到前一个状态");
fundLog.setLpType(FundLog.LATYPE_TD_TDD);
fundLog.setMember(member);
fundLog.setTime(new Date());
((IBaseService<FundLog>)fundLogServiceImpl).add(fundLog);
csuLog.log_INFO("新增资金明细[lgid="+member.getLgid()+"],[orderId="+order.getId()+"],[amt="+fundLog.getAmount()+"],[balance="+fundLog.getBalance()+"]");
}else if(side.equals(Constant.FIX_SIDE_SELL+"")){
stockHold.setAmt(stockHold.getAmt()+(order.getWaitTrans()-order.getLastAmt()));
((IBaseService<StockHold>)stockHoldServiceImpl).modify(stockHold);
csuLog.log_INFO("修改持股变动[lgid="+member.getLgid()+"],[orderId="+order.getId()+"],[product="+order.getProduct().getName()+"],[amt="+(order.getWaitTrans()-order.getLastAmt())+"],[total="+stockHold.getAmt()+"]");
}else{
csuLog.log_ERROR("BusinessMessageRejectMessageHandler逻辑side错误[side="+side+"]");
}
}else{
csuLog.log_ERROR("不支持的消息原类型");
}
((IBaseService<Order>)orderServiceImpl).modify(order);
Announce announce=new Announce();
announce.setBeginTime(new Date());
announce.setUpdateTime(new Date());
Admin admin=new Admin();
admin.setLgid("admin");
admin=adminServiceImpl.getAdminByLgid(admin);
announce.setPromulgator(admin);
announce.setMember(order.getMember());
announce.setAlert(Announce.ALERT_VIEW);
announce.setTitle("订单被拒");
switch(Integer.parseInt(msg.getBusinessRejectReason().getTagValue())){
case 0:
announce.setContent("原因:其他(Other)");
break;
case 1:
announce.setContent("原因:未知id(Unknown ID)");
break;
case 2:
announce.setContent("原因:未知证券(Unknown Security)");
break;
case 3:
announce.setContent("原因:不支持的消息类型(Unsupported Message Type)");
break;
case 4:
announce.setContent("原因:应用程序不可用(Application not available)");
break;
case 5:
announce.setContent("原因:条件必须字段丢失(Conditionally required field missing)");
break;
case 6:
announce.setContent("原因:没有授权(Not Authorized)");
break;
case 7:
announce.setContent("原因:分发到公司当前时间不可用(DeliverTo firm not available at this time)");
break;
case 18:
announce.setContent("原因:无效的价格增长(Invalid price increment)");
break;
default:
announce.setContent("原因:其他(Other)");
break;
}
Calendar ca=new GregorianCalendar();
ca.setTime(new Date());
ca.add(GregorianCalendar.MONTH, 1);
announce.setEndTime(ca.getTime());//结束时间为发布起一个月
announce.setDestType(Announce.DEST_TYPE_MEMBER);
announceServiceImpl.addAnnounce(announce);
csuLog.log_INFO("发送用户通知[lgid="+order.getMember().getLgid()+"],[orderId="+order.getId()+"],[title="+announce.getTitle()+"],[content="+announce.getContent()+"]");
txManager.commit(status);
} catch (Exception e) {
txManager.rollback(status);
DbFail fail=new DbFail();
fail.setMsg(message);
DbFailModule.getInstance().addFail(fail);
csuLog.log_ERROR("执行报告发生异常,回滚事务,将执行报告放入错误重新执行线程");
logger.error("执行数据库错误", e);
}
}
}
fix协议封装挑战-BusinessMessageRejectMessageHandler
最新推荐文章于 2022-05-19 22:34:18 发布