package com.pingan.emall.integration.sao.impl;
import com.paic.pafa.app.biz.service.BusinessServiceException;
import com.pingan.emall.biz.communication.TcpConnector;
import com.pingan.emall.dto.MidFundDTO;
import com.pingan.emall.dto.MidRequestDTO;
import com.pingan.emall.dto.MidResponseDTO;
import com.pingan.emall.dto.MidUserDTO;
import com.pingan.emall.integration.sao.FundTradeSAO;
import com.pingan.emall.util.fund.BlowFish;
import com.pingan.emall.util.fund.FundTradeConstants;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
/**
* 发送mina 消息 入口
* @author lisu005
*
*/
public class FundTradeSAOImpl implements FundTradeSAO {
private static final Logger LOG = Logger.getLogger(FundTradeSAOImpl.class);
private TcpConnector connector;
@Override
public MidResponseDTO invokeMidRequest(MidRequestDTO request) throws BusinessServiceException {
Object sessionKey = null;
try {
sessionKey = connector.connect();
// sign in
String worksKey = signIn(sessionKey);
// invoke detail mid request
return invokeWithSession(sessionKey, request, worksKey);
} catch (Exception e) {
LOG.error("Exception happens when connect to mid gateway", e);
if (e instanceof BusinessServiceException) {
throw (BusinessServiceException) e;
} else {
throw new BusinessServiceException(e);
}
} finally {
if (sessionKey != null) {
connector.close(sessionKey);
}
}
}
@Override
public MidResponseDTO login(MidUserDTO user) throws BusinessServiceException {
Object sessionKey = null;
try {
sessionKey = connector.connect();
// 100 系统签入
String worksKey = signIn(sessionKey);
// 调用301接口登录证券账户
MidRequestDTO request = getLoginRequest(user);
MidResponseDTO loginResponse = invokeWithSession(sessionKey, request, worksKey);
if (!loginResponse.isResponseSucceed()) {
return loginResponse;
}
Map<String, String> userRow = loginResponse.getResultList().get(0);
user.setBranch(userRow.get("BRANCH"));
user.setSession(userRow.get("SESSION"));
// 调用309接口查询资金账户
request = getQueryAccountRequest(user);
MidResponseDTO response = invokeWithSession(sessionKey, request, worksKey);
if (!response.isResponseSucceed()) {
return response;
}
List<Map<String, String>> accountRows = response.getResultList();
// 客户仅有一个资金账户, 则默认为主资金账户
String account = accountRows.get(0).get("ACCOUNT");
if (accountRows.size() == 1) {
userRow.put("ACCOUNT", account);
return loginResponse;
}
// 客户有多个资金账户, 调用618接口查询主资金账户
request = getQueryMultiAccountRequest(user);
response = invokeWithSession(sessionKey, request, worksKey);
if (response.isResponseSucceed()) {
for (Map<String, String> accountRow : response.getResultList()) {
if ("1".equals(accountRow.get("ISMAIN"))) {
account = accountRow.get("ACCOUNT");
break;
}
}
}
userRow.put("ACCOUNT", account);
return loginResponse;
} catch(Exception e) {
LOG.error("Exception happens when connect to mid gateway", e);
if (e instanceof BusinessServiceException) {
throw (BusinessServiceException) e;
} else {
throw new BusinessServiceException(e);
}
} finally {
if (sessionKey != null) {
connector.close(sessionKey);
}
}
}
public boolean createFundAccountIfNotExist(MidUserDTO user, MidFundDTO fund) throws BusinessServiceException {
Object sessionKey = null;
try {
sessionKey = connector.connect();
// 100 系统签入
String worksKey = signIn(sessionKey);
// 查询用户是否开了该基金户, 如已开户直接返回true
MidRequestDTO request = getQueryFundAccountRequest(user, fund);
MidResponseDTO response = invokeWithSession(sessionKey, request, worksKey);
if (response.isResponseSucceed()) {
return true;
}
// 查询用户详细信息用于开户
request = getQueryCustomerInfoRequest(user);
response = invokeWithSession(sessionKey, request, worksKey);
if (!response.isResponseSucceed()) {
StringBuilder builder = new StringBuilder();
builder.append("Failed to query detail information of customer ")
.append(user.getUserCode()).append(", responseMessage : ")
.append(response.getResponseMessage());
LOG.error(builder.toString());
return false;
}
// 基金开户
Map<String, String> userMap = response.getResultList().get(0);
request = getCreateFundAccountRequest(user, userMap, fund);
response = invokeWithSession(sessionKey, request, worksKey);
if (!response.isResponseSucceed()) {
StringBuilder builder = new StringBuilder();
builder.append("Failed to create fund account for customer ")
.append(user.getUserCode()).append(", responseMessage : ")
.append(response.getResponseMessage());
LOG.error(builder.toString());
return false;
}
return true;
} catch(Exception e) {
LOG.error("Exception happens when connect to mid gateway", e);
if (e instanceof BusinessServiceException) {
throw (BusinessServiceException) e;
} else {
throw new BusinessServiceException(e);
}
} finally {
if (sessionKey != null) {
connector.close(sessionKey);
}
}
}
/**
* Do mid sign in
*
* @param sessionKey
* @return worksKey, return null if sign in failed
* @throws Exception
*/
private String signIn(Object sessionKey) throws Exception {
MidRequestDTO signRequest = new MidRequestDTO(FundTradeConstants.FUNCTION_SIGN_IN);
MidResponseDTO signResponse = connector.send(sessionKey, signRequest);
if (!signResponse.isResponseSucceed()) {
throw new BusinessServiceException("Do mid signIn(302) failed, response message : " + signResponse.getResponseMessage());
}
Map<String, String> signRow = signResponse.getResultList().get(0);
// works key
String encWorksKey = signRow.get(FundTradeConstants.WORKS_KEY);
String decWorksKey = BlowFish.decrypt(encWorksKey, null);
return decWorksKey;
}
private MidResponseDTO invokeWithSession(Object sessionKey, MidRequestDTO request, String worksKey) throws Exception {
request.getHeader().setCrc(worksKey);
MidResponseDTO invokeResponse = connector.send(sessionKey, request);
if (!invokeResponse.isResponseSucceed()) {
return invokeResponse;
}
// Handle next packages
List<Map<String, String>> resultList = new ArrayList<Map<String, String>>();
resultList.addAll(invokeResponse.getResultList());
while (invokeResponse.hasNextPackage()) {
request.getBodyParams().clear();
request.getBodyParams().add(FundTradeConstants.FUNCTION_NEXT_PACKAGE);
request.getHeader().setCrc(worksKey);
invokeResponse = connector.send(sessionKey, request);
if (!invokeResponse.isResponseSucceed()) {
break;
}
resultList.addAll(invokeResponse.getResultList());
}
invokeResponse.setResultList(resultList);
return invokeResponse;
}
/**
* 获取MID 301 登录请求
* @param user
* @return MidRequestDTO
* @throws Exception
*/
private MidRequestDTO getLoginRequest(MidUserDTO user) {
MidRequestDTO request = new MidRequestDTO(FundTradeConstants.FUNCTION_LOGIN);
request.getHeader().init(user);
request.getBodyParams().add(FundTradeConstants.USER_ID_CLS_U);
request.getBodyParams().add(user.getUserCode());
request.getBodyParams().add(user.getPassword() + "|1");
request.getBodyParams().add(user.getRemark());
return request;
}
/**
* 获取MID 309 查询资金账户请求
* @param user
* @return MidRequestDTO
*/
private MidRequestDTO getQueryAccountRequest(MidUserDTO user) {
MidRequestDTO request = new MidRequestDTO(FundTradeConstants.FUNCTION_QUERY_ACCOUNT);
request.getHeader().init(user);
request.getBodyParams().add(user.getUserCode()); // 客户代码
request.getBodyParams().add(""); // 资金账户
request.getBodyParams().add("");
return request;
}
/**
* 获取MID 618 查询单账户多银行记录
* @param user
* @return MidRequestDTO
*/
private MidRequestDTO getQueryMultiAccountRequest(MidUserDTO user) {
MidRequestDTO request = new MidRequestDTO(FundTradeConstants.FUNCTION_QUERY_MULTI_ACCOUNT);
request.getHeader().init(user);
request.getBodyParams().add(user.getUserCode()); // 客户代码
request.getBodyParams().add("");
request.getBodyParams().add("");
request.getBodyParams().add("");
request.getBodyParams().add("");
request.getBodyParams().add("");
request.getBodyParams().add("200"); // 本次查询记录数
request.getBodyParams().add("");
request.getBodyParams().add("");
request.getBodyParams().add("1"); // 访问权限控制
return request;
}
private MidRequestDTO getQueryFundAccountRequest(MidUserDTO user, MidFundDTO fund) {
MidRequestDTO request = new MidRequestDTO(FundTradeConstants.FUNCTION_QUERY_FUND_ACCOUNT);
request.getHeader().init(user);
request.getBodyParams().add(fund.getTaCode()); // 基金公司代码
request.getBodyParams().add(user.getUserCode()); // 客户代码
request.getBodyParams().add(""); // 基金账户
request.getBodyParams().add(""); // 账户状态
request.getBodyParams().add(user.getUserAccount()); // 资产账号
return request;
}
private MidRequestDTO getQueryCustomerInfoRequest(MidUserDTO user) {
MidRequestDTO request = new MidRequestDTO(
FundTradeConstants.FUNCTION_QUERY_CUSTOMER);
request.getHeader().init(user);
request.getBodyParams().add(user.getUserCode()); // 客户代码
return request;
}
private MidRequestDTO getCreateFundAccountRequest(MidUserDTO user, Map<String, String> userMap, MidFundDTO fund) {
MidRequestDTO request = new MidRequestDTO(FundTradeConstants.FUNCTION_CREATE_FUND_ACCOUNT);
request.getHeader().init(user);
request.getBodyParams().add(user.getUserAccount()); // 资产账号
request.getBodyParams().add("01"); // 交易行为 01开户
request.getBodyParams().add(fund.getTaCode()); // 登记公司代码
request.getBodyParams().add(""); // 基金账号
request.getBodyParams().add("0"); // 缺省分红方式
request.getBodyParams().add("1"); // 对账单寄送选择, 1:不寄送
request.getBodyParams().add(""); // TA系统交易手段
request.getBodyParams().add(""); // 密函编号
request.getBodyParams().add("2"); // 客户资料标示 2
request.getBodyParams().add(userMap.get("USER_NAME")); // 用户名称
request.getBodyParams().add(userMap.get("USER_TYPE")); // 用户性质
request.getBodyParams().add(userMap.get("USER_FNAME")); // 全称
request.getBodyParams().add(userMap.get("ID_TYPE")); // 证件类型
request.getBodyParams().add(userMap.get("ID_CODE")); // 证件号
request.getBodyParams().add(""); // 法人代表
request.getBodyParams().add(""); // 法人证件类型
request.getBodyParams().add(""); // 法人代表证件号码
request.getBodyParams().add(""); // 联系人
request.getBodyParams().add(""); // 联系人证件类型
request.getBodyParams().add(""); // 联系人证件号码
request.getBodyParams().add(""); // 联系人电话
request.getBodyParams().add(userMap.get("ADDRESS")); // 通讯地址
request.getBodyParams().add(userMap.get("ZIP_CODE")); // 邮政编码
request.getBodyParams().add(""); // 电话
request.getBodyParams().add(""); // 移动电话
request.getBodyParams().add(""); // Email
request.getBodyParams().add(""); // 性别
request.getBodyParams().add(""); // 备注
return request;
}
public void setConnector(TcpConnector connector) {
this.connector = connector;
}
}
import com.paic.pafa.app.biz.service.BusinessServiceException;
import com.pingan.emall.biz.communication.TcpConnector;
import com.pingan.emall.dto.MidFundDTO;
import com.pingan.emall.dto.MidRequestDTO;
import com.pingan.emall.dto.MidResponseDTO;
import com.pingan.emall.dto.MidUserDTO;
import com.pingan.emall.integration.sao.FundTradeSAO;
import com.pingan.emall.util.fund.BlowFish;
import com.pingan.emall.util.fund.FundTradeConstants;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
/**
* 发送mina 消息 入口
* @author lisu005
*
*/
public class FundTradeSAOImpl implements FundTradeSAO {
private static final Logger LOG = Logger.getLogger(FundTradeSAOImpl.class);
private TcpConnector connector;
@Override
public MidResponseDTO invokeMidRequest(MidRequestDTO request) throws BusinessServiceException {
Object sessionKey = null;
try {
sessionKey = connector.connect();
// sign in
String worksKey = signIn(sessionKey);
// invoke detail mid request
return invokeWithSession(sessionKey, request, worksKey);
} catch (Exception e) {
LOG.error("Exception happens when connect to mid gateway", e);
if (e instanceof BusinessServiceException) {
throw (BusinessServiceException) e;
} else {
throw new BusinessServiceException(e);
}
} finally {
if (sessionKey != null) {
connector.close(sessionKey);
}
}
}
@Override
public MidResponseDTO login(MidUserDTO user) throws BusinessServiceException {
Object sessionKey = null;
try {
sessionKey = connector.connect();
// 100 系统签入
String worksKey = signIn(sessionKey);
// 调用301接口登录证券账户
MidRequestDTO request = getLoginRequest(user);
MidResponseDTO loginResponse = invokeWithSession(sessionKey, request, worksKey);
if (!loginResponse.isResponseSucceed()) {
return loginResponse;
}
Map<String, String> userRow = loginResponse.getResultList().get(0);
user.setBranch(userRow.get("BRANCH"));
user.setSession(userRow.get("SESSION"));
// 调用309接口查询资金账户
request = getQueryAccountRequest(user);
MidResponseDTO response = invokeWithSession(sessionKey, request, worksKey);
if (!response.isResponseSucceed()) {
return response;
}
List<Map<String, String>> accountRows = response.getResultList();
// 客户仅有一个资金账户, 则默认为主资金账户
String account = accountRows.get(0).get("ACCOUNT");
if (accountRows.size() == 1) {
userRow.put("ACCOUNT", account);
return loginResponse;
}
// 客户有多个资金账户, 调用618接口查询主资金账户
request = getQueryMultiAccountRequest(user);
response = invokeWithSession(sessionKey, request, worksKey);
if (response.isResponseSucceed()) {
for (Map<String, String> accountRow : response.getResultList()) {
if ("1".equals(accountRow.get("ISMAIN"))) {
account = accountRow.get("ACCOUNT");
break;
}
}
}
userRow.put("ACCOUNT", account);
return loginResponse;
} catch(Exception e) {
LOG.error("Exception happens when connect to mid gateway", e);
if (e instanceof BusinessServiceException) {
throw (BusinessServiceException) e;
} else {
throw new BusinessServiceException(e);
}
} finally {
if (sessionKey != null) {
connector.close(sessionKey);
}
}
}
public boolean createFundAccountIfNotExist(MidUserDTO user, MidFundDTO fund) throws BusinessServiceException {
Object sessionKey = null;
try {
sessionKey = connector.connect();
// 100 系统签入
String worksKey = signIn(sessionKey);
// 查询用户是否开了该基金户, 如已开户直接返回true
MidRequestDTO request = getQueryFundAccountRequest(user, fund);
MidResponseDTO response = invokeWithSession(sessionKey, request, worksKey);
if (response.isResponseSucceed()) {
return true;
}
// 查询用户详细信息用于开户
request = getQueryCustomerInfoRequest(user);
response = invokeWithSession(sessionKey, request, worksKey);
if (!response.isResponseSucceed()) {
StringBuilder builder = new StringBuilder();
builder.append("Failed to query detail information of customer ")
.append(user.getUserCode()).append(", responseMessage : ")
.append(response.getResponseMessage());
LOG.error(builder.toString());
return false;
}
// 基金开户
Map<String, String> userMap = response.getResultList().get(0);
request = getCreateFundAccountRequest(user, userMap, fund);
response = invokeWithSession(sessionKey, request, worksKey);
if (!response.isResponseSucceed()) {
StringBuilder builder = new StringBuilder();
builder.append("Failed to create fund account for customer ")
.append(user.getUserCode()).append(", responseMessage : ")
.append(response.getResponseMessage());
LOG.error(builder.toString());
return false;
}
return true;
} catch(Exception e) {
LOG.error("Exception happens when connect to mid gateway", e);
if (e instanceof BusinessServiceException) {
throw (BusinessServiceException) e;
} else {
throw new BusinessServiceException(e);
}
} finally {
if (sessionKey != null) {
connector.close(sessionKey);
}
}
}
/**
* Do mid sign in
*
* @param sessionKey
* @return worksKey, return null if sign in failed
* @throws Exception
*/
private String signIn(Object sessionKey) throws Exception {
MidRequestDTO signRequest = new MidRequestDTO(FundTradeConstants.FUNCTION_SIGN_IN);
MidResponseDTO signResponse = connector.send(sessionKey, signRequest);
if (!signResponse.isResponseSucceed()) {
throw new BusinessServiceException("Do mid signIn(302) failed, response message : " + signResponse.getResponseMessage());
}
Map<String, String> signRow = signResponse.getResultList().get(0);
// works key
String encWorksKey = signRow.get(FundTradeConstants.WORKS_KEY);
String decWorksKey = BlowFish.decrypt(encWorksKey, null);
return decWorksKey;
}
private MidResponseDTO invokeWithSession(Object sessionKey, MidRequestDTO request, String worksKey) throws Exception {
request.getHeader().setCrc(worksKey);
MidResponseDTO invokeResponse = connector.send(sessionKey, request);
if (!invokeResponse.isResponseSucceed()) {
return invokeResponse;
}
// Handle next packages
List<Map<String, String>> resultList = new ArrayList<Map<String, String>>();
resultList.addAll(invokeResponse.getResultList());
while (invokeResponse.hasNextPackage()) {
request.getBodyParams().clear();
request.getBodyParams().add(FundTradeConstants.FUNCTION_NEXT_PACKAGE);
request.getHeader().setCrc(worksKey);
invokeResponse = connector.send(sessionKey, request);
if (!invokeResponse.isResponseSucceed()) {
break;
}
resultList.addAll(invokeResponse.getResultList());
}
invokeResponse.setResultList(resultList);
return invokeResponse;
}
/**
* 获取MID 301 登录请求
* @param user
* @return MidRequestDTO
* @throws Exception
*/
private MidRequestDTO getLoginRequest(MidUserDTO user) {
MidRequestDTO request = new MidRequestDTO(FundTradeConstants.FUNCTION_LOGIN);
request.getHeader().init(user);
request.getBodyParams().add(FundTradeConstants.USER_ID_CLS_U);
request.getBodyParams().add(user.getUserCode());
request.getBodyParams().add(user.getPassword() + "|1");
request.getBodyParams().add(user.getRemark());
return request;
}
/**
* 获取MID 309 查询资金账户请求
* @param user
* @return MidRequestDTO
*/
private MidRequestDTO getQueryAccountRequest(MidUserDTO user) {
MidRequestDTO request = new MidRequestDTO(FundTradeConstants.FUNCTION_QUERY_ACCOUNT);
request.getHeader().init(user);
request.getBodyParams().add(user.getUserCode()); // 客户代码
request.getBodyParams().add(""); // 资金账户
request.getBodyParams().add("");
return request;
}
/**
* 获取MID 618 查询单账户多银行记录
* @param user
* @return MidRequestDTO
*/
private MidRequestDTO getQueryMultiAccountRequest(MidUserDTO user) {
MidRequestDTO request = new MidRequestDTO(FundTradeConstants.FUNCTION_QUERY_MULTI_ACCOUNT);
request.getHeader().init(user);
request.getBodyParams().add(user.getUserCode()); // 客户代码
request.getBodyParams().add("");
request.getBodyParams().add("");
request.getBodyParams().add("");
request.getBodyParams().add("");
request.getBodyParams().add("");
request.getBodyParams().add("200"); // 本次查询记录数
request.getBodyParams().add("");
request.getBodyParams().add("");
request.getBodyParams().add("1"); // 访问权限控制
return request;
}
private MidRequestDTO getQueryFundAccountRequest(MidUserDTO user, MidFundDTO fund) {
MidRequestDTO request = new MidRequestDTO(FundTradeConstants.FUNCTION_QUERY_FUND_ACCOUNT);
request.getHeader().init(user);
request.getBodyParams().add(fund.getTaCode()); // 基金公司代码
request.getBodyParams().add(user.getUserCode()); // 客户代码
request.getBodyParams().add(""); // 基金账户
request.getBodyParams().add(""); // 账户状态
request.getBodyParams().add(user.getUserAccount()); // 资产账号
return request;
}
private MidRequestDTO getQueryCustomerInfoRequest(MidUserDTO user) {
MidRequestDTO request = new MidRequestDTO(
FundTradeConstants.FUNCTION_QUERY_CUSTOMER);
request.getHeader().init(user);
request.getBodyParams().add(user.getUserCode()); // 客户代码
return request;
}
private MidRequestDTO getCreateFundAccountRequest(MidUserDTO user, Map<String, String> userMap, MidFundDTO fund) {
MidRequestDTO request = new MidRequestDTO(FundTradeConstants.FUNCTION_CREATE_FUND_ACCOUNT);
request.getHeader().init(user);
request.getBodyParams().add(user.getUserAccount()); // 资产账号
request.getBodyParams().add("01"); // 交易行为 01开户
request.getBodyParams().add(fund.getTaCode()); // 登记公司代码
request.getBodyParams().add(""); // 基金账号
request.getBodyParams().add("0"); // 缺省分红方式
request.getBodyParams().add("1"); // 对账单寄送选择, 1:不寄送
request.getBodyParams().add(""); // TA系统交易手段
request.getBodyParams().add(""); // 密函编号
request.getBodyParams().add("2"); // 客户资料标示 2
request.getBodyParams().add(userMap.get("USER_NAME")); // 用户名称
request.getBodyParams().add(userMap.get("USER_TYPE")); // 用户性质
request.getBodyParams().add(userMap.get("USER_FNAME")); // 全称
request.getBodyParams().add(userMap.get("ID_TYPE")); // 证件类型
request.getBodyParams().add(userMap.get("ID_CODE")); // 证件号
request.getBodyParams().add(""); // 法人代表
request.getBodyParams().add(""); // 法人证件类型
request.getBodyParams().add(""); // 法人代表证件号码
request.getBodyParams().add(""); // 联系人
request.getBodyParams().add(""); // 联系人证件类型
request.getBodyParams().add(""); // 联系人证件号码
request.getBodyParams().add(""); // 联系人电话
request.getBodyParams().add(userMap.get("ADDRESS")); // 通讯地址
request.getBodyParams().add(userMap.get("ZIP_CODE")); // 邮政编码
request.getBodyParams().add(""); // 电话
request.getBodyParams().add(""); // 移动电话
request.getBodyParams().add(""); // Email
request.getBodyParams().add(""); // 性别
request.getBodyParams().add(""); // 备注
return request;
}
public void setConnector(TcpConnector connector) {
this.connector = connector;
}
}