@@ -22,6 +22,7 @@ import com.iformall.exception.MallinkException;
import com.iformall.mapper.*;
import com.iformall.pay.*;
import com.iformall.service.ExcelService;
import com.iformall.service.WxAppinfoService;
import com.iformall.service.WxProfitSharingOrderService;
import com.iformall.service.pay.PayServiceFactory;
import com.iformall.service.pay.service.share.PayShareAdapterService;
@@ -61,6 +62,9 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
@Autowired
WxAppinfoMapper wxAppinfoMapper;
@Autowired
WxAppinfoService wxAppinfoService;
@Autowired
WxPayAccountMapper wxPayAccountMapper;
@@ -132,262 +136,359 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
return wxAppinfo;
}
private WxProfitShareAppInoDto getAppinfo(WxProfitSharingOrder wxProfitSharingOrder) {
WxAppinfo appinfo;
Integer payWay;
if (wxProfitSharingOrder.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE.getCode())
|| wxProfitSharingOrder.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE_FINISH.getCode())){
WxPayOrder wxPayOrder = wxPayOrderMapper.selectById(wxProfitSharingOrder.getOrderId(),wxProfitSharingOrder.getTenantId());
if (wxPayOrder == null) {
throw new MallinkException(ErrorCode.ORDER_IS_NOT_FIND.getCode(),"wxProfitSharingOrder["+wxProfitSharingOrder.getId()+"] 没有payOrder信息.");
}
appinfo = getAppinfo(wxPayOrder.getTenantId(),wxPayOrder.getPayVendor());
payWay = wxPayOrder.getPayVendor();
} else if(wxProfitSharingOrder.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_MULTI.getCode())
||wxProfitSharingOrder.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_MULTI_FINISH.getCode())){
WxCardSpend wxCardSpend = wxCardSpendMapper.selectById(wxProfitSharingOrder.getOrderId());
if (wxCardSpend == null)
throw new MallinkException(ErrorCode.ORDER_IS_NOT_FIND.getCode(),"wxProfitSharingOrder["+wxProfitSharingOrder.getId()+"] 没有WxCardSpend信息.");
appinfo = getAppinfo(wxCardSpend.getTenantId(),EnumCardSpendFrom.getPayWay(wxCardSpend.getPayFrom()).getCode());
payWay = EnumCardSpendFrom.getPayWay(wxCardSpend.getPayFrom()).getCode();
} else {
throw new MallinkException(ErrorCode.PROFIT_SHARING_QUERY_UNKNOWN_TYPE);
}
WxProfitShareAppInoDto rt = new WxProfitShareAppInoDto();
rt.setAppinfo(appinfo);
rt.setPayWay(payWay);
return rt;
}
// private WxProfitShareAppInoDto getAppinfo(WxProfitSharingOrder wxProfitSharingOrder) {
// WxAppinfo appinfo;
// Integer payWay;
// if (wxProfitSharingOrder.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE.getCode())
// || wxProfitSharingOrder.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE_FINISH.getCode())){
// WxPayOrder wxPayOrder = wxPayOrderMapper.selectById(wxProfitSharingOrder.getOrderId(),wxProfitSharingOrder.getTenantId());
// if (wxPayOrder == null) {
// throw new MallinkException(ErrorCode.ORDER_IS_NOT_FIND.getCode(),"wxProfitSharingOrder["+wxProfitSharingOrder.getId()+"] 没有payOrder信息.");
// }
// appinfo = getAppinfo(wxPayOrder.getTenantId(),wxPayOrder.getPayVendor());
// payWay = wxPayOrder.getPayVendor();
// } else if(wxProfitSharingOrder.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_MULTI.getCode())
// || wxProfitSharingOrder.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_MULTI_FINISH.getCode())){
// WxCardSpend wxCardSpend = wxCardSpendMapper.selectById(wxProfitSharingOrder.getOrderId());
// if (wxCardSpend == null)
// throw new MallinkException(ErrorCode.ORDER_IS_NOT_FIND.getCode(),"wxProfitSharingOrder["+wxProfitSharingOrder.getId()+"] 没有WxCardSpend信息.");
// appinfo = getAppinfo(wxCardSpend.getTenantId(),EnumCardSpendFrom.getPayWay(wxCardSpend.getPayFrom()).getCode());
// payWay = EnumCardSpendFrom.getPayWay(wxCardSpend.getPayFrom()).getCode();
// } else {
// throw new MallinkException(ErrorCode.PROFIT_SHARING_QUERY_UNKNOWN_TYPE);
// }
// WxProfitShareAppInoDto rt = new WxProfitShareAppInoDto();
// rt.setAppinfo(appinfo);
// rt.setPayWay(payWay);
// return rt;
// }
@Override
@Transactional(isolation=Isolation.SERIALIZABLE, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
public ResultData redoSharingOrder(WxProfitSharingOrder sharingOrder) {
final IdWorker idworker = IdWorker.get();
if (!sharingOrder.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE.getCode()) &&
!sharingOrder.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_MULTI.getCode())) {
return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
if(!EnumProfitSharingOrderStatus.shareStatus().contains(sharingOrder.getSharingStatus())){
return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getCode(),"分账状态不允许");
}
WxProfitShareAppInoDto dto = getAppinfo(sharingOrder);
WxAppinfo appInfo = dto.getAppinfo();
WxPayAccount payAccount = wxPayAccountMapper.selectById(appInfo.getPayId());
WxPayOrder wxPayOrder = wxPayOrderMapper.selectById(sharingOrder.getOrderId(),sharingOrder.getTenantId());
List<JSONObject> receivers;
if (wxPayOrder == null) {
throw new MallinkException(ErrorCode.ORDER_IS_NOT_FIND.getCode(),"wxProfitSharingOrder["+sharingOrder.getId()+"] 没有payOrder信息.");
}
try {
receivers = JSONArray.parseArray(sharingOrder.getReceivers(), JSONObject.class);
if (receivers.size() <= 0)
EnumPayWay enumPayWay = EnumPayWay.getEnum(wxPayOrder.getPayVendor());
EnumAppPlat enumPlat = enumPayWay.getPlat();
WxAppinfo cAppInfo = wxAppinfoService.getCAppInfo(sharingOrder, enumPlat);
WxPayAccount payAccount = wxPayAccountMapper.selectById(cAppInfo.getPayId());
PayShareAdapterService payShareAdapterService = payServiceFactory.getPayShareAdapterService(wxPayOrder.getPayVendor(),wxPayOrder.getPayVersion());
PayShareResult shareResult = null;
JSONArray jsonArray = null;
if(StringUtils.isNotBlank(sharingOrder.getReceivers())){
jsonArray = JSONArray.parseArray(sharingOrder.getReceivers());
}
List<WxProfitSharingResult> resultList = new ArrayList<WxProfitSharingResult>();
if(jsonArray == null || jsonArray.isEmpty()){
final IdWorker idworker = IdWorker.get();
Date currentDate = new Date();
try {
jsonArray.stream().forEach(obj -> {
JSONObject receiver =(JSONObject) obj;
WxProfitSharingResult result = new WxProfitSharingResult();
result.setId(idworker.nextId());
result.updateTenantInfo(sharingOrder);
result.setMerchantId(sharingOrder.getMerchantId());
result.setSharingOrderId(sharingOrder.getId());
result.setSharingReceiverId(Long.valueOf(receiver.getString("description")));
result.setPayAmount(receiver.getInteger("amount"));
result.setCreateTime(currentDate);
result.setUpdateTime(currentDate);
result.setSharingStatus(EnumProfitSharingResultStatus.PROFIT_SHARING_RESULT_PENDING.getCode());
resultList.add(result);
});
}catch (Exception e) {
logger.error("分账详细信息为null",e);
return new ResultData(ErrorCode.PROFIT_SHARING_RECEIVER_INVALID);
} catch (Exception e) {
return new ResultData(ErrorCode.PROFIT_SHARING_RECEIVER_INVALID);
}
shareResult = payShareAdapterService.noReciverShare(cAppInfo, payAccount, sharingOrder);
}else{
shareResult = payShareAdapterService.haveReciversShare(cAppInfo, payAccount, sharingOrder, jsonArray, wxPayOrder.getMchType());
}
final WxProfitSharingOrder frecord = sharingOrder;
List<WxProfitSharingResult> resultList = new ArrayList<WxProfitSharingResult>();
try {
receivers.stream().forEach(receiver -> {
Date currentDate = new Date();
WxProfitSharingResult result = new WxProfitSharingResult();
result.setId(idworker.nextId());
result.updateTenantInfo(frecord);
result.setMerchantId(frecord.getMerchantId());
result.setSharingOrderId(frecord.getId());
result.setSharingReceiverId(Long.valueOf(receiver.getString("description")));
result.setPayAmount(receiver.getInteger("amount"));
result.setCreateTime(currentDate);
result.setUpdateTime(currentDate);
result.setSharingStatus(EnumProfitSharingResultStatus.PROFIT_SHARING_RESULT_PENDING.getCode());
resultList.add(result);
});
}catch (Exception e) {
return new ResultData(ErrorCode.PROFIT_SHARING_RECEIVER_INVALID);
if (null == shareResult) {
return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getCode(),"当前支付模式调用分账未返回结果");
}
PayShareResult shareResult = payServiceFactory.getPayShareAdapterService(dto.getPayWay(),payAccount.getPayVersion()).haveReciversShare(appInfo, payAccount,frecord,
sharingOrder.getTransactionId(), JSONArray.parseArray(JSON.toJSONString(receivers)), EnumProfitSharingOrderType.getEnum(sharingOrder.getType()));
WxProfitSharingOrder updSharingOrder = new WxProfitSharingOrder();
updSharingOrder.updateTenantInfo(sharingOrder);
updSharingOrder.setId(sharingOrder.getId());
updSharingOrder.setOutSettleNo(frecord .getOutSettleNo());
updSharingOrder.setOutSettleNo(sharingOrder.getOutSettleNo());
if (!shareResult.isSuccess()) {
updSharingOrder.setSharingStatus(shareResult.getCode());
updSharingOrder.setErrorMsg("REDO:"+ shareResult.getMsg());
updSharingOrder.setErrorMsg(shareResult.getMsg());
updSharingOrder.setUpdateTime(new Date());
wxProfitSharingOrderMapper.updateById(updSharingOrder);
return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getCode(),updSharingOrder.getErrorMsg());
}
updSharingOrder.setSharingOrderNo(shareResult.getShareOrderNo());
updSharingOrder.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_ACCEPTED.getCode());
updSharingOrder.setErrorMsg("REDO:重试分账成功");
updSharingOrder.setUpdateTime(new Date());
wxProfitSharingOrderMapper.updateById(updSharingOrder);
for (WxProfitSharingResult result:resultList) {
wxProfitSharingResultMapper.insert(result);
}
return new ResultData(shareResult.getData());
}
@Override
public ResultData redoFinishSharingOrder(WxProfitSharingOrder sharingOrder) {
if (!sharingOrder.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE_FINISH.getCode())) {
return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
}
WxProfitShareAppInoDto dto = getAppinfo(sharingOrder);
WxAppinfo appInfo = dto.getAppinfo();
WxPayAccount payAccount = wxPayAccountMapper.selectById(appInfo.getPayId());
PayShareResult shareResult = payServiceFactory.getPayShareAdapterService(dto.getPayWay(),payAccount.getPayVersion()).noReciverShare(appInfo,payAccount,sharingOrder,sharingOrder.getTransactionId(),sharingOrder.getPayAmount());
WxProfitSharingOrder updSharingOrder = new WxProfitSharingOrder();
updSharingOrder.updateTenantInfo(sharingOrder);
updSharingOrder.setId(sharingOrder.getId());
updSharingOrder.setOutSettleNo(sharingOrder.getOutSettleNo());
if (!shareResult.isSuccess()) {
updSharingOrder.setSharingStatus(shareResult.getCode());
updSharingOrder.setErrorMsg("REDO:"+shareResult.getMsg());
updSharingOrder.setUpdateTime(new Date());
wxProfitSharingOrderMapper.updateById(updSharingOrder);
return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getCode(),updSharingOrder.getErrorMsg());
}
updSharingOrder.setSharingOrderNo(shareResult.getShareOrderNo());
updSharingOrder.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_ACCEPTED.getCode());
updSharingOrder.setErrorMsg("REDO:重试分账成功");
updSharingOrder.setUpdateTime(new Date());
wxProfitSharingOrderMapper.updateById(updSharingOrder);
return new ResultData(shareResult.getData());
// final IdWorker idworker = IdWorker.get();
//
// if (!sharingOrder.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE.getCode()) &&
// !sharingOrder.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_MULTI.getCode())) {
// return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
// }
//
// WxProfitShareAppInoDto dto = getAppinfo(sharingOrder);
// WxAppinfo appInfo = dto.getAppinfo();
// WxPayAccount payAccount = wxPayAccountMapper.selectById(appInfo.getPayId());
//
// List<JSONObject> receivers;
//
// try {
// receivers = JSONArray.parseArray(sharingOrder.getReceivers(), JSONObject.class);
// if (receivers.size() <= 0)
// return new ResultData(ErrorCode.PROFIT_SHARING_RECEIVER_INVALID);
// } catch (Exception e) {
// return new ResultData(ErrorCode.PROFIT_SHARING_RECEIVER_INVALID);
// }
//
// final WxProfitSharingOrder frecord = sharingOrder;
// List<WxProfitSharingResult> resultList = new ArrayList<WxProfitSharingResult>();
// try {
// receivers.stream().forEach(receiver -> {
// Date currentDate = new Date();
// WxProfitSharingResult result = new WxProfitSharingResult();
// result.setId(idworker.nextId());
// result.updateTenantInfo(frecord);
// result.setMerchantId(frecord.getMerchantId());
// result.setSharingOrderId(frecord.getId());
// result.setSharingReceiverId(Long.valueOf(receiver.getString("description")));
// result.setPayAmount(receiver.getInteger("amount"));
// result.setCreateTime(currentDate);
// result.setUpdateTime(currentDate);
// result.setSharingStatus(EnumProfitSharingResultStatus.PROFIT_SHARING_RESULT_PENDING.getCode());
// resultList.add(result);
// });
// }catch (Exception e) {
// return new ResultData(ErrorCode.PROFIT_SHARING_RECEIVER_INVALID);
// }
//
//
// PayShareResult shareResult = payServiceFactory.getPayShareAdapterService(dto.getPayWay(),payAccount.getPayVersion()).haveReciversShare(appInfo, payAccount,frecord,
// sharingOrder.getTransactionId(), JSONArray.parseArray(JSON.toJSONString(receivers)), EnumProfitSharingOrderType.getEnum(sharingOrder.getType()));
// WxProfitSharingOrder updSharingOrder = new WxProfitSharingOrder();
// updSharingOrder.updateTenantInfo(sharingOrder);
// updSharingOrder.setId(sharingOrder.getId());
// updSharingOrder.setOutSettleNo(frecord.getOutSettleNo());
// if (!shareResult.isSuccess()) {
// updSharingOrder.setSharingStatus(shareResult.getCode());
// updSharingOrder.setErrorMsg("REDO:"+shareResult.getMsg());
// updSharingOrder.setUpdateTime(new Date());
// wxProfitSharingOrderMapper.updateById(updSharingOrder);
// return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getCode(),updSharingOrder.getErrorMsg());
// }
// updSharingOrder.setSharingOrderNo(shareResult.getShareOrderNo());
// updSharingOrder.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_ACCEPTED.getCode());
// updSharingOrder.setErrorMsg("REDO:重试分账成功");
// updSharingOrder.setUpdateTime(new Date());
// wxProfitSharingOrderMapper.updateById(updSharingOrder);
// for (WxProfitSharingResult result:resultList) {
// wxProfitSharingResultMapper.insert(result);
// }
// return new ResultData(shareResult.getData());
}
// @Override
// public ResultData redoFinishSharingOrder(WxProfitSharingOrder sharingOrder) {
// if (!sharingOrder.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE_FINISH.getCode())) {
// return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
// }
// WxProfitShareAppInoDto dto = getAppinfo(sharingOrder);
// WxAppinfo appInfo = dto.getAppinfo();
// WxPayAccount payAccount = wxPayAccountMapper.selectById(appInfo.getPayId());
//
// PayShareResult shareResult = payServiceFactory.getPayShareAdapterService(dto.getPayWay(),payAccount.getPayVersion()).noReciverShare(appInfo,payAccount,sharingOrder,sharingOrder.getTransactionId(),sharingOrder.getPayAmount());
//
// WxProfitSharingOrder updSharingOrder = new WxProfitSharingOrder();
// updSharingOrder.updateTenantInfo(sharingOrder);
// updSharingOrder.setId(sharingOrder.getId());
// updSharingOrder.setOutSettleNo(sharingOrder.getOutSettleNo());
// if (!shareResult.isSuccess()) {
// updSharingOrder.setSharingStatus(shareResult.getCode());
// updSharingOrder.setErrorMsg("REDO:"+shareResult.getMsg());
// updSharingOrder.setUpdateTime(new Date());
// wxProfitSharingOrderMapper.updateById(updSharingOrder);
// return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getCode(),updSharingOrder.getErrorMsg());
// }
// updSharingOrder.setSharingOrderNo(shareResult.getShareOrderNo());
// updSharingOrder.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_ACCEPTED.getCode());
// updSharingOrder.setErrorMsg("REDO:重试分账成功");
// updSharingOrder.setUpdateTime(new Date());
// wxProfitSharingOrderMapper.updateById(updSharingOrder);
// return new ResultData(shareResult.getData());
// }
@Override
// @Transactional(isolation=Isolation.SERIALIZABLE, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
public ResultData createSharingOrder(WxSharingOrderDto sharingOrderDto,WxPayOrder payOrder,Integer payWay,Integer payVersion,Integer mchType,Integer share) {
public ResultData createSharingOrder(WxSharingOrderDto sharingOrderDto,WxPayOrder payOrder) {
PayShareAdapterService payShareAdapterService = payServiceFactory.getPayShareAdapterService(payWay,payVersion);
PayShareAdapterService payShareAdapterService = payServiceFactory.getPayShareAdapterService(payOrder.getPayVendor(),payOrder.getPayVersion() );
WxProfitSharingOrder record = payShareAdapterService.createSharingOrder(sharingOrderDto,mchType,share);
WxProfitSharingOrder record = payShareAdapterService.createSharingOrder(sharingOrderDto,payOrder.getMchType(),payOrder.getShare() );
//抖音模式涉及到佣金,无法实时分账
if(EnumPayWay.PAY_WAY_TT.getCode().equals(payWay)){
if(EnumPayWay.PAY_WAY_TT.getCode().equals(payOrder.getPayVendor() )){
return new ResultData();
}
if (!sharingOrderDto.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE.getCode()) &&
!sharingOrderDto.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_MULTI.getCode())){
return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
}
EnumProfitSharingOrderType shareType = EnumProfitSharingOrderType.getEnum(sharingOrderDto.getType());
if (null == shareType) {
throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),sharingOrderDto.getType()+"在分账方式中不存在。EnumProfitSharingOrderType");
}
WxAppinfo appInfo = getAppinfo(sharingOrderDto.getTenantId(),payWay);
WxPayAccount payAccount = wxPayAccountMapper.selectById(appInfo.getPayId());
PayShareResult shareResult = null;
JSONArray jsonArray = null;
//总分模式,收款账户分账
if (EnumPayMchType.TOTAL.getCode() == mchType) {
if(StringUtils.isNotBlank(record.getReceivers())){
jsonArray = JSONArray.parseArray(record.getReceivers());
shareResult = payShareAdapterService.haveReciversShare(appInfo, payAccount, record,
sharingOrderDto.getTransactionId(), jsonArray, shareType);
}else {
return new ResultData();
}
//直连模式,不用分账,直接完结分账
}else if (EnumPayMchType.DIRECT.getCode() == mchType) {
shareResult = payShareAdapterService.noReciverShare(appInfo, payAccount, record, sharingOrderDto.getTransactionId(), sharingOrderDto.getShareAmount());
//当前商户的收款账号
WxProfitSharingReceiver receiver = payShareAdapterService.getReceiver(sharingOrderDto, sharingOrderDto.getMerchantId(), payOrder.getTtPayWay(), mchType);
if (null == receiver) {
return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getCode(),"当前商户未配置收款账号");
}
StringBuffer sb = new StringBuffer("[{").append("\"description\":").append(receiver.getId()).append(",\"amount\":").append(sharingOrderDto.getShareAmount());
jsonArray = JSONArray.parseArray(sb.toString());
}
if (null == shareResult) {
return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getCode(),"当前支付模式调用分账未返回结果");
}
WxProfitSharingOrder updSharingOrder = new WxProfitSharingOrder();
updSharingOrder.updateTenantInfo(record);
updSharingOrder.setId(record.getId());
updSharingOrder.setOutSettleNo(record.getOutSettleNo());
if (!shareResult.isSuccess()) {
updSharingOrder.setSharingStatus(shareResult.getCode());
updSharingOrder.setErrorMsg(shareResult.getMsg());
updSharingOrder.setUpdateTime(new Date());
wxProfitSharingOrderMapper.updateById(updSharingOrder);
return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getCode(),updSharingOrder.getErrorMsg());
}
updSharingOrder.setSharingOrderNo(shareResult.getShareOrderNo());
updSharingOrder.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_ACCEPTED.getCode());
updSharingOrder.setUpdateTime(new Date());
wxProfitSharingOrderMapper.updateById(updSharingOrder);
final IdWorker idworker = IdWorker.get();
try {
jsonArray.stream().forEach(obj -> {
JSONObject receiver =(JSONObject) obj;
Date currentDate = new Date();
WxProfitSharingResult result = new WxProfitSharingResult();
result.setId(idworker.nextId());
result.updateTenantInfo(sharingOrderDto);
result.setMerchantId(record.getMerchantId());
result.setSharingOrderId(record.getId());
result.setSharingReceiverId(Long.valueOf(receiver.getString("description")));
result.setPayAmount(receiver.getInteger("amount"));
result.setCreateTime(currentDate);
result.setUpdateTime(currentDate);
result.setSharingStatus(EnumProfitSharingResultStatus.PROFIT_SHARING_RESULT_PENDING.getCode());
wxProfitSharingResultMapper.insert(result);
});
}catch (Exception e) {
logger.error("分账详细信息为null",e);
// return new ResultData(ErrorCode.PROFIT_SHARING_RECEIVER_INVALID);
}
return new ResultData(shareResult.getData());
ResultData resultData = redoSharingOrder(record);
return resultData;
// if (!sharingOrderDto.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE.getCode()) &&
// !sharingOrderDto.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_MULTI.getCode())){
// return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
// }
// EnumProfitSharingOrderType shareType = EnumProfitSharingOrderType.getEnum(sharingOrderDto.getType());
// if (null == shareType) {
// throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),sharingOrderDto.getType()+"在分账方式中不存在。EnumProfitSharingOrderType");
// }
// WxAppinfo appInfo = getAppinfo(sharingOrderDto.getTenantId(),payWay);
// WxPayAccount payAccount = wxPayAccountMapper.selectById(appInfo.getPayId());
// PayShareResult shareResult = null;
// JSONArray jsonArray = null;
//
// if(StringUtils.isNotBlank(record.getReceivers())){
// jsonArray = JSONArray.parseArray(record.getReceivers());
// }
//
// if(jsonArray == null || jsonArray.isEmpty()){
// shareResult = payShareAdapterService.noReciverShare(appInfo, payAccount, record, sharingOrderDto.getTransactionId(), sharingOrderDto.getShareAmount());
// }else{
// shareResult = payShareAdapterService.haveReciversShare(appInfo, payAccount, record, sharingOrderDto.getTransactionId(), jsonArray, shareType);
// }
//
// //总分模式,收款账户分账
//// if (EnumPayMchType.TOTAL.getCode() == mchType) {
//// if(StringUtils.isNotBlank(record.getReceivers())){
//// jsonArray = JSONArray.parseArray(record.getReceivers());
//// shareResult = payShareAdapterService.haveReciversShare(appInfo, payAccount, record,
//// sharingOrderDto.getTransactionId(), jsonArray, shareType);
//// }else {
//// return new ResultData();
//// }
//// //直连模式,不用分账,直接完结分账
//// }else if (EnumPayMchType.DIRECT.getCode() == mchType) {
//// shareResult = payShareAdapterService.noReciverShare(appInfo, payAccount, record, sharingOrderDto.getTransactionId(), sharingOrderDto.getShareAmount());
//// //当前商户的收款账号
//// WxProfitSharingReceiver receiver = payShareAdapterService.getReceiver(sharingOrderDto, sharingOrderDto.getMerchantId(), payOrder.getTtPayWay(), mchType);
//// if (null == receiver) {
//// return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getCode(),"当前商户未配置收款账号");
//// }
//// StringBuffer sb = new StringBuffer("[{").append("\"description\":").append(receiver.getId()).append(",\"amount\":").append(sharingOrderDto.getShareAmount());
//// jsonArray = JSONArray.parseArray(sb.toString());
//// }
// if (null == shareResult) {
// return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getCode(),"当前支付模式调用分账未返回结果");
// }
//
// WxProfitSharingOrder updSharingOrder = new WxProfitSharingOrder();
// updSharingOrder.updateTenantInfo(record);
// updSharingOrder.setId(record.getId());
// updSharingOrder.setOutSettleNo(record.getOutSettleNo());
// if (!shareResult.isSuccess()) {
// updSharingOrder.setSharingStatus(shareResult.getCode());
// updSharingOrder.setErrorMsg(shareResult.getMsg());
// updSharingOrder.setUpdateTime(new Date());
// wxProfitSharingOrderMapper.updateById(updSharingOrder);
// return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getCode(),updSharingOrder.getErrorMsg());
// }
// updSharingOrder.setSharingOrderNo(shareResult.getShareOrderNo());
// updSharingOrder.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_ACCEPTED.getCode());
// updSharingOrder.setUpdateTime(new Date());
// wxProfitSharingOrderMapper.updateById(updSharingOrder);
//
// if(jsonArray == null || jsonArray.isEmpty()){
// final IdWorker idworker = IdWorker.get();
// try {
// jsonArray.stream().forEach(obj -> {
// JSONObject receiver =(JSONObject) obj;
// Date currentDate = new Date();
// WxProfitSharingResult result = new WxProfitSharingResult();
// result.setId(idworker.nextId());
// result.updateTenantInfo(sharingOrderDto);
// result.setMerchantId(record.getMerchantId());
// result.setSharingOrderId(record.getId());
// result.setSharingReceiverId(Long.valueOf(receiver.getString("description")));
// result.setPayAmount(receiver.getInteger("amount"));
// result.setCreateTime(currentDate);
// result.setUpdateTime(currentDate);
// result.setSharingStatus(EnumProfitSharingResultStatus.PROFIT_SHARING_RESULT_PENDING.getCode());
// wxProfitSharingResultMapper.insert(result);
// });
// }catch (Exception e) {
// logger.error("分账详细信息为null",e);
//// return new ResultData(ErrorCode.PROFIT_SHARING_RECEIVER_INVALID);
// }
// }
//
// return new ResultData(shareResult.getData());
}
@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class})
public ResultData querySharingOrder(WxProfitSharingOrder wxProfitSharingOrder) {
public ResultData querySharingOrder(WxProfitSharingOrder sharingOrder) {
WxPayOrder wxPayOrder = wxPayOrderMapper.selectById(sharingOrder.getOrderId(),sharingOrder.getTenantId());
WxProfitSharingOrder record = new WxProfitSharingOrder();
if (wxPayOrder == null) {
throw new MallinkException(ErrorCode.ORDER_IS_NOT_FIND.getCode(),"wxProfitSharingOrder["+sharingOrder.getId()+"] 没有payOrder信息.");
}
WxProfitShareAppInoDto dto = getAppinfo(wxProfitSharingOrder);
WxAppinfo appInfo = dto.getAppinfo();
WxPayAccount payAccount = wxPayAccountMapper.selectById(appInfo.getPayId());
EnumPayWay enumPayWay = EnumPayWay.getEnum(wxPayOrder.getPayVendor());
EnumAppPlat enumPlat = enumPayWay.getPlat();
//是否已创建分账订单
record.setOrderId(wxProfitSharingOrder.getOrderId());
record.updateTenantInfo(wxProfitSharingOrder);
record = wxProfitSharingOrderMapper.selectOne(new QueryWrapper(record));
if (record == null)
return new ResultData(ErrorCode.ORDER_IS_NOT_FIND);
WxAppinfo cAppInfo = wxAppinfoService.getCAppInfo(sharingOrder, enumPlat);
WxPayAccount payAccount = wxPayAccountMapper.selectById(cAppInfo.getPayId());
PayShareQueryResult shareResult = payServiceFactory.getPayShareAdapterService(dto.getPayWay(),payAccount.getPayVersion()).shareQuery(appInfo, payAccount, record);
if (!shareResult.isSuccess()) {
throw new MallinkException(shareResult.getCode(),shareResult.getMsg());
PayShareAdapterService payShareAdapterService = payServiceFactory.getPayShareAdapterService(wxPayOrder.getPayVendor(),wxPayOrder.getPayVersion());
PayShareQueryResult payShareQueryResult = payShareAdapterService.shareQuery(cAppInfo, payAccount, sharingOrder);
if (!payShareQueryResult.isSuccess()) {
throw new MallinkException(payShareQueryResult.getCode(),payShareQueryResult.getMsg());
}
record.setSharingStatus(shareResult.getCode());
record.setUpdateTime(new Date());
wxProfitSharingOrderMapper.updateById(record);
if(EnumPayWay.PAY_WAY_TT.getCode().equals(dto.getPayWay())){
return new ResultData(shareResult.getData());
WxProfitSharingOrder sharingOrderUpd = new WxProfitSharingOrder();
sharingOrderUpd.setId(sharingOrder.getId());
sharingOrderUpd.updateTenantInfo(sharingOrder);
sharingOrderUpd.setSharingStatus(payShareQueryResult.getCode());
sharingOrderUpd.setUpdateTime(new Date());
wxProfitSharingOrderMapper.updateById(sharingOrderUpd);
if(EnumPayWay.PAY_WAY_TT.equals(enumPayWay)){
return new ResultData(payShareQueryResult.getData());
}
List<PayShareQueryResultReceivers> receivers = shareResult.getReceivers();
List<PayShareQueryResultReceivers> receivers = payShareQuery Result.getReceivers();
if (receivers != null) {
WxProfitSharingResult result = new WxProfitSharingResult();
result.setSharingOrderId(record.getId());
result.updateTenantInfo(wxProfitS haringOrder);
result.setSharingOrderId(sharingOrder .getId());
result.updateTenantInfo(s haringOrder);
List <WxProfitSharingResult> wxProfitSharingResultList = wxProfitSharingResultMapper.findList(result);
for(int j = 0; j < receivers.size() ; j++) {
PayShareQueryResultReceivers res = receivers.get(j);
@@ -418,9 +519,9 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
WxProfitSharingResult wxProfitSharingResult = new WxProfitSharingResult();
wxProfitSharingResult.updateTenantInfo(record );
wxProfitSharingResult.setMerchantId(record .getMerchantId());
wxProfitSharingResult.setSharingOrderId(record .getId());
wxProfitSharingResult.updateTenantInfo(sharingOrder );
wxProfitSharingResult.setMerchantId(sharingOrder .getMerchantId());
wxProfitSharingResult.setSharingOrderId(sharingOrder .getId());
wxProfitSharingResult.setSharingReceiverId(0L); //剩余钱分给特约商户,没有相应的接受者
List<WxProfitSharingResult> list = wxProfitSharingResultMapper.findList(wxProfitSharingResult);
@@ -445,70 +546,70 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
}
}
}
return new ResultData(share Result.getData());
return new ResultData(payShareQuery Result.getData());
}
// 分账完结
@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class})
public ResultData finishSharingOrder(WxPayOrder payOrder,WxSharingOrderDto sharingOrderDto,Integer payWay,Integer share) {
final IdWorker idworker = IdWorker.get();
if ((EnumPayWay.PAY_WAY_WECHAT.getCode().equals(payWay)
|| EnumPayWay.PAY_WAY_WECHAT_MA.getCode().equals(payWay)
|| EnumPayWay.PAY_WAY_WECHAT_WAP.getCode().equals(payWay))
&& EnumPayShare.NO.getCode().equals(share)){
logger.info("微信支付不分账模式下无需分账");
return new ResultData();
}
if (!sharingOrderDto.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE_FINISH.getCode())
&& !sharingOrderDto.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_MULTI_FINISH.getCode())){
return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
}
WxAppinfo appInfo = getAppinfo(sharingOrderDto.getTenantId(),payWay);
WxPayAccount payAccount = wxPayAccountMapper.selectById(appInfo.getPayId());
PayShareAdapterService payShareAdapterService = payServiceFactory.getPayShareAdapterService(payWay,payAccount.getPayVersion());
//分账检查是否已创建分账订单
WxProfitSharingOrder record = new WxProfitSharingOrder();
//创建分账订单
Date currentDate = new Date();
record.setId(idworker.nextId());
record.updateTenantInfo(payAccount);
record.setTransactionId(sharingOrderDto.getTransactionId());
record.setPayAmount(sharingOrderDto.getPayAmount());
record.setMerchantId(sharingOrderDto.getMerchantId());
record.setOrderId(sharingOrderDto.getOrderId());
record.setSharingStatus(payShareAdapterService.getSharingOrderInitialStatus());
record.setCreateTime(currentDate);
record.setUpdateTime(currentDate);
record.setPayTimeStart(sharingOrderDto.getPayTimeStart());
record.setPayTimeEnd(sharingOrderDto.getPayTimeEnd());
record.setType(sharingOrderDto.getType());
wxProfitSharingOrderMapper.insert(record);
//分账提交
PayShareResult shareResult = payShareAdapterService.noReciverShare(appInfo,payAccount,record,sharingOrderDto.getTransactionId(),sharingOrderDto.getShareAmount());
WxProfitSharingOrder updSharingOrder = new WxProfitSharingOrder();
updSharingOrder.updateTenantInfo(record);
updSharingOrder.setId(record.getId());
updSharingOrder.setOutSettleNo(record.getOutSettleNo());
if (!shareResult.isSuccess()) {
updSharingOrder.setSharingStatus(shareResult.getCode());
updSharingOrder.setErrorMsg(shareResult.getMsg());
updSharingOrder.setUpdateTime(new Date());
wxProfitSharingOrderMapper.updateById(updSharingOrder);
return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getCode(),updSharingOrder.getErrorMsg());
}
updSharingOrder.setSharingOrderNo(shareResult.getShareOrderNo());
updSharingOrder.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_ACCEPTED.getCode());
updSharingOrder.setUpdateTime(new Date());
wxProfitSharingOrderMapper.updateById(updSharingOrder);
return new ResultData(shareResult.getData());
}
// @Override
// @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class})
// public ResultData finishSharingOrder(WxPayOrder payOrder,WxSharingOrderDto sharingOrderDto,Integer payWay,Integer share) {
// final IdWorker idworker = IdWorker.get();
//
// if ((EnumPayWay.PAY_WAY_WECHAT.getCode().equals(payWay)
// || EnumPayWay.PAY_WAY_WECHAT_MA.getCode().equals(payWay)
// || EnumPayWay.PAY_WAY_WECHAT_WAP.getCode().equals(payWay))
// && EnumPayShare.NO.getCode().equals(share)){
// logger.info("微信支付不分账模式下无需分账");
// return new ResultData();
// }
//
// if (!sharingOrderDto.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE_FINISH.getCode())
// && !sharingOrderDto.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_MULTI_FINISH.getCode())){
// return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
// }
//
// WxAppinfo appInfo = getAppinfo(sharingOrderDto.getTenantId(),payWay);
// WxPayAccount payAccount = wxPayAccountMapper.selectById(appInfo.getPayId());
// PayShareAdapterService payShareAdapterService = payServiceFactory.getPayShareAdapterService(payWay,payAccount.getPayVersion());
//
// //分账检查是否已创建分账订单
// WxProfitSharingOrder record = new WxProfitSharingOrder();
// //创建分账订单
// Date currentDate = new Date();
// record.setId(idworker.nextId());
// record.updateTenantInfo(payAccount);
// record.setTransactionId(sharingOrderDto.getTransactionId());
// record.setPayAmount(sharingOrderDto.getPayAmount());
// record.setMerchantId(sharingOrderDto.getMerchantId());
// record.setOrderId(sharingOrderDto.getOrderId());
// record.setSharingStatus(payShareAdapterService.getSharingOrderInitialStatus());
// record.setCreateTime(currentDate);
// record.setUpdateTime(currentDate);
// record.setPayTimeStart(sharingOrderDto.getPayTimeStart());
// record.setPayTimeEnd(sharingOrderDto.getPayTimeEnd());
// record.setType(sharingOrderDto.getType());
// wxProfitSharingOrderMapper.insert(record);
//
// //分账提交
// PayShareResult shareResult = payShareAdapterService.noReciverShare(appInfo,payAccount,record,sharingOrderDto.getTransactionId(),sharingOrderDto.getShareAmount());
// WxProfitSharingOrder updSharingOrder = new WxProfitSharingOrder();
// updSharingOrder.updateTenantInfo(record);
// updSharingOrder.setId(record.getId());
// updSharingOrder.setOutSettleNo(record.getOutSettleNo());
// if (!shareResult.isSuccess()) {
// updSharingOrder.setSharingStatus(shareResult.getCode());
// updSharingOrder.setErrorMsg(shareResult.getMsg());
// updSharingOrder.setUpdateTime(new Date());
// wxProfitSharingOrderMapper.updateById(updSharingOrder);
// return new ResultData(ErrorCode.PROFIT_SHARING_REQUEST_FAILED.getCode(),updSharingOrder.getErrorMsg());
// }
//
// updSharingOrder.setSharingOrderNo(shareResult.getShareOrderNo());
// updSharingOrder.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_ACCEPTED.getCode());
// updSharingOrder.setUpdateTime(new Date());
// wxProfitSharingOrderMapper.updateById(updSharingOrder);
// return new ResultData(shareResult.getData());
// }
@Override
public PageInfo<WxProfitSharingOrderVo> listAsPage(WxProfitSharingOrderQueryVo order, Integer pageIndex, Integer pageSize) {
@@ -653,61 +754,61 @@ public class WxProfitSharingOrderServiceImpl implements WxProfitSharingOrderServ
}
//更新以前分账用户绑定微信号的分账数据,微信已经停止了微信号分账
@Override
@Deprecated
public void updatetttttttttttttt(String tenantId) {
WxProfitSharingOrder wxProfitSharingOrder = new WxProfitSharingOrder();
wxProfitSharingOrder.setTenantId(tenantId);
wxProfitSharingOrder.setType(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE.getCode());
wxProfitSharingOrder.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_APPLY_FAILED.getCode());
List<WxProfitSharingOrder> list = wxProfitSharingOrderMapper.findList(wxProfitSharingOrder);
list.stream().forEach(sharingOrder->{
if (!sharingOrder.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE.getCode())) {
return;
}
WxProfitShareAppInoDto dto = getAppinfo(sharingOrder);
WxAppinfo appInfo = dto.getAppinfo();
WxPayAccount payAccount = wxPayAccountMapper.selectById(appInfo.getPayId());
List<JSONObject> receivers;
receivers = JSONArray.parseArray(sharingOrder.getReceivers(), JSONObject.class);
if (receivers.size() != 1 )
return;
Integer amount = receivers.get(0).getInteger("amount");
if (!EnumProfitSharingReceiverType.PROFIT_SHARING_RECEIVER_PERSONAL_WECHATID.getMessage().equals(receivers.get(0).getString("type"))) {
return;
}
//准备分账列表
WxProfitSharingReceiver psReceiverQ = new WxProfitSharingReceiver();
psReceiverQ.updateTenantInfo(payAccount);
psReceiverQ.setMerchantId(sharingOrder.getMerchantId());
psReceiverQ.setSharingType(EnumProfitSharingType.PROFIT_SHARING_TYPE_WECHAT.getCode());
psReceiverQ.setStatus(EnumProfitSharingReceiverStatus.PROFIT_SHARING_RECEIVER_STATUS_VALID.getCode());
List<WxProfitSharingReceiver> psReceiverList = wxProfitSharingReceiverMapper.findList(psReceiverQ);
if (null == psReceiverList || psReceiverList.size() !=1 ) {
return ;
}
JSONArray realReceivers = new JSONArray();
psReceiverList.stream().forEach(receiver->{
JSONObject jo = new JSONObject();
jo.put("type",payServiceFactory.getPayShareAdapterService(dto.getPayWay(),payAccount.getPayVersion()).getShareAccount(receiver.getReceiverType()).getMessage());
jo.put("account",receiver.getReceiverAccount());
jo.put("amount", amount);
//jo.put("description",receiver.getReceiverComments()); //改为存ID,
jo.put("description",receiver.getId().toString()); //为重试做准备
realReceivers.add(jo);
});
sharingOrder.setReceivers(JSON.toJSONString(realReceivers));
wxProfitSharingOrderMapper.updateById(sharingOrder);
});
}
// @Override
// @Deprecated
// public void updatetttttttttttttt(String tenantId) {
// WxProfitSharingOrder wxProfitSharingOrder = new WxProfitSharingOrder();
// wxProfitSharingOrder.setTenantId(tenantId);
// wxProfitSharingOrder.setType(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE.getCode());
// wxProfitSharingOrder.setSharingStatus(EnumProfitSharingOrderStatus.PROFIT_SHARING_APPLY_FAILED.getCode());
// List<WxProfitSharingOrder> list = wxProfitSharingOrderMapper.findList(wxProfitSharingOrder);
// list.stream().forEach(sharingOrder->{
// if (!sharingOrder.getType().equals(EnumProfitSharingOrderType.PROFIT_SHARING_SINGLE.getCode())) {
// return;
// }
//
// WxProfitShareAppInoDto dto = getAppinfo(sharingOrder);
// WxAppinfo appInfo = dto.getAppinfo();
// WxPayAccount payAccount = wxPayAccountMapper.selectById(appInfo.getPayId());
//
// List<JSONObject> receivers;
//
// receivers = JSONArray.parseArray(sharingOrder.getReceivers(), JSONObject.class);
// if (receivers.size() != 1 )
// return;
//
// Integer amount = receivers.get(0).getInteger("amount");
// if (!EnumProfitSharingReceiverType.PROFIT_SHARING_RECEIVER_PERSONAL_WECHATID.getMessage().equals(receivers.get(0).getString("type"))) {
// return;
// }
//
// //准备分账列表
// WxProfitSharingReceiver psReceiverQ = new WxProfitSharingReceiver();
// psReceiverQ.updateTenantInfo(payAccount);
// psReceiverQ.setMerchantId(sharingOrder.getMerchantId());
// psReceiverQ.setSharingType(EnumProfitSharingType.PROFIT_SHARING_TYPE_WECHAT.getCode());
// psReceiverQ.setStatus(EnumProfitSharingReceiverStatus.PROFIT_SHARING_RECEIVER_STATUS_VALID.getCode());
// List<WxProfitSharingReceiver> psReceiverList = wxProfitSharingReceiverMapper.findList(psReceiverQ);
// if (null == psReceiverList || psReceiverList.size() !=1 ) {
// return ;
// }
//
// JSONArray realReceivers = new JSONArray();
// psReceiverList.stream().forEach(receiver->{
// JSONObject jo = new JSONObject();
// jo.put("type",payServiceFactory.getPayShareAdapterService(dto.getPayWay(),payAccount.getPayVersion()).getShareAccount(receiver.getReceiverType()).getMessage());
// jo.put("account",receiver.getReceiverAccount());
// jo.put("amount", amount);
// //jo.put("description",receiver.getReceiverComments()); //改为存ID,
// jo.put("description",receiver.getId().toString()); //为重试做准备
// realReceivers.add(jo);
// });
//
//
// sharingOrder.setReceivers(JSON.toJSONString(realReceivers));
// wxProfitSharingOrderMapper.updateById(sharingOrder);
// });
// }
@Override
public ResultData handerSharingOrder(WxProfitSharingOrder sharingOrder) {