NutzCN Logo
问答 在for循环中调用查询和修改数据库导致循环中一条记录多次插入执行
发布于 2345天前 作者 xiaoqiu1235 2548 次浏览 复制 上一个帖子 下一个帖子
标签:

有一个投资返现的逻辑,导致一个集合里面个别人的投资记录出现多次返现,一直找不到原因,请问怎么写比较规范

private void grantActivityGift(Integer loanId, Loan loan, LoanType type, List<LoanTender> tenderList, Member p2p, BankAccount p2pAccount) throws Exception {
        Map<String, Object> remake = new HashMap<>(2);
        for (LoanTender tender : tenderList) {
            /**
             * 注册红包返现给出借人
             */

            ActivityGift activityGift = daoHelper.findByCondition(ActivityGift.class, Cnd.where("tenderid", "=", tender.id)
                    .and("typeid", "<=", ActivityGift.TYPE_XNTJ)
                    .and("mid", "=", tender.mid)
                    .and("used", "=", 0)
                    .and("flag", "=", 1));
            BankAccount tenderAccount = daoHelper.findByCondition(BankAccount.class, Cnd.where("mid", "=", tender.mid));

            if (!Common.isEmpty(activityGift)) {
                //调用标的转账
                remake.clear();
                remake.put("message", "红包返现");
                remake.put("to", tenderAccount.getAccountId());
                remake.put("from", p2pAccount.getAccountId());
                //平台账户直接转账到 出借人 账户 可用到可用,商户不能冻结
                bankService.doTransferBmu(loan.code, tender.mid, p2p.id, activityGift.amount, Account.REWARD_ACTIVITY, Account.CTYPE_OUTPUT, BankTransferFlow.B_TYPE_8, remake);
                //更新出借人账户
                bankService.updateAccount(FuiouBankService.ACCOUNT_TYPE_0, activityGift.amount, tender.mid, p2pAccount.getAccountId(), tenderAccount.getAccountId(), remake, Account.REWARD_ACTIVITY, Account.CTYPE_INCOME, true);
                //更新平台账户
                bankService.updateAccount(FuiouBankService.ACCOUNT_TYPE_4, activityGift.amount, p2p.id, p2pAccount.getAccountId(), tenderAccount.getAccountId(), remake, Account.REWARD_ACTIVITY, Account.CTYPE_OUTPUT, true);

                //更新红包状态为已返还
                daoHelper.update(ActivityGift.class, Chain.make("used", 1), Cnd.where("id", "=", activityGift.id).and("used", "=", 0));
            }


            /**
             * 检查新手体验标中的钱是否赎回,发放新手体验标收益金
             */
            Record xsbAmount = daoHelper.getRecord("SELECT xl.amount FROM xstyb_lxxx xl JOIN xsb_tender xt ON xt.mid = xl.mid where xl.mid = " + tender.mid + " AND xt.type = 0 AND xt.`status` = 1");
            if (xsbAmount != null) {
                remake.clear();
                remake.put("message", "发放体验金利息");
                remake.put("to", tenderAccount.getAccountId());
                remake.put("from", p2pAccount.getAccountId());
                //更改出借人账户资金余额
                bankService.doTransferBmu(loan.code, tender.mid, p2p.id, xsbAmount.getDouble("amount"), Account.ACTION_TYJ_COLLECT, Account.CTYPE_INCOME, BankTransferFlow.B_TYPE_8, remake);
                //更改出借人账户
                bankService.updateAccount(FuiouBankService.ACCOUNT_TYPE_7, xsbAmount.getDouble("amount"), tender.mid, p2pAccount.getAccountId(), tenderAccount.getAccountId(), remake, Account.ACTION_TYJ_COLLECT, Account.CTYPE_INCOME, true);
                //更改平台账户
                bankService.updateAccount(FuiouBankService.ACCOUNT_TYPE_4, xsbAmount.getDouble("amount"), p2p.id, p2pAccount.getAccountId(), tenderAccount.getAccountId(), remake, Account.ACTION_TYJ_COLLECT, Account.CTYPE_OUTPUT, true);
                //更新体验金利息状态为已发放
                daoHelper.update(NewcomerTender.class, Chain.make("redeem_amount", xsbAmount.getDouble("amount")).add("type", 1), Cnd.where("mid", "=", tender.mid));
            }

            /**
             * 五月出借返现活动,不是新手标和体验标
             */
            if (type.dayloanflag != LoanType.LOAN_TYPE_XSB && type.dayloanflag != LoanType.LOAN_TYPE_NEW) {
                fiveActivity(loanId, loan, type, p2p, p2pAccount, tender, tenderAccount);
            }
        }
    }

就是下面整个返现出现这样的问题,上面返回红包没有出现

private void grantActivityGift(Integer loanId, Loan loan, LoanType type, List<LoanTender> tenderList, Member p2p, BankAccount p2pAccount) throws Exception {
        Map<String, Object> remake = new HashMap<>(2);
        for (LoanTender tender : tenderList) {
            /**
             * 注册红包返现给出借人
             */

            ActivityGift activityGift = daoHelper.findByCondition(ActivityGift.class, Cnd.where("tenderid", "=", tender.id)
                    .and("typeid", "<=", ActivityGift.TYPE_XNTJ)
                    .and("mid", "=", tender.mid)
                    .and("used", "=", 0)
                    .and("flag", "=", 1));
            BankAccount tenderAccount = daoHelper.findByCondition(BankAccount.class, Cnd.where("mid", "=", tender.mid));

            if (!Common.isEmpty(activityGift)) {
                //调用标的转账
                remake.clear();
                remake.put("message", "红包返现");
                remake.put("to", tenderAccount.getAccountId());
                remake.put("from", p2pAccount.getAccountId());
                //平台账户直接转账到 出借人 账户 可用到可用,商户不能冻结
                bankService.doTransferBmu(loan.code, tender.mid, p2p.id, activityGift.amount, Account.REWARD_ACTIVITY, Account.CTYPE_OUTPUT, BankTransferFlow.B_TYPE_8, remake);
                //更新出借人账户
                bankService.updateAccount(FuiouBankService.ACCOUNT_TYPE_0, activityGift.amount, tender.mid, p2pAccount.getAccountId(), tenderAccount.getAccountId(), remake, Account.REWARD_ACTIVITY, Account.CTYPE_INCOME, true);
                //更新平台账户
                bankService.updateAccount(FuiouBankService.ACCOUNT_TYPE_4, activityGift.amount, p2p.id, p2pAccount.getAccountId(), tenderAccount.getAccountId(), remake, Account.REWARD_ACTIVITY, Account.CTYPE_OUTPUT, true);

                //更新红包状态为已返还
                daoHelper.update(ActivityGift.class, Chain.make("used", 1), Cnd.where("id", "=", activityGift.id).and("used", "=", 0));
            }


            /**
             * 检查新手体验标中的钱是否赎回,发放新手体验标收益金
             */
            Record xsbAmount = daoHelper.getRecord("SELECT xl.amount FROM xstyb_lxxx xl JOIN xsb_tender xt ON xt.mid = xl.mid where xl.mid = " + tender.mid + " AND xt.type = 0 AND xt.`status` = 1");
            if (xsbAmount != null) {
                remake.clear();
                remake.put("message", "发放体验金利息");
                remake.put("to", tenderAccount.getAccountId());
                remake.put("from", p2pAccount.getAccountId());
                //更改出借人账户资金余额
                bankService.doTransferBmu(loan.code, tender.mid, p2p.id, xsbAmount.getDouble("amount"), Account.ACTION_TYJ_COLLECT, Account.CTYPE_INCOME, BankTransferFlow.B_TYPE_8, remake);
                //更改出借人账户
                bankService.updateAccount(FuiouBankService.ACCOUNT_TYPE_7, xsbAmount.getDouble("amount"), tender.mid, p2pAccount.getAccountId(), tenderAccount.getAccountId(), remake, Account.ACTION_TYJ_COLLECT, Account.CTYPE_INCOME, true);
                //更改平台账户
                bankService.updateAccount(FuiouBankService.ACCOUNT_TYPE_4, xsbAmount.getDouble("amount"), p2p.id, p2pAccount.getAccountId(), tenderAccount.getAccountId(), remake, Account.ACTION_TYJ_COLLECT, Account.CTYPE_OUTPUT, true);
                //更新体验金利息状态为已发放
                daoHelper.update(NewcomerTender.class, Chain.make("redeem_amount", xsbAmount.getDouble("amount")).add("type", 1), Cnd.where("mid", "=", tender.mid));
            }

            /**
             * 五月出借返现活动,不是新手标和体验标
             */
            if (type.dayloanflag != LoanType.LOAN_TYPE_XSB && type.dayloanflag != LoanType.LOAN_TYPE_NEW) {
                fiveActivity(loanId, loan, type, p2p, p2pAccount, tender, tenderAccount);
            }
        }
    }
2 回复

第二段代码拷贝错了

/**
     * 五月出借返现活动,不是新手标和体验标
     */
    private void fiveActivity(Integer loanId, Loan loan, LoanType type, Member p2p, BankAccount p2pAccount, LoanTender tender, BankAccount tenderAccount) throws Exception {
        int activityFanxian = daoHelper.getCountByCnd("activity_fanxian_amount",
                Cnd.where("mid", "=", tender.mid).and("tenderId", "=", tender.id).and("loanId", "=", loanId));
        int loanTenderBack = daoHelper.getCountByCnd("loan_tender_back",
                Cnd.where("mid", "=", tender.mid).and("tenderid", "=", tender.id));

        String tTime = Common.FMT_DATE_3.format(tender.ctime);
        Record item = daoHelper.getItem("activity_fanxian", "*",
                Cnd.where("min", "<=", tender.amount).and("max", ">", tender.amount).and("stime", "<=", tTime).and("etime", ">=", tTime));

        if (activityFanxian <= 0 && loanTenderBack <= 0 && item != null) {
            Map<String, Object> remake = new HashMap<>(2);
            remake.put("message", "五月出借返现活动");
            remake.put("tname", " @p2p@");
            double rate = item.getDouble("rate");
            //年化利率计算返现金额(奖励 = 出借金额 x(活动年化收益率/ 12月)x 持标月数)
            int period = loan.period <= 12 ? loan.period : (loan.period / 30);
            double amt = tender.amount * rate / 100 / 12 * period;
            Double famt = DoubleUtil.round(amt, 2);
            if (famt > 0) {
                //平台账户直接转账到 出借人 账户 可用到可用,商户不能冻结
                bankService.doTransferBmu(loan.code, tender.mid, p2p.id, famt, Account.ACTION_TENDER_FANXIAN, Account.CTYPE_OUTPUT, BankTransferFlow.B_TYPE_8, remake);
                //更新出借人账户
                bankService.updateAccount(FuiouBankService.ACCOUNT_TYPE_0, famt, tender.mid, p2pAccount.getAccountId(), tenderAccount.getAccountId(), remake, Account.ACTION_TENDER_FANXIAN, Account.CTYPE_INCOME, true);
                //更新平台账户
                bankService.updateAccount(FuiouBankService.ACCOUNT_TYPE_4, famt, p2p.id, p2pAccount.getAccountId(), tenderAccount.getAccountId(), remake, Account.ACTION_TENDER_FANXIAN, Account.CTYPE_OUTPUT, true);
                daoHelper.save("activity_fanxian_amount",
                        Chain.make("mid", tender.mid).add("tenderId", tender.id).add("loanId", loanId).add("amount", famt).add("fanxianId", item.getInt("id")).add("ctime", new Date()));

                LoanTenderBack tenderBack = new LoanTenderBack();
                tenderBack.setMid(tender.mid);
                tenderBack.setTenderid(tender.id);
                tenderBack.setAmount(famt);
                tenderBack.setCtime(new Date());
                tenderBack.setType(LoanTenderBack.TYPE_LOAN_TENDER_BACK);
                daoHelper.save(tenderBack);
            }
        }
    }

业务逻辑的问题,自己搞吧

添加回复
请先登陆
回到顶部