TransactionStatus status = null;
public Map brokerageCalc(Map param) throws Exception {
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
TransactionStatus status = null;
Map model = new HashMap();
String oper = "计算";
try {
List<Map> payHisList = null;
if ("R".equals(param.get("brokerageStatue"))) {
oper = "重算";
// 回滚佣金记录
proxyBrokeragedDao.rollbackBrokerageRecord(param);
// 待计算列表
Map payHisParam = new HashMap();
payHisParam.put("brokerageStatue", "R");
payHisList = proxyBrokeragedDao.getPayHisNotPaid(payHisParam);
} else {
// 所有待计算列表
payHisList = proxyBrokeragedDao.getPayHisNotPaid(null);
}
// 佣金费率配置
Map brokerageSetParam = new HashMap();
brokerageSetParam.put("sysDate", new Date());
for (Map payHis : payHisList) {
List<ProxyBaseInfo> proxys = new ArrayList(); // 代理链
// 获取代理商(是玩家也是代理商)
ProxyBaseInfo proxy = proxyBaseInfoDao.getProxyById((String) payHis.get("playerId"));
proxys.add(proxy);
// 获取佣金费率
List<Map> brokerageSetlist;
if (proxy == null) {
brokerageSetParam.put("type", "PL");
brokerageSetlist = proxyBrokeragedDao.getAllBrokerageinfo(brokerageSetParam);
} else {
brokerageSetParam.put("type", proxy.getType());
brokerageSetlist = proxyBrokeragedDao.getAllBrokerageinfo(brokerageSetParam);
}
Integer maxDepthLevel = getMaxDephLevel(brokerageSetlist);
getPascadeProxys((String) payHis.get("playerId"), maxDepthLevel, proxys);
// 开启事务
status = transactionManager.getTransaction(def);///修改数据的时候 首先开启事务
try {
for (Map setting : brokerageSetlist) {
Integer depthLevel = ((BigDecimal) setting.get("depthLevel")).intValue();
ProxyBaseInfo tmpProxy = proxys.get(depthLevel);
if (tmpProxy != null && ProxyBaseInfo.TYPE_P.equals(tmpProxy.getType())) { // 正式代理商才計算佣金
BigDecimal rate = new BigDecimal(setting.get("rate").toString())
.divide(new BigDecimal("100"));
BigDecimal amount = new BigDecimal(payHis.get("amount").toString());
BigDecimal brokerage = amount.multiply(rate); // 佣金金额
Map brokerageModel = new HashMap();
brokerageModel.put("proxyBrokerageRecordId", UUIDGenerator.generatorUUID());
brokerageModel.put("payHistoryId", payHis.get("payHistoryId"));
brokerageModel.put("playerId", payHis.get("playerId"));
brokerageModel.put("proxyBaseInfoId", tmpProxy.getProxyBaseInfoId());
brokerageModel.put("proxyBrokerageRecordId", UUIDGenerator.generatorUUID());
brokerageModel.put("gameId", null);
brokerageModel.put("payTime", payHis.get("payTime"));
brokerageModel.put("amount", amount);
brokerageModel.put("rate", setting.get("rate"));
brokerageModel.put("brokerage", brokerage);
brokerageModel.put("depthLevel", depthLevel);
brokerageModel.put("statue", "Y");
brokerageModel.put("createdBy", "SYSTEM");
brokerageModel.put("updatedBy", "SYSTEM");
brokerageModel.put("remarks", "直属" + depthLevel + "级,佣金比例:" + setting.get("rate") + "
");
proxyBrokeragedDao.insertBrokerageRecord(brokerageModel);
}
}
// 更新支付记录为已计算
payHis.put("brokerageStatue", "C");
proxyBrokeragedDao.updatePayHis(payHis);
// 提交事务
transactionManager.commit(status);//跟新完后,需要手动的commit();
} catch (Exception e) {
if (status != null) {
transactionManager.rollback(status);//出了错误,需要手动的回滚所有操作。保持所有一致性
}
// 发生异常修改支付记录佣金计算状态为异常
logger.error("重算充值记录异常:payHistoryId=" + payHis.get("payHistoryId"), e);
throw e;
}
}
model.put("rcode", "0");
model.put("rmsg", oper + "成功!");
} catch (Exception e) {
logger.error("重算异常", e);
model.put("rcode", "1");
model.put("rmsg", oper + "失败!");
}
return model;
}