chengf
2026-03-11 b88a288f4f787b509463678e3cd9ccfa3f37014b
service-fee/src/main/java/com/java110/fee/cmd/fee/PayFeePreCmd.java
@@ -6,36 +6,38 @@
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.core.factory.CommunitySettingFactory;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.core.log.LoggerFactory;
import com.java110.core.smo.IComputeFeeSMO;
import com.java110.dto.account.AccountDetailDto;
import com.java110.dto.account.AccountDto;
import com.java110.dto.community.CommunityDto;
import com.java110.dto.couponUser.CouponUserDto;
import com.java110.dto.coupon.CouponUserDto;
import com.java110.dto.fee.FeeAttrDto;
import com.java110.dto.fee.FeeConfigDto;
import com.java110.dto.fee.FeeDetailDto;
import com.java110.dto.fee.FeeDto;
import com.java110.dto.feeDiscount.ComputeDiscountDto;
import com.java110.intf.acct.IAccountDetailInnerServiceSMO;
import com.java110.intf.acct.IAccountInnerServiceSMO;
import com.java110.intf.acct.ICouponUserV1InnerServiceSMO;
import com.java110.dto.fee.ComputeDiscountDto;
import com.java110.dto.payment.PaymentPoolDto;
import com.java110.dto.payment.PaymentPoolValueDto;
import com.java110.dto.wechat.SmallWeChatDto;
import com.java110.intf.acct.*;
import com.java110.intf.community.ICommunityV1InnerServiceSMO;
import com.java110.intf.community.IRepairUserInnerServiceSMO;
import com.java110.intf.community.IRoomInnerServiceSMO;
import com.java110.intf.fee.*;
import com.java110.intf.user.IOwnerCarInnerServiceSMO;
import com.java110.po.account.AccountDetailPo;
import com.java110.po.account.AccountPo;
import com.java110.po.accountDetail.AccountDetailPo;
import com.java110.po.feeAccountDetail.FeeAccountDetailPo;
import com.java110.utils.cache.CommonCache;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.constant.ResponseConstant;
import com.java110.utils.constant.WechatConstant;
import com.java110.utils.exception.CmdException;
import com.java110.utils.exception.ListenerExecuteException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.StringUtil;
import com.java110.utils.util.*;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
@@ -48,6 +50,7 @@
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * 类表述:删除
@@ -61,14 +64,20 @@
 */
@Java110Cmd(serviceCode = "fee.payFeePre")
public class PayFeePreCmd extends Cmd {
    private static Logger logger = LoggerFactory.getLogger(PayFeePreCmd.class);
    private static Logger logger = LoggerFactory.getLogger(PayFeePreCmd.class);
    @Autowired
    private IFeeInnerServiceSMO feeInnerServiceSMOImpl;
    @Autowired
    private IRoomInnerServiceSMO roomInnerServiceSMOImpl;
    @Autowired
    private IPaymentPoolV1InnerServiceSMO paymentPoolV1InnerServiceSMOImpl;
    @Autowired
    private IPaymentPoolValueV1InnerServiceSMO paymentPoolValueV1InnerServiceSMOImpl;
    @Autowired
    private IFeeConfigInnerServiceSMO feeConfigInnerServiceSMOImpl;
@@ -101,13 +110,19 @@
    private IAccountInnerServiceSMO accountInnerServiceSMOImpl;
    @Autowired
    private ICommunityV1InnerServiceSMO communityV1InnerServiceSMOImpl;
    @Autowired
    private IAccountDetailInnerServiceSMO accountDetailInnerServiceSMOImpl;
    @Autowired
    private ICommunityV1InnerServiceSMO communityV1InnerServiceSMOImpl;
    @Autowired
    private IFeeAccountDetailServiceSMO feeAccountDetailServiceSMOImpl;
    //键(积分账户最大使用积分)
    public static final String MAXIMUM_NUMBER = "MAXIMUM_NUMBER";
    //键(积分账户抵扣比例)
    public static final String DEDUCTION_PROPORTION = "DEDUCTION_PROPORTION";
    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) {
@@ -167,8 +182,11 @@
    public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {
        logger.debug("ServiceDataFlowEvent : {}", event);
        String userId = cmdDataFlowContext.getReqHeaders().get("user-id");
        String appId = cmdDataFlowContext.getReqHeaders().get("app-id");
        reqJson.put("appId", appId);
        reqJson.put("userId", userId);
        FeeDto feeDto = new FeeDto();
        feeDto.setFeeId(reqJson.getString("feeId"));
@@ -180,13 +198,19 @@
        feeDto = feeDtos.get(0);
        reqJson.put("feeTypeCd", feeDto.getFeeTypeCd());
        reqJson.put("feeId", feeDto.getFeeId());
        Map feePriceAll = computeFeeSMOImpl.getFeePrice(feeDto);
        //todo 计算应收
        BigDecimal receivableAmount = new BigDecimal(feePriceAll.get("feePrice").toString());
        BigDecimal cycles = new BigDecimal(Double.parseDouble(reqJson.getString("cycles")));
        double tmpReceivableAmount = cycles.multiply(receivableAmount).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
        double tmpReceivableAmount = cycles.multiply(receivableAmount).setScale(4, BigDecimal.ROUND_HALF_EVEN).doubleValue();
        tmpReceivableAmount = MoneyUtil.computePriceScale(tmpReceivableAmount, feeDto.getScale(), Integer.parseInt(feeDto.getDecimalPlace()));
        JSONObject paramOut = new JSONObject();
        paramOut.put("receivableAmount", tmpReceivableAmount);
        paramOut.put("oId", GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_oId));
        SmallWeChatDto smallWeChatDto = get(reqJson.getString("communityId"));
        paramOut.put("oId", smallWeChatDto.getOrderPre() + GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_oId));
        //实收金额
        BigDecimal tmpReceivedAmout = new BigDecimal(tmpReceivableAmount);
@@ -208,6 +232,13 @@
        if (receivedAmount <= 0) {
            receivedAmount = 0.0;
        }
        //todo 小数点处理
        receivedAmount = MoneyUtil.computePriceScale(
                receivedAmount,
                feeDto.getScale(),
                Integer.parseInt(feeDto.getDecimalPlace())
        );
        paramOut.put("receivedAmount", receivedAmount);
        String feeName = getObjName(feeDto);
@@ -217,6 +248,43 @@
        reqJson.putAll(paramOut);
        CommonCache.setValue("payFeePre" + paramOut.getString("oId"), reqJson.toJSONString(), 24 * 60 * 60);
        cmdDataFlowContext.setResponseEntity(responseEntity);
    }
    public SmallWeChatDto get(String community){
        SmallWeChatDto smallWeChatDto = new SmallWeChatDto();
        //由于富有支付方式要从数据库里面取值,所以此处要查询
        PaymentPoolDto paymentPoolDto = new PaymentPoolDto();
//        paymentPoolDto.setCommunityId(community);
        paymentPoolDto.setPage(1);
        paymentPoolDto.setRow(10);
        List<PaymentPoolDto> paymentPoolDtos = paymentPoolV1InnerServiceSMOImpl.queryPaymentPools(paymentPoolDto);
        List<PaymentPoolDto> collect = paymentPoolDtos.stream().filter(e -> e.getPaymentType().equals("FUIOU")).collect(Collectors.toList());
        PaymentPoolValueDto paymentPoolValueDto = new PaymentPoolValueDto();
        paymentPoolValueDto.setPpId(collect.get(0).getPpId());
        paymentPoolValueDto.setCommunityId(community);
        List<PaymentPoolValueDto> values =  paymentPoolValueV1InnerServiceSMOImpl.queryPaymentPoolValues(paymentPoolValueDto);
        Map<String, List<PaymentPoolValueDto>> payMap = values.stream().collect(Collectors.groupingBy(PaymentPoolValueDto::getColumnKey));
        if(payMap.containsKey("FUIOU_APP_ID")){
            smallWeChatDto.setAppId(payMap.get("FUIOU_APP_ID").get(0).getColumnValue());
        }else{
            smallWeChatDto.setAppId(MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, "appId"));
        }
        if(payMap.containsKey("FUIOU_MCHNT_KEY")){
            smallWeChatDto.setAppSecret(payMap.get("FUIOU_MCHNT_KEY").get(0).getColumnValue());
        }else{
            smallWeChatDto.setAppSecret(MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, "appSecret"));
        }
        if(payMap.containsKey("FUIOU_MERCHANT_ID")){
            smallWeChatDto.setMchId(payMap.get("FUIOU_MERCHANT_ID").get(0).getColumnValue());
        }else{
            smallWeChatDto.setMchId(MappingCache.getValue(MappingConstant.WECHAT_STORE_DOMAIN, "mchId"));
        }
        if(payMap.containsKey("FUIOU_ORDER_PRE")){
            smallWeChatDto.setOrderPre(payMap.get("FUIOU_ORDER_PRE").get(0).getColumnValue());
        }else{
            smallWeChatDto.setOrderPre("1066");
        }
        return smallWeChatDto;
    }
    private String getObjName(FeeDto feeDto) {
@@ -277,174 +345,76 @@
        accountDto.setAcctIds(acctIds.toArray(new String[acctIds.size()]));
        List<AccountDto> accountDtos = accountInnerServiceSMOImpl.queryAccounts(accountDto);
        if (accountDtos == null || accountDtos.size() < 1) {
        if (ListUtil.isNull(accountDtos)) {
            reqJson.put("deductionAmount", 0.0);
            return 0.0;
        }
        BigDecimal money = new BigDecimal(0);
        BigDecimal totalAccountAmount = new BigDecimal(0);
        BigDecimal cashMoney = new BigDecimal("0.00"); //抵扣的现金账户
        BigDecimal pointsMoney = new BigDecimal(0.00); //抵扣的积分数
        BigDecimal pointsMoneyNow = new BigDecimal(0.00); //积分转换为现金的实际数
        BigDecimal totalAccountAmount = new BigDecimal(0.00);
        //获取应收金额
        BigDecimal dedAmount = new BigDecimal("0.00");
        if (reqJson.containsKey("receivedMoney") && !StringUtil.isEmpty(reqJson.getString("receivedMoney"))) {
            dedAmount = new BigDecimal(reqJson.getString("receivedMoney"));
        } else {
            dedAmount = new BigDecimal(reqJson.getString("deductionAmount"));
        }
        for (AccountDto tmpAccountDto : accountDtos) {
            if (!StringUtil.isEmpty(tmpAccountDto.getAcctType()) && tmpAccountDto.getAcctType().equals("2004")) { //积分账户
            int compare = dedAmount.compareTo(BigDecimal.ZERO);
            if (AccountDto.ACCT_TYPE_CASH.equals(tmpAccountDto.getAcctType()) && compare > 0) { //现金账户
                //账户金额
                BigDecimal amount = new BigDecimal(tmpAccountDto.getAmount());
                //获取最大抵扣积分
                BigDecimal maximumNumber = new BigDecimal(tmpAccountDto.getMaximumNumber());
                //获取积分抵扣比例
                BigDecimal deductionProportion = new BigDecimal(tmpAccountDto.getDeductionProportion());
                int flag = amount.compareTo(maximumNumber);
                BigDecimal redepositAmount = new BigDecimal("0.00");
                BigDecimal integralAmount = new BigDecimal("0.00");
                if (flag == 1) { //账户积分大于最大使用积分,就用最大使用积分抵扣
                    redepositAmount = maximumNumber;
                    integralAmount = amount.subtract(maximumNumber);
                }
                if (flag > -1) { //账户积分大于等于最大使用积分,就用最大使用积分抵扣
                    redepositAmount = maximumNumber;
                    integralAmount = amount.subtract(maximumNumber);
                }
                if (flag == -1) { //账户积分小于最大使用积分,就用账户积分抵扣
                    redepositAmount = amount;
                }
                if (flag < 1) { //账户积分小于等于最大使用积分,就用账户积分抵扣
                    redepositAmount = amount;
                }
                if (flag == 0) { //账户积分等于最大使用积分
                    redepositAmount = amount;
                }
                //更新账户信息
                AccountPo accountPo = new AccountPo();
                accountPo.setAcctId(tmpAccountDto.getAcctId());
                accountPo.setAmount(integralAmount.toString());
                accountInnerServiceSMOImpl.updateAccount(accountPo);
                //生成账户详情
                AccountDetailPo accountDetailPo = new AccountDetailPo();
                accountDetailPo.setDetailId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_detailId));
                accountDetailPo.setAcctId(tmpAccountDto.getAcctId());
                accountDetailPo.setDetailType("2002"); //1001 转入 2002 转出
                accountDetailPo.setRelAcctId("-1");
                accountDetailPo.setAmount(redepositAmount.toString());
                accountDetailPo.setObjType("6006"); //6006 个人 7007 商户
                accountDetailPo.setObjId(tmpAccountDto.getObjId());
                accountDetailPo.setOrderId("-1");
                accountDetailPo.setbId("-1");
                accountDetailPo.setRemark("手机端积分抵扣");
                accountDetailPo.setCreateTime(new Date());
                accountDetailInnerServiceSMOImpl.saveAccountDetails(accountDetailPo);
                //计算积分换算的金额
                BigDecimal divide = redepositAmount.divide(deductionProportion);
                BigDecimal dedAmount = new BigDecimal(deductionAmount);
                //计算实付金额
                int flag2 = divide.compareTo(dedAmount);
                BigDecimal subtract = new BigDecimal("0.00");
                //生成抵扣明细记录
                FeeAccountDetailPo feeAccountDetailPo = new FeeAccountDetailPo();
                if (flag2 == -1) { //积分换算金额小于应付金额
                    subtract = dedAmount.subtract(divide);
                    BigDecimal multiply = divide.multiply(deductionProportion);
                    feeAccountDetailPo.setAmount(multiply.toString()); //积分抵扣金额
                } else if (flag < 1) { //积分换算金额小于等于应付金额
                    subtract = dedAmount.subtract(divide);
                    BigDecimal multiply = divide.multiply(deductionProportion);
                    feeAccountDetailPo.setAmount(multiply.toString()); //积分抵扣金额
                } else {
                    BigDecimal multiply = dedAmount.multiply(deductionProportion);
                    feeAccountDetailPo.setAmount(multiply.toString()); //积分抵扣金额
                }
                reqJson.put("receivedMoney", subtract);
                feeAccountDetailPo.setFadId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_fadId));
                feeAccountDetailPo.setDetailId(accountDetailPo.getDetailId());
                feeAccountDetailPo.setCommunityId(reqJson.getString("communityId"));
                feeAccountDetailPo.setState("1003"); //1001 无抵扣 1002 现金账户抵扣 1003 积分账户抵扣 1004 优惠券抵扣
                feeAccountDetailServiceSMOImpl.saveFeeAccountDetail(feeAccountDetailPo);
                money = subtract;
            } else if (!StringUtil.isEmpty(tmpAccountDto.getAcctType()) && tmpAccountDto.getAcctType().equals("2003")) { //现金账户
                //账户金额
                BigDecimal amount = new BigDecimal(tmpAccountDto.getAmount());
                //获取应收金额
                BigDecimal dedAmount = new BigDecimal("0.00");
                if (reqJson.containsKey("receivedMoney") && !StringUtil.isEmpty(reqJson.getString("receivedMoney"))) {
                    dedAmount = new BigDecimal(reqJson.getString("receivedMoney"));
                } else {
                    dedAmount = new BigDecimal(reqJson.getString("deductionAmount"));
                }
                int flag = amount.compareTo(dedAmount);
                if (flag == 1 || flag == 0) { //现金账户大于应收金额,或现金账户等于应收金额,就用应收金额抵扣
                    cashMoney = dedAmount;
                    dedAmount = new BigDecimal("0.0");
                }
                if (flag == -1) { //现金账户小于应收金额,就用现金账户抵扣
                    cashMoney = amount;
                    dedAmount = dedAmount.subtract(amount);
                }
                money = money.add(cashMoney);
            } else if ("2004".equals(tmpAccountDto.getAcctType()) && compare > 0) { //积分账户
                //积分账户最大使用积分
                String maximumNumber = CommunitySettingFactory.getValue(reqJson.getString("communityId"), MAXIMUM_NUMBER);
                BigDecimal maxNumber = new BigDecimal(maximumNumber);
                //积分账户抵扣比例
                String deductionProportion = CommunitySettingFactory.getValue(reqJson.getString("communityId"), DEDUCTION_PROPORTION);
                BigDecimal deductionProportionNum = new BigDecimal(deductionProportion);
                //积分账户金额
                BigDecimal amount = new BigDecimal(tmpAccountDto.getAmount());
                int flag = amount.compareTo(maxNumber);
                if (flag == -1) { //积分账户金额小于积分账户最大使用积分,就用积分账户抵扣
                    pointsMoney = amount;
                } else if (flag == 0) { //积分账户金额等于积分账户最大使用积分,就用积分账户抵扣
                    pointsMoney = amount;
                } else if (flag == 1) { //积分账户金额大于积分账户最大使用积分,就用最大使用积分抵扣
                    pointsMoney = maxNumber;
                }
                //计算抵扣积分抵扣的金额  积分除以比例等于账户能抵扣的钱数
                BigDecimal integralMoney = pointsMoney.divide(deductionProportionNum);
                BigDecimal redepositAmount = new BigDecimal("0.00");
                BigDecimal integralAmount = new BigDecimal("0.00");
                if (flag == 1) { //现金账户大于应收金额,就用应收金额抵扣
                int count = integralMoney.compareTo(dedAmount);
                if (count == 1 || count == 0) { //积分抵扣的金额大于应收金额,或积分抵扣的金额等于应收金额,就用应收金额抵扣
                    redepositAmount = dedAmount;
                    integralAmount = amount.subtract(dedAmount);
                    pointsMoney = dedAmount.multiply(deductionProportionNum);//应收金额乘以抵扣比例即为需要扣除的积分数
                    pointsMoneyNow = dedAmount;
                    dedAmount = new BigDecimal("0.0");
                }
                if (flag > -1) { //现金账户大于等于应收金额,就用应收金额抵扣
                    redepositAmount = dedAmount;
                    integralAmount = amount.subtract(dedAmount);
                if (count == -1) { //积分抵扣的金额小于应收金额,就用积分抵扣的金额抵扣
                    redepositAmount = integralMoney;
                    dedAmount = dedAmount.subtract(integralMoney);//剩余应收金额
                    pointsMoneyNow = integralMoney;
                }
                if (flag == -1) { //现金账户小于实收金额,就用现金账户抵扣
                    redepositAmount = amount;
                }
                if (flag < 1) { //现金账户小于等于应收金额,就用现金账户抵扣
                    redepositAmount = amount;
                }
                if (flag == 0) { //现金账户等于应收金额
                    redepositAmount = amount;
                }
                //更新账户信息
                AccountPo accountPo = new AccountPo();
                accountPo.setAcctId(tmpAccountDto.getAcctId());
                accountPo.setAmount(integralAmount.toString());
                accountInnerServiceSMOImpl.updateAccount(accountPo);
                //生成账户详情
                AccountDetailPo accountDetailPo = new AccountDetailPo();
                accountDetailPo.setDetailId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_detailId));
                accountDetailPo.setAcctId(tmpAccountDto.getAcctId());
                accountDetailPo.setDetailType("2002"); //1001 转入 2002 转出
                accountDetailPo.setRelAcctId("-1");
                accountDetailPo.setAmount(redepositAmount.toString());
                accountDetailPo.setObjType("6006"); //6006 个人 7007 商户
                accountDetailPo.setObjId(tmpAccountDto.getObjId());
                accountDetailPo.setOrderId("-1");
                accountDetailPo.setbId("-1");
                accountDetailPo.setRemark("手机端现金账户抵扣");
                accountDetailPo.setCreateTime(new Date());
                accountDetailInnerServiceSMOImpl.saveAccountDetails(accountDetailPo);
                //生成抵扣明细记录
                FeeAccountDetailPo feeAccountDetailPo = new FeeAccountDetailPo();
                feeAccountDetailPo.setFadId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_fadId));
                feeAccountDetailPo.setDetailId(accountDetailPo.getDetailId());
                feeAccountDetailPo.setCommunityId(reqJson.getString("communityId"));
                feeAccountDetailPo.setState("1002"); //1001 无抵扣 1002 现金账户抵扣 1003 积分账户抵扣 1004 优惠券抵扣
                feeAccountDetailPo.setAmount(redepositAmount.toString()); //积分抵扣金额
                feeAccountDetailServiceSMOImpl.saveFeeAccountDetail(feeAccountDetailPo);
                int flag2 = money.compareTo(amount);
                if (flag2 == 1) { //剩余金额大于现金账户
                    money = money.subtract(amount);
                }
                if (flag > -1) { //剩余金额大于等于现金账户
                    money = money.subtract(amount);
                }
                if (flag == -1) { //剩余金额小于现金账户
                    money = new BigDecimal(0);
                }
                if (flag < 1) { //剩余金额小于等于现金账户
                    money = new BigDecimal(0);
                }
                if (flag == 0) { //剩余金额等于现金账户
                    money = new BigDecimal(0);
                }
                money = money.add(redepositAmount);
            }
//            totalAccountAmount = totalAccountAmount.add(new BigDecimal(tmpAccountDto.getAmount()));
        }
       /* double tmpDeductionAmount = totalAccountAmount.subtract(new BigDecimal(deductionAmount)).doubleValue();
        if (tmpDeductionAmount < 0) {
            reqJson.put("deductionAmount", totalAccountAmount.doubleValue());
            reqJson.put("selectUserAccount", BeanConvertUtil.beanCovertJSONArray(accountDtos));
            return totalAccountAmount.doubleValue();
        }
        reqJson.put("deductionAmount", deductionAmount);
        reqJson.put("selectUserAccount", BeanConvertUtil.beanCovertJSONArray(accountDtos));
        return deductionAmount;*/
        reqJson.put("deductionAmount", money.doubleValue());
        reqJson.put("cashMoney", cashMoney); //现金抵扣
        reqJson.put("pointsMoney", pointsMoney); //积分抵扣扣除数
        reqJson.put("pointsMoneyNow", pointsMoneyNow); //积分抵扣 实际现金数
        reqJson.put("selectUserAccount", BeanConvertUtil.beanCovertJSONArray(accountDtos));
        return money.doubleValue();
    }