123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618 |
- package com.caimei.modules.order.service;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.caimei.modules.order.dao.NewOrderDao;
- import com.caimei.modules.order.dao.NewShopOrderDao;
- import com.caimei.modules.order.entity.*;
- import com.caimei.modules.order.utils.Disguiser;
- import com.caimei.modules.user.dao.CmReportingClubMapper;
- import com.caimei.modules.user.entity.CmReportingClub;
- import com.caimei.redis.RedisService;
- import com.caimei.utils.MathUtil;
- import com.thinkgem.jeesite.common.service.BaseService;
- import okhttp3.*;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import javax.annotation.Resource;
- import java.io.IOException;
- import java.lang.reflect.Field;
- import java.math.BigDecimal;
- import java.text.SimpleDateFormat;
- import java.util.*;
- import java.util.concurrent.TimeUnit;
- import java.util.concurrent.atomic.AtomicReference;
- import java.util.stream.Collectors;
- @Service
- @Transactional(readOnly = true)
- public class SplitAccountService extends BaseService {
- public static OkHttpClient client = new OkHttpClient.Builder()
- .connectTimeout(3, TimeUnit.SECONDS)
- .readTimeout(20, TimeUnit.SECONDS)
- .build();
- @Resource
- private NewOrderDao newOrderDao;
- @Resource
- private RedisService redisService;
- @Resource
- private NewShopOrderDao newShopOrderDao;
- @Resource
- private CmReportingClubMapper cmReportingClubMapper;
- /**
- * !!!!注意:分帐时合利宝收费规则为谁分收谁0.1%手续费,这里是供应商承担的,如果要采美,服务商承担各自的费用,要提前计算好!!!
- *
- * 当前版本分帐逻辑变更,前台传入分帐帐号及金额,直接分帐
- * splits : "E123456,10;E123456,20;E123456,30"
- *
- * @param shopOrderId
- */
- @Transactional(readOnly = false, rollbackFor = Exception.class)
- public void SplitByHand(String shopOrderId, String splits) {
- logger.info("【手动分账开始】>>>>>>>>>>手动分账");
- /** todo
- * 校验splits参数总金额是否正确
- */
- String[] split = splits.split(";");
- if (split.length <= 0) {
- logger.info("分帐帐号及金额输入不正确!");
- return;
- }
- ArrayList<AccountPayOrder.AccountPayOrderExt.SplitBillRule> splitBillRules = new ArrayList<>();
- // 保存分帐参数,固定三方:成本,采美,服务商
- List<SplitAccountPo> splitBillDetail = new ArrayList<>();
- ShopOrderVo shopOrder = newOrderDao.getShopOrderListByOrderId(Integer.valueOf(shopOrderId));
- Double costPrice = 0d;
- for (int i = 0; i < split.length; i++) {
- AccountPayOrder.AccountPayOrderExt.SplitBillRule splitBillRule = new AccountPayOrder.AccountPayOrderExt.SplitBillRule();
- String[] rules = split[i].split(",");
- /**
- * splits : "E123456,10;E123456,20;E123456,30"
- */
- if (i > 0) {
- BigDecimal cost = BigDecimal.valueOf(Double.parseDouble(rules[1]));
- splitBillRule.setSplitBillAmount(cost);
- splitBillRule.setSplitBillMerchantNo(rules[0]);
- splitBillRules.add(splitBillRule);
- }
- /**
- * cm_split_account 分帐详情参数
- */
- Integer productType = 0 == i ? 1 : 1 == i ? 3 : 5;
- Integer type = 0 == i ? 4 : 5;
- SplitAccountPo splitAccount = new SplitAccountPo();
- splitAccount.setOrderId(shopOrder.getOrderId());
- splitAccount.setShopOrderId(shopOrder.getShopOrderId());
- /** 前台获取金额
- * (暂定前台输入金额未计算手续费,采美和第三方分帐出去金额总量的0.1%为手续费,从成本扣除)
- */
- splitAccount.setSplitAccount(Double.parseDouble(rules[1]));
- splitAccount.setProductType(productType);
- splitAccount.setType(type);
- // 前台获取
- splitAccount.setSubUserNo(rules[0]);
- splitBillDetail.add(splitAccount);
- if (0 == i) {
- costPrice = Double.parseDouble(rules[1]);
- }
- }
- if (null != splitBillRules && splitBillRules.size() > 0) {
- //第三方分账接口
- try {
- AccountPayOrder accountPayOrder = new AccountPayOrder();
- accountPayOrder.setP1_bizType("AccountPaySub");
- accountPayOrder.setP2_signType("MD5");
- String format1 = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss.SSS").format(new Date());
- accountPayOrder.setP3_timestamp(format1);
- String substring = format1.substring(20);
- // fz+当前微秒时间+原唯一订单号
- accountPayOrder.setP4_orderId("FZ" + substring + shopOrderId);
- //付款账户子订单绑定商户号(付款商户商编)
- accountPayOrder.setP5_customerNumber(shopOrder.getSplitCode());
- AccountPayOrder.AccountPayOrderExt accountPayOrderExt = new AccountPayOrder.AccountPayOrderExt();
- //收款账户商编 填写splitBillRules时候不填写MerchantNo,Amount并且即使填写这两个参数不生效!!
- //accountPayOrderExt.setInMerchantNo(splitMoneyVo.getName());
- //accountPayOrderExt.setAmount(splitMoneyVo.getSplitMoney());
- accountPayOrderExt.setOrderType(AccountPayOrderType.TRANSFER);
- accountPayOrderExt.setServerCallbackUrl(Constant.prodSplit);
- accountPayOrderExt.setGoodsName("分账");
- if (null != splitBillRules && splitBillRules.size() > 0) {
- accountPayOrderExt.setSplitBillRules(splitBillRules);
- }
- String ext = JSON.toJSONString(accountPayOrderExt);
- logger.info("分账规则串json串:" + ext);
- accountPayOrder.setP6_ext(ext);
- // 生成签名
- StringBuilder builder = new StringBuilder();
- builder.append(Constant.SPLIT)
- .append(accountPayOrder.getP1_bizType()).append(Constant.SPLIT)
- .append(accountPayOrder.getP2_signType()).append(Constant.SPLIT)
- .append(accountPayOrder.getP3_timestamp()).append(Constant.SPLIT)
- .append(accountPayOrder.getP4_orderId()).append(Constant.SPLIT)
- .append(accountPayOrder.getP5_customerNumber()).append(Constant.SPLIT)
- .append(accountPayOrder.getP6_ext()).append(Constant.SPLIT)
- .append(Constant.XUNI);
- String sign = Disguiser.disguiseMD5(builder.toString().trim());
- Map<String, String> bean = convertBean(accountPayOrder);
- logger.info("--------------------> 发送分账参数: " + bean);
- Map<String, String> map = postForm(bean, Constant.FZ, sign, Map.class);
- logger.info("----------------分账返回数据: " + map.toString());
- if (map != null) {
- String code = map.get("rt5_retCode");
- if (!"0000".equals(code)) {
- String msg = map.get("rt6_retMsg");
- logger.info("【手动分账】>>>>>>>>>>手动分账失败>>>>>>>msg:" + msg);
- } else {
- // 返回值0000分帐成功
- for (SplitAccountPo splitAccount : splitBillDetail) {
- splitAccount.setPayStatus(1);
- // 保存分账详情
- newOrderDao.insertSplitAccount(splitAccount);
- }
- /**
- * 付款单
- */
- // 修改子订单付款状态及付款金额
- newOrderDao.updateShopOrderByPayStatus(Integer.valueOf(shopOrderId), costPrice, shopOrder.getPayStatus());
- SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String currentTime2 = format2.format(new Date());
- // 保存付款单表
- PayShopPo payShop = new PayShopPo();
- payShop.setShopId(shopOrder.getShopId());
- payShop.setName("线上支付分账");
- payShop.setTotalAmount(costPrice);
- payShop.setWipePayment(0d);
- payShop.setPayType(6);
- payShop.setStatus(1);
- payShop.setDelFlag(0);
- payShop.setApplyTime(currentTime2);
- payShop.setReviewTime(currentTime2);
- payShop.setPayTime(currentTime2);
- newOrderDao.insertPayShop(payShop);
- // 保存 付供应商记录
- PayShopRecordPo shopRecord = new PayShopRecordPo();
- shopRecord.setShopId(shopOrder.getShopId());
- shopRecord.setShopOrderId(shopOrder.getShopOrderId());
- shopRecord.setShopOrderNo(shopOrder.getShopOrderNo());
- shopRecord.setPayAmount(costPrice);
- shopRecord.setWipePayment(0d);
- shopRecord.setPayType(6);
- shopRecord.setPayTime(currentTime2);
- shopRecord.setPayShopId(payShop.getId());
- shopRecord.setStatus(1);
- shopRecord.setDelFlag(0);
- newOrderDao.insertPayShopRecord(shopRecord);
- // 子订单是否全部付款
- List<String> payStatus = newShopOrderDao.findPayStatusByOrderID(shopOrder.getOrderId());
- boolean isPay = true;
- for (String shops : payStatus) {
- if (!"3".equals(shops)) {
- isPay = false;
- break;
- }
- }
- // 修改主订单付款状态
- if (isPay) {
- newOrderDao.updateOrderByPayStatus(shopOrder.getOrderId(), 3);
- } else {
- newOrderDao.updateOrderByPayStatus(shopOrder.getOrderId(), 2);
- }
- redisService.remove("XSFZMDS");
- logger.info("【手动分账】>>>>>>>>>>此订单分账结束");
- }
- }
- } catch (Exception e) {
- logger.error("【手动分账】>>>>>>>>>>错误信息", e);
- }
- }
- }
- @Transactional(readOnly = false, rollbackFor = Exception.class)
- public void SplitAccount(String[] shopOrderIds) {
- logger.info("【手动分账开始】>>>>>>>>>>手动分账");
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(new Date());
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String currentTime = format.format(calendar.getTime());
- List<String> ids = new ArrayList<>();
- ids = Arrays.stream(shopOrderIds).collect(Collectors.toList());
- ids.forEach(i -> {
- // 查询未分账已支付收款
- List<OrderReceiptRelationPo> orderRelations = newOrderDao.getUndividedPaidReceipt(currentTime, i);
- List<SplitAccountPo> splitBillDetail = new ArrayList<>();
- AtomicReference<Double> costPrice = new AtomicReference<Double>(0d);
- AtomicReference<Double> cmCostPrice = new AtomicReference<Double>(0d);
- AtomicReference<Double> organizeCostPrice = new AtomicReference<Double>(0d);
- AtomicReference<Double> total = new AtomicReference<Double>(0d);
- // 收款对应的订单信息
- ShopOrderVo shopOrder = newOrderDao.getShopOrderListByOrderId(Integer.valueOf(i));
- for (OrderReceiptRelationPo orderRelation : orderRelations) {
- logger.info("【分账】>>>>>>>>>>子订单id:" + orderRelation.getShopOrderId() + "进入分账");
- setSplitAccountDetail(costPrice, organizeCostPrice, cmCostPrice, total, shopOrder, orderRelation);
- }
- /** todo
- * 当前版本优惠券和促销活动为商家承担
- */
- if (shopOrder.getCouponAmount() > 0 || shopOrder.getPromotionFullReduction() > 0) {
- costPrice.set(MathUtil.sub(costPrice, MathUtil.add(shopOrder.getCouponAmount(), shopOrder.getPromotionFullReduction())).doubleValue());
- }
- if (MathUtil.compare(total, MathUtil.add(MathUtil.add(costPrice, cmCostPrice), organizeCostPrice)) < 0) {
- logger.info("收款总金额不足分帐------------->total: " + total.get()
- + "costPrice: " + costPrice.get() + "----------"
- + "cmCostPrice: " + cmCostPrice.get() + "-------------"
- + "organizeCostPrice: " + organizeCostPrice.get() + "---------------");
- return;
- }
- /**
- * 线上订单,付第三方如果不为0,需要从供应商成本中支付
- */
- double payOther = newOrderDao.findPayOther(Integer.valueOf(i));
- if (MathUtil.compare(payOther, 0) > 0) {
- SplitAccountPo splitAccount = new SplitAccountPo();
- splitAccount.setOrderId(shopOrder.getOrderId());
- splitAccount.setShopOrderId(shopOrder.getShopOrderId());
- splitAccount.setSplitAccount(payOther);
- splitAccount.setProductType(6);
- splitAccount.setType(6);
- splitAccount.setSubUserNo(Constant.CUSTOMERNUM3);
- logger.info("付第三方分账参数------------->" + splitAccount);
- splitBillDetail.add(splitAccount);
- // 当前版本付第三方从供应商成本支出
- costPrice.updateAndGet(v -> MathUtil.sub(v, payOther).doubleValue());
- }
- if (costPrice.get() > 0) {
- SplitAccountPo splitAccount = new SplitAccountPo();
- splitAccount.setShopOrderId(shopOrder.getShopOrderId());
- splitAccount.setOrderId(shopOrder.getOrderId());
- splitAccount.setSplitAccount(costPrice.get());
- splitAccount.setProductType(1);
- // 该商品设置了商户号
- splitAccount.setType(4);
- splitAccount.setSubUserNo(shopOrder.getSplitCode());
- logger.info("成本分账参数------------->" + splitAccount.toString());
- splitBillDetail.add(splitAccount);
- }
- /** 成本分完,金额未尽,有组织佣金的情况,优先组织佣金
- * 当前版本,采美供应商上架丽格商城,会存在三成本均>0的情况
- * 若无组织佣金,所有剩余金额归于采美佣金
- */
- if (organizeCostPrice.get() > 0) {
- SplitAccountPo splitAccount = new SplitAccountPo();
- Integer identity = newOrderDao.findUserIdentityByOrder(shopOrder.getOrderId());
- splitAccount.setShopOrderId(shopOrder.getShopOrderId());
- splitAccount.setOrderId(shopOrder.getOrderId());
- splitAccount.setSplitAccount(organizeCostPrice.get());
- splitAccount.setProductType(identity == 8 ? 7 : 5);
- // 该商品设置了商户号
- splitAccount.setType(4);
- String code = null;
- //分销人员分账修改报备记录
- if (identity == 8) {
- CmReportingClub cmReportingClubById = cmReportingClubMapper.getByCmReportingClub(new CmReportingClub().orderId(shopOrder.getOrderId()).orderStatus(1));
- if (null != cmReportingClubById) {
- //分销团队分账号
- code = cmReportingClubById.splitCode();
- cmReportingClubMapper.updateCmReportingClub(new CmReportingClub()
- .id(cmReportingClubById.id())
- .splitSettleStatus(2)
- );
- } else {
- logger.info("分销订单分账异常,订单Id:" + shopOrder.getOrderId() + ">>>>!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
- return;
- }
- } else {
- code = newOrderDao.findSplitCodeByOrganize(shopOrder.getOrganizeId());
- }
- String splitName = identity == 8 ? "分销团队" : "组织";
- if (StringUtils.isBlank(code)) {
- logger.info(splitName + "的分帐号未空,无法分帐" + splitName + "佣金!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
- return;
- }
- splitAccount.setSubUserNo(code);
- logger.info(splitName + "佣金分账参数------------->" + splitAccount.toString());
- splitBillDetail.add(splitAccount);
- }
- /** 当前版本还要考虑分组织/集团佣金
- * 如果还有钱则为佣金,分到网络
- */
- if (cmCostPrice.get() > 0) {
- // 此处不考虑手续费,外部计算总额
- SplitAccountPo splitAccount = new SplitAccountPo();
- splitAccount.setOrderId(shopOrder.getOrderId());
- splitAccount.setShopOrderId(shopOrder.getShopOrderId());
- splitAccount.setSplitAccount(cmCostPrice.get());
- splitAccount.setProductType(3);
- splitAccount.setType(5);
- splitAccount.setSubUserNo(Constant.CUSTOMERNUM2);
- logger.info("佣金分账参数------------->" + splitAccount);
- splitBillDetail.add(splitAccount);
- }
- HashMap<String, BigDecimal> sbm = new HashMap<>();
- for (SplitAccountPo splitAccountPo : splitBillDetail) {
- String subUserNo = splitAccountPo.getSubUserNo();
- // 计算当前商户号总分账金额
- if (sbm.containsKey(subUserNo)) {
- BigDecimal v = MathUtil.add(sbm.get(subUserNo), splitAccountPo.getSplitAccount());
- sbm.put(subUserNo, v);
- } else {
- sbm.put(subUserNo, BigDecimal.valueOf(splitAccountPo.getSplitAccount()));
- }
- // splitcode相同的收款的时候已经是成本已分帐
- if (subUserNo.equals(shopOrder.getSplitCode())) {
- // 供应商自己收款,此部分金额留在自己商户号,作为成本分账
- splitAccountPo.setPayStatus(1);
- // 保存分账详情
- newOrderDao.insertSplitAccount(splitAccountPo);
- }
- }
- ArrayList<AccountPayOrder.AccountPayOrderExt.SplitBillRule> splitBillRules = new ArrayList<>();
- sbm.forEach((key, value) -> {
- /**
- * 不是自己的splitcode分走,是自己的不动
- * 把成本之外的金额加入splitRule分账参数,延续上面逻辑,供应商自己的成本由自己的商户号收款,
- * 自己成本部分不需要加入分账参数,仅需在else中做业务表参数处理
- */
- if (!key.equals(shopOrder.getSplitCode())) {
- // 佣金
- AccountPayOrder.AccountPayOrderExt.SplitBillRule splitBillRule = new AccountPayOrder.AccountPayOrderExt.SplitBillRule();
- // 分账计算的时候未处理手续费,总额处理手续费 x + x * 0.1% = value
- BigDecimal div = MathUtil.div(value, 1.001, 2);
- if (MathUtil.sub(value, div).doubleValue() < 0.01) {
- // 如果手续费小于最低手续费,取0.01
- div = MathUtil.round(MathUtil.sub(value, 0.01), 2);
- }
- splitBillRule.setSplitBillAmount(div);
- splitBillRule.setSplitBillMerchantNo(key);
- splitBillRules.add(splitBillRule);
- } else {
- // 修改子订单付款状态及付款金额
- newOrderDao.updateShopOrderByPayStatus(Integer.valueOf(i), value.doubleValue(), shopOrder.getPayStatus());
- SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String currentTime2 = format2.format(new Date());
- // 保存付款单表
- PayShopPo payShop = new PayShopPo();
- payShop.setShopId(shopOrder.getShopId());
- payShop.setName("线上支付分账");
- payShop.setTotalAmount(value.doubleValue());
- payShop.setWipePayment(0d);
- payShop.setPayType(6);
- payShop.setStatus(1);
- payShop.setDelFlag(0);
- payShop.setApplyTime(currentTime2);
- payShop.setReviewTime(currentTime2);
- payShop.setPayTime(currentTime2);
- newOrderDao.insertPayShop(payShop);
- // 保存 付供应商记录
- PayShopRecordPo shopRecord = new PayShopRecordPo();
- shopRecord.setShopId(shopOrder.getShopId());
- shopRecord.setShopOrderId(shopOrder.getShopOrderId());
- shopRecord.setShopOrderNo(shopOrder.getShopOrderNo());
- shopRecord.setPayAmount(value.doubleValue());
- shopRecord.setWipePayment(0d);
- shopRecord.setPayType(6);
- shopRecord.setPayTime(currentTime2);
- shopRecord.setPayShopId(payShop.getId());
- shopRecord.setStatus(1);
- shopRecord.setDelFlag(0);
- newOrderDao.insertPayShopRecord(shopRecord);
- // 子订单是否全部付款
- List<String> payStatus = newShopOrderDao.findPayStatusByOrderID(shopOrder.getOrderId());
- boolean isPay = true;
- for (String shops : payStatus) {
- if (!"3".equals(shops)) {
- isPay = false;
- break;
- }
- }
- // 修改主订单付款状态
- if (isPay) {
- newOrderDao.updateOrderByPayStatus(shopOrder.getOrderId(), 3);
- } else {
- newOrderDao.updateOrderByPayStatus(shopOrder.getOrderId(), 2);
- }
- }
- });
- /**
- * 在上述else代码逻辑中已完成成本的业务表参数处理,在分账详情中排除成本,剩余佣金部分在分账结束收到
- * 成功返回码时处理(成本不参与分账)
- */
- splitBillDetail.removeIf(s -> s.getSubUserNo().equals(shopOrder.getSplitCode()));
- if (null != splitBillRules && splitBillRules.size() > 0) {
- //第三方分账接口
- try {
- AccountPayOrder accountPayOrder = new AccountPayOrder();
- accountPayOrder.setP1_bizType("AccountPaySub");
- accountPayOrder.setP2_signType("MD5");
- String format1 = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss.SSS").format(new Date());
- accountPayOrder.setP3_timestamp(format1);
- String substring = format1.substring(20);
- // fz+当前微秒时间+原唯一订单号
- accountPayOrder.setP4_orderId("FZ" + substring + shopOrder.getShopOrderId());
- //付款账户子订单绑定商户号
- accountPayOrder.setP5_customerNumber(shopOrder.getSplitCode());
- AccountPayOrder.AccountPayOrderExt accountPayOrderExt = new AccountPayOrder.AccountPayOrderExt();
- //收款账户商编 填写splitBillRules时候不填写MerchantNo,Amount并且即使填写这两个参数不生效!!
- //accountPayOrderExt.setInMerchantNo(splitMoneyVo.getName());
- //accountPayOrderExt.setAmount(splitMoneyVo.getSplitMoney());
- accountPayOrderExt.setOrderType(AccountPayOrderType.TRANSFER);
- accountPayOrderExt.setServerCallbackUrl(Constant.prodSplit);
- accountPayOrderExt.setGoodsName("分账");
- if (null != splitBillRules && splitBillRules.size() > 0) {
- accountPayOrderExt.setSplitBillRules(splitBillRules);
- }
- String ext = JSON.toJSONString(accountPayOrderExt);
- logger.info("分账规则串json串:" + ext);
- accountPayOrder.setP6_ext(ext);
- // 生成签名
- StringBuilder builder = new StringBuilder();
- builder.append(Constant.SPLIT)
- .append(accountPayOrder.getP1_bizType()).append(Constant.SPLIT)
- .append(accountPayOrder.getP2_signType()).append(Constant.SPLIT)
- .append(accountPayOrder.getP3_timestamp()).append(Constant.SPLIT)
- .append(accountPayOrder.getP4_orderId()).append(Constant.SPLIT)
- .append(accountPayOrder.getP5_customerNumber()).append(Constant.SPLIT)
- .append(accountPayOrder.getP6_ext()).append(Constant.SPLIT)
- .append(Constant.XUNI);
- String sign = Disguiser.disguiseMD5(builder.toString().trim());
- Map<String, String> bean = convertBean(accountPayOrder);
- logger.info("--------------------> 发送分账参数: " + bean);
- Map<String, String> map = postForm(bean, Constant.FZ, sign, Map.class);
- logger.info("----------------分账返回数据: " + map.toString());
- if (map != null) {
- String code = map.get("rt5_retCode");
- if (!"0000".equals(code)) {
- String msg = map.get("rt6_retMsg");
- logger.info("【手动分账】>>>>>>>>>>手动分账失败>>>>>>>msg:" + msg);
- } else {
- for (SplitAccountPo splitAccount : splitBillDetail) {
- splitAccount.setPayStatus(1);
- // 保存分账详情
- newOrderDao.insertSplitAccount(splitAccount);
- }
- redisService.remove("XSFZMDS");
- logger.info("【手动分账】>>>>>>>>>>此订单分账结束");
- }
- }
- } catch (Exception e) {
- logger.error("【手动分账】>>>>>>>>>>错误信息", e);
- }
- }
- });
- }
- public void setSplitAccountDetail(AtomicReference<Double> costPrice, AtomicReference<Double> organizePrice,
- AtomicReference<Double> cmCostPrice, AtomicReference<Double> total,
- ShopOrderVo shopOrder, OrderReceiptRelationPo orderRelation) {
- // 待分账总金额
- double splitAmount = orderRelation.getAssociateAmount();
- total.updateAndGet(v -> MathUtil.add(v, splitAmount).doubleValue());
- // 总手续费
- double procedureFee;
- if (12 == orderRelation.getPayType()) {
- procedureFee = 10.00;
- } else if (17 == orderRelation.getPayType()) {
- //b2c 0.2%
- procedureFee = MathUtil.mul(splitAmount, 0.002, 2).doubleValue();
- //b2c最低手续费0.1
- if (procedureFee < 0.1) {
- procedureFee = 0.1;
- }
- } else if (13 == orderRelation.getPayType() || 15 == orderRelation.getPayType()) {
- //微信0.65%
- procedureFee = MathUtil.mul(splitAmount, 0.0065, 2).doubleValue();
- } else if (29 == orderRelation.getPayType()) {
- procedureFee = MathUtil.mul(splitAmount, 0.003, 2).doubleValue();
- if (procedureFee < 0.1) {
- procedureFee = 0.1;
- }
- } else if (30 == orderRelation.getPayType()) {
- procedureFee = MathUtil.mul(splitAmount, 0.006, 2).doubleValue();
- if (procedureFee < 0.1) {
- procedureFee = 0.1;
- }
- } else {
- //手续费 其他0.25%
- procedureFee = MathUtil.mul(splitAmount, 0.0025, 2).doubleValue();
- }
- if (MathUtil.compare(procedureFee, 0.01) <= 0) {
- procedureFee = 0.01;
- }
- // 商品数据
- List<OrderProductVo> orderProductList = newOrderDao.getOrderProductByShopOrderId(shopOrder.getShopOrderId());
- for (OrderProductVo orderProduct : orderProductList) {
- /** 价格 * 数量
- * 前版本为全比例成本,当前版本重新加回固定成本,新增字段cmCostPrice(平台服务费,分帐分采美),organizeCostPrice(组织/集团成本。分帐分组织)
- * 在此规则下,分帐方式取cop固定成本值。因存在成本修改,不能取cm_sku实时更新成本!
- */
- costPrice.updateAndGet(v -> MathUtil.add(v, MathUtil.mul(orderProduct.getCostPrice(), orderProduct.getNum(), 2)).doubleValue());
- organizePrice.updateAndGet(v -> MathUtil.add(v, MathUtil.mul(orderProduct.getOrganizeCostPrice(), orderProduct.getNum(), 2)).doubleValue());
- cmCostPrice.updateAndGet(v -> MathUtil.add(v, MathUtil.mul(orderProduct.getCmCostPrice(), orderProduct.getNum(), 2)).doubleValue());
- }
- //手续费承担方 线上支付手续费:默认1采美承担,2供应商承担
- Integer supportFlag = newOrderDao.findSupport(shopOrder.getShopOrderId());
- if (2 == supportFlag) {
- //供应商自己承担手续费
- costPrice.set(costPrice.get() - procedureFee);
- } else {
- cmCostPrice.set(cmCostPrice.get() - procedureFee);
- }
- }
- public <T> T postForm(Map<String, String> params, String url, String sign, Class<T> clazz) {
- FormBody.Builder builder = new FormBody.Builder();
- for (Map.Entry<String, String> entry : params.entrySet()) {
- builder.add(entry.getKey(), entry.getValue());
- }
- builder.add("sign", sign);
- Request request = new Request.Builder() // okHttp post
- .url(url)
- .post(builder.build())
- .build();
- Response response = null;
- try {
- response = client.newCall(request).execute();
- } catch (IOException e) {
- throw new IllegalStateException("请求出错", e);
- }
- if (!response.isSuccessful()) {
- try {
- logger.info(response.body().string());
- } catch (IOException e) {
- e.printStackTrace();
- }
- throw new RuntimeException("请求失败了: http response code: " + response.code());
- }
- ResponseBody body = response.body();
- String content = null;
- try {
- content = body.string();
- } catch (IOException e) {
- throw new IllegalStateException("IO异常", e);
- }
- JSONObject res = JSON.parseObject(content);
- if (!res.getBooleanValue("rt4_success")) {
- logger.error("error: " + res.getString("rt6_retMsg"));
- }
- /** rt4_success 为 true,需验签 **/
- return res.toJavaObject(clazz);
- }
- public Map<String, String> convertBean(Object bean) {
- Class clazz = bean.getClass();
- Field[] fields = clazz.getDeclaredFields();
- for (Field f : fields) {
- f.setAccessible(true);
- }
- try {
- Map<String, String> retMap = new LinkedHashMap<>();
- for (Field f : fields) {
- String key = f.toString().substring(f.toString().lastIndexOf(".") + 1);
- Object value = f.get(bean);
- if (value == null) {
- value = "";
- }
- retMap.put(key, (String) value);
- }
- return retMap;
- } catch (Exception e) {
- logger.info("分账", e);
- throw new IllegalStateException("分账异常", e);
- }
- }
- }
|