package com.szwl.controller;
import cn.com.crbank.ommo.bean.ResultMessage;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huifu.bspay.sdk.opps.core.utils.DateTools;
import com.szwl.constant.HuifuConstant;
import com.szwl.constant.ResponseCodesEnum;
import com.szwl.feign.bean.PayFeign;
import com.szwl.constant.JoinpayConstant;
import com.szwl.model.bo.R;
import com.szwl.model.bo.ResponseModel;
import com.szwl.model.entity.*;
import com.szwl.service.*;
import com.szwl.service.es.EsTOrderService;
import com.szwl.util.IDGenerator;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
/**
*
* 前端控制器
*
*
* @author wuhs
* @since 2022-04-23
*/
@RestController
@RequestMapping("/tOrder")
public class TOrderController {
@Autowired
TAdminService tAdminService;
@Autowired
TEquipmentService tEquipmentService;
@Autowired
TOrderService orderService;
@Autowired
PayFeign payFeign;
@Autowired
TShandeMchService tShandeMchService;
@Autowired
EsTOrderService esTOrderService;
@Autowired
TPriceService priceService;
@Autowired
TJoinpayMchService joinpayMchService;
@Autowired
THuifuMchService huifuMchService;
@Autowired
private TProportionService proportionService;
/**
* 退款
* @param
* @return
*/
@PostMapping("/refund")
public ResponseModel> refund(@RequestBody TOrder torder) {
// 退款金额: refusePrice
BigDecimal refusePrice = torder.getPrice().setScale(2, RoundingMode.HALF_DOWN);
// 退款数量: refundNumber
Integer refundNumber = torder.getProductNumber();
// 退款明细:note
String note = torder.getNote();
String[] refundDetails = note.split(",");
Map refundMap = new HashMap<>();
for (String refundDetail : refundDetails) {
String[] keyValue = refundDetail.split("-");
String key = keyValue[0];
String value = keyValue[1];
refundMap.put(key, Integer.valueOf(value));
}
if(refusePrice.compareTo(new BigDecimal("0.00"))<=0){
return R.fail(ResponseCodesEnum.A0001,"退款金额非法");
}
TOrder order = R.getDataIfSuccess(payFeign.getOrder(String.valueOf(torder.getId())));
if(order.getRefundQuantity() != null && order.getRefundQuantity()>0) {
order.setRefundQuantity(refundNumber + order.getRefundQuantity());
} else {
order.setRefundQuantity(refundNumber);
}
List orderDetails = R.getDataIfSuccess(payFeign.getOrderDetailsBySn(order.getSn()));
TEquipment equipment = tEquipmentService.getById(order.getEquipmentId());
Long equipmentId = equipment.getId();
if (order.getProductName().equals("280充值")) {
return R.fail(ResponseCodesEnum.A0002,"订单不退款");
}
if (order.getStatus().equals("0")) {
return R.fail(ResponseCodesEnum.A0002,"订单非支付状态");
}
//判断是不是直连微信退款
if(StringUtils.isNotEmpty(order.getPayPlatform())&&order.getPayPlatform().equals("3")){
String ifSuccess = R.getDataIfSuccess(payFeign.refund(order.getSn(),refusePrice));
return R.ok(ifSuccess);
}
String notifyUrl = JoinpayConstant.Notify_Refund_Url;
JSONArray altInfoArray = JSONArray.parseArray(order.getAltInfo());
JSONArray altRefInfo = new JSONArray();
// 汇付分销明细
String acctSplitBunch = "";
BigDecimal p = BigDecimal.ZERO;
BigDecimal price = BigDecimal.ZERO;
//订单总金额
BigDecimal sumPrice = BigDecimal.ZERO;
BigDecimal refundAmount = order.getRefundAmount();
if(refundAmount != null){
sumPrice = sumPrice.add(order.getPrice()).subtract(refundAmount).setScale(2, RoundingMode.HALF_DOWN);
}else {
sumPrice = sumPrice.add(order.getPrice()).setScale(2, RoundingMode.HALF_DOWN);
}
// BigDecimal pri = new BigDecimal(order.getPrice().toString()).setScale(2, RoundingMode.HALF_DOWN);
BigDecimal p8_MarketRefAmount = BigDecimal.ZERO;
//获取分销 关联设备
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(TProportion::getEquipmentId,equipmentId);
TProportion proportion = proportionService.getOne(wrapper);
if(refusePrice.compareTo(sumPrice)<0){
//退部分的钱
JSONArray altInfo = getAltInfo(proportion, refusePrice);
altInfoArray = JSONArray.parseArray(altInfo.toString());
for (int i = 0; i < altInfoArray.size(); i++) {
JSONObject altInfoObject = altInfoArray.getJSONObject(i);
JSONObject refInfo = new JSONObject();
refInfo.put("altMchNo", altInfoObject.getString("altMchNo"));
String altAmount = altInfoObject.getString("altAmount");
BigDecimal altAmountPrice = new BigDecimal(altAmount);
// altAmountPrice = altAmountPrice.multiply(refusePrice.divide(order.getPrice(),2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
p = p.add(altAmountPrice);
refInfo.put("altRefAmount", altAmountPrice.toString());
if(order.getMarketingAmount()!=null){
p8_MarketRefAmount = order.getMarketingAmount().multiply(refusePrice.divide(order.getPrice(),2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
}
price = price.add(altInfoObject.getBigDecimal("altAmount"));
if (!altInfoObject.getBigDecimal("altAmount").toString().equals("0.00")) {
altRefInfo.add(refInfo);
}
}
}else {
//退全部的钱
for (int i = 0; i < altInfoArray.size(); i++) {
JSONObject altInfoObject = altInfoArray.getJSONObject(i);
JSONObject refInfo = new JSONObject();
refInfo.put("altMchNo", altInfoObject.getString("altMchNo"));
String altAmount = altInfoObject.getString("altAmount");
BigDecimal altAmountPrice = new BigDecimal(altAmount).setScale(2, RoundingMode.HALF_DOWN);
// BigDecimal divide = altAmountPrice.divide(order.getPrice(),3,BigDecimal.ROUND_HALF_UP).setScale(3, RoundingMode.HALF_DOWN);
// altAmountPrice = refusePrice.multiply(divide).setScale(2, RoundingMode.HALF_DOWN);
// refInfo.put("altRefAmount", altAmountPrice.toString());
p = p.add(altAmountPrice);
refInfo.put("altRefAmount", altAmountPrice.toString());
if(order.getMarketingAmount()!=null){
p8_MarketRefAmount = order.getMarketingAmount().multiply(refusePrice.divide(order.getPrice(),2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
}
price = price.add(altInfoObject.getBigDecimal("altAmount"));
if (!altInfoObject.getBigDecimal("altAmount").toString().equals("0.00")) {
altRefInfo.add(refInfo);
}
}
}
// 特殊处理下:添加多一个平台方分销的金额
// 平台获得的收益
BigDecimal adminPrice = refusePrice.subtract(p).setScale(2, RoundingMode.HALF_DOWN);
JSONObject refInfo = new JSONObject();
refInfo.put("altMchNo", JoinpayConstant.mch_no);
refInfo.put("altRefAmount", adminPrice.toString());
price = price.add(adminPrice);
if (!adminPrice.toString().equals("0.00")) {
altRefInfo.add(refInfo);
}
//需要知道订单是哪一个平台的,不能以账号的为标准 0或null为汇聚,1为汇付支付
String payPlatform = order.getPayPlatform();
String companyType = order.getCompanyType();
if (StringUtils.isNotEmpty(payPlatform) && payPlatform.equals("1")) {
try {
Date date = order.getCreateDate();
String payDate = DateTools.formatYYYYMMDD(date);
String remark = "";
// BigDecimal refundAmount = order.getRefundAmount();
// 判断是否退过款
if(refundAmount != null && refundAmount.compareTo(BigDecimal.ZERO) > 0) {
acctSplitBunch = getAcctSplitBunch(proportion, refusePrice, companyType).toString();
} else {
// 如果退款金额等于订单金额,就直接用原来的分销明细,如果小于就重新计算分销明细
if(refusePrice.compareTo(order.getPrice()) < 0) {
acctSplitBunch = getAcctSplitBunch(proportion, refusePrice, companyType).toString();
}
}
String refundData = huifuMchService.refund(order.getSn(), price, payDate, remark, acctSplitBunch, companyType);
//订单号
if(StrUtil.isNotEmpty(refundData)&&(refundData.equals(HuifuConstant.TRANS_STAT_S)||refundData.equals(HuifuConstant.TRANS_STAT_P))) {
order.setStatus(2);
payFeign.updateOrder(order);
// 修改订单明细
for (TOrderDetails orderDetail : orderDetails) {
String productNo = orderDetail.getProductNo();
Integer productNumber = refundMap.get(productNo);
if(productNumber != null) {
// 退款中
orderDetail.setRefundStatus("4");
// 退款数量
orderDetail.setRefundQuantity(productNumber);
// 退款金额
orderDetail.setRefundAmount(orderDetail.getPrice().multiply(new BigDecimal(productNumber)));
payFeign.updateOrderDetails(orderDetail);
}
}
return R.ok("申请退款成功");
}else {
return R.fail(refundData);
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
String result = orderService.refund(order.getSn(), orderService.initRefundSn(equipment.getClientId(), refundNumber), refusePrice.subtract(p8_MarketRefAmount), p8_MarketRefAmount,"", notifyUrl, altRefInfo.toString());
if ("退款申请成功".equals(result)) {
order.setStatus(2);
if(StringUtils.isNotEmpty(order.getCoupons())){
//设置退款营销金额
if(order.getRefundMarketingAmount()!=null){
//累加
order.setRefundMarketingAmount(order.getRefundMarketingAmount().add(p8_MarketRefAmount));
}else {
order.setRefundMarketingAmount(p8_MarketRefAmount);
}
}
// 修改订单明细
for (TOrderDetails orderDetail : orderDetails) {
String productNo = orderDetail.getProductNo();
Integer productNumber = refundMap.get(productNo);
if(productNumber != null) {
// 退款中
orderDetail.setRefundStatus("4");
// 退款数量
orderDetail.setRefundQuantity(productNumber);
// 退款金额
orderDetail.setRefundAmount(orderDetail.getPrice().multiply(new BigDecimal(productNumber)));
payFeign.updateOrderDetails(orderDetail);
}
}
order.setRefundTrxNo(String.valueOf(refusePrice));
payFeign.updateOrder(order);
return R.ok("申请退款已成功");
}else {
return R.fail("申请退款失败");
}
} catch (Exception e) {
e.printStackTrace();
}
}
return R.ok("申请退款已成功");
}
/**
* 小程序退款
* @param
* @return
*/
@GetMapping("/refuseByXcu")
public ResponseEntity> refuseByXcu(String id) {
TOrder order = R.getDataIfSuccess(payFeign.getOrder(id));
List orderDetails = R.getDataIfSuccess(payFeign.getOrderDetailsBySn(order.getSn()));
String orderStatus = order.getOrderStatus();
Integer refundNumber = order.getProductNumber();
order.setRefundQuantity(refundNumber);
if(StringUtils.isNotEmpty(orderStatus)){
if(orderStatus.equals("1")||orderStatus.equals("2")){
return ResponseEntity
.status(HttpStatus.OK)
.body(new ResultMessage()
.setCode(false)
.setData("")
.setMessage("订单非排队状态"));
}
}
BigDecimal refusePrice = order.getPrice().setScale(2, RoundingMode.HALF_DOWN);
if(refusePrice.compareTo(new BigDecimal("0.00"))<=0){
return ResponseEntity
.status(HttpStatus.OK)
.body(new ResultMessage()
.setCode(false)
.setData("")
.setMessage("退款金额非法"));
}
TEquipment equipment = tEquipmentService.getById(order.getEquipmentId());
Long equipmentId = equipment.getId();
if (order.getProductName().equals("280充值")) {
return ResponseEntity
.status(HttpStatus.OK)
.body(new ResultMessage()
.setCode(false)
.setData("订单不退款")
.setMessage("订单不退款"));
}
if (order.getStatus().equals("0")) {
return ResponseEntity
.status(HttpStatus.OK)
.body(new ResultMessage()
.setCode(false)
.setData("订单非支付状态")
.setMessage("订单非支付状态"));
}
String notifyUrl = JoinpayConstant.Notify_Refund_Url;
JSONArray altInfoArray = JSONArray.parseArray(order.getAltInfo());
JSONArray altRefInfo = new JSONArray();
// 汇付分销明细
String acctSplitBunch = "";
BigDecimal p = BigDecimal.ZERO;
BigDecimal price = BigDecimal.ZERO;
//订单总金额
BigDecimal sumPrice = BigDecimal.ZERO;
if(order.getRefundAmount()!=null){
sumPrice=sumPrice.add(order.getPrice()).subtract(order.getRefundAmount()).setScale(2, RoundingMode.HALF_DOWN);
}else {
sumPrice=sumPrice.add(order.getPrice()).setScale(2, RoundingMode.HALF_DOWN);
}
BigDecimal p8_MarketRefAmount = BigDecimal.ZERO;
for (int i = 0; i < altInfoArray.size(); i++) {
JSONObject altInfoObject = altInfoArray.getJSONObject(i);
JSONObject refInfo = new JSONObject();
refInfo.put("altMchNo", altInfoObject.getString("altMchNo"));
String altAmount = altInfoObject.getString("altAmount");
// BigDecimal pri = new BigDecimal(order.getPrice().toString()).setScale(2, RoundingMode.HALF_DOWN);
if(refusePrice.compareTo(sumPrice)<0){
//退部分的钱
BigDecimal altAmountPrice = new BigDecimal(altAmount);
altAmountPrice = altAmountPrice.multiply(refusePrice.divide(sumPrice,2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
p = p.add(altAmountPrice);
refInfo.put("altRefAmount", altAmountPrice.toString());
if(order.getMarketingAmount()!=null){
p8_MarketRefAmount = order.getMarketingAmount().multiply(refusePrice.divide(sumPrice,2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
}
}else {
//退全部的钱
BigDecimal altAmountPrice = new BigDecimal(altAmount).setScale(2, RoundingMode.HALF_DOWN);
// BigDecimal divide = altAmountPrice.divide(sumPrice,2,BigDecimal.ROUND_HALF_UP).setScale(2, RoundingMode.HALF_DOWN);
// altAmountPrice = refusePrice.multiply(divide).setScale(2, RoundingMode.HALF_DOWN);
p = p.add(altAmountPrice);
refInfo.put("altRefAmount", altAmountPrice.toString());
if(order.getMarketingAmount()!=null){
p8_MarketRefAmount = order.getMarketingAmount().multiply(refusePrice.divide(sumPrice,2,BigDecimal.ROUND_HALF_UP)).setScale(2, RoundingMode.HALF_DOWN);
}
}
price = price.add(altInfoObject.getBigDecimal("altAmount"));
if (!altInfoObject.getBigDecimal("altAmount").toString().equals("0.00")) {
altRefInfo.add(refInfo);
}
}
// 特殊处理下:添加多一个平台方分销的金额
// 平台获得的收益
// BigDecimal adminPrice = new BigDecimal(order.getPrice().toString()).subtract(price);
BigDecimal adminPrice = refusePrice.subtract(p).setScale(2, RoundingMode.HALF_DOWN);
JSONObject refInfo = new JSONObject();
refInfo.put("altMchNo", JoinpayConstant.mch_no);
refInfo.put("altRefAmount", adminPrice.toString());
price = price.add(adminPrice);
if (!adminPrice.toString().equals("0.00")) {
altRefInfo.add(refInfo);
}
//需要知道订单是哪一个平台的,不能以账号的为标准 0或null为汇聚,1为杉德支付
String payPlatform = order.getPayPlatform();
String companyType = order.getCompanyType();
if (StringUtils.isNotEmpty(payPlatform) && payPlatform.equals("1")) {
try {
Date date = order.getCreateDate();
String payDate = DateTools.formatYYYYMMDD(date);
String remark = "";
BigDecimal refundAmount = order.getRefundAmount();
//获取分销 关联设备
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(TProportion::getEquipmentId,equipmentId);
TProportion proportion = proportionService.getOne(wrapper);
// 判断是否退过款
if(refundAmount != null && refundAmount.compareTo(BigDecimal.ZERO) > 0) {
acctSplitBunch = getAcctSplitBunch(proportion, refusePrice, companyType).toString();
} else {
// 如果退款金额等于订单金额,就直接用原来的分销明细,如果小于就重新计算分销明细
if(refusePrice.compareTo(order.getPrice()) < 0) {
acctSplitBunch = getAcctSplitBunch(proportion, refusePrice, companyType).toString();
}
}
String refundData = huifuMchService.refund(order.getSn(), price, payDate, remark, acctSplitBunch, companyType);
//订单号
if(StrUtil.isNotEmpty(refundData)&&(refundData.equals(HuifuConstant.TRANS_STAT_S)||refundData.equals(HuifuConstant.TRANS_STAT_P))) {
order.setStatus(2);
payFeign.updateOrder(order);
// 修改订单明细
for (TOrderDetails orderDetail : orderDetails) {
// 退款中
orderDetail.setRefundStatus("4");
// 退款数量
orderDetail.setRefundQuantity(orderDetail.getProductNumber());
// 退款金额
orderDetail.setRefundAmount(orderDetail.getAmount());
payFeign.updateOrderDetails(orderDetail);
}
return ResponseEntity
.status(HttpStatus.OK)
.body(new ResultMessage()
.setCode(true)
.setData("申请退款已成功")
.setMessage("申请退款已成功"));
}else {
return ResponseEntity
.status(HttpStatus.OK)
.body(new ResultMessage()
.setCode(false)
.setData("申请退款失败")
.setMessage("申请退款失败"));
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
String result = orderService.refund(order.getSn(), orderService.initRefundSn(equipment.getClientId(), refundNumber), refusePrice.subtract(p8_MarketRefAmount), p8_MarketRefAmount,"", notifyUrl, altRefInfo.toString());
if ("退款申请成功".equals(result)) {
order.setStatus(2);
if(StringUtils.isNotEmpty(order.getCoupons())){
//设置退款营销金额
if(order.getRefundMarketingAmount()!=null){
//累加
order.setRefundMarketingAmount(order.getRefundMarketingAmount().add(p8_MarketRefAmount));
}else {
order.setRefundMarketingAmount(p8_MarketRefAmount);
}
}
// 修改订单明细
for (TOrderDetails orderDetail : orderDetails) {
// 退款中
orderDetail.setRefundStatus("4");
// 退款数量
orderDetail.setRefundQuantity(orderDetail.getProductNumber());
// 退款金额
orderDetail.setRefundAmount(orderDetail.getAmount());
payFeign.updateOrderDetails(orderDetail);
}
order.setRefundTrxNo(String.valueOf(refusePrice));
payFeign.updateOrder(order);
return ResponseEntity
.status(HttpStatus.OK)
.body(new ResultMessage()
.setCode(true)
.setData("申请退款已成功")
.setMessage("申请退款已成功"));
}else {
return ResponseEntity
.status(HttpStatus.OK)
.body(new ResultMessage()
.setCode(false)
.setData("申请退款失败")
.setMessage("申请退款失败"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
return ResponseEntity
.status(HttpStatus.OK)
.body(new ResultMessage()
.setCode(true)
.setData("申请退款已成功")
.setMessage("申请退款已成功"));
}
@ApiOperation(value = "添加优惠码")
@GetMapping("/equipmentPay")
public ResponseModel> equipmentPay(Double number, Long equipmentId, String frpCode) {
TEquipment equipment = tEquipmentService.getById(equipmentId);
if (!frpCode.equals("ALIPAY_NATIVE") && !frpCode.equals("WEIXIN_NATIVE")) {
return R.fail("参数错误");
}
if (equipment == null) {
return R.fail("找不到设备");
}
TAdmin admin = tAdminService.getById(equipment.getAdminId());
Double money = null;
LambdaQueryWrapper query1 = Wrappers.lambdaQuery();
query1.eq(TPrice::getName,"MG280");
List prices = priceService.list(query1);
for(TPrice price:prices){
if(price.getName().equals("MG280")){
money = price.getPrice();
}
}
BigDecimal price = null;
double pri = money * number;
price = BigDecimal.valueOf(pri);
String sn = orderService.initSn(equipmentId);
String orderNo = sn;
String productName = "280充值";
BigDecimal cutPrice = price.multiply(new BigDecimal(100)).divide(new BigDecimal(100));
BigDecimal amount = cutPrice;
String productDesc = "";
String commonParameter = "";
String returnUrl = null;
String notifyUrl = JoinpayConstant.Notify_Url_PromoCode;
String isShowPic = "1";
String openId = null;
String authCode = null;
String appid = null;
String transactionModel = null;
String tradeMerchantNo = null;
String buyerId = null;
String isAlt = "0";
String altType = null;
String altUrl = null;
BigDecimal marketingAmount = null;
// frpCode = "WEIXIN_NATIVE";
TOrder order1 = new TOrder();
order1.setId(IDGenerator.orderID());
order1.setSn(sn);
order1.setType(0);
order1.setAdminId(admin.getId());
order1.setProductName(productName);
order1.setPrice(price);
order1.setEquipmentId(equipmentId);
order1.setClientId(equipment.getClientId());
order1.setStatus(0);
order1.setFrpCode(frpCode);
order1.setCreateDate(new Date());
order1.setModifyDate(new Date());
payFeign.addOrder(order1);
JSONArray altInfo = new JSONArray();
String result = null;
try {
result = joinpayMchService.uniPay(
orderNo, amount, productName, productDesc,
commonParameter, returnUrl, notifyUrl, frpCode,
isShowPic, openId, authCode, appid, transactionModel, tradeMerchantNo,
buyerId, isAlt, altType, altInfo, altUrl, marketingAmount
);
} catch (Exception e) {
e.printStackTrace();
return R.fail("申请支付失败");
}
JSONObject resultJson = JSONObject.parseObject(result);
// 汇聚支付支付申请返回支付二维码图片
String rd_Pic = resultJson.getString("rd_Pic");
if (resultJson == null || StringUtils.isBlank(rd_Pic)) {
return R.fail("找不到支付图片");
}
JSONObject kindData = new JSONObject();
kindData.put("sn", sn);
kindData.put("price", price);
kindData.put("image", rd_Pic);
return R.ok(kindData);
}
/**
* 汇付分销明细
* @param proportion
* @param price
* @return
*/
public JSONObject getAcctSplitBunch(TProportion proportion, BigDecimal price, String companyType) {
// 获取分账有几方:0:分账方2个,1:分账方3个,2:分账方4个,3:分账方超4个
Integer type = proportion.getType();
// 汇付分账明细参数
JSONObject acctSplitBunch = new JSONObject();
JSONArray acctInfos = new JSONArray();
BigDecimal adminProportion = null,
agencyProportion = null,
merchantProportion = null,
selfProportion = null;
BigDecimal agencyAmount = null;
BigDecimal merchantAmount = null;
BigDecimal selfAmount = null;
BigDecimal adminAmount = null;
THuifuMch agencyHuifu = null;
THuifuMch merchantHuifu = null;
THuifuMch selfHuifu = null;
JSONObject agencyJson = null;
JSONObject merchantJson = null;
JSONObject selfJson = null;
JSONObject adminJson = null;
//取消平台扣手续费
BigDecimal cutPrice = price.setScale(2, RoundingMode.HALF_UP);
BigDecimal refuseAmount = new BigDecimal(0.00);
switch (type) {
case 0:
// 公司平台分销
adminProportion = proportion.getAdminProportion();
// 商家自己的分销
selfProportion = proportion.getProportion();
// 平台分销获得利润
adminAmount = cutPrice.multiply(adminProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_UP);
// 商家分销获得利润
selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
// 商家汇付信息:agencyHuifu
LambdaQueryWrapper queryWrapper1 = new LambdaQueryWrapper<>();
queryWrapper1.eq(THuifuMch::getAdminId,proportion.getAdminId());
selfHuifu = huifuMchService.getOne(queryWrapper1);
// 公司平台分销
adminJson = new JSONObject();
adminJson.put("div_amt", adminAmount.toString());
if(StrUtil.isNotEmpty(companyType) && companyType.equals("1")) {
adminJson.put("huifu_id", HuifuConstant.SC_DIV_HUIFU_ID);
} else {
adminJson.put("huifu_id", HuifuConstant.SZ_DIV_HUIFU_ID);
}
acctInfos.add(adminJson);
// 商家分销获得利润
selfJson = new JSONObject();
selfJson.put("div_amt", selfAmount.toString());
selfJson.put("huifu_id", selfHuifu.getHuifuId());
acctInfos.add(selfJson);
break;
case 1:
// 公司平台分销
adminProportion = proportion.getAdminProportion();
// 商家自己的分销
selfProportion = proportion.getProportion();
// 一级分销
agencyProportion = proportion.getAgencyProportion();
// 平台分销获得利润
adminAmount = cutPrice.multiply(adminProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_UP);
// 商家分销获得利润
selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
// 汇付平台
LambdaQueryWrapper queryWrapper2 = new LambdaQueryWrapper<>();
queryWrapper2.eq(THuifuMch::getAdminId,proportion.getAdminId());
selfHuifu = huifuMchService.getOne(queryWrapper2);
LambdaQueryWrapper queryWrapper3 = new LambdaQueryWrapper<>();
queryWrapper3.eq(THuifuMch::getAdminId,proportion.getAgencyId());
agencyHuifu = huifuMchService.getOne(queryWrapper3);
// 公司平台分销
adminJson = new JSONObject();
adminJson.put("div_amt", adminAmount.toString());
if(StrUtil.isNotEmpty(companyType) && companyType.equals("1")) {
adminJson.put("huifu_id", HuifuConstant.SC_DIV_HUIFU_ID);
} else {
adminJson.put("huifu_id", HuifuConstant.SZ_DIV_HUIFU_ID);
}
acctInfos.add(adminJson);
// 商家分销获得利润
selfJson = new JSONObject();
selfJson.put("div_amt", selfAmount.toString());
selfJson.put("huifu_id", selfHuifu.getHuifuId());
acctInfos.add(selfJson);
if(!(agencyProportion.compareTo(refuseAmount) == 0||agencyProportion==null)){
// 一级分销获得利润
agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
agencyJson = new JSONObject();
agencyJson.put("div_amt", agencyAmount.toString());
agencyJson.put("huifu_id", agencyHuifu.getHuifuId());
acctInfos.add(agencyJson);
}
break;
case 2:
// 公司平台分销
adminProportion = proportion.getAdminProportion();
// 商家自己的分销
selfProportion = proportion.getProportion();
// 一级分销
agencyProportion = proportion.getAgencyProportion();
// 二级分销
merchantProportion = proportion.getMerchantProportion();
// 平台分销获得利润
adminAmount = cutPrice.multiply(adminProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_UP);
// 商家分销获得利润
selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
LambdaQueryWrapper queryWrapper4 = new LambdaQueryWrapper<>();
queryWrapper4.eq(THuifuMch::getAdminId,proportion.getAdminId());
selfHuifu = huifuMchService.getOne(queryWrapper4);
LambdaQueryWrapper queryWrapper5 = new LambdaQueryWrapper<>();
queryWrapper5.eq(THuifuMch::getAdminId,proportion.getAgencyId());
agencyHuifu = huifuMchService.getOne(queryWrapper5);
LambdaQueryWrapper queryWrapper6 = new LambdaQueryWrapper<>();
queryWrapper6.eq(THuifuMch::getAdminId,proportion.getMerchantId());
merchantHuifu = huifuMchService.getOne(queryWrapper6);
// 公司平台分销
adminJson = new JSONObject();
adminJson.put("div_amt", adminAmount.toString());
if(StrUtil.isNotEmpty(companyType) && companyType.equals("1")) {
adminJson.put("huifu_id", HuifuConstant.SC_DIV_HUIFU_ID);
} else {
adminJson.put("huifu_id", HuifuConstant.SZ_DIV_HUIFU_ID);
}
acctInfos.add(adminJson);
// 商家分销获得利润
selfJson = new JSONObject();
selfJson.put("div_amt", selfAmount.toString());
selfJson.put("huifu_id", selfHuifu.getHuifuId());
acctInfos.add(selfJson);
if(!(agencyProportion.compareTo(refuseAmount) == 0||agencyProportion==null)){
// 一级分销获得利润
agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
agencyJson = new JSONObject();
agencyJson.put("div_amt", agencyAmount.toString());
agencyJson.put("huifu_id", agencyHuifu.getHuifuId());
acctInfos.add(agencyJson);
}
if(!(merchantProportion.compareTo(refuseAmount) == 0||merchantProportion==null)){
// 二级分销获得利润
merchantAmount = cutPrice.multiply(merchantProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
merchantJson = new JSONObject();
merchantJson.put("div_amt", merchantAmount.toString());
merchantJson.put("huifu_id", merchantHuifu.getHuifuId());
acctInfos.add(merchantJson);
}
break;
}
// 计算div_amt的总和
BigDecimal totalAmount = BigDecimal.ZERO;
for (int i = 0; i < acctInfos.size(); i++) {
JSONObject jsonObject = acctInfos.getJSONObject(i);
BigDecimal divAmt = new BigDecimal(jsonObject.getString("div_amt"));
totalAmount = totalAmount.add(divAmt);
}
// 如果总和超过原本金额,调整div_amt
if (totalAmount.compareTo(price) > 0) {
BigDecimal diffAmount = totalAmount.subtract(price);
for (int i = 0; i < acctInfos.size(); i++) {
JSONObject jsonObject = acctInfos.getJSONObject(i);
String huifuId = jsonObject.getString("huifu_id");
if (huifuId.equals(HuifuConstant.SZ_DIV_HUIFU_ID) || huifuId.equals(HuifuConstant.SC_DIV_HUIFU_ID)) {
BigDecimal currentDivAmt = new BigDecimal(jsonObject.getString("div_amt"));
BigDecimal adjustedDivAmt = currentDivAmt.subtract(diffAmount).max(BigDecimal.ZERO);
jsonObject.put("div_amt", adjustedDivAmt.toString());
break;
}
}
}
acctSplitBunch.put("acct_infos", acctInfos);
return acctSplitBunch;
}
/**
* 汇聚分销明细
* @param proportion
* @param price
* @return
*/
public JSONArray getAltInfo(TProportion proportion, BigDecimal price) {
// 获取分账有几方:0:分账方2个,1:分账方3个,2:分账方4个,3:分账方超4个
Integer type = proportion.getType();
// 汇聚分账参数
JSONArray altInfo = new JSONArray();
BigDecimal agencyProportion = null,
merchantProportion = null,
selfProportion = null;
BigDecimal agencyAmount = null;
BigDecimal merchantAmount = null;
BigDecimal selfAmount = null;
TJoinpayMch agencyMch = null;
TJoinpayMch merchantMch = null;
TJoinpayMch selfMch = null;
JSONObject agencyJson = null;
JSONObject merchantJson = null;
JSONObject selfJson = null;
//取消平台扣手续费
BigDecimal cutPrice = price.setScale(2, RoundingMode.HALF_UP);
BigDecimal refuseAmount = new BigDecimal(0.00);
switch (type) {
case 0:
// 商家自己的分销
selfProportion = proportion.getProportion();
// 商家分销获得利润
selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
// 汇聚平台
LambdaQueryWrapper queryWrapper1 = new LambdaQueryWrapper<>();
queryWrapper1.eq(TJoinpayMch::getAdminId,proportion.getAdminId());
selfMch = joinpayMchService.getOne(queryWrapper1);
selfJson = new JSONObject();
selfJson.put("altMchNo", selfMch.getAltMchNo());
selfJson.put("altAmount", selfAmount.toString());
selfJson.put("isGuar", "12");
altInfo.add(selfJson);
break;
case 1:
// 商家自己的分销
selfProportion = proportion.getProportion();
// 一级分销
agencyProportion = proportion.getAgencyProportion();
// 商家分销获得利润
selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
// 汇聚平台
LambdaQueryWrapper queryWrapper2 = new LambdaQueryWrapper<>();
queryWrapper2.eq(TJoinpayMch::getAdminId,proportion.getAdminId());
selfMch = joinpayMchService.getOne(queryWrapper2);
LambdaQueryWrapper queryWrapper3 = new LambdaQueryWrapper<>();
queryWrapper3.eq(TJoinpayMch::getAdminId,proportion.getAgencyId());
agencyMch = joinpayMchService.getOne(queryWrapper3);
if(!(agencyProportion.compareTo(refuseAmount) == 0||agencyProportion==null)){
// 一级分销获得利润
agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
agencyJson = new JSONObject();
agencyJson.put("altMchNo", agencyMch.getAltMchNo());
agencyJson.put("altAmount", agencyAmount.toString());
agencyJson.put("isGuar", "12");
altInfo.add(agencyJson);
}
// 商家自己分销获得利润
selfJson = new JSONObject();
selfJson.put("altMchNo", selfMch.getAltMchNo());
selfJson.put("altAmount", selfAmount.toString());
selfJson.put("isGuar", "12");
altInfo.add(selfJson);
break;
case 2:
// 商家自己的分销
selfProportion = proportion.getProportion();
// 一级分销
agencyProportion = proportion.getAgencyProportion();
// 二级分销
merchantProportion = proportion.getMerchantProportion();
// 三级分销
// personageProportion = proportion.getPersonageProportion();
// 商家分销获得利润
selfAmount = cutPrice.multiply(selfProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
// 汇聚
LambdaQueryWrapper queryWrapper4 = new LambdaQueryWrapper<>();
queryWrapper4.eq(TJoinpayMch::getAdminId,proportion.getAdminId());
selfMch = joinpayMchService.getOne(queryWrapper4);
LambdaQueryWrapper queryWrapper5 = new LambdaQueryWrapper<>();
queryWrapper5.eq(TJoinpayMch::getAdminId,proportion.getAgencyId());
agencyMch = joinpayMchService.getOne(queryWrapper5);
LambdaQueryWrapper queryWrapper6 = new LambdaQueryWrapper<>();
queryWrapper6.eq(TJoinpayMch::getAdminId,proportion.getMerchantId());
merchantMch = joinpayMchService.getOne(queryWrapper6);
if(!(agencyProportion.compareTo(refuseAmount) == 0||agencyProportion==null)){
// 代理分销获得利润
agencyAmount = cutPrice.multiply(agencyProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
agencyJson = new JSONObject();
agencyJson.put("altMchNo", agencyMch.getAltMchNo());
agencyJson.put("altAmount", agencyAmount.toString());
agencyJson.put("isGuar", "12");
altInfo.add(agencyJson);
}
if(!(merchantProportion.compareTo(refuseAmount) == 0||merchantProportion==null)){
// 经销商分销获得利润
merchantAmount = cutPrice.multiply(merchantProportion.divide(new BigDecimal(100))).setScale(2, RoundingMode.HALF_DOWN);
merchantJson = new JSONObject();
merchantJson.put("altMchNo", merchantMch.getAltMchNo());
merchantJson.put("altAmount", merchantAmount.toString());
merchantJson.put("isGuar", "12");
altInfo.add(merchantJson);
}
// 商家自己分销获得利润
merchantJson = new JSONObject();
merchantJson.put("altMchNo", selfMch.getAltMchNo());
merchantJson.put("altAmount", selfAmount.toString());
merchantJson.put("isGuar", "12");altInfo.add(merchantJson);
break;
}
Iterator