a1

mail@pastecode.io avatar
unknown
java
a year ago
30 kB
3
Indexable
Never
package ScanCA;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import jdbc.JDBCPool;

import mq.QManager;
import mqaccess.MQAccess;

import org.springframework.jdbc.support.rowset.SqlRowSet;

import uti.Encrypts;
import uti.Func;
import uti.Para;
import uti.SIBSRLA_NEW;
import uti.SIBS_NEW;
import uti.SendEmail;

import com.ibm.broker.javacompute.MbJavaComputeNode;
import com.ibm.broker.plugin.MbElement;
import com.ibm.broker.plugin.MbException;
import com.ibm.broker.plugin.MbMessage;
import com.ibm.broker.plugin.MbMessageAssembly;
import com.ibm.broker.plugin.MbOutputTerminal;
import com.ibm.broker.plugin.MbXMLNSC;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;

import dto.TMTranDTO;

public class New1_ScanCA_JavaCompute extends MbJavaComputeNode {	
	// luu giao dich tmtran
	public static List<TMTranDTO>[] collectTMTransArr = null;
//	public static List<TMTranDTO> collectTMTrans= new Vector<TMTranDTO>();
	public static boolean RequestStopAll = false;//<MSG><HDR><TABLE>STOP</TABLE></HDR></MSG>
	private static Map<String, Boolean> isMaskAcctNum;
	public static int threadNum = 1;
	public static boolean isArrayInitialized = false;
	public static Object arrayInitializedLock = new Object();
	public static boolean logSMSEnable = false;
	
	//thulv hotfix
	MQAccess mqaccess = new MQAccess();
	MQQueueManager mqmng = mqaccess.accessQMgr(Para.QueueManager);
	MQQueue mqIris = mqaccess.accessMQueue(mqmng, Para.QueueAutoMsgIRIS);
	MQQueue mqVietel = mqaccess.accessMQueue(mqmng, Para.QueueAutoMsgVietel);
	MQQueue mqVnpt = mqaccess.accessMQueue(mqmng, Para.QueueAutoMsgVNPT);
	MQQueue mq = mqaccess.accessMQueue(mqmng, Para.QueueAUTOMSGLOG);
	MQMessage msgQ = new MQMessage();
	MQPutMessageOptions pmo = new MQPutMessageOptions();
	
	
	public void onInitialize(){
		Func.log(Para.fullPathLog + ".ca",Func.getTime() + " Flow_CA is started!");
		try{
			isMaskAcctNum = new HashMap<String, Boolean>();
			isMaskAcctNum.put("9", false);
			isMaskAcctNum.put("10", false);
			String query = "select id, para2 from services where id in (9,10)";
			JDBCPool.init();
			SqlRowSet rs = JDBCPool.jdbcTemplate.queryForRowSet(query);
			while (rs.next()) {
				String serviceID = rs.getString("id");
				String para2 = rs.getString("para2");
				boolean isMask = !(" ".equals(para2) || "0".equals(para2) || para2 == null);
				isMaskAcctNum.put(serviceID, isMask);
			}
			
			try{
				threadNum = Integer.parseInt(getUserDefinedAttribute("THREAD_GOP_TIN").toString());
			}catch (Exception e) {
			}
			
			collectTMTransArr = new List[threadNum];
			for (int i = 0; i < collectTMTransArr.length; i++) {
//				ArrayList<TMTranDTO> arrayList = new ArrayList<>();
//				collectTMTransArr[i] = Collections.synchronizedList(arrayList); 
				collectTMTransArr[i] = new Vector<TMTranDTO>(); 
			}
			
			
			try{
				SIBSRLA_NEW.MAX_CONN_POOL_SIZE = Integer.parseInt(getUserDefinedAttribute("CONN_POOL_SIZE").toString());
			}catch (Exception e) {
			}
			
			try{
				SIBSRLA_NEW.MAX_CONN_LIFE_TIME_IN_MINUTES = Long.parseLong(getUserDefinedAttribute("CONN_LIFE_MINUTES").toString());
			}catch (Exception e) {
			}
			
			try{
				SIBSRLA_NEW.SIBS_HOST = getUserDefinedAttribute("SIBS_HOST").toString();
			}catch (Exception e) {
			}
			
			try{
				SIBSRLA_NEW.SIBS_USER = getUserDefinedAttribute("SIBS_USER").toString();
			}catch (Exception e) {
			}
			
			try{
				SIBSRLA_NEW.SIBS_PASSWORD = getUserDefinedAttribute("SIBS_PASSWORD").toString();
			}catch (Exception e) {
			}
			
			try{
				String LOG_SMS_ENABLE = getUserDefinedAttribute("LOG_SMS_ENABLE").toString();
				if ("1".equals(LOG_SMS_ENABLE)){
					logSMSEnable = true;
				}
			}catch (Exception e) {
			}
			
		} finally{
			synchronized (New1_ScanCA_JavaCompute.arrayInitializedLock) {
				isArrayInitialized = true;
				arrayInitializedLock.notifyAll();
				Func.log(Para.fullPathLog + ".ca"," Flow_CA Initialized!");
			}
		}
		
	}
	public void evaluate(MbMessageAssembly assembly) throws MbException {
		//long d = System.currentTimeMillis();

		try {
			//Func.log(Para.fullPathLog + ".ca","begin:");
			MbOutputTerminal out = getOutputTerminal("out");
			MbOutputTerminal alt = getOutputTerminal("alternate");
			
			MbElement rootElement= assembly.getMessage().getRootElement();
			/* process */
			String sOutMessage = null;
			String sTable = getHeader(rootElement, "TABLE");
			if (sTable == null) return;
			if (sTable.toUpperCase().indexOf("TMTR24") > -1){
				// du lieu tu TMTR24 (DU LIEU NIGHTMODE)
				processTMTran(rootElement, assembly, alt, "1");
				return;
			} else if (sTable.toUpperCase().indexOf("TMTRAN") > -1){
				// du lieu tu TMTRAN
				processTMTran(rootElement, assembly, alt, "0");
				return;
			} else if (sTable.toUpperCase().indexOf("DDTRN2") > -1){
				// du lieu tu DDTRN2 (các giao dịch tự động kiểu đổ lương)
				sOutMessage = processDDTRN2(rootElement, assembly, alt);
			} else if (sTable.toUpperCase().indexOf("STOP") > -1){
				// request stop tien trinh gop tin nhan
				RequestStopAll = true;
				Func.log(Para.fullPathLog + ".ca","request Stop flow!");
				return;
			} else if (sTable.toUpperCase().indexOf("XTABLEX") > -1){
				// tao tin nhan tu bang tam gop tin nhan
				sOutMessage = CreateMessageFromXTABLEX(rootElement, assembly, alt);
			}
			
			if (sOutMessage == null) return;
			sOutMessage = Func.TrimMessage(sOutMessage);
			String[] sOutArr = sOutMessage.split("\\~");
			/* end*/
//			long ttt = System.currentTimeMillis();
			
			//thulv hot fix tach queue
			try {
				for(String outMsg : sOutArr){
//					if(outMsg.contains(Para.portVietel)){
//						
//						outMsg="<MSG>"+outMsg+"</MSG>";
//						msgQ.clearMessage();
//						msgQ.writeString(outMsg);
//						mqVietel.put(msgQ, pmo);
//						mq.put(msgQ, pmo);
//		                
//					}else if(outMsg.contains(Para.portVNPT)){
//		
//						outMsg="<MSG>"+outMsg+"</MSG>";
//						msgQ.clearMessage();
//						msgQ.writeString(outMsg);
//						mqVnpt.put(msgQ, pmo);
//						mq.put(msgQ, pmo);
//
//		                
//					}else 
						if(outMsg.contains(Para.portIRIS)){
						outMsg="<MSG>"+outMsg+"</MSG>";
						msgQ.clearMessage();
						msgQ.writeString(outMsg);
						mqIris.put(msgQ, pmo);
						mq.put(msgQ, pmo);

					}else{
						MbMessage outMessage = new MbMessage();
						MbMessageAssembly outAssembly = new MbMessageAssembly(assembly, outMessage);
						MbElement outRoot = outMessage.getRootElement().createElementAsLastChild("XMLNSC");
						outRoot.createElementAsLastChild(MbElement.TYPE_NAME, "MSG", outMsg);		
						//outMessage.finalizeMessage(MbMessage.FINALIZE_NONE);
						if (outAssembly != null){
							out.propagate(outAssembly);
							//outAssembly.getMessage().clearMessage();
						}
					}
				}
				
                
            } catch (Exception e) {
                Func.log(Para.fullPathLog + ".ca2", "Khong day duoc msg ra Queue:" + e.toString());
            }
			
//			Func.log(Para.fullPathLog + ".ca","_timePropagate=" + (System.currentTimeMillis() - ttt));
		} catch (Exception e) {

			Func.log(Para.fullPathLog + ".ca","evaluate_error=" + e.toString());
		} finally {
			//Func.log(Para.fullPathLog + ".ca","_evaluate=" + (System.currentTimeMillis() - d));
		}
	}
	
	
	// tao tin nhan tu bang XTABLEX: da gop tin nhan
	private String CreateMessageFromXTABLEX(MbElement rootElement,
			MbMessageAssembly assembly, MbOutputTerminal alt) {
//		JDBCPool.jdbcTemplate.execute("insert into CA2 values (sysdate)");
//		long tAll = System.currentTimeMillis();
		
		List<String> columns = new Vector<String>();
		List<String> values = new Vector<String>();
		
		String sStat = getData(rootElement, "TMTXSTAT");
		if (sStat != null) sStat = sStat.trim();
		String sAccNo = getData(rootElement, "TMACCTNO");
		String sAmt = getData(rootElement, "TMTXAMT");
		String sCurr = getData(rootElement, "TMGLCUR");
		if (sCurr != null) sCurr = sCurr.trim();
		else sCurr = "";
		String sDorc = getData(rootElement, "TMDORC");
		if (sDorc != null) sDorc = sDorc.trim();
		else sDorc = "";
		String sDate = getData(rootElement, "TMENTDT7");
		String sTime = getData(rootElement, "TMTIMENT");
		String sTmapptype = getData(rootElement, "TMAPPTYPE");
		
		String sTMTXCD= getData(rootElement, "TMTXCD");
		String sEFTH = getData(rootElement, "TMEFTH");

		String sHasFee= getData(rootElement, "HASFEE");
		String sFee= getData(rootElement, "FEE");
		String sFeeDorc= getData(rootElement, "FEEDORC");
		
		columns.add("TMTXSTAT");
		columns.add("TMACCTNO");
		columns.add("TMTXAMT");
		columns.add("TMGLCUR");
		
		columns.add("TMDORC");
		columns.add("TMENTDT7");
		columns.add("TMTIMENT");
		columns.add("TMAPPTYPE");
		
		columns.add("TMEFTH");

		columns.add("HASFEE");
		columns.add("FEE");
		columns.add("FEEDORC");

		columns.add("TMTXCD");
		
		values.add(sStat);
		values.add(sAccNo);
		values.add(sAmt);
		values.add(sCurr);
		
		values.add(sDorc);
		values.add(sDate);
		values.add(sTime);
		values.add(sTmapptype);
		
		values.add(sEFTH);
		if (sEFTH != null) sEFTH = sEFTH.trim();
		else sEFTH = "";
		
		values.add(sHasFee);
		values.add(sFee);
		values.add(sFeeDorc);
		
		values.add(sTMTXCD);
		
		String cbal = null;
		String sService = "9";		
		StringBuilder sMsgBody = new StringBuilder();
		String sChange = "";
		String sGomPhiGD = "";
		String sDorcForLimit = "";
		
//		Func.log(Para.fullPathLog + ".ca",sAccNo + "|" + sAmt + "_timeSetdata=" + (System.currentTimeMillis() - tAll));
//		long ttt1 = System.currentTimeMillis();
		
		if ("1".equals(sHasFee)){
			BigDecimal dAmt1= null;
			BigDecimal dAmt2= null;
			
			if (sStat.equals("CE")) {
				// Giao dịch đảo
				dAmt1= new BigDecimal((sDorc.equals("C")? "-" : "") + sAmt);
				dAmt2= new BigDecimal((sFeeDorc.equals("C")? "-" : "") + sFee);
			} else {
				dAmt1= new BigDecimal((sDorc.equals("C")? "" : "-") + sAmt);
				dAmt2= new BigDecimal((sFeeDorc.equals("C")? "" : "-") + sFee);
			}
			dAmt1 = dAmt1.add(dAmt2);
			
			sAmt = String.valueOf(dAmt1);
			if (dAmt1.compareTo(BigDecimal.ZERO)>0){
				sChange = "+";
				sService = "9";
				sDorcForLimit = "C";
			} else {
				sChange = "";
				sService = "10";
				sDorcForLimit = "D";
			}
			
			
			//sGomPhiGD = "(gom phi GD)";
		} else {
			if (sStat.equals("CE")) {
				if (sDorc.equals("C"))
					sChange = "-";
				else
					sChange = "+";
			} else {
				if (sDorc.equals("C"))
					sChange = "+";
				else
					sChange = "-";
			}
			
			if (sDorc.equals("C"))
				sService = "9";
		    else
		    	sService = "10";
			
			sDorcForLimit = sDorc;
		}
//		Func.log(Para.fullPathLog + ".ca",sAccNo + "|" + sAmt + "_timeBeforeCbal=" + (System.currentTimeMillis() - ttt1));
		
		
		//get cbal:
		long d1 = System.currentTimeMillis();
		try {
			int curTime = Func.getNumberTime();
			if (Func.SWITCH_START <= curTime || curTime <=  Func.SWITCH_END){
				// get cbal by jdbc 
				cbal = SIBS_NEW.getCbal(sAccNo);
			} else {
				// get cbal by record level access
				cbal = SIBSRLA_NEW.getCbal(sAccNo);
				if (cbal == null){
					cbal = SIBSRLA_NEW.getCbal(sAccNo);
				}
				if (cbal == null){
					if (!sDate.equals(Func.getJulianDate())){
						cbal = SIBSRLA_NEW.getCbal2(sAccNo);
					} else {
						Func.logx(Para.fullPathLog + ".ca2","_tmtran: ko phat sinh trong ddmemo!");
						cbal = null;
					}
				}
			}
		} catch (Exception e) {
			cbal = null;
		}
		long dcbal = (System.currentTimeMillis() - d1);
		if (dcbal > 100){
			Func.logx(Para.fullPathLog + ".ca2","_cbal=" + dcbal);
		}
//		ttt1 = System.currentTimeMillis();
		sMsgBody.append("TK");
		if (isMaskAcctNum.get(sService)){
			sMsgBody.append(sAccNo.substring(0, 3) + "xxx" + sAccNo.substring(sAccNo.length() - 4, sAccNo.length()));
		}else{
			sMsgBody.append(sAccNo);
		}
		sMsgBody.append(" tai BIDV "); 
		sMsgBody.append(sChange);
		sMsgBody.append(Func.formatCurrency(sAmt, sCurr));
		sMsgBody.append(sCurr);
		sMsgBody.append(sGomPhiGD);
		sMsgBody.append(" vao ");
		sMsgBody.append(Func.formatForSMS(sTime, sDate));
		
		if (cbal != null){
			sMsgBody.append(". So du:");
			sMsgBody.append(Func.formatCurrency(cbal, sCurr));
			sMsgBody.append(sCurr);
		} else {
			Func.log(Para.fullPathLog + ".error","_tmtran_" + "ERROR: Khong lay duoc so du cua giao dich: " + sMsgBody);
			SendEmail.email("ERROR: Khong lay duoc so du cua giao dich: " + sMsgBody);
		}
		
		if (sEFTH != null && !sEFTH.equals("")){
			sMsgBody.append(". ND: ");
			sMsgBody.append(sEFTH);
		}
		
		//test
		if ("1".equals(sHasFee)){
			Func.logx(Para.fullPathLog + ".ca2",sService + "______co phi____:" + sMsgBody);
		} else {
			Func.logx(Para.fullPathLog + ".ca2",sService + "________________:" + sMsgBody);
		}
		
		int iServiceID = Integer.parseInt(sService);
		long d2 = System.currentTimeMillis();
		if (logSMSEnable = true){
			Func.logx(Para.fullPathLog + ".ca2","SIBS_HOST:" + SIBSRLA_NEW.SIBS_HOST + 
					"|SIBS_USER:" + SIBSRLA_NEW.SIBS_USER + 
					"|TMACCTNO:"+ sAccNo + 
					"|TMACCTNO:"+ sAccNo + 
					"|TMENTDT7:" + sDate +
					"|TMTIMENT:" + sTime +
					"|TMTXAMT:" + sAmt + 
					"|SMS:" + sMsgBody);
		}

		//String sOutMessage =  JDBCPool.createMessage(sMsgBody, iServiceID, null, sAccNo, null);
		//List<String> result = JDBCPool.createMsgCA_TMTran_New(sAccNo, sAmt, sMsgBody.toString(), sTmapptype,sDorcForLimit, iServiceID);
		List<String> result = JDBCPool.createMsgCA_TMTran_New_V2(
				sAccNo, sAmt, sMsgBody.toString(), 
				sTmapptype,sDorcForLimit, iServiceID, sDate,
				sCurr);
		long d2x = System.currentTimeMillis() - d2;
		if (d2x > 5000){
			Func.logx(Para.fullPathLog + ".ca2",sAccNo + "|" + sAmt + "_ora=" + d2x);
		}
//		ttt1 = System.currentTimeMillis();
		String sOutMessage = null;
		String resultCode = null;
		if (result != null){
			resultCode = result.get(0);
			sOutMessage = result.get(1);
		}
		if (resultCode.equals("3")){
			Func.logx(Para.fullPathLog + ".ca2", sAccNo + "|" + sAmt + "_tmtran:Gd chua du han muc!");
			return null;
		}
		if (sOutMessage == null ){			
			try {
				SentToQueue(assembly, alt, "XTABLEX", "", columns, values);
			} catch (Exception e) {
				Func.logx(Para.fullPathLog + ".ca2","Khong day duoc msg ra QueueCDCCATRANSLOG:" + e.toString());
			}
			return null;
		} else {
			if (cbal==null) {
				Func.log(Para.fullPathLog + "_error.sodu","_tmtran_" + "ERROR: Khong lay duoc so du cua giao dich: " + sOutMessage);
			}
		}
//		Func.log(Para.fullPathLog + ".ca",sAccNo + "|" + sAmt + "_timeAFterOra=" + (System.currentTimeMillis() - ttt1));

//		Func.log(Para.fullPathLog + ".ca",sAccNo + "|" + sAmt + "_timeScanCa=" + (System.currentTimeMillis() - tAll));
		return sOutMessage;
	}

	public void onDelete(){
//		SIBSRLA.closeConnection();
		SIBSRLA_NEW.shutdownAS400ConnectionPool();
		SIBS_NEW.shutdownAS400JDBCConnectionPool();
		JDBCPool.destroy();
		Func.log(Para.fullPathLog + ".ca"," Flow_CA has been stopped!");
	}
	
	/*
	 * 0: ok ko phai lam gi tiep; 1: dua message vao queue backup;
	 */
	private int processTMTran(MbElement rootElement, MbMessageAssembly assembly, MbOutputTerminal alt, String isNightMode) {
		List<String> columns = new Vector<String>();
		List<String> values = new Vector<String>();		
		
		String sAct = getHeader(rootElement, "ACT");
		
		String sStat = getData(rootElement, "TMTXSTAT");			
		String sAccNo = getData(rootElement, "TMACCTNO");
		String sAmt = getData(rootElement, "TMTXAMT");
		String sCurr = getData(rootElement, "TMGLCUR");
		
		String sDorc = getData(rootElement, "TMDORC");
		String sDate = getData(rootElement, "TMENTDT7");
		String sTime = getData(rootElement, "TMTIMENT");
		String sSeq = getData(rootElement, "TMTXSEQ");
		
		String sTellID = getData(rootElement, "TMTELLID");
		String sBrn = getData(rootElement, "TMSRVBRN");
		String sTmsseq = getData(rootElement, "TMSSEQ");
		String sTmapptype = getData(rootElement, "TMAPPTYPE");
		
		String sTMTXCD= getData(rootElement, "TMTXCD");
		
		String sEFTH = Func.getRemark(getData(rootElement, "TMEFTH"),sTMTXCD);
		String sTMTLMNE= getData(rootElement, "TMTLXMNE");
		String sTMHOSTTXCD= getData(rootElement, "TMHOSTTXCD");
		String sTMHAFFT= getData(rootElement, "TMHAFFT");
		
		
		
		columns.add("TMTXSTAT");
		columns.add("TMACCTNO");
		columns.add("TMTXAMT");
		columns.add("TMGLCUR");
		
		columns.add("TMDORC");
		columns.add("TMENTDT7");
		columns.add("TMTIMENT");		
		columns.add("TMTXSEQ");
		
		columns.add("TMTELLID");
		columns.add("TMSRVBRN");
		columns.add("TMSSEQ");
		columns.add("TMAPPTYPE");
		
		columns.add("TMEFTH");
		columns.add("TMTLXMNE");
		columns.add("TMHOSTTXCD");
		columns.add("TMHAFFT");

		columns.add("TMTXCD");
		
		values.add(sStat);
		values.add(sAccNo);
		values.add(sAmt);
		values.add(sCurr);
		
		values.add(sDorc);
		values.add(sDate);
		values.add(sTime);
		values.add(sSeq);
		
		values.add(sTellID);
		values.add(sBrn);
		values.add(sTmsseq);
		values.add(sTmapptype);
		
		values.add(sEFTH);
		values.add(sTMTLMNE);
		values.add(sTMHOSTTXCD);
		values.add(sTMHAFFT);
		
		values.add(sTMTXCD);
		
		if (!validateTMTran(sStat, sAccNo, sAmt, sCurr, sDorc, sDate, sTime, sSeq,
				sTellID, sBrn, sTmsseq, sEFTH)){
			Func.log(Para.fullPathLog + ".ca","tmtran:validate: fail!");
			return 0;
		}
		
		//long d2 = System.currentTimeMillis();
		Func.log(Para.fullPathLog + ".ca","_tmtran=" + sStat + "#" +sAccNo + "#" +sAmt + "#" +sCurr + "#" +sDorc + "#" +sDate + "#" +sTime + "#" +sSeq + "#" +sTellID + "#" +sBrn + "#" +sTmsseq + "#" +sTmapptype + "#" +sEFTH + "#" +sTMTLMNE + "#" +sTMHOSTTXCD + "#" +sTMHAFFT + "#" +sTMTXCD);
		List<String> result = JDBCPool.VerifyMsgCA_TMTran(sAct, sStat, sAccNo, sAmt, sCurr,
				sDorc, sDate, sTime, sSeq, sTellID, sBrn, sTmsseq,  sEFTH, sTmapptype, sTMTLMNE, sTMHOSTTXCD, sTMHAFFT,
				sTMTXCD, isNightMode);
		//long d2x = System.currentTimeMillis() - d2;
		//Func.log(Para.fullPathLog + ".ca","_ora=" + d2x);
		
		//String sOutMessage = null;
		String resultCode = null;
		if (result != null){
			resultCode = result.get(0);
			//sOutMessage = result.get(1);
		}

		if (result == null || resultCode == null){			
			try {
				Func.log(Para.fullPathLog + ".error","Loi khi goi procedure:" + result + resultCode + "_tmtran=" + sStat + "#" +sAccNo + "#" +sAmt + "#" +sCurr + "#" +sDorc + "#" +sDate + "#" +sTime + "#" +sSeq + "#" +sTellID + "#" +sBrn + "#" +sTmsseq + "#" +sTmapptype + "#" +sEFTH + "#" +sTMTLMNE + "#" +sTMHOSTTXCD + "#" +sTMHAFFT + "#" +sTMTXCD);
				SentToQueue(assembly, alt, "TMTRAN",sAct, columns, values);
			} catch (Exception e) {
				Func.log(Para.fullPathLog + ".ca","Khong day duoc msg ra QueueCDCCATRANSLOG:" + e.toString());
			}
			
			Func.log(Para.fullPathLog + ".ca","_tmtran has error, check stderr:Tai khoan " + sAccNo + "|" + sDorc + "|" +resultCode);
			return 0;
		}
		
		Func.log(Para.fullPathLog + ".ca","_tmtran resultCode=" + result.get(0));
		
		if (result.get(0).equals("1")){
			Func.log(Para.fullPathLog + ".ca",sAccNo + "|" + sDorc +"_tmtran: chua dkdv!");
			return 0;
		}
		
		if (result.get(0).equals("2")){
			Func.log(Para.fullPathLog + ".ca",sAccNo + "|" + sDorc +"_tmtran:Gd ton tai!");
			return 0;
		}
		
		if (result.get(0).equals("3")){
			Func.log(Para.fullPathLog + ".ca",sAccNo + "|" + sAmt +"_tmtran:Gd chua du han muc!");
			return 0;
		}
		
		if (result.get(0).equals("4")){
			Func.log(Para.fullPathLog + ".ca",sAccNo + "|" + sTellID +"_tmtran:Gd duoc khai bao ngat!");
			return 0;
		}
		
		if (!New1_ScanCA_JavaCompute.RequestStopAll){
			TMTranDTO dto = new TMTranDTO(sAct, sStat, sAccNo, sAmt, sCurr,
				sDorc, sDate, sTime, sSeq, sTellID, sBrn, sTmsseq,  sEFTH, sTmapptype, sTMTLMNE, sTMHOSTTXCD, sTMHAFFT,
				sTMTXCD, isNightMode);
			// kiem tra neu trong bang tam co qua nhieu gd thi phai dung lai, tranh tran bo nho
//			if (collectTMTrans.size() >= TMTranDTO.MAX_TRANS){
			if (isFullTransArray()){
				RequestStopAll = true;
//				Func.log(Para.fullPathLog + ".error", "Loi collectTMTrans.size() >= TMTranDTO.MAX_TRANS:" + String.valueOf(collectTMTrans.size()));
				Func.log(Para.fullPathLog + ".error", "Loi collectTMTrans.size() >= TMTranDTO.MAX_TRANS");
				SentToQueue(assembly, alt, "TMTRAN",sAct, columns, values);
				return 1;//dua vao queue tam
			}
//			them giao dich vao bang
//			collectTMTrans.add(dto);
			long t = System.currentTimeMillis();
			String hash = Encrypts.sha256(dto.getPartitionKey() + "BSMS");
//			Func.log(Para.fullPathLog + ".ca", "_time hash:" + (System.currentTimeMillis() - t));
			t = System.currentTimeMillis();
			int mod = Math.abs(hash.hashCode()) % threadNum;
//			Func.log(Para.fullPathLog + ".ca", "_time mod:" + (System.currentTimeMillis() - t));
			t = System.currentTimeMillis();
			collectTMTransArr[mod].add(dto);
//			Func.log(Para.fullPathLog + ".ca", "_time add:" + (System.currentTimeMillis() - t));
		} else {
			Func.log(Para.fullPathLog + ".error", "RequestStopAll=true! HAY STOP, START LAI ScanCA_1");
			SentToQueue(assembly, alt, "TMTRAN",sAct, columns, values);
			return 1;//dua vao queue tam
		}
		
		return 0;
	}
	
	private boolean isFullTransArray(){
		for (int i = 0; i < collectTMTransArr.length; i++){
			if (collectTMTransArr[i] != null && collectTMTransArr[i].size() > TMTranDTO.MAX_TRANS){
				return true;
			}
		}
		return false;
	}
	
	private String processDDTRN2(MbElement rootElement, MbMessageAssembly assembly, MbOutputTerminal alt) {
		List<String> columns = new Vector<String>();
		List<String> values = new Vector<String>();	
		
		String sAct = getHeader(rootElement, "ACT");
		
		String sTRSTAT = getData(rootElement, "TRSTAT");
		String sBATCH = getData(rootElement, "BATCH");
		String sSEQ = getData(rootElement, "SEQ");
		String sTRACCT = getData(rootElement, "TRACCT");
		
		String sTRATYP = getData(rootElement, "TRATYP");
		String sTRBR = getData(rootElement, "TRBR");	
		String sDORC = getData(rootElement, "DORC");
		String sTRANCD = getData(rootElement, "TRANCD");
		
		String sAMT = getData(rootElement, "AMT");
		String sTRCTYP = getData(rootElement, "TRCTYP");		
		String sTRDATE = getData(rootElement, "TRDATE");
		String sTRTIME = getData(rootElement, "TRTIME");
		
		String sTRUSER = getData(rootElement, "TRUSER");		
		String sEFTACC = getData(rootElement, "EFTACC");
		String sTREFTH = Func.getRemark(getData(rootElement, "TREFTH"),sTRANCD);
		String sTRJOBN = getData(rootElement, "TRJOBN");

		columns.add("TRSTAT");
		columns.add("BATCH");
		columns.add("SEQ");
		columns.add("TRACCT");
		
		columns.add("TRATYP");
		columns.add("TRBR");
		columns.add("DORC");
		columns.add("TRANCD");
		
		columns.add("AMT");
		columns.add("TRCTYP");
		columns.add("TRDATE");
		columns.add("TRTIME");
		
		columns.add("TRUSER");
		columns.add("EFTACC");
		columns.add("TREFTH");
		columns.add("TRJOBN");
		
		values.add(sTRSTAT);
		values.add(sBATCH);
		values.add(sSEQ);
		values.add(sTRACCT);
		
		values.add(sTRATYP);
		values.add(sTRBR);
		values.add(sDORC);
		values.add(sTRANCD);
		
		values.add(sAMT);
		values.add(sTRCTYP);
		values.add(sTRDATE);
		values.add(sTRTIME);
		
		values.add(sTRUSER);
		values.add(sEFTACC);
		values.add(sTREFTH);
		values.add(sTRJOBN);
		
		if (!validateDDTrn2(sTRSTAT, sBATCH, sSEQ, sTRACCT, sTRATYP, sTRBR, sDORC, sTRANCD,
				sAMT, sTRCTYP, sTRDATE, sTRTIME, sTRJOBN)){
			Func.log(Para.fullPathLog + ".ca","ddtrn2:validate: fail!");
			return null;
		}
		
		long d2 = System.currentTimeMillis();
		List<String> result = JDBCPool.createMsgCA_DDTrn2(sAct, sTRSTAT, sBATCH, sSEQ, sTRACCT, sTRATYP, sTRBR, sDORC, sTRANCD,
				sAMT, sTRCTYP, sTRDATE, sTRTIME, sTRUSER, sEFTACC, sTREFTH, sTRJOBN);
		long d2x = System.currentTimeMillis() - d2;
		if (d2x > 50){
			Func.log(Para.fullPathLog + ".ca","_ora=" + d2x);
		}

		String sOutMessage = null;
		String resultCode = null;
		if (result != null){
			resultCode = result.get(0);
			sOutMessage = result.get(1);
		}
		
		if (result == null || resultCode == null || (resultCode.equals("0") && sOutMessage == null )){
			SentToQueue(assembly, alt, "DDTRN2",sAct, columns, values);			
			Func.log(Para.fullPathLog + ".ca","_ddtrn2 has error, check stderr:Tai khoan " + sTRACCT + "|" + sDORC + "|" +resultCode);
			return null;
		}
		
		if (resultCode.equals("1")){
			//Func.log(Para.fullPathLog + ".ca","_ddtrn2: chua dkdv!");
			return null;
		}
		
		if (resultCode.equals("2")){
			Func.log(Para.fullPathLog + ".ca","_ddtrn2:Gd ton tai!");
			return null;
		}
		
		if (resultCode.equals("3")){
			Func.log(Para.fullPathLog + ".ca","_ddtrn2:Gd chua du han muc!");
			return null;
		}
		
		if (resultCode.equals("4")){
			Func.log(Para.fullPathLog + ".ca","_ddtrn2:Gd batch!");
			return null;
		}
		
		//get cbal:
		//long d1 = System.currentTimeMillis();
		try {
			String cbal = null;
			int curTime = Func.getNumberTime();
			
			if (Func.SWITCH_START <= curTime || curTime <=  Func.SWITCH_END ){
				cbal = SIBS_NEW.getCbal(sTRACCT);
			} else {
				cbal = SIBSRLA_NEW.getCbal(sTRACCT);
				if (cbal == null){
					cbal = SIBSRLA_NEW.getCbal(sTRACCT);
				}
				if (cbal == null){
					if (!sTRDATE.equals(Func.getJulianDate())){
						cbal = SIBSRLA_NEW.getCbal2(sTRACCT);
					} else {
						Func.log(Para.fullPathLog + ".ca","_ddtrn2: ko phat sinh trong ddmemo!");
						cbal = null;
					}
				}
			}
			
			if (cbal != null){
				sOutMessage = sOutMessage.replaceFirst(
						Func.CBALFLAG, ";So du:" +  Func.formatCurrency(cbal, sTRCTYP) + sTRCTYP.trim());
			} else {
				Func.log(Para.fullPathLog + ".error","_ddtrn2_" + "ERROR: Khong lay duoc so du cua giao dich: " + sAct + "#" + sTRSTAT + "#" + sBATCH + "#" + sSEQ + "#" + sTRACCT + "#" + sTRATYP + "#" + sTRBR + "#" + sDORC + "#" + sTRANCD + "#" +
						sAMT + "#" + sTRCTYP + "#" + sTRDATE + "#" + sTRTIME + "#" + sTRUSER + "#" + sEFTACC + "#" + sTREFTH + "#" + sTRJOBN);
				SendEmail.email("ERROR: Khong lay duoc so du cua giao dich: " + sAct + "#" + sTRSTAT + "#" + sBATCH + "#" + sSEQ + "#" + sTRACCT + "#" + sTRATYP + "#" + sTRBR + "#" + sDORC + "#" + sTRANCD + "#" +
						sAMT + "#" + sTRCTYP + "#" + sTRDATE + "#" + sTRTIME + "#" + sTRUSER + "#" + sEFTACC + "#" + sTREFTH + "#" + sTRJOBN);

				Func.log(Para.fullPathLog + "_error.sodu","_ddtrn2_" + "ERROR: Khong lay duoc so du cua giao dich: " + sOutMessage);
				
				sOutMessage = sOutMessage.replaceFirst(Func.CBALFLAG, "");
			}
			
		} catch (Exception e) {
			Func.log(Para.fullPathLog + ".error","_ddtrn2_" + "ERROR: Khong lay duoc so du cua giao dich: " + sAct + "#" + sTRSTAT + "#" + sBATCH + "#" + sSEQ + "#" + sTRACCT + "#" + sTRATYP + "#" + sTRBR + "#" + sDORC + "#" + sTRANCD + "#" +
					sAMT + "#" + sTRCTYP + "#" + sTRDATE + "#" + sTRTIME + "#" + sTRUSER + "#" + sEFTACC + "#" + sTREFTH + "#" + sTRJOBN);
			SendEmail.email("ERROR: Khong lay duoc so du cua giao dich: " + sAct + "#" + sTRSTAT + "#" + sBATCH + "#" + sSEQ + "#" + sTRACCT + "#" + sTRATYP + "#" + sTRBR + "#" + sDORC + "#" + sTRANCD + "#" +
					sAMT + "#" + sTRCTYP + "#" + sTRDATE + "#" + sTRTIME + "#" + sTRUSER + "#" + sEFTACC + "#" + sTREFTH + "#" + sTRJOBN);
			
			sOutMessage = sOutMessage.replaceFirst(Func.CBALFLAG, "");
		}
		//long dsibs = System.currentTimeMillis() - d1;
		//if (dsibs > 10){
		Func.log(Para.fullPathLog + ".ca","_ddtrn2=" + (System.currentTimeMillis() -d2));
		//}
		
		return sOutMessage;
	}
	
	private boolean validateDDTrn2(String strstat, String sbatch, String sseq,
			String stracct, String stratyp, String strbr, String sdorc,
			String strancd, String samt, String strctyp, String strdate,
			String strtime, String strjobn) {
		if (stracct == null || stracct.equals("") ||
				sdorc == null || sdorc.equals("") ||
				samt == null || samt.equals("")){
			return false;
		}
		/*if (strjobn != null){
			if (strjobn.trim().toUpperCase().equalsIgnoreCase("DDRUNC") ||
					strjobn.trim().toUpperCase().equalsIgnoreCase("CDRUNC")){
				return false;
			}
		}*/
		return true;
	}
	
	private boolean validateTMTran(String sStat, String sAccNo, String sAmt, String sCurr,
				String sDorc, String sDate, String sTime, String sSeq,
				String sTellID, String sBrn, String sTmsseq, String sEFTH){
		try {
			if (sAccNo == null || sAccNo.equals("")
					|| sAmt == null || sAmt.equals("")
					|| sDorc == null || sDorc.equals("")
					|| sDate == null || sDate.equals("")
					|| sSeq == null || sSeq.equals("")
					|| sTellID == null || sTellID.equals("")
					){
				return false;
			}
			
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	private String getHeader(MbElement rootElement, String tabName) {
//		try {
//			return rootElement.getFirstElementByPath("/XMLNSC/MSG/HDR/" + tabName).getValue().toString();
//		} catch (Exception e) {
//			Func.log(Para.fullPathLog + ".ca","getHeader=" + e.toString());
//			return null;
//		}
		return ScanCAUtils.getHeader(rootElement, tabName);
	}
	
	private String getData(MbElement rootElement, String tagName) {
//		try {
//			return rootElement.getFirstElementByPath("/XMLNSC/MSG/DATA/" + tagName).getValue().toString();
//		} catch (Exception e) {
//			return null;
//		}
		return ScanCAUtils.getData(rootElement, tagName);
	}

	public static void SentToQueue(MbMessageAssembly assembly, MbOutputTerminal out, String table, String act,
			List<String> columns, List<String> values){
		//MbOutputTerminal out = getOutputTerminal("out");
		//MbOutputTerminal alt = getOutputTerminal("alternate");
		try{
			MbMessage outMessage = new MbMessage();
			
			MbElement outBody = outMessage.getRootElement().createElementAsFirstChild("XMLNSC");
			MbElement msg1 = outBody.createElementAsFirstChild(MbXMLNSC.FOLDER, "MSG",	null);
			MbElement data = msg1.createElementAsFirstChild(MbXMLNSC.FOLDER, "DATA", null);
			
			for(int i=0; i< columns.size(); i++){
				data.createElementAsLastChild(MbXMLNSC.FOLDER,columns.get(i),values.get(i));
			}
			MbElement hdr = msg1.createElementAsFirstChild(MbXMLNSC.FOLDER, "HDR", null);
			hdr.createElementAsLastChild(MbXMLNSC.FOLDER,"TABLE",table);
			hdr.createElementAsLastChild(MbXMLNSC.FOLDER,"ACT",act);
			
			MbMessageAssembly outAssembly = new MbMessageAssembly(assembly, outMessage);
			
			out.propagate(outAssembly);
			//outMessage.finalizeMessage(MbMessage.FINALIZE_NONE);	
			outMessage.clearMessage();
			outMessage = null;
			outAssembly = null;
		} catch (Exception e){
			System.err.println(e);
			System.out.println("SentToQueue err=" + e.toString());
		}
	}
}